@@ -7,7 +7,9 @@ use std::marker::PhantomData;
7
7
use crate :: arguments:: Arguments ;
8
8
use crate :: database:: { Database , HasArguments } ;
9
9
use crate :: encode:: Encode ;
10
+ use crate :: from_row:: FromRow ;
10
11
use crate :: query:: Query ;
12
+ use crate :: query_as:: QueryAs ;
11
13
use crate :: types:: Type ;
12
14
use crate :: Either ;
13
15
@@ -392,7 +394,6 @@ where
392
394
for tuple in tuples {
393
395
separated. push ( "(" ) ;
394
396
395
- // use a `Separated` with a separate (hah) internal state
396
397
push_tuple ( separated. query_builder . separated ( ", " ) , tuple) ;
397
398
398
399
separated. push_unseparated ( ")" ) ;
@@ -425,6 +426,27 @@ where
425
426
}
426
427
}
427
428
429
+ /// Produce an executable query from this builder.
430
+ ///
431
+ /// ### Note: Query is not Checked
432
+ /// It is your responsibility to ensure that you produce a syntactically correct query here,
433
+ /// this API has no way to check it for you.
434
+ ///
435
+ /// ### Note: Reuse
436
+ /// You can reuse this builder afterwards to amortize the allocation overhead of the query
437
+ /// string, however you must call [`.reset()`][Self::reset] first, which returns `Self`
438
+ /// to the state it was in immediately after [`new()`][Self::new].
439
+ ///
440
+ /// Calling any other method but `.reset()` after `.build()` will panic for sanity reasons.
441
+ pub fn build_query_as < ' q , T : FromRow < ' q , DB :: Row > > (
442
+ & ' q mut self ,
443
+ ) -> QueryAs < ' q , DB , T , <DB as HasArguments < ' args > >:: Arguments > {
444
+ QueryAs {
445
+ inner : self . build ( ) ,
446
+ output : PhantomData ,
447
+ }
448
+ }
449
+
428
450
/// Reset this `QueryBuilder` back to its initial state.
429
451
///
430
452
/// The query is truncated to the initial fragment provided to [`new()`][Self::new] and
@@ -435,6 +457,16 @@ where
435
457
436
458
self
437
459
}
460
+
461
+ /// Get the current build SQL; **note**: may not be syntactically correct.
462
+ pub fn sql ( & self ) -> & str {
463
+ & self . query
464
+ }
465
+
466
+ /// Deconstruct this `QueryBuilder`, returning the built SQL. May not be syntactically correct.
467
+ pub fn into_sql ( self ) -> String {
468
+ self . query
469
+ }
438
470
}
439
471
440
472
/// A wrapper around `QueryBuilder` for creating comma(or other token)-separated lists.
0 commit comments