Skip to content

Commit 75a064e

Browse files
committed
Added and method to Delete and Update builder
1 parent 29d276e commit 75a064e

File tree

6 files changed

+81
-5
lines changed

6 files changed

+81
-5
lines changed

src/delete.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ use crate::{
55
};
66

77
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+
822
/// Gets the current state of the DeleteBuilder and returns it as string
923
pub fn as_string(&self) -> String {
1024
let fmts = fmt::Formatter::one_line();
@@ -136,6 +150,13 @@ impl<'a> DeleteBuilder<'a> {
136150
}
137151

138152
/// 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+
/// ```
139160
pub fn where_clause(mut self, condition: &'a str) -> Self {
140161
push_unique(&mut self._where, condition.trim().to_owned());
141162
self

src/select.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,32 @@ impl<'a> SelectBuilder<'a> {
260260
self
261261
}
262262

263-
/// The select by clause
263+
/// The select clause
264264
pub fn select(mut self, column: &'a str) -> Self {
265265
push_unique(&mut self._select, column.trim().to_owned());
266266
self
267267
}
268268

269-
/// The union by clause
269+
/// The union clause
270270
pub fn union(mut self, select: Self) -> Self {
271271
self._union.push(select);
272272
self
273273
}
274274

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+
/// ```
276283
pub fn where_clause(mut self, condition: &'a str) -> Self {
277284
push_unique(&mut self._where, condition.trim().to_owned());
278285
self
279286
}
280287

281-
/// The with by clause
288+
/// The with clause
282289
pub fn with(mut self, name: &'a str, select: Self) -> Self {
283290
self._with.push((name.trim(), select));
284291
self

src/update.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ use crate::{
55
};
66

77
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+
823
/// Gets the current state of the UpdateBuilder and returns it as string
924
pub fn as_string(&self) -> String {
1025
let fmts = fmt::Formatter::one_line();
@@ -142,6 +157,14 @@ impl<'a> UpdateBuilder<'a> {
142157
}
143158

144159
/// 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+
/// ```
145168
pub fn where_clause(mut self, condition: &'a str) -> Self {
146169
push_unique(&mut self._where, condition.trim().to_owned());
147170
self

tests/delete_builder_api_spec.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ mod builder_methods {
106106
}
107107
}
108108

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+
109122
mod delete_clause {
110123
use super::*;
111124
use pretty_assertions::assert_eq;

tests/select_builder_api_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ mod and_clause {
140140
use pretty_assertions::assert_eq;
141141

142142
#[test]
143-
fn method_and_should_add_a_where_clause() {
143+
fn method_and_should_be_an_alias_to_where_clause() {
144144
let query = SelectBuilder::new().and("login = 'foo'").as_string();
145145
let expected_query = "WHERE login = 'foo'";
146146

tests/update_builder_api_spec.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ mod builder_methods {
103103
}
104104
}
105105

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+
}
106118
mod set_clause {
107119
use super::*;
108120
use pretty_assertions::assert_eq;

0 commit comments

Comments
 (0)