@@ -7,43 +7,49 @@ use result::QueryResult;
77
88/// The structure returned by [`insert`](fn.insert.html). The only thing that can be done with it
99/// is call `into`.
10- pub struct IncompleteInsertStatement < T > {
10+ pub struct IncompleteInsertStatement < T , Op > {
1111 records : T ,
12+ operator : Op
1213}
1314
14- impl < T > IncompleteInsertStatement < T > {
15+ impl < T , Op > IncompleteInsertStatement < T , Op > {
1516 #[ doc( hidden) ]
16- pub fn new ( records : T ) -> Self {
17+ pub fn new ( records : T , operator : Op ) -> Self {
1718 IncompleteInsertStatement {
1819 records : records,
20+ operator : operator,
1921 }
2022 }
2123
2224 /// Specify which table the data passed to `insert` should be added to.
23- pub fn into < S > ( self , target : S ) -> InsertStatement < S , T > where
24- InsertStatement < S , T > : AsQuery ,
25+ pub fn into < S > ( self , target : S ) -> InsertStatement < S , T , Op > where
26+ InsertStatement < S , T , Op > : AsQuery ,
2527 {
2628 InsertStatement {
29+ operator : self . operator ,
2730 target : target,
2831 records : self . records ,
2932 }
3033 }
3134}
3235
33- pub struct InsertStatement < T , U > {
36+ pub struct InsertStatement < T , U , Op > {
37+ operator : Op ,
3438 target : T ,
3539 records : U ,
3640}
3741
38- impl < T , U , DB > QueryFragment < DB > for InsertStatement < T , U > where
42+ impl < T , U , Op , DB > QueryFragment < DB > for InsertStatement < T , U , Op > where
3943 DB : Backend ,
4044 T : Table ,
4145 T :: FromClause : QueryFragment < DB > ,
4246 U : Insertable < T , DB > + Copy ,
47+ Op : QueryFragment < DB > ,
4348{
4449 fn to_sql ( & self , out : & mut DB :: QueryBuilder ) -> BuildQueryResult {
4550 let values = self . records . values ( ) ;
46- out. push_sql ( "INSERT INTO " ) ;
51+ try!( self . operator . to_sql ( out) ) ;
52+ out. push_sql ( " INTO " ) ;
4753 try!( self . target . from_clause ( ) . to_sql ( out) ) ;
4854 out. push_sql ( " (" ) ;
4955 try!( values. column_names ( out) ) ;
@@ -54,6 +60,7 @@ impl<T, U, DB> QueryFragment<DB> for InsertStatement<T, U> where
5460
5561 fn collect_binds ( & self , out : & mut DB :: BindCollector ) -> QueryResult < ( ) > {
5662 let values = self . records . values ( ) ;
63+ try!( self . operator . collect_binds ( out) ) ;
5764 try!( self . target . from_clause ( ) . collect_binds ( out) ) ;
5865 try!( values. values_bind_params ( out) ) ;
5966 Ok ( ( ) )
@@ -64,14 +71,14 @@ impl<T, U, DB> QueryFragment<DB> for InsertStatement<T, U> where
6471 }
6572}
6673
67- impl_query_id ! ( noop: InsertStatement <T , U >) ;
74+ impl_query_id ! ( noop: InsertStatement <T , U , Op >) ;
6875
69- impl < T , U > AsQuery for InsertStatement < T , U > where
76+ impl < T , U , Op > AsQuery for InsertStatement < T , U , Op > where
7077 T : Table ,
71- InsertQuery < T :: AllColumns , InsertStatement < T , U > > : Query ,
78+ InsertQuery < T :: AllColumns , InsertStatement < T , U , Op > > : Query ,
7279{
7380 type SqlType = <Self :: Query as Query >:: SqlType ;
74- type Query = InsertQuery < T :: AllColumns , InsertStatement < T , U > > ;
81+ type Query = InsertQuery < T :: AllColumns , Self > ;
7582
7683 fn as_query ( self ) -> Self :: Query {
7784 InsertQuery {
@@ -81,7 +88,7 @@ impl<T, U> AsQuery for InsertStatement<T, U> where
8188 }
8289}
8390
84- impl < T , U > InsertStatement < T , U > {
91+ impl < T , U , Op > InsertStatement < T , U , Op > {
8592 /// Specify what expression is returned after execution of the `insert`.
8693 /// # Examples
8794 ///
@@ -115,9 +122,9 @@ impl<T, U> InsertStatement<T, U> {
115122 /// # #[cfg(not(feature = "postgres"))]
116123 /// # fn main() {}
117124 /// ```
118- pub fn returning < E > ( self , returns : E ) -> InsertQuery < E , InsertStatement < T , U > > where
119- E : Expression ,
120- InsertQuery < E , InsertStatement < T , U > > : Query ,
125+ pub fn returning < E > ( self , returns : E ) -> InsertQuery < E , Self > where
126+ E : Expression + SelectableExpression < T > ,
127+ InsertQuery < E , Self > : Query ,
121128 {
122129 InsertQuery {
123130 returning : returns,
@@ -132,9 +139,8 @@ pub struct InsertQuery<T, U> {
132139 statement : U ,
133140}
134141
135- impl < T , U , V > Query for InsertQuery < T , InsertStatement < U , V > > where
136- U : Table ,
137- T : Expression + SelectableExpression < U > + NonAggregate ,
142+ impl < T , U > Query for InsertQuery < T , U > where
143+ T : Expression + NonAggregate ,
138144{
139145 type SqlType = T :: SqlType ;
140146}
@@ -163,3 +169,22 @@ impl<T, U, DB> QueryFragment<DB> for InsertQuery<T, U> where
163169}
164170
165171impl_query_id ! ( noop: InsertQuery <T , U >) ;
172+
173+ pub struct Insert ;
174+
175+ impl < DB : Backend > QueryFragment < DB > for Insert {
176+ fn to_sql ( & self , out : & mut DB :: QueryBuilder ) -> BuildQueryResult {
177+ out. push_sql ( "INSERT" ) ;
178+ Ok ( ( ) )
179+ }
180+
181+ fn collect_binds ( & self , _out : & mut DB :: BindCollector ) -> QueryResult < ( ) > {
182+ Ok ( ( ) )
183+ }
184+
185+ fn is_safe_to_cache_prepared ( & self ) -> bool {
186+ true
187+ }
188+ }
189+
190+ impl_query_id ! ( Insert ) ;
0 commit comments