-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement IfElseStatement for MySQL backend
- Loading branch information
Showing
6 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::{QueryBuilder, SimpleExpr}; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct IfElseStatement { | ||
pub when: SimpleExpr, | ||
pub then: SimpleExpr, | ||
pub otherwise: Option<SimpleExpr> | ||
} | ||
|
||
impl IfElseStatement { | ||
|
||
pub fn new(when: SimpleExpr, then: SimpleExpr, otherwise: Option<SimpleExpr>) -> Self { | ||
Self { | ||
when, | ||
then, | ||
otherwise | ||
} | ||
} | ||
|
||
pub fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String { | ||
let mut sql = String::with_capacity(256); | ||
query_builder.prepare_if_else_statement(&Box::new(self.clone()), &mut sql); | ||
sql | ||
} | ||
|
||
} | ||
pub trait IfElseStatementBuilder { | ||
/// Build corresponding SQL statement for certain database backend and return SQL string | ||
fn build<T: QueryBuilder>(&self, query_builder: T) -> String; | ||
|
||
/// Build corresponding SQL statement for certain database backend and return SQL string | ||
fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String { | ||
self.build(query_builder) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn if_without_else() { | ||
let query = Query::select().column(Asterisk).from(Glyph::Table).take(); | ||
let then = SimpleExpr::SubQuery(None, Box::new(query.into_sub_query_statement())); | ||
let if_statement = IfElseStatement::new( | ||
Expr::col(Glyph::Id).eq(1), | ||
then, | ||
None | ||
); | ||
assert_eq!( | ||
if_statement.to_string(MysqlQueryBuilder), | ||
[ | ||
"IF `id` = 1 THEN", | ||
"(SELECT * FROM `glyph`)", | ||
"END IF" | ||
].join("\n") | ||
) | ||
} | ||
|
||
#[test] | ||
fn if_with_else() { | ||
let query = Query::select().column(Asterisk).from(Glyph::Table).take(); | ||
let then = SimpleExpr::SubQuery(None, Box::new(query.into_sub_query_statement())); | ||
let if_statement = IfElseStatement::new( | ||
Expr::col(Glyph::Id).eq(1), | ||
then, | ||
Some(Expr::val("23").into()) | ||
); | ||
assert_eq!( | ||
if_statement.to_string(MysqlQueryBuilder), | ||
[ | ||
"IF `id` = 1 THEN", | ||
"(SELECT * FROM `glyph`)", | ||
"ELSE", | ||
"'23'", | ||
"END IF" | ||
].join("\n") | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ mod foreign_key; | |
mod index; | ||
mod query; | ||
mod table; | ||
mod if_else; | ||
|
||
#[path = "../common.rs"] | ||
mod common; | ||
|