Skip to content

Commit 9d54570

Browse files
committed
chore: writer.rs doesn't care anymore about creating tokens
1 parent 7416ef1 commit 9d54570

7 files changed

Lines changed: 53 additions & 25 deletions

File tree

canyon_core/src/query/operators.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Display for Operator {
5050

5151
impl<'a, D: SqlDialect> ToSqlTokens<'a, D> for Operator {
5252
fn to_tokens(&self) -> impl IntoIterator<Item = SqlToken<'a>> + 'a {
53-
let mut out = SqlTokens::with_capacity(16);
53+
let mut out = SqlTokens::default();
5454

5555
match *self {
5656
Self::Eq => out.symbol(Symbol::Equals),
@@ -99,7 +99,6 @@ impl LikeKind {
9999
out.placeholder();
100100
out.whitespace();
101101
out.keyword(Keyword::As);
102-
out.whitespace();
103102
out.ident(Cow::from(<PlaceholderDatatype as Into<&str>>::into(
104103
D::PLACEHOLDER_DATA_TYPE,
105104
)));
@@ -124,7 +123,6 @@ impl<'a, D: SqlDialect> ToSqlTokens<'a, D> for LikeKind {
124123
fn to_tokens(&self) -> impl IntoIterator<Item = SqlToken<'a>> + 'a {
125124
let mut out = SqlTokens::with_capacity(19);
126125

127-
out.keyword(Keyword::Like);
128126
out.whitespace();
129127
out.keyword(Keyword::Concat);
130128
out.symbol(Symbol::LParen);

canyon_core/src/query/querybuilder/syntax/clause.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::query::operators::Operator;
1+
use crate::query::operators::{LikeKind, Operator};
22
use crate::query::querybuilder::syntax::column::ColumnRef;
33
use crate::query::querybuilder::syntax::dialect::SqlDialect;
44
use crate::query::querybuilder::syntax::emitter::types::helpers::Range;
@@ -10,7 +10,7 @@ pub struct ConditionClause<'a> {
1010
pub(crate) kind: ConditionClauseKind,
1111
pub(crate) column_name: ColumnRef<'a>,
1212
pub(crate) operator: Operator,
13-
pub(crate) value_indexes: Range,
13+
pub(crate) value_indexes: Option<Range>,
1414
}
1515

1616
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
@@ -51,25 +51,50 @@ impl<'a, D: SqlDialect> ToSqlTokens<'a, D> for ConditionClause<'a> {
5151
// Operator
5252
out.operator(self.operator);
5353

54-
out.whitespace();
55-
56-
if self.value_indexes.is_range() {
57-
out.symbol(Symbol::LParen);
58-
let mut indexes = (&self.value_indexes).into_iter().peekable();
59-
while indexes.next().is_some() {
60-
out.placeholder();
61-
if indexes.peek().is_some() {
62-
out.symbol(Symbol::Comma);
54+
match self.operator {
55+
Operator::Like(kind) | Operator::NotLike(kind) => {
56+
let like_tokens = <LikeKind as ToSqlTokens<'_, D>>::to_tokens(&kind);
57+
out.extend(like_tokens);
58+
}
59+
_ => {
60+
if let Some(ref range) = self.value_indexes
61+
&& range.is_range()
62+
{
63+
__impl::output_range_of_placeholders::<D>(range, &mut out);
64+
} else {
6365
out.whitespace();
66+
out.placeholder();
6467
}
6568
}
66-
out.symbol(Symbol::RParen);
67-
} else {
68-
out.placeholder();
6969
}
7070

7171
out.whitespace();
7272

7373
out
7474
}
7575
}
76+
77+
mod __impl {
78+
use crate::query::operators::Operator;
79+
use crate::query::querybuilder::syntax::dialect::SqlDialect;
80+
use crate::query::querybuilder::syntax::emitter::types::helpers::Range;
81+
use crate::query::querybuilder::syntax::symbol::Symbol;
82+
use crate::query::querybuilder::syntax::tokens::{SqlTokens, ToSqlTokens};
83+
84+
pub(crate) fn output_range_of_placeholders<D: SqlDialect>(
85+
range: &Range,
86+
out: &mut SqlTokens<'_>,
87+
) {
88+
out.whitespace();
89+
out.symbol(Symbol::LParen);
90+
let mut indexes = range.into_iter().peekable();
91+
while indexes.next().is_some() {
92+
out.placeholder();
93+
if indexes.peek().is_some() {
94+
out.symbol(Symbol::Comma);
95+
out.whitespace();
96+
}
97+
}
98+
out.symbol(Symbol::RParen);
99+
}
100+
}

canyon_core/src/query/querybuilder/syntax/emitter/types/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ mod tests {
131131
kind: ConditionClauseKind::Where,
132132
column_name: "id".into(),
133133
operator: Operator::Eq,
134-
value_indexes: Range::new_unbounded(3),
134+
value_indexes: Some(Range::new_unbounded(3)),
135135
});
136136

137137
let sql = render_standard(&ast, &mut base_ast);

canyon_core/src/query/querybuilder/syntax/emitter/types/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mod tests {
170170
kind: ConditionClauseKind::Where,
171171
column_name: "id".into(),
172172
operator: Operator::Eq,
173-
value_indexes: Range::new_unbounded(3),
173+
value_indexes: Some(Range::new_unbounded(3)),
174174
});
175175

176176
let sql = render_standard(&ast, &mut base_ast);
@@ -212,7 +212,7 @@ mod tests {
212212
kind: ConditionClauseKind::Where,
213213
column_name: "id".into(),
214214
operator: Operator::Eq,
215-
value_indexes: Range::new_unbounded(3),
215+
value_indexes: Some(Range::new_unbounded(3)),
216216
});
217217

218218
let sql = render_standard(&ast, &mut base_ast);

canyon_core/src/query/querybuilder/syntax/writer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ mod __impl {
5151
SqlToken::Symbol(sym) => __detail::render_symbol(sym, f)?,
5252
SqlToken::Operator(op) => write!(f, "{}", op)?,
5353
SqlToken::Placeholder => {
54-
__detail::write_value_placeholder::<D>(placeholder_counter, f)?;
55-
*placeholder_counter += 1;
54+
__detail::write_value_placeholder::<D>(placeholder_counter, f)?
5655
}
5756
SqlToken::WhiteSpace => write!(f, " ")?,
5857
SqlToken::Number(num) => write!(f, "{}", num)?,
@@ -62,6 +61,9 @@ mod __impl {
6261
}
6362

6463
mod __detail {
64+
use crate::query::operators::Operator;
65+
use crate::query::querybuilder::syntax::tokens::ToSqlTokens;
66+
use crate::query::querybuilder::syntax::writer::__impl::output_token_to_string_buffer;
6567
use crate::{
6668
connection::database_type::DatabaseType,
6769
query::querybuilder::syntax::{dialect::SqlDialect, symbol::Symbol},
@@ -103,6 +105,8 @@ mod __detail {
103105
_ => write!(f, "{}{}", placeholder_symbol, placeholder_counter),
104106
};
105107

108+
*placeholder_counter += 1;
109+
106110
Ok(())
107111
}
108112
}

canyon_core/src/query/querybuilder/types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod __impl {
184184
kind,
185185
column_name: column_name.into(),
186186
operator,
187-
value_indexes: Range::new_unbounded(_self.params.len()),
187+
value_indexes: Some(Range::new_unbounded(_self.params.len())),
188188
});
189189
}
190190

@@ -199,7 +199,7 @@ mod __impl {
199199
kind,
200200
column_name: column_name.into(),
201201
operator,
202-
value_indexes: value_indexes_range,
202+
value_indexes: Some(value_indexes_range),
203203
});
204204
}
205205
}

tests/crud/querybuilder_operations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ fn test_crud_find_with_querybuilder_and_fulllike() {
8989

9090
assert_eq!(
9191
filtered_leagues_result.build().unwrap().sql,
92-
"SELECT * FROM league WHERE name LIKE CONCAT('%', CAST($1 AS VARCHAR) ,'%')"
92+
"SELECT * FROM \"league\" WHERE \"name\" LIKE CONCAT ('%', CAST ($1 AS VARCHAR), '%');"
9393
)
9494
}
95+
9596
//
9697
// /// Builds a new SQL statement for retrieves entities of the `T` type, filtered
9798
// /// with the parameters that modifies the base SQL to SELECT * FROM <entity>

0 commit comments

Comments
 (0)