Skip to content

Commit 9431a7c

Browse files
committed
moves limit and offset methods to postgres syntax
1 parent c8dba3c commit 9431a7c

File tree

8 files changed

+209
-197
lines changed

8 files changed

+209
-197
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = "Write SQL queries in a simple and composable way"
44
documentation = "https://docs.rs/sql_query_builder"
55
repository = "https://github.com/belchior/sql_query_builder"
66
authors = ["Belchior Oliveira <[email protected]>"]
7-
version = "1.0.3"
7+
version = "1.0.4"
88
edition = "2021"
99
rust-version = "1.58"
1010
license = "MIT"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ sql_query_builder = { version = "1.x.x", features = ["postgresql"] }
4747

4848

4949
## How it's works
50+
5051
In simple terms this library will not try to understand what you are writing inside the arguments, this is good
5152
because it's removes a lot of complexity and verbosity to generate a SQL query, in contrast debugging tends to be more difficult and silly error can araise.
5253
The lib has the [debug()](https://docs.rs/sql_query_builder/latest/sql_query_builder/struct.Select.html#method.debug) method with a nice output to minimize the effort to debug a complex query.
@@ -67,7 +68,7 @@ let select = sql::Select::new()
6768

6869
Methods like `limit` and `offset` will override the previous value, the two select is equivalent
6970

70-
```rust
71+
```text
7172
use sql_query_builder as sql;
7273
7374
let select = sql::Select::new()

src/select/select.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
structure::{Select, SelectClause},
55
};
66

