Build query with syntax

This commit is contained in:
Billy Chan 2021-07-14 23:12:32 +08:00 committed by Chris Tsang
parent d59ee1e298
commit 56fd77eddf
10 changed files with 88 additions and 91 deletions

View File

@ -17,7 +17,7 @@
Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic
API to make working with databases in Rust a first-class experience. API to make working with databases in Rust a first-class experience.
```rust ```markdown
This is a preview of SeaORM, and is not yet released. This is a preview of SeaORM, and is not yet released.
``` ```
@ -145,9 +145,9 @@ fruit::Entity::delete_many()
Licensed under either of Licensed under either of
- Apache License, Version 2.0 - Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT license - MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
at your option. at your option.

View File

@ -88,12 +88,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
bind_oper!(lte); bind_oper!(lte);
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Id.between(2,3)) /// .filter(cake::Column::Id.between(2,3))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` BETWEEN 2 AND 3" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` BETWEEN 2 AND 3"
/// ); /// );
@ -106,12 +106,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
} }
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Id.not_between(2,3)) /// .filter(cake::Column::Id.not_between(2,3))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` NOT BETWEEN 2 AND 3" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` NOT BETWEEN 2 AND 3"
/// ); /// );
@ -124,12 +124,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
} }
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Name.like("cheese")) /// .filter(cake::Column::Name.like("cheese"))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE 'cheese'" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE 'cheese'"
/// ); /// );
@ -139,12 +139,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
} }
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Name.not_like("cheese")) /// .filter(cake::Column::Name.not_like("cheese"))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` NOT LIKE 'cheese'" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` NOT LIKE 'cheese'"
/// ); /// );
@ -154,12 +154,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
} }
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Name.starts_with("cheese")) /// .filter(cake::Column::Name.starts_with("cheese"))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE 'cheese%'" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE 'cheese%'"
/// ); /// );
@ -170,12 +170,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
} }
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Name.ends_with("cheese")) /// .filter(cake::Column::Name.ends_with("cheese"))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese'" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese'"
/// ); /// );
@ -186,12 +186,12 @@ pub trait ColumnTrait: IdenStatic + Iterable {
} }
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Name.contains("cheese")) /// .filter(cake::Column::Name.contains("cheese"))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'"
/// ); /// );

View File

@ -17,7 +17,7 @@
//! Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic //! Inspired by ActiveRecord, Eloquent and TypeORM, SeaORM aims to provide you an intuitive and ergonomic
//! API to make working with databases in Rust a first-class experience. //! API to make working with databases in Rust a first-class experience.
//! //!
//! ```ignore //! ```markdown
//! This is a preview of SeaORM, and is not yet released. //! This is a preview of SeaORM, and is not yet released.
//! ``` //! ```
//! //!

View File

