@@ -233,6 +233,9 @@ auto operator<=(aggregate_field<M> field, auto val) {
233233 return build_where (field, val, " <=" );
234234}
235235
236+ struct token_t {};
237+ inline constexpr auto token = token_t {};
238+
236239inline auto count () { return aggregate_field<uint64_t >{" COUNT(*)" }; }
237240
238241template <typename T>
@@ -357,19 +360,6 @@ class query_builder {
357360 where_clause_.insert (0 , " where " );
358361 }
359362 }
360- #ifdef ORMPP_ENABLE_PG
361- if (!where_clause_.empty ()) {
362- int index = 1 ;
363- for (size_t i = 0 ; i < where_clause_.size (); i++) {
364- if (where_clause_[i] == ' ?' ) {
365- where_clause_[i] = ' $' ;
366- std::string index_str = std::to_string (index++);
367- std::memcpy (&where_clause_[i + 1 ], index_str.data (),
368- (std::min)(index_str.size (), size_t (2 )));
369- }
370- }
371- }
372- #endif
373363
374364 sql_.append (join_clause_)
375365 .append (where_clause_)
@@ -380,6 +370,20 @@ class query_builder {
380370 .append (limit_clause_)
381371 .append (offset_clause_);
382372
373+ #ifdef ORMPP_ENABLE_PG
374+ if (sql_.find (' ?' ) != std::string::npos) {
375+ int index = 1 ;
376+ for (size_t i = 0 ; i < sql_.size (); i++) {
377+ if (sql_[i] == ' ?' ) {
378+ sql_[i] = ' $' ;
379+ std::string index_str = std::to_string (index++);
380+ std::memcpy (&sql_[i + 1 ], index_str.data (),
381+ (std::min)(index_str.size (), size_t (2 )));
382+ }
383+ }
384+ }
385+ #endif
386+
383387 if constexpr (!ylt::reflection::is_ylt_refl_v<R> && !std::is_void_v<R> &&
384388 !iguana::tuple_v<R>) {
385389 auto t = db_->template query_s <std::tuple<R>>(sql_, args...);
@@ -448,6 +452,11 @@ class query_builder {
448452 return stage_offset{ctx};
449453 }
450454
455+ stage_offset offset (token_t ) {
456+ ctx->offset_clause_ .append (" offset ? " );
457+ return stage_offset{ctx};
458+ }
459+
451460 template <typename To, typename ... Args>
452461 auto collect (Args... args) {
453462 return ctx->template collect <To>(args...);
@@ -467,6 +476,11 @@ class query_builder {
467476 return stage_limit{ctx};
468477 }
469478
479+ stage_limit limit (token_t ) {
480+ ctx->limit_clause_ = " LIMIT ? " ;
481+ return stage_limit{ctx};
482+ }
483+
470484 template <typename To, typename ... Args>
471485 auto collect (Args... args) {
472486 return ctx->template collect <To>(args...);
@@ -550,6 +564,11 @@ class query_builder {
550564 return stage_limit{ctx};
551565 }
552566
567+ stage_limit limit (token_t ) {
568+ ctx->limit_c = " LIMIT ? " ;
569+ return stage_limit{ctx};
570+ }
571+
553572 template <typename ... Args>
554573 stage_group_by group_by (Args... fields) {
555574 ctx->group_by_clause_ = " GROUP BY " ;
0 commit comments