7-
impl<'a> Select<'a> {
7+
impl Select<'_> {
88
/// The same as [where_clause](Select::where_clause) method, useful to write more idiomatic SQL query
99
///
1010
/// # Examples
@@ -142,47 +142,11 @@ impl<'a> Select<'a> {
142142
self
143143
}
144144

145-
/// The limit clause. This method overrides the previous value
146-
///
147-
/// # Examples
148-
/// ```
149-
/// use sql_query_builder as sql;
150-
///
151-
/// let select = sql::Select::new()
152-
/// .limit("123");
153-
///
154-
/// let select = sql::Select::new()
155-
/// .limit("1000")
156-
/// .limit("123");
157-
/// ```
158-
pub fn limit(mut self, num: &'a str) -> Self {
159-
self._limit = num.trim();
160-
self
161-
}
162-
163145
/// Create Select's instance
164146
pub fn new() -> Self {
165147
Self::default()
166148
}
167149

168-
/// The offset clause. This method overrides the previous value
169-
///
170-
/// # Examples
171-
/// ```
172-
/// use sql_query_builder as sql;
173-
///
174-
/// let select = sql::Select::new()
175-
/// .offset("1500");
176-
///
177-
/// let select = sql::Select::new()
178-
/// .offset("1000")
179-
/// .offset("1500");
180-
/// ```
181-
pub fn offset(mut self, num: &'a str) -> Self {
182-
self._offset = num.trim();
183-
self
184-
}
185-
186150
/// The order by clause
187151
pub fn order_by(mut self, column: &str) -> Self {
188152
push_unique(&mut self._order_by, column.trim().to_owned());
@@ -311,6 +275,42 @@ impl<'a> Select<'a> {
311275
self
312276
}
313277

278+
/// The limit clause. This method overrides the previous value, this method can be used enabling the feature flag `postgresql`
279+
///
280+
/// # Examples
281+
/// ```text
282+
/// use sql_query_builder as sql;
283+
///
284+
/// let select = sql::Select::new()
285+
/// .limit("123");
286+
///
287+
/// let select = sql::Select::new()
288+
/// .limit("1000")
289+
/// .limit("123");
290+
/// ```
291+
pub fn limit(mut self, num: &'a str) -> Self {
292+
self._limit = num.trim();
293+
self
294+
}
295+
296+
/// The offset clause. This method overrides the previous value, this method can be used enabling the feature flag `postgresql`
297+
///
298+
/// # Examples
299+
/// ```text
300+
/// use sql_query_builder as sql;
301+
///
302+
/// let select = sql::Select::new()
303+
/// .offset("1500");
304+
///
305+
/// let select = sql::Select::new()
306+
/// .offset("1000")
307+
/// .offset("1500");
308+
/// ```
309+
pub fn offset(mut self, num: &'a str) -> Self {
310+
self._offset = num.trim();
311+
self
312+
}
313+
314314
/// The union clause, this method can be used enabling the feature flag `postgresql`
315315
pub fn union(mut self, select: Self) -> Self {
316316
self._union.push(select);

src/select/select_internal.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,12 @@ impl Select<'_> {
5959

6060
format!("{left_stmt}{right_stmt}{raw_after}{space_after}")
6161
}
62-
}
6362

64-
impl Select<'_> {
65-
fn concat_group_by(&self, query: String, fmts: &fmt::Formatter) -> String {
66-
let fmt::Formatter { comma, lb, space, .. } = fmts;
67-
let sql = if self._group_by.is_empty() == false {
68-
let columns = self._group_by.join(comma);
69-
format!("GROUP BY{space}{columns}{space}{lb}")
63+
fn concat_limit(&self, query: String, fmts: &fmt::Formatter) -> String {
64+
let fmt::Formatter { lb, space, .. } = fmts;
65+
let sql = if self._limit.is_empty() == false {
66+
let count = self._limit;
67+
format!("LIMIT{space}{count}{space}{lb}")
7068
} else {
7169
"".to_owned()
7270
};
@@ -76,16 +74,16 @@ impl Select<'_> {
7674
&self._raw_after,
7775
query,
7876
fmts,
79-
SelectClause::GroupBy,
77+
SelectClause::Limit,
8078
sql,
8179
)
8280
}
8381

84-
fn concat_having(&self, query: String, fmts: &fmt::Formatter) -> String {
82+
fn concat_offset(&self, query: String, fmts: &fmt::Formatter) -> String {
8583
let fmt::Formatter { lb, space, .. } = fmts;
86-
let sql = if self._having.is_empty() == false {
87-
let conditions = self._having.join(" AND ");
88-
format!("HAVING{space}{conditions}{space}{lb}")
84+
let sql = if self._offset.is_empty() == false {
85+
let start = self._offset;
86+
format!("OFFSET{space}{start}{space}{lb}")
8987
} else {
9088
"".to_owned()
9189
};
@@ -95,16 +93,18 @@ impl Select<'_> {
9593
&self._raw_after,
9694
query,
9795
fmts,
98-
SelectClause::Having,
96+
SelectClause::Offset,
9997
sql,
10098
)
10199
}
100+
}
102101

103-
fn concat_join(&self, query: String, fmts: &fmt::Formatter) -> String {
104-
let fmt::Formatter { lb, space, .. } = fmts;
105-
let sql = if self._join.is_empty() == false {
106-
let joins = self._join.join(format!("{space}{lb}").as_str());
107-
format!("{joins}{space}{lb}")
102+
impl Select<'_> {
103+
fn concat_group_by(&self, query: String, fmts: &fmt::Formatter) -> String {
104+
let fmt::Formatter { comma, lb, space, .. } = fmts;
105+
let sql = if self._group_by.is_empty() == false {
106+
let columns = self._group_by.join(comma);
107+
format!("GROUP BY{space}{columns}{space}{lb}")
108108
} else {
109109
"".to_owned()
110110
};
@@ -114,16 +114,16 @@ impl Select<'_> {
114114
&self._raw_after,
115115
query,
116116
fmts,
117-
SelectClause::Join,
117+
SelectClause::GroupBy,
118118
sql,
119119
)
120120
}
121121

122-
fn concat_limit(&self, query: String, fmts: &fmt::Formatter) -> String {
122+
fn concat_having(&self, query: String, fmts: &fmt::Formatter) -> String {
123123
let fmt::Formatter { lb, space, .. } = fmts;
124-
let sql = if self._limit.is_empty() == false {
125-
let count = self._limit;
126-
format!("LIMIT{space}{count}{space}{lb}")
124+
let sql = if self._having.is_empty() == false {
125+
let conditions = self._having.join(" AND ");
126+
format!("HAVING{space}{conditions}{space}{lb}")
127127
} else {
128128
"".to_owned()
129129
};
@@ -133,16 +133,16 @@ impl Select<'_> {
133133
&self._raw_after,
134134
query,
135135
fmts,
136-
SelectClause::Limit,
136+
SelectClause::Having,
137137
sql,
138138
)
139139
}
140140

141-
fn concat_offset(&self, query: String, fmts: &fmt::Formatter) -> String {
141+
fn concat_join(&self, query: String, fmts: &fmt::Formatter) -> String {
142142
let fmt::Formatter { lb, space, .. } = fmts;
143-
let sql = if self._offset.is_empty() == false {
144-
let start = self._offset;
145-
format!("OFFSET{space}{start}{space}{lb}")
143+
let sql = if self._join.is_empty() == false {
144+
let joins = self._join.join(format!("{space}{lb}").as_str());
145+
format!("{joins}{space}{lb}")
146146
} else {
147147
"".to_owned()
148148
};
@@ -152,7 +152,7 @@ impl Select<'_> {
152152
&self._raw_after,
153153
query,
154154
fmts,
155-
SelectClause::Offset,
155+
SelectClause::Join,
156156
sql,
157157
)
158158
}
@@ -234,8 +234,12 @@ impl Concat for Select<'_> {
234234
query = self.concat_group_by(query, &fmts);
235235
query = self.concat_having(query, &fmts);
236236
query = self.concat_order_by(query, &fmts);
237-
query = self.concat_limit(query, &fmts);
238-
query = self.concat_offset(query, &fmts);
237+
238+
#[cfg(feature = "postgresql")]
239+
{
240+
query = self.concat_limit(query, &fmts);
241+
query = self.concat_offset(query, &fmts);
242+
}
239243

240244
#[cfg(feature = "postgresql")]
241245
{

src/structure.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::marker::PhantomData;
2+
13
#[cfg(feature = "postgresql")]
24
pub enum Combinator {
35
Except,
@@ -94,20 +96,23 @@ pub struct Select<'a> {
9496
pub(crate) _group_by: Vec<String>,
9597
pub(crate) _having: Vec<String>,
9698
pub(crate) _join: Vec<String>,
97-
pub(crate) _limit: &'a str,
98-
pub(crate) _offset: &'a str,
9999
pub(crate) _order_by: Vec<String>,
100100
pub(crate) _raw_after: Vec<(SelectClause, String)>,
101101
pub(crate) _raw_before: Vec<(SelectClause, String)>,
102102
pub(crate) _raw: Vec<String>,
103103
pub(crate) _select: Vec<String>,
104104
pub(crate) _where: Vec<String>,
105+
_not_used: PhantomData<&'a ()>, // exist only to accept a lifetime without feature flag
105106

106107
#[cfg(feature = "postgresql")]
107108
pub(crate) _except: Vec<Self>,
108109
#[cfg(feature = "postgresql")]
109110
pub(crate) _intersect: Vec<Self>,
110111
#[cfg(feature = "postgresql")]
112+
pub(crate) _limit: &'a str,
113+
#[cfg(feature = "postgresql")]
114+
pub(crate) _offset: &'a str,
115+
#[cfg(feature = "postgresql")]
111116
pub(crate) _union: Vec<Self>,
112117
#[cfg(feature = "postgresql")]
113118
pub(crate) _with: Vec<(&'a str, std::sync::Arc<dyn crate::behavior::WithQuery>)>,

0 commit comments

Comments
 (0)