@ -113,8 +113,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests_cfg::{cake, fruit}; use crate::tests_cfg::{cake, fruit};
use crate::{ColumnTrait, EntityTrait, QueryFilter, QuerySelect, QueryTrait}; use crate::{ColumnTrait, EntityTrait, QueryFilter, QuerySelect, QueryTrait, Syntax};
use sea_query::MysqlQueryBuilder;
#[test] #[test]
fn alias_1() { fn alias_1() {
@ -122,7 +121,7 @@ mod tests {
cake::Entity::find() cake::Entity::find()
.column_as(cake::Column::Id, "B") .column_as(cake::Column::Id, "B")
.apply_alias("A_") .apply_alias("A_")
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`, `cake`.`id` AS `A_B` FROM `cake`", "SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`, `cake`.`id` AS `A_B` FROM `cake`",
); );
@ -134,7 +133,7 @@ mod tests {
cake::Entity::find() cake::Entity::find()
.left_join(fruit::Entity) .left_join(fruit::Entity)
.select_also(fruit::Entity) .select_also(fruit::Entity)
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,", "SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,",
@ -150,7 +149,7 @@ mod tests {
cake::Entity::find() cake::Entity::find()
.left_join(fruit::Entity) .left_join(fruit::Entity)
.select_with(fruit::Entity) .select_with(fruit::Entity)
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,", "SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,",
@ -169,7 +168,7 @@ mod tests {
.select_also(fruit::Entity) .select_also(fruit::Entity)
.filter(cake::Column::Id.eq(1)) .filter(cake::Column::Id.eq(1))
.filter(fruit::Column::Id.eq(2)) .filter(fruit::Column::Id.eq(2))
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,", "SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,",
@ -188,7 +187,7 @@ mod tests {
.select_with(fruit::Entity) .select_with(fruit::Entity)
.filter(cake::Column::Id.eq(1)) .filter(cake::Column::Id.eq(1))
.filter(fruit::Column::Id.eq(2)) .filter(fruit::Column::Id.eq(2))
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,", "SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,",

View File

@ -31,28 +31,28 @@ impl Delete {
/// ///
/// Model /// Model
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Delete::one(cake::Model { /// Delete::one(cake::Model {
/// id: 1, /// id: 1,
/// name: "Apple Pie".to_owned(), /// name: "Apple Pie".to_owned(),
/// }) /// })
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#, /// r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#,
/// ); /// );
/// ``` /// ```
/// ActiveModel /// ActiveModel
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Delete::one(cake::ActiveModel { /// Delete::one(cake::ActiveModel {
/// id: ActiveValue::set(1), /// id: ActiveValue::set(1),
/// name: ActiveValue::set("Apple Pie".to_owned()), /// name: ActiveValue::set("Apple Pie".to_owned()),
/// }) /// })
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#, /// r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#,
/// ); /// );
@ -75,12 +75,12 @@ impl Delete {
/// Delete many ActiveModel /// Delete many ActiveModel
/// ///
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::{Expr, PostgresQueryBuilder}}; /// use sea_orm::{entity::*, query::*, tests_cfg::fruit, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Delete::many(fruit::Entity) /// Delete::many(fruit::Entity)
/// .filter(fruit::Column::Name.contains("Apple")) /// .filter(fruit::Column::Name.contains("Apple"))
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE '%Apple%'"#, /// r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE '%Apple%'"#,
/// ); /// );
@ -179,8 +179,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests_cfg::{cake, fruit}; use crate::tests_cfg::{cake, fruit};
use crate::{entity::*, query::*}; use crate::{entity::*, query::*, Syntax};
use sea_query::PostgresQueryBuilder;
#[test] #[test]
fn delete_1() { fn delete_1() {
@ -189,7 +188,7 @@ mod tests {
id: 1, id: 1,
name: "Apple Pie".to_owned(), name: "Apple Pie".to_owned(),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#, r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#,
); );
@ -198,7 +197,7 @@ mod tests {
id: ActiveValue::set(1), id: ActiveValue::set(1),
name: ActiveValue::set("Apple Pie".to_owned()), name: ActiveValue::set("Apple Pie".to_owned()),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#, r#"DELETE FROM "cake" WHERE "cake"."id" = 1"#,
); );
@ -209,7 +208,7 @@ mod tests {
assert_eq!( assert_eq!(
Delete::many(fruit::Entity) Delete::many(fruit::Entity)
.filter(fruit::Column::Name.contains("Cheese")) .filter(fruit::Column::Name.contains("Cheese"))
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE '%Cheese%'"#, r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE '%Cheese%'"#,
); );

View File

@ -23,13 +23,13 @@ pub trait QuerySelect: Sized {
/// Add a select column /// Add a select column
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .select_only() /// .select_only()
/// .column(cake::Column::Name) /// .column(cake::Column::Name)
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"SELECT "cake"."name" FROM "cake""# /// r#"SELECT "cake"."name" FROM "cake""#
/// ); /// );
@ -44,13 +44,13 @@ pub trait QuerySelect: Sized {
/// Add a select column with alias /// Add a select column with alias
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .select_only() /// .select_only()
/// .column_as(cake::Column::Id.count(), "count") /// .column_as(cake::Column::Id.count(), "count")
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"SELECT COUNT("cake"."id") AS "count" FROM "cake""# /// r#"SELECT COUNT("cake"."id") AS "count" FROM "cake""#
/// ); /// );
@ -68,14 +68,14 @@ pub trait QuerySelect: Sized {
/// Add a group by column /// Add a group by column
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .select_only() /// .select_only()
/// .column(cake::Column::Name) /// .column(cake::Column::Name)
/// .group_by(cake::Column::Name) /// .group_by(cake::Column::Name)
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"SELECT "cake"."name" FROM "cake" GROUP BY "cake"."name""# /// r#"SELECT "cake"."name" FROM "cake" GROUP BY "cake"."name""#
/// ); /// );
@ -90,13 +90,13 @@ pub trait QuerySelect: Sized {
/// Add an AND HAVING expression /// Add an AND HAVING expression
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .having(cake::Column::Id.eq(4)) /// .having(cake::Column::Id.eq(4))
/// .having(cake::Column::Id.eq(5)) /// .having(cake::Column::Id.eq(5))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` HAVING `cake`.`id` = 4 AND `cake`.`id` = 5" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` HAVING `cake`.`id` = 4 AND `cake`.`id` = 5"
/// ); /// );
@ -151,13 +151,13 @@ pub trait QueryOrder: Sized {
/// Add an order_by expression /// Add an order_by expression
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .order_by(cake::Column::Id, Order::Asc) /// .order_by(cake::Column::Id, Order::Asc)
/// .order_by(cake::Column::Name, Order::Desc) /// .order_by(cake::Column::Name, Order::Desc)
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC, `cake`.`name` DESC" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC, `cake`.`name` DESC"
/// ); /// );
@ -172,12 +172,12 @@ pub trait QueryOrder: Sized {
/// Add an order_by expression (ascending) /// Add an order_by expression (ascending)
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .order_by_asc(cake::Column::Id) /// .order_by_asc(cake::Column::Id)
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC"
/// ); /// );
@ -193,12 +193,12 @@ pub trait QueryOrder: Sized {
/// Add an order_by expression (descending) /// Add an order_by expression (descending)
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .order_by_desc(cake::Column::Id) /// .order_by_desc(cake::Column::Id)
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` DESC" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` DESC"
/// ); /// );
@ -221,13 +221,13 @@ pub trait QueryFilter: Sized {
/// Add an AND WHERE expression /// Add an AND WHERE expression
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
/// .filter(cake::Column::Id.eq(4)) /// .filter(cake::Column::Id.eq(4))
/// .filter(cake::Column::Id.eq(5)) /// .filter(cake::Column::Id.eq(5))
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 AND `cake`.`id` = 5" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 AND `cake`.`id` = 5"
/// ); /// );
@ -235,7 +235,7 @@ pub trait QueryFilter: Sized {
/// ///
/// Add a condition tree. /// Add a condition tree.
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// cake::Entity::find() /// cake::Entity::find()
@ -244,7 +244,7 @@ pub trait QueryFilter: Sized {
/// .add(cake::Column::Id.eq(4)) /// .add(cake::Column::Id.eq(4))
/// .add(cake::Column::Id.eq(5)) /// .add(cake::Column::Id.eq(5))
/// ) /// )
/// .build(MysqlQueryBuilder) /// .build(Syntax::MySql)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 OR `cake`.`id` = 5" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 OR `cake`.`id` = 5"
/// ); /// );

View File

@ -39,28 +39,28 @@ where
/// ///
/// Model /// Model
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Insert::one(cake::Model { /// Insert::one(cake::Model {
/// id: 1, /// id: 1,
/// name: "Apple Pie".to_owned(), /// name: "Apple Pie".to_owned(),
/// }) /// })
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#, /// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#,
/// ); /// );
/// ``` /// ```
/// ActiveModel /// ActiveModel
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Insert::one(cake::ActiveModel { /// Insert::one(cake::ActiveModel {
/// id: Unset(None), /// id: Unset(None),
/// name: Set("Apple Pie".to_owned()), /// name: Set("Apple Pie".to_owned()),
/// }) /// })
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"INSERT INTO "cake" ("name") VALUES ('Apple Pie')"#, /// r#"INSERT INTO "cake" ("name") VALUES ('Apple Pie')"#,
/// ); /// );
@ -75,7 +75,7 @@ where
/// Insert many Model or ActiveModel /// Insert many Model or ActiveModel
/// ///
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Insert::many(vec![ /// Insert::many(vec![
@ -88,7 +88,7 @@ where
/// name: "Orange Scone".to_owned(), /// name: "Orange Scone".to_owned(),
/// } /// }
/// ]) /// ])
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie'), (2, 'Orange Scone')"#, /// r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie'), (2, 'Orange Scone')"#,
/// ); /// );
@ -162,8 +162,7 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests_cfg::cake; use crate::tests_cfg::cake;
use crate::{ActiveValue, Insert, QueryTrait}; use crate::{ActiveValue, Insert, QueryTrait, Syntax};
use sea_query::PostgresQueryBuilder;
#[test] #[test]
fn insert_1() { fn insert_1() {
@ -173,7 +172,7 @@ mod tests {
id: ActiveValue::unset(), id: ActiveValue::unset(),
name: ActiveValue::set("Apple Pie".to_owned()), name: ActiveValue::set("Apple Pie".to_owned()),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"INSERT INTO "cake" ("name") VALUES ('Apple Pie')"#, r#"INSERT INTO "cake" ("name") VALUES ('Apple Pie')"#,
); );
@ -187,7 +186,7 @@ mod tests {
id: ActiveValue::set(1), id: ActiveValue::set(1),
name: ActiveValue::set("Apple Pie".to_owned()), name: ActiveValue::set("Apple Pie".to_owned()),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#, r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#,
); );
@ -201,7 +200,7 @@ mod tests {
id: 1, id: 1,
name: "Apple Pie".to_owned(), name: "Apple Pie".to_owned(),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#, r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#,
); );
@ -221,7 +220,7 @@ mod tests {
name: "Orange Scone".to_owned(), name: "Orange Scone".to_owned(),
} }
]) ])
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie'), (2, 'Orange Scone')"#, r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie'), (2, 'Orange Scone')"#,
); );
@ -241,7 +240,7 @@ mod tests {
assert_eq!( assert_eq!(
Insert::<cake::ActiveModel>::new() Insert::<cake::ActiveModel>::new()
.add_many(vec![apple, orange]) .add_many(vec![apple, orange])
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (NULL, 'Apple'), (2, 'Orange')"#, r#"INSERT INTO "cake" ("id", "name") VALUES (NULL, 'Apple'), (2, 'Orange')"#,
); );

View File

@ -62,15 +62,14 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests_cfg::{cake, filling, fruit}; use crate::tests_cfg::{cake, filling, fruit};
use crate::{ColumnTrait, EntityTrait, ModelTrait, QueryFilter, QueryTrait}; use crate::{ColumnTrait, EntityTrait, ModelTrait, QueryFilter, QueryTrait, Syntax};
use sea_query::MysqlQueryBuilder;
#[test] #[test]
fn join_1() { fn join_1() {
assert_eq!( assert_eq!(
cake::Entity::find() cake::Entity::find()
.left_join(fruit::Entity) .left_join(fruit::Entity)
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`", "SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
@ -86,7 +85,7 @@ mod tests {
cake::Entity::find() cake::Entity::find()
.inner_join(fruit::Entity) .inner_join(fruit::Entity)
.filter(fruit::Column::Name.contains("cherry")) .filter(fruit::Column::Name.contains("cherry"))
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`", "SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
@ -102,7 +101,7 @@ mod tests {
assert_eq!( assert_eq!(
fruit::Entity::find() fruit::Entity::find()
.reverse_join(cake::Entity) .reverse_join(cake::Entity)
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`", "SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
@ -120,7 +119,7 @@ mod tests {
assert_eq!( assert_eq!(
find_fruit find_fruit
.filter(cake::Column::Id.eq(11)) .filter(cake::Column::Id.eq(11))
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`", "SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
@ -141,7 +140,7 @@ mod tests {
assert_eq!( assert_eq!(
cake_model cake_model
.find_related(fruit::Entity) .find_related(fruit::Entity)
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`", "SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
@ -157,7 +156,7 @@ mod tests {
assert_eq!( assert_eq!(
cake::Entity::find() cake::Entity::find()
.left_join(filling::Entity) .left_join(filling::Entity)
.build(MysqlQueryBuilder) .build(Syntax::MySql)
.to_string(), .to_string(),
[ [
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`", "SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
@ -174,7 +173,7 @@ mod tests {
let find_filling: Select<filling::Entity> = cake::Entity::find_related(); let find_filling: Select<filling::Entity> = cake::Entity::find_related();
assert_eq!( assert_eq!(
find_filling.build(MysqlQueryBuilder).to_string(), find_filling.build(Syntax::MySql).to_string(),
[ [
"SELECT `filling`.`id`, `filling`.`name` FROM `filling`", "SELECT `filling`.`id`, `filling`.`name` FROM `filling`",
"INNER JOIN `cake_filling` ON `cake_filling`.`filling_id` = `filling`.`id`", "INNER JOIN `cake_filling` ON `cake_filling`.`filling_id` = `filling`.`id`",

View File

@ -14,11 +14,12 @@ pub trait QueryTrait {
fn into_query(self) -> Self::QueryStatement; fn into_query(self) -> Self::QueryStatement;
/// Build the query as [`Statement`] /// Build the query as [`Statement`]
fn build<B>(&self, builder: B) -> Statement fn build(&self, syntax: Syntax) -> Statement {
where let query_builder = syntax.get_query_builder();
B: QueryBuilderWithSyntax, Statement::from_string_values_tuple(
{ syntax,
Statement::from_string_values_tuple(builder.syntax(), self.as_query().build(builder)) self.as_query().build_any(query_builder.as_ref()),
)
} }
} }

View File

@ -30,14 +30,14 @@ impl Update {
/// Update one ActiveModel /// Update one ActiveModel
/// ///
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder}; /// use sea_orm::{entity::*, query::*, tests_cfg::cake, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Update::one(cake::ActiveModel { /// Update::one(cake::ActiveModel {
/// id: ActiveValue::set(1), /// id: ActiveValue::set(1),
/// name: ActiveValue::set("Apple Pie".to_owned()), /// name: ActiveValue::set("Apple Pie".to_owned()),
/// }) /// })
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#, /// r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
/// ); /// );
@ -59,13 +59,13 @@ impl Update {
/// Update many ActiveModel /// Update many ActiveModel
/// ///
/// ``` /// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::{Expr, PostgresQueryBuilder}}; /// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::Expr, Syntax};
/// ///
/// assert_eq!( /// assert_eq!(
/// Update::many(fruit::Entity) /// Update::many(fruit::Entity)
/// .col_expr(fruit::Column::Name, Expr::value("Golden Apple")) /// .col_expr(fruit::Column::Name, Expr::value("Golden Apple"))
/// .filter(fruit::Column::Name.contains("Apple")) /// .filter(fruit::Column::Name.contains("Apple"))
/// .build(PostgresQueryBuilder) /// .build(Syntax::Postgres)
/// .to_string(), /// .to_string(),
/// r#"UPDATE "fruit" SET "name" = 'Golden Apple' WHERE "fruit"."name" LIKE '%Apple%'"#, /// r#"UPDATE "fruit" SET "name" = 'Golden Apple' WHERE "fruit"."name" LIKE '%Apple%'"#,
/// ); /// );
@ -184,8 +184,8 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::tests_cfg::{cake, fruit}; use crate::tests_cfg::{cake, fruit};
use crate::{entity::*, query::*}; use crate::{entity::*, query::*, Syntax};
use sea_query::{Expr, PostgresQueryBuilder, Value}; use sea_query::{Expr, Value};
#[test] #[test]
fn update_1() { fn update_1() {
@ -194,7 +194,7 @@ mod tests {
id: ActiveValue::set(1), id: ActiveValue::set(1),
name: ActiveValue::set("Apple Pie".to_owned()), name: ActiveValue::set("Apple Pie".to_owned()),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#, r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
); );
@ -208,7 +208,7 @@ mod tests {
name: ActiveValue::set("Orange".to_owned()), name: ActiveValue::set("Orange".to_owned()),
cake_id: ActiveValue::unset(), cake_id: ActiveValue::unset(),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#, r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#,
); );
@ -222,7 +222,7 @@ mod tests {
name: ActiveValue::unchanged("Apple".to_owned()), name: ActiveValue::unchanged("Apple".to_owned()),
cake_id: ActiveValue::set(Some(3)), cake_id: ActiveValue::set(Some(3)),
}) })
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"UPDATE "fruit" SET "cake_id" = 3 WHERE "fruit"."id" = 2"#, r#"UPDATE "fruit" SET "cake_id" = 3 WHERE "fruit"."id" = 2"#,
); );
@ -234,7 +234,7 @@ mod tests {
Update::many(fruit::Entity) Update::many(fruit::Entity)
.col_expr(fruit::Column::CakeId, Expr::value(Value::Null)) .col_expr(fruit::Column::CakeId, Expr::value(Value::Null))
.filter(fruit::Column::Id.eq(2)) .filter(fruit::Column::Id.eq(2))
.build(PostgresQueryBuilder) .build(Syntax::Postgres)
.to_string(), .to_string(),
r#"UPDATE "fruit" SET "cake_id" = NULL WHERE "fruit"."id" = 2"#, r#"UPDATE "fruit" SET "cake_id" = NULL WHERE "fruit"."id" = 2"#,
); );