File tree Expand file tree Collapse file tree 6 files changed +81
-5
lines changed Expand file tree Collapse file tree 6 files changed +81
-5
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,20 @@ use crate::{
5
5
} ;
6
6
7
7
impl < ' a > DeleteBuilder < ' a > {
8
+ /// The same as `where_clause` method, useful to write more idiomatic SQL query
9
+ /// ```
10
+ /// use sql_query_builder::DeleteBuilder;
11
+ ///
12
+ /// let delete = DeleteBuilder::new()
13
+ /// .delete_from("users")
14
+ /// .where_clause("created_at < $1")
15
+ /// .and("active = false");
16
+ /// ```
17
+ pub fn and ( mut self , condition : & ' a str ) -> Self {
18
+ self = self . where_clause ( condition) ;
19
+ self
20
+ }
21
+
8
22
/// Gets the current state of the DeleteBuilder and returns it as string
9
23
pub fn as_string ( & self ) -> String {
10
24
let fmts = fmt:: Formatter :: one_line ( ) ;
@@ -136,6 +150,13 @@ impl<'a> DeleteBuilder<'a> {
136
150
}
137
151
138
152
/// The where clause
153
+ /// ```
154
+ /// use sql_query_builder::DeleteBuilder;
155
+ ///
156
+ /// let delete = DeleteBuilder::new()
157
+ /// .delete_from("users")
158
+ /// .where_clause("login = 'foo'");
159
+ /// ```
139
160
pub fn where_clause ( mut self , condition : & ' a str ) -> Self {
140
161
push_unique ( & mut self . _where , condition. trim ( ) . to_owned ( ) ) ;
141
162
self
Original file line number Diff line number Diff line change @@ -260,25 +260,32 @@ impl<'a> SelectBuilder<'a> {
260
260
self
261
261
}
262
262
263
- /// The select by clause
263
+ /// The select clause
264
264
pub fn select ( mut self , column : & ' a str ) -> Self {
265
265
push_unique ( & mut self . _select , column. trim ( ) . to_owned ( ) ) ;
266
266
self
267
267
}
268
268
269
- /// The union by clause
269
+ /// The union clause
270
270
pub fn union ( mut self , select : Self ) -> Self {
271
271
self . _union . push ( select) ;
272
272
self
273
273
}
274
274
275
- /// The where by clause
275
+ /// The where clause
276
+ /// ```
277
+ /// use sql_query_builder::SelectBuilder;
278
+ ///
279
+ /// let select = SelectBuilder::new()
280
+ /// .from("users")
281
+ /// .where_clause("login = $1");
282
+ /// ```
276
283
pub fn where_clause ( mut self , condition : & ' a str ) -> Self {
277
284
push_unique ( & mut self . _where , condition. trim ( ) . to_owned ( ) ) ;
278
285
self
279
286
}
280
287
281
- /// The with by clause
288
+ /// The with clause
282
289
pub fn with ( mut self , name : & ' a str , select : Self ) -> Self {
283
290
self . _with . push ( ( name. trim ( ) , select) ) ;
284
291
self
Original file line number Diff line number Diff line change @@ -5,6 +5,21 @@ use crate::{
5
5
} ;
6
6
7
7
impl < ' a > UpdateBuilder < ' a > {
8
+ /// The same as `where_clause` method, useful to write more idiomatic SQL query
9
+ /// ```
10
+ /// use sql_query_builder::UpdateBuilder;
11
+ ///
12
+ /// let update = UpdateBuilder::new()
13
+ /// .update("users")
14
+ /// .set("name = $1")
15
+ /// .where_clause("login = $2")
16
+ /// .and("active = true");
17
+ /// ```
18
+ pub fn and ( mut self , condition : & ' a str ) -> Self {
19
+ self = self . where_clause ( condition) ;
20
+ self
21
+ }
22
+
8
23
/// Gets the current state of the UpdateBuilder and returns it as string
9
24
pub fn as_string ( & self ) -> String {
10
25
let fmts = fmt:: Formatter :: one_line ( ) ;
@@ -142,6 +157,14 @@ impl<'a> UpdateBuilder<'a> {
142
157
}
143
158
144
159
/// The where clause
160
+ /// ```
161
+ /// use sql_query_builder::UpdateBuilder;
162
+ ///
163
+ /// let update = UpdateBuilder::new()
164
+ /// .update("users")
165
+ /// .set("name = $1")
166
+ /// .where_clause("login = $2");
167
+ /// ```
145
168
pub fn where_clause ( mut self , condition : & ' a str ) -> Self {
146
169
push_unique ( & mut self . _where , condition. trim ( ) . to_owned ( ) ) ;
147
170
self
Original file line number Diff line number Diff line change @@ -106,6 +106,19 @@ mod builder_methods {
106
106
}
107
107
}
108
108
109
+ mod and_clause {
110
+ use super :: * ;
111
+ use pretty_assertions:: assert_eq;
112
+
113
+ #[ test]
114
+ fn method_and_should_be_an_alias_to_where_clause ( ) {
115
+ let query = DeleteBuilder :: new ( ) . and ( "login = 'foo'" ) . as_string ( ) ;
116
+ let expected_query = "WHERE login = 'foo'" ;
117
+
118
+ assert_eq ! ( query, expected_query) ;
119
+ }
120
+ }
121
+
109
122
mod delete_clause {
110
123
use super :: * ;
111
124
use pretty_assertions:: assert_eq;
Original file line number Diff line number Diff line change @@ -140,7 +140,7 @@ mod and_clause {
140
140
use pretty_assertions:: assert_eq;
141
141
142
142
#[ test]
143
- fn method_and_should_add_a_where_clause ( ) {
143
+ fn method_and_should_be_an_alias_to_where_clause ( ) {
144
144
let query = SelectBuilder :: new ( ) . and ( "login = 'foo'" ) . as_string ( ) ;
145
145
let expected_query = "WHERE login = 'foo'" ;
146
146
Original file line number Diff line number Diff line change @@ -103,6 +103,18 @@ mod builder_methods {
103
103
}
104
104
}
105
105
106
+ mod and_clause {
107
+ use super :: * ;
108
+ use pretty_assertions:: assert_eq;
109
+
110
+ #[ test]
111
+ fn method_and_should_be_an_alias_to_where_clause ( ) {
112
+ let query = UpdateBuilder :: new ( ) . and ( "login = 'foo'" ) . as_string ( ) ;
113
+ let expected_query = "WHERE login = 'foo'" ;
114
+
115
+ assert_eq ! ( query, expected_query) ;
116
+ }
117
+ }
106
118
mod set_clause {
107
119
use super :: * ;
108
120
use pretty_assertions:: assert_eq;
You can’t perform that action at this time.
0 commit comments