@@ -4,7 +4,7 @@ use std::fmt::Display;
4
4
use std:: fmt:: Write ;
5
5
use std:: marker:: PhantomData ;
6
6
7
- use crate :: arguments:: Arguments ;
7
+ use crate :: arguments:: { Arguments , IntoArguments } ;
8
8
use crate :: database:: { Database , HasArguments } ;
9
9
use crate :: encode:: Encode ;
10
10
use crate :: from_row:: FromRow ;
49
49
}
50
50
}
51
51
52
+ /// Construct a `QueryBuilder` with existing SQL and arguments.
53
+ ///
54
+ /// ### Note
55
+ /// This does *not* check if `arguments` is valid for the given SQL.
56
+ pub fn with_arguments < A > ( init : impl Into < String > , arguments : A ) -> Self
57
+ where
58
+ DB : Database ,
59
+ A : IntoArguments < ' args , DB > ,
60
+ {
61
+ let init = init. into ( ) ;
62
+
63
+ QueryBuilder {
64
+ init_len : init. len ( ) ,
65
+ query : init,
66
+ arguments : Some ( arguments. into_arguments ( ) ) ,
67
+ }
68
+ }
69
+
52
70
#[ inline]
53
71
fn sanity_check ( & self ) {
54
72
assert ! (
@@ -635,4 +653,23 @@ mod test {
635
653
"SELECT * FROM users WHERE id = 99"
636
654
) ;
637
655
}
656
+
657
+ #[ test]
658
+ fn test_query_builder_with_args ( ) {
659
+ let mut qb: QueryBuilder < ' _ , Postgres > = QueryBuilder :: new ( "" ) ;
660
+
661
+ let query = qb
662
+ . push ( "SELECT * FROM users WHERE id = " )
663
+ . push_bind ( 42i32 )
664
+ . build ( ) ;
665
+
666
+ let mut qb: QueryBuilder < ' _ , Postgres > =
667
+ QueryBuilder :: new_with ( query. sql ( ) , query. take_arguments ( ) ) ;
668
+ let query = qb. push ( "OR membership_level =" ) . push_bind ( 3i32 ) . build ( ) ;
669
+
670
+ assert_eq ! (
671
+ query. sql( ) ,
672
+ "SELECT * FROM users WHERE id = $1 OR membership_level = $2"
673
+ ) ;
674
+ }
638
675
}
0 commit comments