Skip to content

Commit 039dd8c

Browse files
committed
refactor(WIP)!: querybuilder now is made of AST types for query generation
1 parent 22c5318 commit 039dd8c

31 files changed

Lines changed: 627 additions & 739 deletions

File tree

canyon_core/src/connection/database_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::fmt::Display;
2727
/// let builder = QueryBuilder::new_for(table, columns, DatabaseType::Deferred)?;
2828
/// let query = builder.build()?; // will resolve to the default DB type
2929
/// ```
30-
#[derive(Deserialize, Debug, Eq, PartialEq, Clone, Copy)]
30+
#[derive(Deserialize, Debug, Eq, PartialEq, Clone, Copy, Default)]
3131
pub enum DatabaseType {
3232
/// The Postgres database backend.
3333
#[cfg(feature = "postgres")]
@@ -70,7 +70,7 @@ pub enum DatabaseType {
7070
/// .where_("id", Comp::Eq, &42)
7171
/// .build()?; // resolved dynamically to the active database type
7272
/// ```
73-
Deferred,
73+
#[default] Deferred,
7474
}
7575

7676
impl Display for DatabaseType {

canyon_core/src/query/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait Inspectionable<'a> {
3737
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
3838
}
3939

40-
pub trait TableMetadata: std::fmt::Display {
40+
pub trait TableMetadata<'a>: std::fmt::Display {
4141
fn as_str(&self) -> &'static str;
4242
}
4343

canyon_core/src/query/operators.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub trait Operator: Display {
77

88
/// Enumerated type for represent the comparison operations
99
/// in SQL sentences
10+
#[derive(Debug)]
1011
pub enum Comp {
1112
/// Operator "=" equals
1213
Eq,
@@ -20,8 +21,11 @@ pub enum Comp {
2021
Lt,
2122
/// Operator "=<" less or equals than value
2223
LtEq,
24+
/// A "LIKE" comp operator
25+
Like(LikeKind)
2326
}
2427

28+
2529
impl Display for Comp {
2630
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2731
let op = match *self {
@@ -31,25 +35,14 @@ impl Display for Comp {
3135
Self::GtEq => ">=",
3236
Self::Lt => "<",
3337
Self::LtEq => "<=",
38+
Self::Like(ref __kind) => "LIKE"
3439
};
3540
write!(f, "{}", op)
3641
}
3742
}
3843

39-
impl Operator for Comp {
40-
fn as_str(&self, placeholder_counter: usize, _with_type: &DatabaseType) -> String {
41-
match *self {
42-
Self::Eq => format!(" = ${placeholder_counter}"),
43-
Self::Neq => format!(" <> ${placeholder_counter}"),
44-
Self::Gt => format!(" > ${placeholder_counter}"),
45-
Self::GtEq => format!(" >= ${placeholder_counter}"),
46-
Self::Lt => format!(" < ${placeholder_counter}"),
47-
Self::LtEq => format!(" <= ${placeholder_counter}"),
48-
}
49-
}
50-
}
51-
52-
pub enum Like {
44+
#[derive(Debug)]
45+
pub enum LikeKind {
5346
/// Operator "LIKE" as '%pattern%'
5447
Full,
5548
/// Operator "LIKE" as '%pattern'
@@ -58,7 +51,7 @@ pub enum Like {
5851
Right,
5952
}
6053

61-
impl Operator for Like {
54+
impl Operator for LikeKind {
6255
fn as_str(&self, placeholder_counter: usize, datasource_type: &DatabaseType) -> String {
6356
let type_data_to_cast_str = match datasource_type {
6457
#[cfg(feature = "postgres")]
@@ -71,30 +64,30 @@ impl Operator for Like {
7164
};
7265

7366
match *self {
74-
Like::Full => {
67+
Self::Full => {
7568
format!(
7669
" LIKE CONCAT('%', CAST(${placeholder_counter} AS {type_data_to_cast_str}) ,'%')"
7770
)
7871
}
79-
Like::Left => format!(
72+
Self::Left => format!(
8073
" LIKE CONCAT('%', CAST(${placeholder_counter} AS {type_data_to_cast_str}))"
8174
),
82-
Like::Right => format!(
75+
Self::Right => format!(
8376
" LIKE CONCAT(CAST(${placeholder_counter} AS {type_data_to_cast_str}) ,'%')"
8477
),
8578
}
8679
}
8780
}
8881

89-
impl Display for Like {
82+
impl Display for LikeKind {
9083
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
9184
write!(
9285
f,
9386
"{}",
9487
match *self {
95-
Like::Full => "Like::Full",
96-
Like::Left => "Like::Left",
97-
Like::Right => "Like::Right",
88+
Self::Full => "Like::Full",
89+
Self::Left => "Like::Left",
90+
Self::Right => "Like::Right",
9891
}
9992
)
10093
}

canyon_core/src/query/querybuilder/ast.rs

Lines changed: 0 additions & 90 deletions
This file was deleted.

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ pub trait UpdateQueryBuilderOps<'a>: QueryBuilderOps<'a> {
1616

1717
/// Similar to [`Self::set`] but storing the underlying update values for each column in the
1818
/// internal values collection of the [`crate::query::querybuilder::QueryBuilder`]
19-
fn set_with_values<Z, Q>(self, columns: &'a [(Z, Q)]) -> Result<Self, Box<dyn Error + Send + Sync + 'a>>
19+
fn set_values<Z, Q>(self, columns: &'a [(Z, Q)]) -> Result<Self, Box<dyn Error + Send + Sync + 'a>>
2020
where
2121
Z: FieldIdentifier,
2222
Q: QueryParameter,
23-
Self: Sized,
24-
Vec<&'a dyn QueryParameter>: Extend<&'a Q>;
23+
Self: Sized;
2524
}
2625

2726
pub trait SelectQueryBuilderOps<'a>: QueryBuilderOps<'a> {
@@ -39,7 +38,7 @@ pub trait SelectQueryBuilderOps<'a>: QueryBuilderOps<'a> {
3938
/// > Note: The order on the column parameters is irrelevant
4039
fn left_join(
4140
self,
42-
join_table: impl TableMetadata,
41+
join_table: impl TableMetadata<'a>,
4342
col1: impl FieldIdentifier,
4443
col2: impl FieldIdentifier,
4544
) -> Self;
@@ -54,7 +53,7 @@ pub trait SelectQueryBuilderOps<'a>: QueryBuilderOps<'a> {
5453
/// > Note: The order on the column parameters is irrelevant
5554
fn inner_join(
5655
self,
57-
join_table: impl TableMetadata,
56+
join_table: impl TableMetadata<'a>,
5857
col1: impl FieldIdentifier,
5958
col2: impl FieldIdentifier,
6059
) -> Self;
@@ -69,7 +68,7 @@ pub trait SelectQueryBuilderOps<'a>: QueryBuilderOps<'a> {
6968
/// > Note: The order on the column parameters is irrelevant
7069
fn right_join(
7170
self,
72-
join_table: impl TableMetadata,
71+
join_table: impl TableMetadata<'a>,
7372
col1: impl FieldIdentifier,
7473
col2: impl FieldIdentifier,
7574
) -> Self;
@@ -84,7 +83,7 @@ pub trait SelectQueryBuilderOps<'a>: QueryBuilderOps<'a> {
8483
/// > Note: The order on the column parameters is irrelevant
8584
fn full_join(
8685
self,
87-
join_table: impl TableMetadata,
86+
join_table: impl TableMetadata<'a>,
8887
col1: impl FieldIdentifier,
8988
col2: impl FieldIdentifier,
9089
) -> Self;
@@ -168,8 +167,7 @@ pub trait QueryBuilderOps<'a> {
168167
where
169168
Z: FieldIdentifier,
170169
Q: QueryParameter,
171-
Vec<&'a (dyn QueryParameter + 'a)>: Extend<&'a Q>,
172-
Self: std::marker::Sized;
170+
Self: Sized;
173171

174172
/// Generates an `OR` SQL clause for constraint the query that will create
175173
/// the filter in conjunction with an `IN` operator that will ac
@@ -183,8 +181,7 @@ pub trait QueryBuilderOps<'a> {
183181
where
184182
Z: FieldIdentifier,
185183
Q: QueryParameter,
186-
Vec<&'a (dyn QueryParameter + 'a)>: Extend<&'a Q>,
187-
Self: std::marker::Sized;
184+
Self: Sized;
188185

189186
/// Generates an `OR` SQL clause for constraint the query.
190187
///
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pub mod select;
2+
3+
use crate::connection::database_type::DatabaseType;
4+
use crate::query::parameters::QueryParameter;
5+
use crate::query::querybuilder::syntax::clause::ConditionClause;
6+
use crate::query::querybuilder::syntax::table_metadata::TableMetadata;
7+
use crate::query::querybuilder::syntax::query_kind::QueryKind;
8+
9+
/// Base AST for common parts
10+
#[derive(Default)] pub struct BaseAst<'a> {
11+
pub kind: QueryKind,
12+
pub table: TableMetadata<'a>,
13+
pub conditions: Vec<ConditionClause<'a>>,
14+
}
15+
16+
impl<'a> BaseAst<'a> {
17+
pub fn new(kind: QueryKind, table: TableMetadata<'a>, database_type: DatabaseType) -> Self {
18+
Self {
19+
kind,
20+
table,
21+
conditions: Vec::new()
22+
}
23+
}
24+
}
25+
26+
/// Select AST
27+
28+
29+
pub struct InsertAst<'a> {
30+
pub base: BaseAst<'a>,
31+
pub columns: Vec<&'a str>,
32+
pub values: Vec<&'a dyn QueryParameter>,
33+
}
34+
35+
impl<'a> InsertAst<'a> {
36+
pub fn new(table: TableMetadata<'a>, db: DatabaseType) -> Self {
37+
Self { base: BaseAst::new(QueryKind::Insert, table, db), columns: Vec::new(), values: Vec::new() }
38+
}
39+
}
40+
41+
pub struct UpdateAst<'a> {
42+
pub base: BaseAst<'a>,
43+
pub set_clauses: Vec<(&'a str, &'a dyn QueryParameter)>,
44+
}
45+
46+
impl<'a> UpdateAst<'a> {
47+
pub fn new(table: TableMetadata<'a>, db: DatabaseType) -> Self {
48+
Self { base: BaseAst::new(QueryKind::Update, table, db), set_clauses: Vec::new() }
49+
}
50+
}
51+
52+
pub struct DeleteAst<'a> {
53+
pub base: BaseAst<'a>,
54+
}
55+
56+
impl<'a> DeleteAst<'a> {
57+
pub fn new(table: TableMetadata<'a>, db: DatabaseType) -> Self {
58+
Self { base: BaseAst::new(QueryKind::Delete, table, db) }
59+
}
60+
}

0 commit comments

Comments
 (0)