diff --git a/Cargo.toml b/Cargo.toml index 41b823dc..338ed06b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,6 @@ members = [ "sea-orm-macros", "sea-orm-codegen", "sea-orm-cli", - "examples/sqlx-mysql", - "examples/cli", ] [package] @@ -24,9 +22,7 @@ publish = false [package.metadata.docs.rs] features = [ "default", - "sqlx-mysql", - "sqlx-postgres", - "sqlx-sqlite", + "sqlx-all", "runtime-async-std-native-tls", ] rustdoc-args = ["--cfg", "docsrs"] @@ -88,6 +84,7 @@ with-uuid = [ "sqlx-macros/uuid", ] sqlx-dep = ["sqlx"] +sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"] sqlx-json = ["sqlx/json", "with-json"] sqlx-chrono = ["sqlx/chrono", "with-chrono"] sqlx-decimal = ["sqlx/decimal", "with-rust_decimal"] diff --git a/examples/cli/.env b/examples/cli/.env deleted file mode 100644 index c1e940d8..00000000 --- a/examples/cli/.env +++ /dev/null @@ -1,2 +0,0 @@ -DATABASE_URL=mysql://sea:sea@localhost/bakery -DATABASE_SCHEMA=bakery \ No newline at end of file diff --git a/examples/cli/Cargo.toml b/examples/cli/Cargo.toml deleted file mode 100644 index 75185d3e..00000000 --- a/examples/cli/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "sea-orm-cli-example" -version = "0.1.0" -edition = "2018" -publish = false - -[dependencies] -sea-orm = { path = "../../" } -strum = { version = "^0.20", features = [ "derive" ] } diff --git a/examples/cli/README.md b/examples/cli/README.md deleted file mode 100644 index 1d34c06e..00000000 --- a/examples/cli/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# SeaORM CLI Example - -Prepare: - -Setup a test database and configure the connection string in `.env`. -Run `bakery.sql` to setup the test table and data. - -Building sea-orm-cli: - -```sh -(cd ../../sea-orm-cli ; cargo build) -``` - -Generating entity: - -```sh -../../target/debug/sea-orm-cli generate entity -o src/entity -``` diff --git a/examples/cli/src/entity/cake.rs b/examples/cli/src/entity/cake.rs deleted file mode 100644 index 29f55ac6..00000000 --- a/examples/cli/src/entity/cake.rs +++ /dev/null @@ -1,75 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "cake" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Fruit, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(Some(255u32)).def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Fruit => Entity::has_many(super::fruit::Entity).into(), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Fruit.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - super::cake_filling::Relation::Filling.def() - } - fn via() -> Option { - Some(super::cake_filling::Relation::Cake.def().rev()) - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/entity/cake_filling.rs b/examples/cli/src/entity/cake_filling.rs deleted file mode 100644 index 1ac64920..00000000 --- a/examples/cli/src/entity/cake_filling.rs +++ /dev/null @@ -1,81 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "cake_filling" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub cake_id: i32, - pub filling_id: i32, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - CakeId, - FillingId, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - CakeId, - FillingId, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - false - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Cake, - Filling, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - fn def(&self) -> ColumnDef { - match self { - Self::CakeId => ColumnType::Integer.def(), - Self::FillingId => ColumnType::Integer.def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Cake => Entity::belongs_to(super::cake::Entity) - .from(Column::CakeId) - .to(super::cake::Column::Id) - .into(), - Self::Filling => Entity::belongs_to(super::filling::Entity) - .from(Column::FillingId) - .to(super::filling::Column::Id) - .into(), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Cake.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Filling.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/entity/filling.rs b/examples/cli/src/entity/filling.rs deleted file mode 100644 index bedb1ab4..00000000 --- a/examples/cli/src/entity/filling.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "filling" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation {} - -impl ColumnTrait for Column { - type EntityName = Entity; - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(Some(255u32)).def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - _ => panic!("No RelationDef"), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - super::cake_filling::Relation::Cake.def() - } - fn via() -> Option { - Some(super::cake_filling::Relation::Filling.def().rev()) - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/entity/fruit.rs b/examples/cli/src/entity/fruit.rs deleted file mode 100644 index 7ff7d9b8..00000000 --- a/examples/cli/src/entity/fruit.rs +++ /dev/null @@ -1,80 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "fruit" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, - pub cake_id: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, - CakeId, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Cake, - Vendor, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(Some(255u32)).def(), - Self::CakeId => ColumnType::Integer.def().null(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Cake => Entity::belongs_to(super::cake::Entity) - .from(Column::CakeId) - .to(super::cake::Column::Id) - .into(), - Self::Vendor => Entity::has_many(super::vendor::Entity).into(), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Cake.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Vendor.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/entity/mod.rs b/examples/cli/src/entity/mod.rs deleted file mode 100644 index 395d29f9..00000000 --- a/examples/cli/src/entity/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -pub mod cake; -pub mod cake_filling; -pub mod filling; -pub mod fruit; -pub mod vendor; diff --git a/examples/cli/src/entity/prelude.rs b/examples/cli/src/entity/prelude.rs deleted file mode 100644 index b4e85c78..00000000 --- a/examples/cli/src/entity/prelude.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -pub use super::cake::Entity as Cake; -pub use super::cake_filling::Entity as CakeFilling; -pub use super::filling::Entity as Filling; -pub use super::fruit::Entity as Fruit; -pub use super::vendor::Entity as Vendor; diff --git a/examples/cli/src/entity/vendor.rs b/examples/cli/src/entity/vendor.rs deleted file mode 100644 index 2262519f..00000000 --- a/examples/cli/src/entity/vendor.rs +++ /dev/null @@ -1,72 +0,0 @@ -//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 - -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "vendor" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, - pub fruit_id: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, - FruitId, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Fruit, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(Some(255u32)).def(), - Self::FruitId => ColumnType::Integer.def().null(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Fruit => Entity::belongs_to(super::fruit::Entity) - .from(Column::FruitId) - .to(super::fruit::Column::Id) - .into(), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Fruit.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/main.rs b/examples/cli/src/main.rs deleted file mode 100644 index 1a7baf89..00000000 --- a/examples/cli/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod entity; - -fn main() {} diff --git a/examples/sqlx-mysql/Cargo.toml b/examples/sqlx-mysql/Cargo.toml deleted file mode 100644 index 7954fb69..00000000 --- a/examples/sqlx-mysql/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "sea-orm-sqlx-mysql-example" -version = "0.1.0" -edition = "2018" -publish = false - -[dependencies] -async-std = { version = "^1.9", features = [ "attributes" ] } -sea-orm = { path = "../../" } -serde_json = { version = "^1" } -futures = { version = "^0.3" } -async-stream = { version = "^0.3" } -futures-util = { version = "^0.3" } \ No newline at end of file diff --git a/examples/sqlx-mysql/Readme.md b/examples/sqlx-mysql/Readme.md deleted file mode 100644 index 98a14ada..00000000 --- a/examples/sqlx-mysql/Readme.md +++ /dev/null @@ -1,141 +0,0 @@ -# SeaORM SQLx MySql example - -Prepare: - -Setup a test database and configure the connection string in `main.rs`. -Run `bakery.sql` to setup the test table and data. - -Running: - -```sh -cargo run -``` - -All about selects: - -```sh -SqlxMySqlPoolConnection - -===== ===== - -find all cakes: SELECT `cake`.`id`, `cake`.`name` FROM `cake` - -Model { id: 1, name: "New York Cheese" } - -Model { id: 2, name: "Chocolate Forest" } - -find all fruits: SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` - -Model { id: 1, name: "Blueberry", cake_id: Some(1) } - -Model { id: 2, name: "Rasberry", cake_id: Some(1) } - -Model { id: 3, name: "Strawberry", cake_id: Some(2) } - -Model { id: 4, name: "Apple", cake_id: None } - -Model { id: 5, name: "Banana", cake_id: None } - -Model { id: 6, name: "Cherry", cake_id: None } - -Model { id: 7, name: "Lemon", cake_id: None } - -Model { id: 8, name: "Orange", cake_id: None } - -Model { id: 9, name: "Pineapple", cake_id: None } - -===== ===== - -find cakes and fruits: SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`, `fruit`.`id` AS `B_id`, `fruit`.`name` AS `B_name`, `fruit`.`cake_id` AS `B_cake_id` FROM `cake` LEFT JOIN `fruit` ON `cake`.`id` = `fruit`.`cake_id` - -(Model { id: 1, name: "New York Cheese" }, Some(Model { id: 1, name: "Blueberry", cake_id: Some(1) })) - -(Model { id: 1, name: "New York Cheese" }, Some(Model { id: 2, name: "Rasberry", cake_id: Some(1) })) - -(Model { id: 2, name: "Chocolate Forest" }, Some(Model { id: 3, name: "Strawberry", cake_id: Some(2) })) - -===== ===== - -find one by primary key: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 1 LIMIT 1 - -Model { id: 1, name: "New York Cheese" } - -find one by name: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%chocolate%' LIMIT 1 - -Some(Model { id: 2, name: "Chocolate Forest" }) - -find models belong to: SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id` WHERE `cake`.`id` = 1 - -Model { id: 1, name: "Blueberry", cake_id: Some(1) } - -Model { id: 2, name: "Rasberry", cake_id: Some(1) } - -===== ===== - -count fruits by cake: SELECT `cake`.`name`, COUNT(`fruit`.`id`) AS `num_of_fruits` FROM `cake` LEFT JOIN `fruit` ON `cake`.`id` = `fruit`.`cake_id` GROUP BY `cake`.`name` - -SelectResult { name: "New York Cheese", num_of_fruits: 2 } - -SelectResult { name: "Chocolate Forest", num_of_fruits: 1 } - -===== ===== - -find cakes and fillings: SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`, `filling`.`id` AS `B_id`, `filling`.`name` AS `B_name` FROM `cake` LEFT JOIN `cake_filling` ON `cake`.`id` = `cake_filling`.`cake_id` LEFT JOIN `filling` ON `cake_filling`.`filling_id` = `filling`.`id` ORDER BY `cake`.`id` ASC - -(Model { id: 1, name: "New York Cheese" }, [Model { id: 1, name: "Vanilla" }, Model { id: 2, name: "Lemon" }]) - -(Model { id: 2, name: "Chocolate Forest" }, [Model { id: 2, name: "Lemon" }, Model { id: 3, name: "Mango" }]) - -find fillings for cheese cake: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 1 LIMIT 1 -SELECT `filling`.`id`, `filling`.`name` FROM `filling` INNER JOIN `cake_filling` ON `cake_filling`.`filling_id` = `filling`.`id` INNER JOIN `cake` ON `cake`.`id` = `cake_filling`.`cake_id` WHERE `cake`.`id` = 1 - -Model { id: 1, name: "Vanilla" } - -Model { id: 2, name: "Lemon" } - -find cakes for lemon: SELECT `filling`.`id`, `filling`.`name` FROM `filling` WHERE `filling`.`id` = 2 LIMIT 1 -SELECT `cake`.`id`, `cake`.`name` FROM `cake` INNER JOIN `cake_filling` ON `cake_filling`.`cake_id` = `cake`.`id` INNER JOIN `filling` ON `filling`.`id` = `cake_filling`.`filling_id` WHERE `filling`.`id` = 2 - -Model { id: 1, name: "New York Cheese" } - -Model { id: 2, name: "Chocolate Forest" } - -``` - -All about operations: - -```sh -INSERT INTO `fruit` (`name`) VALUES ('pear') - -Inserted: InsertResult { last_insert_id: 21 } - -SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = 21 LIMIT 1 - -Pear: Some(Model { id: 21, name: "pear", cake_id: None }) - -UPDATE `fruit` SET `name` = 'Sweet pear' WHERE `fruit`.`id` = 21 - -Updated: ActiveModel { id: ActiveValue { value: 21, state: Unchanged }, name: ActiveValue { value: "Sweet pear", state: Set }, cake_id: ActiveValue { value: None, state: Unchanged } } - -===== ===== - -INSERT INTO `fruit` (`name`) VALUES ('banana') -SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = 22 LIMIT 1 - -Inserted: ActiveModel { id: ActiveValue { value: 22, state: Unchanged }, name: ActiveValue { value: "banana", state: Unchanged }, cake_id: ActiveValue { value: None, state: Unchanged } } - -UPDATE `fruit` SET `name` = 'banana banana' WHERE `fruit`.`id` = 22 - -Updated: ActiveModel { id: ActiveValue { value: 22, state: Unchanged }, name: ActiveValue { value: "banana banana", state: Set }, cake_id: ActiveValue { value: None, state: Unchanged } } - -DELETE FROM `fruit` WHERE `fruit`.`id` = 22 - -Deleted: DeleteResult { rows_affected: 1 } - -===== ===== - -INSERT INTO `fruit` (`name`) VALUES ('pineapple') -SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = 23 LIMIT 1 - -Saved: ActiveModel { id: ActiveValue { value: 23, state: Unchanged }, name: ActiveValue { value: "pineapple", state: Unchanged } } -``` \ No newline at end of file diff --git a/examples/sqlx-mysql/import.sh b/examples/sqlx-mysql/import.sh deleted file mode 100644 index 7e4820c1..00000000 --- a/examples/sqlx-mysql/import.sh +++ /dev/null @@ -1,7 +0,0 @@ -cp ../../src/tests_cfg/cake.rs src/example_cake.rs -cp ../../src/tests_cfg/fruit.rs src/example_fruit.rs -cp ../../src/tests_cfg/filling.rs src/example_filling.rs -cp ../../src/tests_cfg/cake_filling.rs src/example_cake_filling.rs - -sed -i 's/^use crate::/use sea_orm::/g' src/*.rs -sed -i '/^use crate as sea_orm;/d' src/*.rs \ No newline at end of file diff --git a/examples/sqlx-mysql/src/entities.rs b/examples/sqlx-mysql/src/entities.rs deleted file mode 100644 index 40a4bd9a..00000000 --- a/examples/sqlx-mysql/src/entities.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub use super::cake::Entity as Cake; -pub use super::cake_filling::Entity as CakeFilling; -pub use super::filling::Entity as Filling; -pub use super::fruit::Entity as Fruit; diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs deleted file mode 100644 index 475315e8..00000000 --- a/examples/sqlx-mysql/src/example_cake.rs +++ /dev/null @@ -1,75 +0,0 @@ -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "cake" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Fruit, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(None).def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Fruit => Entity::has_many(super::fruit::Entity).into(), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Fruit.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - super::cake_filling::Relation::Filling.def() - } - - fn via() -> Option { - Some(super::cake_filling::Relation::Cake.def().rev()) - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_cake_filling.rs b/examples/sqlx-mysql/src/example_cake_filling.rs deleted file mode 100644 index 19de83e4..00000000 --- a/examples/sqlx-mysql/src/example_cake_filling.rs +++ /dev/null @@ -1,68 +0,0 @@ -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "cake_filling" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub cake_id: i32, - pub filling_id: i32, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - CakeId, - FillingId, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - CakeId, - FillingId, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - false - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Cake, - Filling, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - - fn def(&self) -> ColumnDef { - match self { - Self::CakeId => ColumnType::Integer.def(), - Self::FillingId => ColumnType::Integer.def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Cake => Entity::belongs_to(super::cake::Entity) - .from(Column::CakeId) - .to(super::cake::Column::Id) - .into(), - Self::Filling => Entity::belongs_to(super::filling::Entity) - .from(Column::FillingId) - .to(super::filling::Column::Id) - .into(), - } - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs deleted file mode 100644 index 925b92fc..00000000 --- a/examples/sqlx-mysql/src/example_filling.rs +++ /dev/null @@ -1,65 +0,0 @@ -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "filling" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation {} - -impl ColumnTrait for Column { - type EntityName = Entity; - - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(None).def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - super::cake_filling::Relation::Cake.def() - } - - fn via() -> Option { - Some(super::cake_filling::Relation::Filling.def().rev()) - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_fruit.rs b/examples/sqlx-mysql/src/example_fruit.rs deleted file mode 100644 index b875da24..00000000 --- a/examples/sqlx-mysql/src/example_fruit.rs +++ /dev/null @@ -1,71 +0,0 @@ -use sea_orm::entity::prelude::*; - -#[derive(Copy, Clone, Default, Debug, DeriveEntity)] -pub struct Entity; - -impl EntityName for Entity { - fn table_name(&self) -> &str { - "fruit" - } -} - -#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] -pub struct Model { - pub id: i32, - pub name: String, - pub cake_id: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] -pub enum Column { - Id, - Name, - CakeId, -} - -#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] -pub enum PrimaryKey { - Id, -} - -impl PrimaryKeyTrait for PrimaryKey { - fn auto_increment() -> bool { - true - } -} - -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation { - Cake, -} - -impl ColumnTrait for Column { - type EntityName = Entity; - - fn def(&self) -> ColumnDef { - match self { - Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(None).def(), - Self::CakeId => ColumnType::Integer.def(), - } - } -} - -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Cake => Entity::belongs_to(super::cake::Entity) - .from(Column::CakeId) - .to(super::cake::Column::Id) - .into(), - } - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Cake.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/main.rs b/examples/sqlx-mysql/src/main.rs deleted file mode 100644 index e9a2a737..00000000 --- a/examples/sqlx-mysql/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -use sea_orm::Database; - -mod entities; -mod example_cake; -mod example_cake_filling; -mod example_filling; -mod example_fruit; -mod operation; -mod select; - -use entities::*; -use example_cake as cake; -use example_cake_filling as cake_filling; -use example_filling as filling; -use example_fruit as fruit; -use operation::*; -use select::*; - -#[async_std::main] -async fn main() { - let db = Database::connect("mysql://sea:sea@localhost/bakery") - .await - .unwrap(); - - println!("{:?}\n", db); - - println!("===== =====\n"); - - all_about_select(&db).await.unwrap(); - - println!("===== =====\n"); - - all_about_operation(&db).await.unwrap(); -} diff --git a/examples/sqlx-mysql/src/operation.rs b/examples/sqlx-mysql/src/operation.rs deleted file mode 100644 index b1273e10..00000000 --- a/examples/sqlx-mysql/src/operation.rs +++ /dev/null @@ -1,94 +0,0 @@ -use super::*; -use sea_orm::{entity::*, error::*, query::*, DbConn}; - -pub async fn all_about_operation(db: &DbConn) -> Result<(), DbErr> { - insert_and_update(db).await?; - - println!("===== =====\n"); - - save_active_model(db).await?; - - println!("===== =====\n"); - - save_custom_active_model(db).await?; - - Ok(()) -} - -pub async fn insert_and_update(db: &DbConn) -> Result<(), DbErr> { - let pear = fruit::ActiveModel { - name: Set("pear".to_owned()), - ..Default::default() - }; - let res: InsertResult = Fruit::insert(pear).exec(db).await?; - - println!(); - println!("Inserted: last_insert_id = {}\n", res.last_insert_id); - - let pear: Option = Fruit::find_by_id(res.last_insert_id).one(db).await?; - - println!(); - println!("Pear: {:?}\n", pear); - - let mut pear: fruit::ActiveModel = pear.unwrap().into(); - pear.name = Set("Sweet pear".to_owned()); - - let pear: fruit::ActiveModel = Fruit::update(pear).exec(db).await?; - - println!(); - println!("Updated: {:?}\n", pear); - - Ok(()) -} - -pub async fn save_active_model(db: &DbConn) -> Result<(), DbErr> { - let banana = fruit::ActiveModel { - name: Set("Banana".to_owned()), - ..Default::default() - }; - let mut banana = banana.save(db).await?; - - println!(); - println!("Inserted: {:?}\n", banana); - - banana.name = Set("Banana Mongo".to_owned()); - - let banana = banana.save(db).await?; - - println!(); - println!("Updated: {:?}\n", banana); - - let result = banana.delete(db).await?; - - println!(); - println!("Deleted: {:?}\n", result); - - Ok(()) -} - -mod form { - use super::fruit::*; - use sea_orm::entity::prelude::*; - - #[derive( - Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, DeriveActiveModelBehavior, - )] - pub struct Model { - pub id: i32, - pub name: String, - } -} - -async fn save_custom_active_model(db: &DbConn) -> Result<(), DbErr> { - let pineapple = form::ActiveModel { - id: Unset(None), - name: Set("Pineapple".to_owned()), - }; - - let pineapple = pineapple.save(db).await?; - - println!(); - println!("Saved: {:?}\n", pineapple); - - Ok(()) -} diff --git a/examples/sqlx-mysql/src/select.rs b/examples/sqlx-mysql/src/select.rs deleted file mode 100644 index 9b2cf15c..00000000 --- a/examples/sqlx-mysql/src/select.rs +++ /dev/null @@ -1,310 +0,0 @@ -use super::*; -use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult}; - -pub async fn all_about_select(db: &DbConn) -> Result<(), DbErr> { - find_all(db).await?; - - println!("===== =====\n"); - - find_together(db).await?; - - println!("===== =====\n"); - - find_one(db).await?; - - println!("===== =====\n"); - - count_fruits_by_cake(db).await?; - - println!("===== =====\n"); - - find_many_to_many(db).await?; - - if false { - println!("===== =====\n"); - - all_about_select_json(db).await?; - } - - println!("===== =====\n"); - - find_all_stream(&db).await.unwrap(); - - println!("===== =====\n"); - - find_first_page(&db).await.unwrap(); - - println!("===== =====\n"); - - find_num_pages(&db).await.unwrap(); - - Ok(()) -} - -async fn find_all(db: &DbConn) -> Result<(), DbErr> { - print!("find all cakes: "); - - let cakes: Vec = Cake::find().all(db).await?; - - println!(); - for cc in cakes.iter() { - println!("{:?}\n", cc); - } - - print!("find all fruits: "); - - let fruits = Fruit::find().all(db).await?; - - println!(); - for ff in fruits.iter() { - println!("{:?}\n", ff); - } - - Ok(()) -} - -async fn find_together(db: &DbConn) -> Result<(), DbErr> { - print!("find cakes and fruits: "); - - let both = Cake::find().find_also_related(Fruit).all(db).await?; - - println!(); - for bb in both.iter() { - println!("{:?}\n", bb); - } - - Ok(()) -} - -impl Cake { - fn find_by_name(name: &str) -> Select { - Self::find().filter(cake::Column::Name.contains(name)) - } -} - -async fn find_one(db: &DbConn) -> Result<(), DbErr> { - print!("find one by primary key: "); - - let cheese: Option = Cake::find_by_id(1).one(db).await?; - let cheese = cheese.unwrap(); - - println!(); - println!("{:?}", cheese); - println!(); - - print!("find one by name: "); - - let chocolate = Cake::find_by_name("chocolate").one(db).await?; - - println!(); - println!("{:?}", chocolate); - println!(); - - print!("find models belong to: "); - - let fruits = cheese.find_related(Fruit).all(db).await?; - - println!(); - for ff in fruits.iter() { - println!("{:?}\n", ff); - } - - Ok(()) -} - -async fn count_fruits_by_cake(db: &DbConn) -> Result<(), DbErr> { - #[derive(Debug, FromQueryResult)] - struct SelectResult { - name: String, - num_of_fruits: i32, - } - - print!("count fruits by cake: "); - - let select = Cake::find() - .left_join(Fruit) - .select_only() - .column(cake::Column::Name) - .column_as(fruit::Column::Id.count(), "num_of_fruits") - .group_by(cake::Column::Name); - - let results = select.into_model::().all(db).await?; - - println!(); - for rr in results.iter() { - println!("{:?}\n", rr); - } - - Ok(()) -} - -async fn find_many_to_many(db: &DbConn) -> Result<(), DbErr> { - print!("find cakes and fillings: "); - - let both: Vec<(cake::Model, Vec)> = - Cake::find().find_with_related(Filling).all(db).await?; - - println!(); - for bb in both.iter() { - println!("{:?}\n", bb); - } - - print!("find fillings for cheese cake: "); - - let cheese = Cake::find_by_id(1).one(db).await?; - - if let Some(cheese) = cheese { - let fillings: Vec = cheese.find_related(Filling).all(db).await?; - - println!(); - for ff in fillings.iter() { - println!("{:?}\n", ff); - } - } - - print!("find cakes for lemon: "); - - let lemon = Filling::find_by_id(2).one(db).await?; - - if let Some(lemon) = lemon { - let cakes: Vec = lemon.find_related(Cake).all(db).await?; - - println!(); - for cc in cakes.iter() { - println!("{:?}\n", cc); - } - } - - Ok(()) -} - -async fn all_about_select_json(db: &DbConn) -> Result<(), DbErr> { - find_all_json(&db).await?; - - println!("===== =====\n"); - - find_together_json(&db).await?; - - println!("===== =====\n"); - - count_fruits_by_cake_json(&db).await?; - - Ok(()) -} - -async fn find_all_json(db: &DbConn) -> Result<(), DbErr> { - print!("find all cakes: "); - - let cakes = Cake::find().into_json().all(db).await?; - - println!("\n{}\n", serde_json::to_string_pretty(&cakes).unwrap()); - - print!("find all fruits: "); - - let fruits = Fruit::find().into_json().all(db).await?; - - println!("\n{}\n", serde_json::to_string_pretty(&fruits).unwrap()); - - Ok(()) -} - -async fn find_together_json(db: &DbConn) -> Result<(), DbErr> { - print!("find cakes and fruits: "); - - let cakes_fruits = Cake::find() - .find_with_related(Fruit) - .into_json() - .all(db) - .await?; - - println!( - "\n{}\n", - serde_json::to_string_pretty(&cakes_fruits).unwrap() - ); - - Ok(()) -} - -async fn count_fruits_by_cake_json(db: &DbConn) -> Result<(), DbErr> { - print!("count fruits by cake: "); - - let count = Cake::find() - .left_join(Fruit) - .select_only() - .column(cake::Column::Name) - .column_as(fruit::Column::Id.count(), "num_of_fruits") - .group_by(cake::Column::Name) - .into_json() - .all(db) - .await?; - - println!("\n{}\n", serde_json::to_string_pretty(&count).unwrap()); - - Ok(()) -} - -async fn find_all_stream(db: &DbConn) -> Result<(), DbErr> { - use async_std::task::sleep; - use futures::TryStreamExt; - use std::time::Duration; - - println!("find all cakes: "); - let mut cake_paginator = cake::Entity::find().paginate(db, 2); - while let Some(cake_res) = cake_paginator.fetch_and_next().await? { - for cake in cake_res { - println!("{:?}", cake); - } - } - - println!(); - println!("find all fruits: "); - let mut fruit_paginator = fruit::Entity::find().paginate(db, 2); - while let Some(fruit_res) = fruit_paginator.fetch_and_next().await? { - for fruit in fruit_res { - println!("{:?}", fruit); - } - } - - println!(); - println!("find all fruits with stream: "); - let mut fruit_stream = fruit::Entity::find().paginate(db, 2).into_stream(); - while let Some(fruits) = fruit_stream.try_next().await? { - for fruit in fruits { - println!("{:?}", fruit); - } - sleep(Duration::from_millis(250)).await; - } - - println!(); - println!("find all fruits in json with stream: "); - let mut json_stream = fruit::Entity::find() - .into_json() - .paginate(db, 2) - .into_stream(); - while let Some(jsons) = json_stream.try_next().await? { - for json in jsons { - println!("{:?}", json); - } - sleep(Duration::from_millis(250)).await; - } - - Ok(()) -} - -async fn find_first_page(db: &DbConn) -> Result<(), DbErr> { - println!("fruits first page: "); - let page = fruit::Entity::find().paginate(db, 2).fetch_page(0).await?; - for fruit in page { - println!("{:?}", fruit); - } - - Ok(()) -} - -async fn find_num_pages(db: &DbConn) -> Result<(), DbErr> { - println!("fruits number of page: "); - let num_pages = fruit::Entity::find().paginate(db, 2).num_pages().await?; - println!("{:?}", num_pages); - - Ok(()) -} diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 13c56cc1..c9b6cecc 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -19,6 +19,7 @@ path = "src/main.rs" clap = { version = "^2.33.3" } dotenv = { version = "^0.15" } async-std = { version = "^1.9", features = [ "attributes" ] } +sea-orm = { path = "..", features = [ "sqlx-all" ] } sea-orm-codegen = { path = "../sea-orm-codegen" } sea-schema = { version = "^0.2.4", default-features = false, features = [ "sqlx-mysql", diff --git a/sea-orm-codegen/Cargo.toml b/sea-orm-codegen/Cargo.toml index f78914ba..8679a8ee 100644 --- a/sea-orm-codegen/Cargo.toml +++ b/sea-orm-codegen/Cargo.toml @@ -26,7 +26,3 @@ syn = { version = "^1", default-features = false, features = [ quote = "^1" heck = "^0.3" proc-macro2 = "^1" - -[dev-dependencies] -async-std = { version = "^1.9", features = ["attributes"] } -sea-orm = { path = "../" } diff --git a/sea-orm-codegen/tests/mod.rs b/sea-orm-codegen/tests/mod.rs deleted file mode 100644 index efc0a4c7..00000000 --- a/sea-orm-codegen/tests/mod.rs +++ /dev/null @@ -1,132 +0,0 @@ -mod entity; - -use entity::*; - -use sea_orm::{entity::*, error::*, DbBackend, MockDatabase, MockExecResult, Transaction}; - -#[async_std::test] -async fn test_insert() -> Result<(), DbErr> { - let exec_result = MockExecResult { - last_insert_id: 1, - rows_affected: 1, - }; - - let db = MockDatabase::new(DbBackend::Postgres) - .append_exec_results(vec![exec_result.clone()]) - .into_connection(); - - let apple = cake::ActiveModel { - name: Set("Apple Pie".to_owned()), - ..Default::default() - }; - - let insert_result = cake::Entity::insert(apple).exec(&db).await?; - - assert_eq!(insert_result.last_insert_id, exec_result.last_insert_id); - - assert_eq!( - db.into_transaction_log(), - vec![Transaction::from_sql_and_values( - DbBackend::Postgres, - r#"INSERT INTO "cake" ("name") VALUES ($1)"#, - vec!["Apple Pie".into()] - )] - ); - - Ok(()) -} - -#[async_std::test] -async fn test_select() -> Result<(), DbErr> { - let query_results = vec![cake_filling::Model { - cake_id: 2, - filling_id: 3, - }]; - - let db = MockDatabase::new(DbBackend::Postgres) - .append_query_results(vec![query_results.clone()]) - .into_connection(); - - let selected_models = cake_filling::Entity::find_by_id((2, 3)).all(&db).await?; - - assert_eq!(selected_models, query_results); - - assert_eq!( - db.into_transaction_log(), - vec![Transaction::from_sql_and_values( - DbBackend::Postgres, - [ - r#"SELECT "cake_filling"."cake_id", "cake_filling"."filling_id" FROM "cake_filling""#, - r#"WHERE "cake_filling"."cake_id" = $1 AND "cake_filling"."filling_id" = $2"#, - ].join(" ").as_str(), - vec![2i32.into(), 3i32.into()] - )] - ); - - Ok(()) -} - -#[async_std::test] -async fn test_update() -> Result<(), DbErr> { - let exec_result = MockExecResult { - last_insert_id: 1, - rows_affected: 1, - }; - - let db = MockDatabase::new(DbBackend::Postgres) - .append_exec_results(vec![exec_result.clone()]) - .into_connection(); - - let orange = fruit::ActiveModel { - id: Set(1), - name: Set("Orange".to_owned()), - ..Default::default() - }; - - let updated_model = fruit::Entity::update(orange.clone()).exec(&db).await?; - - assert_eq!(updated_model, orange); - - assert_eq!( - db.into_transaction_log(), - vec![Transaction::from_sql_and_values( - DbBackend::Postgres, - r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2"#, - vec!["Orange".into(), 1i32.into()] - )] - ); - - Ok(()) -} - -#[async_std::test] -async fn test_delete() -> Result<(), DbErr> { - let exec_result = MockExecResult { - last_insert_id: 1, - rows_affected: 1, - }; - - let db = MockDatabase::new(DbBackend::Postgres) - .append_exec_results(vec![exec_result.clone()]) - .into_connection(); - - let orange = fruit::ActiveModel { - id: Set(3), - ..Default::default() - }; - - let delete_result = fruit::Entity::delete(orange).exec(&db).await?; - - assert_eq!(delete_result.rows_affected, exec_result.rows_affected); - - assert_eq!( - db.into_transaction_log(), - vec![Transaction::from_sql_and_values( - DbBackend::Postgres, - r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#, - vec![3i32.into()] - )] - ); - - Ok(()) -} diff --git a/examples/bakery.sql b/src/tests_cfg/bakery.sql similarity index 100% rename from examples/bakery.sql rename to src/tests_cfg/bakery.sql