From 3727f7b8b57272b4bab391b74ff61c9c32b19398 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 25 Aug 2022 12:51:22 +0800 Subject: [PATCH 01/15] [examples] create input model with `DeriveIntoActiveModel` (#975) --- examples/basic/src/operation.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/basic/src/operation.rs b/examples/basic/src/operation.rs index 037103f21..cc4aee4a3 100644 --- a/examples/basic/src/operation.rs +++ b/examples/basic/src/operation.rs @@ -70,20 +70,17 @@ mod form { use super::fruit::*; use sea_orm::entity::prelude::*; - #[derive( - Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel, DeriveActiveModelBehavior, - )] - pub struct Model { - pub id: i32, + #[derive(Clone, Debug, PartialEq, Eq, DeriveIntoActiveModel)] + pub struct InputModel { pub name: String, } } async fn save_custom_active_model(db: &DbConn) -> Result<(), DbErr> { - let pineapple = form::ActiveModel { - id: NotSet, - name: Set("Pineapple".to_owned()), - }; + let pineapple = form::InputModel { + name: "Pineapple".to_owned(), + } + .into_active_model(); let pineapple = pineapple.save(db).await?; From 9b6b879122c5e0d3c667e18019869fa5b7c5cb7a Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 25 Aug 2022 13:56:41 +0800 Subject: [PATCH 02/15] Migrator test cases with tracing_subscriber --- sea-orm-migration/tests/main.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sea-orm-migration/tests/main.rs b/sea-orm-migration/tests/main.rs index 13f0b4045..2dfcf93df 100644 --- a/sea-orm-migration/tests/main.rs +++ b/sea-orm-migration/tests/main.rs @@ -6,6 +6,11 @@ use sea_orm_migration::prelude::*; #[async_std::test] async fn main() -> Result<(), DbErr> { + tracing_subscriber::fmt() + .with_max_level(tracing::Level::DEBUG) + .with_test_writer() + .init(); + let url = std::env::var("DATABASE_URL").expect("Environment variable 'DATABASE_URL' not set"); let db_name = "sea_orm_migration"; let db = Database::connect(&url).await?; From d016f66a5fd20b79d166172210099b74d307f07f Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 2 Sep 2022 15:12:16 +0800 Subject: [PATCH 03/15] Cargo.toml specify SeaORM homepage --- Cargo.toml | 1 + sea-orm-cli/Cargo.toml | 1 + sea-orm-codegen/Cargo.toml | 1 + sea-orm-macros/Cargo.toml | 1 + sea-orm-migration/Cargo.toml | 1 + 5 files changed, 5 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 277a2a684..f119bc756 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ authors = ["Chris Tsang "] edition = "2021" description = "🐚 An async & dynamic ORM for Rust" license = "MIT OR Apache-2.0" +homepage = "https://www.sea-ql.org/SeaORM" documentation = "https://docs.rs/sea-orm" repository = "https://github.com/SeaQL/sea-orm" categories = ["database"] diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 9520da952..f1e26a564 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -8,6 +8,7 @@ authors = [ "Billy Chan " ] edition = "2021" description = "Command line utility for SeaORM" license = "MIT OR Apache-2.0" +homepage = "https://www.sea-ql.org/SeaORM" documentation = "https://docs.rs/sea-orm" repository = "https://github.com/SeaQL/sea-orm" categories = [ "database" ] diff --git a/sea-orm-codegen/Cargo.toml b/sea-orm-codegen/Cargo.toml index ce0a09233..32d539867 100644 --- a/sea-orm-codegen/Cargo.toml +++ b/sea-orm-codegen/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Billy Chan "] edition = "2021" description = "Code Generator for SeaORM" license = "MIT OR Apache-2.0" +homepage = "https://www.sea-ql.org/SeaORM" documentation = "https://docs.rs/sea-orm" repository = "https://github.com/SeaQL/sea-orm" categories = ["database"] diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index 47abdb775..dbbbcc02c 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -5,6 +5,7 @@ authors = [ "Billy Chan " ] edition = "2021" description = "Derive macros for SeaORM" license = "MIT OR Apache-2.0" +homepage = "https://www.sea-ql.org/SeaORM" documentation = "https://docs.rs/sea-orm" repository = "https://github.com/SeaQL/sea-orm" categories = [ "database" ] diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index f0f8c5d42..2e3585022 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -8,6 +8,7 @@ authors = [ "Billy Chan " ] edition = "2021" description = "Migration utility for SeaORM" license = "MIT OR Apache-2.0" +homepage = "https://www.sea-ql.org/SeaORM" documentation = "https://docs.rs/sea-orm" repository = "https://github.com/SeaQL/sea-orm" categories = [ "database" ] From 1ff53d81d8e81034fb7953744e7c4205ac9eb8db Mon Sep 17 00:00:00 2001 From: Samuel Boyden Date: Sun, 4 Sep 2022 16:15:16 +0100 Subject: [PATCH 04/15] Add CodeCTRL to COMMUNITY.md (#1015) --- COMMUNITY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/COMMUNITY.md b/COMMUNITY.md index 12f1e367b..6c634e43e 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -11,6 +11,7 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [Sensei](https://l2.technology/sensei) ([repository](https://github.com/L2-Technology/sensei)) | A Bitcoin lightning node implementation - [Spyglass](https://docs.spyglass.fyi/) ([repository](https://github.com/a5huynh/spyglass)) | 🔭 A personal search engine that indexes what you want w/ a simple set of rules. - [My Data My Consent](https://mydatamyconsent.com/) | Online data sharing for people and businesses simplified +- [CodeCTRL](https://codectrl.authentura.com) ([repository](https://github.com/Authentura/codectrl)) | A self-hostable code logging platform ### Frameworks From 9ea34984fb3449ad36bb196350db1b4c3722883e Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 4 Sep 2022 23:30:59 +0800 Subject: [PATCH 05/15] Update COMMUNITY.md --- COMMUNITY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/COMMUNITY.md b/COMMUNITY.md index 6c634e43e..a0072acd9 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -51,3 +51,4 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [service_auth](https://github.com/shorii/service_auth) | A simple JWT authentication web-application - [rj45less-server](https://github.com/pmnxis/rj45less-server) | A simple unique number allocator for custom router - [SophyCore](https://github.com/FarDragi/SophyCore) | Main system that centralizes all rules, to be used by both the discord bot and the future site +- [lldap](https://github.com/nitnelave/lldap) | Light LDAP implementation for authentication From a0bd359c650ea88809bd7edc4a32f526aa0ecac1 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 4 Sep 2022 23:41:01 +0800 Subject: [PATCH 06/15] Update COMMUNITY.md --- COMMUNITY.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/COMMUNITY.md b/COMMUNITY.md index a0072acd9..db354aadb 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -7,9 +7,9 @@ If you have built an app using SeaORM and want to showcase it, feel free to open ### Startups - [Caido](https://caido.io/) | A lightweight web security auditing toolkit -- [Svix](https://www.svix.com/) ([repository](https://github.com/svix/svix-webhooks)) | The enterprise ready webhooks service -- [Sensei](https://l2.technology/sensei) ([repository](https://github.com/L2-Technology/sensei)) | A Bitcoin lightning node implementation -- [Spyglass](https://docs.spyglass.fyi/) ([repository](https://github.com/a5huynh/spyglass)) | 🔭 A personal search engine that indexes what you want w/ a simple set of rules. +- [Svix](https://www.svix.com/) ([repository](https://github.com/svix/svix-webhooks)) ![GitHub stars](https://img.shields.io/github/stars/svix/svix-webhooks.svg?style=social) | The enterprise ready webhooks service +- [Sensei](https://l2.technology/sensei) ([repository](https://github.com/L2-Technology/sensei)) ![GitHub stars](https://img.shields.io/github/stars/L2-Technology/sensei.svg?style=social) | A Bitcoin lightning node implementation +- [Spyglass](https://docs.spyglass.fyi/) ([repository](https://github.com/a5huynh/spyglass)) ![GitHub stars](https://img.shields.io/github/stars/a5huynh/spyglass.svg?style=social) | 🔭 A personal search engine that indexes what you want w/ a simple set of rules. - [My Data My Consent](https://mydatamyconsent.com/) | Online data sharing for people and businesses simplified - [CodeCTRL](https://codectrl.authentura.com) ([repository](https://github.com/Authentura/codectrl)) | A self-hostable code logging platform @@ -32,7 +32,7 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [mediarepo](https://mediarepo.trivernis.dev) ([repository](https://github.com/Trivernis/mediarepo)) | A tag-based media management application - [THUBurrow](https://thuburrow.com) ([repository](https://github.com/BobAnkh/THUBurrow)) | A campus forum built by Next.js and Rocket - [Backpack](https://github.com/JSH32/Backpack) | Open source self hosted file sharing platform on crack -- [Stump](https://github.com/aaronleopold/stump) | A free and open source comics server with OPDS support +- [Stump](https://github.com/aaronleopold/stump) ![GitHub stars](https://img.shields.io/github/stars/aaronleopold/stump.svg?style=social) | A free and open source comics server with OPDS support - [mugen](https://github.com/koopa1338/mugen-dms) | DMS written in 🦀 - [JinShu](https://github.com/gengteng/jinshu) | A cross-platform **I**nstant **M**essaging system written in 🦀 - [rust-juniper-playground](https://github.com/Yama-Tomo/rust-juniper-playground) | juniper with SeaORM example @@ -40,7 +40,7 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [pansy](https://github.com/niuhuan/pansy) | An illustrations app using SeaORM, SQLite, flutter. runs on the desktop and mobile terminals - [Orca](https://github.com/workfoxes/orca) | An No-code Test Automation platfrom using actix, SeaORM, react. runs on the desktop and cloud - [symbols](https://github.com/nappa85/symbols) | A proc-macro utility to populates enum variants with primary keys values -- [Warpgate](https://github.com/warp-tech/warpgate) | Smart SSH bastion that works with any SSH client +- [Warpgate](https://github.com/warp-tech/warpgate) ![GitHub stars](https://img.shields.io/github/stars/warp-tech/warpgate.svg?style=social) | Smart SSH bastion that works with any SSH client - [suzuya](https://github.com/SH11235/suzuya) | A merchandise management application using SeaORM, Actix-Web, Tera - [snmp-sim-rust](https://github.com/sonalake/snmp-sim-rust) | SNMP Simulator - [template_flow](https://github.com/hilary888/template_flow) | An experiment exploring replacing placeholders in pre-prepared templates with their actual values @@ -51,4 +51,4 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [service_auth](https://github.com/shorii/service_auth) | A simple JWT authentication web-application - [rj45less-server](https://github.com/pmnxis/rj45less-server) | A simple unique number allocator for custom router - [SophyCore](https://github.com/FarDragi/SophyCore) | Main system that centralizes all rules, to be used by both the discord bot and the future site -- [lldap](https://github.com/nitnelave/lldap) | Light LDAP implementation for authentication +- [lldap](https://github.com/nitnelave/lldap) ![GitHub stars](https://img.shields.io/github/stars/nitnelave/lldap.svg?style=social) | Light LDAP implementation for authentication From a80c33ef4c46279353625e22007171e7331e8cc5 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 5 Sep 2022 00:38:12 +0800 Subject: [PATCH 07/15] Rewrite enum and text casting (#973) * Rewrite enum and text casting * Add doc tests * Refactoring --- src/entity/column.rs | 17 ++++----- src/executor/insert.rs | 22 ++++------- src/executor/update.rs | 18 +++------ src/query/combine.rs | 18 ++------- src/query/helper.rs | 55 ++++++++++++++++++++++++++- src/query/insert.rs | 13 ++----- src/query/join.rs | 13 ++----- src/query/select.rs | 19 +++------ src/query/update.rs | 14 ++----- src/tests_cfg/lunch_set.rs | 23 +++++++++++ src/tests_cfg/mod.rs | 3 ++ src/tests_cfg/sea_orm_active_enums.rs | 11 ++++++ 12 files changed, 132 insertions(+), 94 deletions(-) create mode 100644 src/tests_cfg/lunch_set.rs create mode 100644 src/tests_cfg/sea_orm_active_enums.rs diff --git a/src/entity/column.rs b/src/entity/column.rs index 8dbff6ea4..92e32fb6c 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -1,5 +1,5 @@ -use crate::{EntityName, IdenStatic, Iterable}; -use sea_query::{Alias, BinOper, DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value}; +use crate::{cast_text_as_enum, EntityName, IdenStatic, IntoSimpleExpr, Iterable}; +use sea_query::{BinOper, DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value}; use std::str::FromStr; /// Defines a Column for an Entity @@ -100,13 +100,7 @@ macro_rules! bind_oper_with_enum_casting { where V: Into, { - let val = Expr::val(v); - let col_def = self.def(); - let col_type = col_def.get_column_type(); - let expr = match col_type.get_enum_name() { - Some(enum_name) => val.as_enum(Alias::new(enum_name)), - None => val.into(), - }; + let expr = cast_text_as_enum(Expr::val(v), self); Expr::tbl(self.entity_name(), *self).binary(BinOper::$bin_op, expr) } }; @@ -305,6 +299,11 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr { bind_subquery_func!(in_subquery); bind_subquery_func!(not_in_subquery); + + /// Construct a [`SimpleExpr::Column`] wrapped in [`Expr`]. + fn into_expr(self) -> Expr { + Expr::expr(self.into_simple_expr()) + } } impl ColumnType { diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 5f3f9c951..3c7b69c80 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -1,10 +1,8 @@ use crate::{ - error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, Insert, IntoActiveModel, - Iterable, PrimaryKeyTrait, SelectModel, SelectorRaw, Statement, TryFromU64, -}; -use sea_query::{ - Alias, Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple, + cast_enum_as_text, error::*, ActiveModelTrait, ConnectionTrait, EntityTrait, Insert, + IntoActiveModel, Iterable, PrimaryKeyTrait, SelectModel, SelectorRaw, Statement, TryFromU64, }; +use sea_query::{Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple}; use std::{future::Future, marker::PhantomData}; /// Defines a structure to perform INSERT operations in an ActiveModel @@ -149,16 +147,10 @@ where let db_backend = db.get_database_backend(); let found = match db.support_returning() { true => { - let returning = - Query::returning().exprs(::Column::iter().map(|c| { - let col = Expr::col(c); - let col_def = ColumnTrait::def(&c); - let col_type = col_def.get_column_type(); - match col_type.get_enum_name() { - Some(_) => col.as_enum(Alias::new("text")), - None => col.into(), - } - })); + let returning = Query::returning().exprs( + ::Column::iter() + .map(|c| cast_enum_as_text(Expr::col(c), &c)), + ); insert_statement.returning(returning); SelectorRaw::::Model>>::from_statement( db_backend.build(&insert_statement), diff --git a/src/executor/update.rs b/src/executor/update.rs index f00c536f9..a7b343a1a 100644 --- a/src/executor/update.rs +++ b/src/executor/update.rs @@ -1,8 +1,8 @@ use crate::{ - error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, IntoActiveModel, + cast_enum_as_text, error::*, ActiveModelTrait, ConnectionTrait, EntityTrait, IntoActiveModel, Iterable, SelectModel, SelectorRaw, Statement, UpdateMany, UpdateOne, }; -use sea_query::{Alias, Expr, FromValueTuple, Query, UpdateStatement}; +use sea_query::{Expr, FromValueTuple, Query, UpdateStatement}; use std::future::Future; /// Defines an update operation @@ -91,16 +91,10 @@ where { match db.support_returning() { true => { - let returning = - Query::returning().exprs(::Column::iter().map(|c| { - let col = Expr::col(c); - let col_def = c.def(); - let col_type = col_def.get_column_type(); - match col_type.get_enum_name() { - Some(_) => col.as_enum(Alias::new("text")), - None => col.into(), - } - })); + let returning = Query::returning().exprs( + ::Column::iter() + .map(|c| cast_enum_as_text(Expr::col(c), &c)), + ); query.returning(returning); let db_backend = db.get_database_backend(); let found: Option<::Model> = diff --git a/src/query/combine.rs b/src/query/combine.rs index ef32798ba..677d7aa0a 100644 --- a/src/query/combine.rs +++ b/src/query/combine.rs @@ -1,12 +1,10 @@ use crate::{ - ColumnTrait, EntityTrait, IdenStatic, IntoSimpleExpr, Iterable, QueryTrait, Select, SelectTwo, - SelectTwoMany, + cast_enum_as_text, ColumnTrait, EntityTrait, IdenStatic, Iterable, QueryTrait, Select, + SelectTwo, SelectTwoMany, }; use core::marker::PhantomData; pub use sea_query::JoinType; -use sea_query::{ - Alias, ColumnRef, DynIden, Expr, Iden, Order, SeaRc, SelectExpr, SelectStatement, SimpleExpr, -}; +use sea_query::{Alias, ColumnRef, Iden, Order, SeaRc, SelectExpr, SelectStatement, SimpleExpr}; macro_rules! select_def { ( $ident: ident, $str: expr ) => { @@ -148,18 +146,10 @@ where F: EntityTrait, S: QueryTrait, { - let text_type = SeaRc::new(Alias::new("text")) as DynIden; for col in ::iter() { - let col_def = col.def(); - let col_type = col_def.get_column_type(); let alias = format!("{}{}", SelectB.as_str(), col.as_str()); - let expr = Expr::expr(col.into_simple_expr()); - let expr = match col_type.get_enum_name() { - Some(_) => expr.as_enum(text_type.clone()), - None => expr.into(), - }; selector.query().expr(SelectExpr { - expr, + expr: cast_enum_as_text(col.into_expr(), &col), alias: Some(SeaRc::new(Alias::new(&alias))), window: None, }); diff --git a/src/query/helper.rs b/src/query/helper.rs index 6ab31d53b..f476d60cf 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -38,11 +38,34 @@ pub trait QuerySelect: Sized { /// r#"SELECT "cake"."name" FROM "cake""# /// ); /// ``` + /// + /// Enum column will be casted into text (PostgreSQL only) + /// + /// ``` + /// use sea_orm::{entity::*, query::*, tests_cfg::lunch_set, DbBackend}; + /// + /// assert_eq!( + /// lunch_set::Entity::find() + /// .select_only() + /// .column(lunch_set::Column::Tea) + /// .build(DbBackend::Postgres) + /// .to_string(), + /// r#"SELECT CAST("lunch_set"."tea" AS text) FROM "lunch_set""# + /// ); + /// assert_eq!( + /// lunch_set::Entity::find() + /// .select_only() + /// .column(lunch_set::Column::Tea) + /// .build(DbBackend::MySql) + /// .to_string(), + /// r#"SELECT `lunch_set`.`tea` FROM `lunch_set`"# + /// ); + /// ``` fn column(mut self, col: C) -> Self where C: ColumnTrait, { - self.query().expr(col.into_simple_expr()); + self.query().expr(cast_enum_as_text(col.into_expr(), &col)); self } @@ -532,3 +555,33 @@ pub(crate) fn unpack_table_alias(table_ref: &TableRef) -> Option { | TableRef::DatabaseSchemaTableAlias(_, _, _, alias) => Some(SeaRc::clone(alias)), } } + +#[derive(Iden)] +struct Text; + +pub(crate) fn cast_enum_as_text(expr: Expr, col: &C) -> SimpleExpr +where + C: ColumnTrait, +{ + cast_enum_text_inner(expr, col, |col, _| col.as_enum(Text)) +} + +pub(crate) fn cast_text_as_enum(expr: Expr, col: &C) -> SimpleExpr +where + C: ColumnTrait, +{ + cast_enum_text_inner(expr, col, |col, enum_name| col.as_enum(enum_name)) +} + +fn cast_enum_text_inner(expr: Expr, col: &C, f: F) -> SimpleExpr +where + C: ColumnTrait, + F: Fn(Expr, DynIden) -> SimpleExpr, +{ + let col_def = col.def(); + let col_type = col_def.get_column_type(); + match col_type.get_enum_name() { + Some(enum_name) => f(expr, SeaRc::new(Alias::new(enum_name))), + None => expr.into(), + } +} diff --git a/src/query/insert.rs b/src/query/insert.rs index a2b1c63f9..3ca6f82b0 100644 --- a/src/query/insert.rs +++ b/src/query/insert.rs @@ -1,9 +1,9 @@ use crate::{ - ActiveModelTrait, ColumnTrait, EntityName, EntityTrait, IntoActiveModel, Iterable, + cast_text_as_enum, ActiveModelTrait, EntityName, EntityTrait, IntoActiveModel, Iterable, PrimaryKeyTrait, QueryTrait, }; use core::marker::PhantomData; -use sea_query::{Alias, Expr, InsertStatement, OnConflict, ValueTuple}; +use sea_query::{Expr, InsertStatement, OnConflict, ValueTuple}; /// Performs INSERT operations on a ActiveModel #[derive(Debug)] @@ -134,14 +134,7 @@ where } if av_has_val { columns.push(col); - let val = Expr::val(av.into_value().unwrap()); - let col_def = col.def(); - let col_type = col_def.get_column_type(); - let expr = match col_type.get_enum_name() { - Some(enum_name) => val.as_enum(Alias::new(enum_name)), - None => val.into(), - }; - values.push(expr); + values.push(cast_text_as_enum(Expr::val(av.into_value().unwrap()), &col)); } } self.query.columns(columns); diff --git a/src/query/join.rs b/src/query/join.rs index d23c0333c..744ed3544 100644 --- a/src/query/join.rs +++ b/src/query/join.rs @@ -1,9 +1,9 @@ use crate::{ - join_tbl_on_condition, unpack_table_ref, ColumnTrait, EntityTrait, IdenStatic, Iterable, + cast_enum_as_text, join_tbl_on_condition, unpack_table_ref, EntityTrait, IdenStatic, Iterable, Linked, QuerySelect, Related, Select, SelectA, SelectB, SelectTwo, SelectTwoMany, }; pub use sea_query::JoinType; -use sea_query::{Alias, Condition, DynIden, Expr, IntoIden, SeaRc, SelectExpr}; +use sea_query::{Alias, Condition, Expr, IntoIden, SeaRc, SelectExpr}; impl Select where @@ -92,22 +92,15 @@ where .join_as(JoinType::LeftJoin, table_ref, to_tbl, condition); } slf = slf.apply_alias(SelectA.as_str()); - let text_type = SeaRc::new(Alias::new("text")) as DynIden; let mut select_two = SelectTwo::new_without_prepare(slf.query); for col in ::iter() { - let col_def = col.def(); - let col_type = col_def.get_column_type(); let alias = format!("{}{}", SelectB.as_str(), col.as_str()); let expr = Expr::tbl( Alias::new(&format!("r{}", l.link().len() - 1)).into_iden(), col.into_iden(), ); - let expr = match col_type.get_enum_name() { - Some(_) => expr.as_enum(text_type.clone()), - None => expr.into(), - }; select_two.query().expr(SelectExpr { - expr, + expr: cast_enum_as_text(expr, &col), alias: Some(SeaRc::new(Alias::new(&alias))), window: None, }); diff --git a/src/query/select.rs b/src/query/select.rs index 3c9829480..3f53430e9 100644 --- a/src/query/select.rs +++ b/src/query/select.rs @@ -1,8 +1,11 @@ -use crate::{ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySelect, QueryTrait}; +use crate::{ + cast_enum_as_text, ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySelect, + QueryTrait, +}; use core::fmt::Debug; use core::marker::PhantomData; pub use sea_query::JoinType; -use sea_query::{Alias, DynIden, Expr, IntoColumnRef, SeaRc, SelectStatement, SimpleExpr}; +use sea_query::{IntoColumnRef, SelectStatement, SimpleExpr}; /// Defines a structure to perform select operations #[derive(Clone, Debug)] @@ -119,18 +122,8 @@ where } fn column_list(&self) -> Vec { - let table = SeaRc::new(E::default()) as DynIden; - let text_type = SeaRc::new(Alias::new("text")) as DynIden; E::Column::iter() - .map(|col| { - let expr = Expr::tbl(table.clone(), col); - let col_def = col.def(); - let col_type = col_def.get_column_type(); - match col_type.get_enum_name() { - Some(_) => expr.as_enum(text_type.clone()), - None => expr.into(), - } - }) + .map(|col| cast_enum_as_text(col.into_expr(), &col)) .collect() } diff --git a/src/query/update.rs b/src/query/update.rs index a48d19a57..63adac7ac 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -1,9 +1,9 @@ use crate::{ - ActiveModelTrait, ColumnTrait, EntityTrait, Iterable, PrimaryKeyToColumn, QueryFilter, - QueryTrait, + cast_text_as_enum, ActiveModelTrait, ColumnTrait, EntityTrait, Iterable, PrimaryKeyToColumn, + QueryFilter, QueryTrait, }; use core::marker::PhantomData; -use sea_query::{Alias, Expr, IntoIden, SimpleExpr, UpdateStatement}; +use sea_query::{Expr, IntoIden, SimpleExpr, UpdateStatement}; /// Defines a structure to perform UPDATE query operations on a ActiveModel #[derive(Clone, Debug)] @@ -109,13 +109,7 @@ where } let av = self.model.get(col); if av.is_set() { - let val = Expr::val(av.into_value().unwrap()); - let col_def = col.def(); - let col_type = col_def.get_column_type(); - let expr = match col_type.get_enum_name() { - Some(enum_name) => val.as_enum(Alias::new(enum_name)), - None => val.into(), - }; + let expr = cast_text_as_enum(Expr::val(av.into_value().unwrap()), &col); self.query.value_expr(col, expr); } } diff --git a/src/tests_cfg/lunch_set.rs b/src/tests_cfg/lunch_set.rs new file mode 100644 index 000000000..b063245ef --- /dev/null +++ b/src/tests_cfg/lunch_set.rs @@ -0,0 +1,23 @@ +use super::sea_orm_active_enums::*; +use crate as sea_orm; +use crate::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] +#[sea_orm(table_name = "lunch_set")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub name: String, + pub tea: Tea, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation {} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + panic!("No RelationDef") + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/tests_cfg/mod.rs b/src/tests_cfg/mod.rs index 7f6178ef4..9210657c7 100644 --- a/src/tests_cfg/mod.rs +++ b/src/tests_cfg/mod.rs @@ -8,7 +8,9 @@ pub mod entity_linked; pub mod filling; pub mod fruit; pub mod indexes; +pub mod lunch_set; pub mod rust_keyword; +pub mod sea_orm_active_enums; pub mod vendor; pub use cake::Entity as Cake; @@ -17,5 +19,6 @@ pub use cake_filling::Entity as CakeFilling; pub use cake_filling_price::Entity as CakeFillingPrice; pub use filling::Entity as Filling; pub use fruit::Entity as Fruit; +pub use lunch_set::Entity as LunchSet; pub use rust_keyword::Entity as RustKeyword; pub use vendor::Entity as Vendor; diff --git a/src/tests_cfg/sea_orm_active_enums.rs b/src/tests_cfg/sea_orm_active_enums.rs new file mode 100644 index 000000000..a5aba5b19 --- /dev/null +++ b/src/tests_cfg/sea_orm_active_enums.rs @@ -0,0 +1,11 @@ +use crate as sea_orm; +use crate::entity::prelude::*; + +#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")] +pub enum Tea { + #[sea_orm(string_value = "EverydayTea")] + EverydayTea, + #[sea_orm(string_value = "BreakfastTea")] + BreakfastTea, +} From e919831e72678c8f9ec9119208099689dbb2c2da Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Mon, 5 Sep 2022 00:41:51 +0800 Subject: [PATCH 08/15] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7da0ba595..a09d389f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). * Replaced `usize` with `u64` in `PaginatorTrait` https://github.com/SeaQL/sea-orm/pull/789 +### Enhancements + +* `fn column()` also handle enum type https://github.com/SeaQL/sea-orm/pull/973 + ## 0.9.2 - 2022-08-20 ### Enhancements From dd2dc5dd56053a2d9649e309157900c134d703a9 Mon Sep 17 00:00:00 2001 From: Wyatt Herkamp Date: Mon, 5 Sep 2022 00:00:52 -0400 Subject: [PATCH 09/15] Add's Nitro_Repo (#1012) Closes https://github.com/wyatt-herkamp/nitro_repo/issues/478 Co-authored-by: Billy Chan --- COMMUNITY.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/COMMUNITY.md b/COMMUNITY.md index db354aadb..6c4d4305b 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -50,5 +50,6 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [KrakenPics](https://github.com/kraken-pics/backend) | A public file host written in rust using seaorm & actix_web - [service_auth](https://github.com/shorii/service_auth) | A simple JWT authentication web-application - [rj45less-server](https://github.com/pmnxis/rj45less-server) | A simple unique number allocator for custom router -- [SophyCore](https://github.com/FarDragi/SophyCore) | Main system that centralizes all rules, to be used by both the discord bot and the future site +- [SophyCore](https://github.com/FarDragi/SophyCore) | Main system that centralizes all rules, to be used by both the discord bot and the future site - [lldap](https://github.com/nitnelave/lldap) ![GitHub stars](https://img.shields.io/github/stars/nitnelave/lldap.svg?style=social) | Light LDAP implementation for authentication +- [nitro_repo](https://github.com/wyatt-herkamp/nitro_repo) | An OpenSource, lightweight, and fast artifact manager. \ No newline at end of file From 967fd68649d0a6872a3be33c3acbce2b6f6be706 Mon Sep 17 00:00:00 2001 From: Dionysus <108764316+FistedByDionysus@users.noreply.github.com> Date: Sun, 4 Sep 2022 21:02:19 -0700 Subject: [PATCH 10/15] Add MoonRamp under Built with SeaORM (#1013) Co-authored-by: Billy Chan --- COMMUNITY.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/COMMUNITY.md b/COMMUNITY.md index 6c4d4305b..d90f42f7a 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -52,4 +52,5 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [rj45less-server](https://github.com/pmnxis/rj45less-server) | A simple unique number allocator for custom router - [SophyCore](https://github.com/FarDragi/SophyCore) | Main system that centralizes all rules, to be used by both the discord bot and the future site - [lldap](https://github.com/nitnelave/lldap) ![GitHub stars](https://img.shields.io/github/stars/nitnelave/lldap.svg?style=social) | Light LDAP implementation for authentication -- [nitro_repo](https://github.com/wyatt-herkamp/nitro_repo) | An OpenSource, lightweight, and fast artifact manager. \ No newline at end of file +- [nitro_repo](https://github.com/wyatt-herkamp/nitro_repo) | An OpenSource, lightweight, and fast artifact manager. +- [MoonRamp](https://github.com/MoonRamp/MoonRamp) | A free and open source crypto payment gateway \ No newline at end of file From 5d785ca908f5e5a587fb519bd687679f6c22b389 Mon Sep 17 00:00:00 2001 From: Michael Lohr Date: Mon, 5 Sep 2022 06:03:05 +0200 Subject: [PATCH 11/15] Update COMMUNITY.md (#1014) Co-authored-by: Billy Chan --- COMMUNITY.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/COMMUNITY.md b/COMMUNITY.md index d90f42f7a..2f93b63a5 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -53,4 +53,5 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [SophyCore](https://github.com/FarDragi/SophyCore) | Main system that centralizes all rules, to be used by both the discord bot and the future site - [lldap](https://github.com/nitnelave/lldap) ![GitHub stars](https://img.shields.io/github/stars/nitnelave/lldap.svg?style=social) | Light LDAP implementation for authentication - [nitro_repo](https://github.com/wyatt-herkamp/nitro_repo) | An OpenSource, lightweight, and fast artifact manager. -- [MoonRamp](https://github.com/MoonRamp/MoonRamp) | A free and open source crypto payment gateway \ No newline at end of file +- [MoonRamp](https://github.com/MoonRamp/MoonRamp) | A free and open source crypto payment gateway +- [url_shortener](https://github.com/michidk/url_shortener) | A simple self-hosted URL shortener written in Rust \ No newline at end of file From d532deb6363d997beae029a8983d64c7e9574af6 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 5 Sep 2022 15:33:02 +0800 Subject: [PATCH 12/15] Update COMMUNITY.md Shout out to @bkonkle for the great article --- COMMUNITY.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/COMMUNITY.md b/COMMUNITY.md index 2f93b63a5..347d1e518 100644 --- a/COMMUNITY.md +++ b/COMMUNITY.md @@ -54,4 +54,12 @@ If you have built an app using SeaORM and want to showcase it, feel free to open - [lldap](https://github.com/nitnelave/lldap) ![GitHub stars](https://img.shields.io/github/stars/nitnelave/lldap.svg?style=social) | Light LDAP implementation for authentication - [nitro_repo](https://github.com/wyatt-herkamp/nitro_repo) | An OpenSource, lightweight, and fast artifact manager. - [MoonRamp](https://github.com/MoonRamp/MoonRamp) | A free and open source crypto payment gateway -- [url_shortener](https://github.com/michidk/url_shortener) | A simple self-hosted URL shortener written in Rust \ No newline at end of file +- [url_shortener](https://github.com/michidk/url_shortener) | A simple self-hosted URL shortener written in Rust + +## Learning Resources + +If you have article, tutorial, podcast or video reated to SeaORM and want to share it with the community, feel free to submit a PR and add it to the list below! + +### Tutorials + +- Async GraphQL with Rust: [Part 1](https://konkle.us/async-graphql-rust-1-introduction/), [Part 2](https://konkle.us/async-graphql-with-rust-part-two/), [Part 3](https://konkle.us/async-graphql-with-rust-part-three/) by [Brandon Konkle](https://github.com/bkonkle) From 581684c441fc3ff94cbaf1a9fe30df221d146550 Mon Sep 17 00:00:00 2001 From: Ivan Krivosheev Date: Sat, 13 Aug 2022 01:42:09 +0300 Subject: [PATCH 13/15] issues-969 Replace sea-query-driver to sea-query-binder --- Cargo.toml | 46 ++++++++++++++++++++++++++----------- src/driver/sqlx_mysql.rs | 12 +++++----- src/driver/sqlx_postgres.rs | 14 +++++------ src/driver/sqlx_sqlite.rs | 12 +++++----- 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f119bc756..5d83249c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,9 @@ futures-util = { version = "^0.3" } log = { version = "^0.4" } tracing = { version = "^0.1", features = ["log"] } rust_decimal = { version = "^1", optional = true } -sea-orm-macros = { version = "^0.10.0", path = "sea-orm-macros", optional = true } -sea-query = { version = "^0.26.3", features = ["thread-safe"] } +sea-orm-macros = { version = "^0.9.1", path = "sea-orm-macros", optional = true } +sea-query = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.27", features = ["thread-safe"] } +sea-query-binder = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.1" } sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1.0", optional = true } @@ -69,19 +70,20 @@ default = [ ] macros = ["sea-orm-macros"] mock = [] -with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sqlx?/json"] -with-chrono = ["chrono", "sea-query/with-chrono", "sqlx?/chrono"] -with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sqlx?/decimal"] -with-uuid = ["uuid", "sea-query/with-uuid", "sqlx?/uuid"] -with-time = ["time", "sea-query/with-time", "sqlx?/time"] +with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sea-query-binder/with-json", "sqlx?/json"] +with-chrono = ["chrono", "sea-query/with-chrono", "sea-query-binder/with-chrono", "sqlx?/chrono"] +with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sea-query-binder/with-rust_decimal", "sqlx?/decimal"] +with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder/with-uuid", "sqlx?/uuid"] +with-time = ["time", "sea-query/with-time", "sea-query-binder/with-time", "sqlx?/time"] sqlx-dep = [] sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"] -sqlx-mysql = ["sqlx-dep", "sea-query/sqlx-mysql", "sqlx/mysql"] -sqlx-postgres = ["sqlx-dep", "sea-query/sqlx-postgres", "sqlx/postgres"] -sqlx-sqlite = ["sqlx-dep", "sea-query/sqlx-sqlite", "sqlx/sqlite"] +sqlx-mysql = ["sqlx-dep", "sea-query-binder/sqlx-mysql", "sqlx/mysql"] +sqlx-postgres = ["sqlx-dep", "sea-query-binder/sqlx-postgres", "sqlx/postgres"] +sqlx-sqlite = ["sqlx-dep", "sea-query-binder/sqlx-postgres", "sqlx/sqlite"] runtime-async-std = [] runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls", + "sea-query-binder/runtime-async-std-native-tls", "runtime-async-std", ] runtime-async-std-rustls = [ @@ -89,8 +91,24 @@ runtime-async-std-rustls = [ "runtime-async-std", ] runtime-actix = [] -runtime-actix-native-tls = ["sqlx/runtime-actix-native-tls", "runtime-actix"] -runtime-actix-rustls = ["sqlx/runtime-actix-rustls", "runtime-actix"] +runtime-actix-native-tls = [ + "sqlx/runtime-actix-native-tls", + "sea-query-binder/runtime-actix-native-tls", + "runtime-actix", +] +runtime-actix-rustls = [ + "sqlx/runtime-actix-rustls", + "sea-query-binder/runtime-actix-rustls", + "runtime-actix", +] runtime-tokio = [] -runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls", "runtime-tokio"] -runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls", "runtime-tokio"] +runtime-tokio-native-tls = [ + "sqlx/runtime-tokio-native-tls", + "sea-query-binder/runtime-tokio-native-tls", + "runtime-tokio", +] +runtime-tokio-rustls = [ + "sqlx/runtime-tokio-rustls", + "sea-query-binder/runtime-tokio-rustls", + "runtime-tokio", +] diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index 01b840ddb..c1526c697 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -1,3 +1,4 @@ +use sea_query::Values; use std::{future::Future, pin::Pin, sync::Arc}; use sqlx::{ @@ -5,8 +6,7 @@ use sqlx::{ MySql, MySqlPool, }; -sea_query::sea_query_driver_mysql!(); -use sea_query_driver_mysql::bind_query; +use sea_query_binder::{SqlxBinder, SqlxValues}; use tracing::instrument; use crate::{ @@ -215,10 +215,10 @@ impl From for ExecResult { } } -pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, MySql, MySqlArguments> { - let mut query = sqlx::query(&stmt.sql); +pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, MySql, SqlxValues> { if let Some(values) = &stmt.values { - query = bind_query(query, values); + sqlx::query_with(&stmt.sql, SqlxValues(values.clone())) + } else { + sqlx::query_with(&stmt.sql, SqlxValues(Values(Vec::new()))) } - query } diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index bd5611485..11150fb98 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -1,12 +1,12 @@ +use sea_query::Values; +use sea_query_binder::SqlxValues; use std::{future::Future, pin::Pin, sync::Arc}; use sqlx::{ - postgres::{PgArguments, PgConnectOptions, PgQueryResult, PgRow}, + postgres::{PgConnectOptions, PgQueryResult, PgRow}, PgPool, Postgres, }; -sea_query::sea_query_driver_postgres!(); -use sea_query_driver_postgres::bind_query; use tracing::instrument; use crate::{ @@ -215,10 +215,10 @@ impl From for ExecResult { } } -pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Postgres, PgArguments> { - let mut query = sqlx::query(&stmt.sql); +pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Postgres, SqlxValues> { if let Some(values) = &stmt.values { - query = bind_query(query, values); + sqlx::query_with(&stmt.sql, SqlxValues(values.clone())) + } else { + sqlx::query_with(&stmt.sql, SqlxValues(Values(Vec::new()))) } - query } diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 8bf562148..985430168 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -1,3 +1,4 @@ +use sea_query::Values; use std::{future::Future, pin::Pin, sync::Arc}; use sqlx::{ @@ -5,8 +6,7 @@ use sqlx::{ Sqlite, SqlitePool, }; -sea_query::sea_query_driver_sqlite!(); -use sea_query_driver_sqlite::bind_query; +use sea_query_binder::SqlxValues; use tracing::instrument; use crate::{ @@ -222,10 +222,10 @@ impl From for ExecResult { } } -pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Sqlite, SqliteArguments> { - let mut query = sqlx::query(&stmt.sql); +pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Sqlite, SqlxValues> { if let Some(values) = &stmt.values { - query = bind_query(query, values); + sqlx::query_with(&stmt.sql, SqlxValues(values.clone())) + } else { + sqlx::query_with(&stmt.sql, SqlxValues(Values(Vec::new()))) } - query } From bac22d30c5257c32021ad5e64aa32a5ec8ae548b Mon Sep 17 00:00:00 2001 From: Ivan Krivosheev Date: Sat, 13 Aug 2022 01:50:02 +0300 Subject: [PATCH 14/15] issues-969 sea-query-binder make optional Update Cargo.toml Co-authored-by: Billy Chan --- Cargo.toml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5d83249c0..db0976fcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ path = "src/lib.rs" [dependencies] async-stream = { version = "^0.3" } async-trait = { version = "^0.1" } -chrono = { version = "^0.4.20", default-features = false, features = ["clock"], optional = true } +chrono = { version = "^0.4", optional = true } time = { version = "^0.3", optional = true } futures = { version = "^0.3" } futures-util = { version = "^0.3" } @@ -35,7 +35,7 @@ tracing = { version = "^0.1", features = ["log"] } rust_decimal = { version = "^1", optional = true } sea-orm-macros = { version = "^0.9.1", path = "sea-orm-macros", optional = true } sea-query = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.27", features = ["thread-safe"] } -sea-query-binder = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.1" } +sea-query-binder = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.1", optional = true } sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1.0", optional = true } @@ -70,16 +70,16 @@ default = [ ] macros = ["sea-orm-macros"] mock = [] -with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sea-query-binder/with-json", "sqlx?/json"] -with-chrono = ["chrono", "sea-query/with-chrono", "sea-query-binder/with-chrono", "sqlx?/chrono"] -with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sea-query-binder/with-rust_decimal", "sqlx?/decimal"] -with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder/with-uuid", "sqlx?/uuid"] -with-time = ["time", "sea-query/with-time", "sea-query-binder/with-time", "sqlx?/time"] +with-json = ["serde_json", "sea-query/with-json", "chrono/serde", "sea-query-binder?/with-json", "sqlx?/json"] +with-chrono = ["chrono", "sea-query/with-chrono", "sea-query-binder?/with-chrono", "sqlx?/chrono"] +with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal", "sea-query-binder?/with-rust_decimal", "sqlx?/decimal"] +with-uuid = ["uuid", "sea-query/with-uuid", "sea-query-binder?/with-uuid", "sqlx?/uuid"] +with-time = ["time", "sea-query/with-time", "sea-query-binder?/with-time", "sqlx?/time"] sqlx-dep = [] sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"] sqlx-mysql = ["sqlx-dep", "sea-query-binder/sqlx-mysql", "sqlx/mysql"] sqlx-postgres = ["sqlx-dep", "sea-query-binder/sqlx-postgres", "sqlx/postgres"] -sqlx-sqlite = ["sqlx-dep", "sea-query-binder/sqlx-postgres", "sqlx/sqlite"] +sqlx-sqlite = ["sqlx-dep", "sea-query-binder/sqlx-sqlite", "sqlx/sqlite"] runtime-async-std = [] runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls", From ecbd7c2713796722afdc13a7cd9a3c051289320d Mon Sep 17 00:00:00 2001 From: Ivan Krivosheev Date: Sat, 3 Sep 2022 18:42:51 +0300 Subject: [PATCH 15/15] issues-969 Update dependencies issues-969 Fix sqlx_query function --- Cargo.toml | 8 ++++---- src/driver/sqlx_mysql.rs | 10 +++++----- src/driver/sqlx_postgres.rs | 10 +++++----- src/driver/sqlx_sqlite.rs | 10 +++++----- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index db0976fcf..8751e6604 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,16 +26,16 @@ path = "src/lib.rs" [dependencies] async-stream = { version = "^0.3" } async-trait = { version = "^0.1" } -chrono = { version = "^0.4", optional = true } +chrono = { version = "^0.4.20", default-features = false, features = ["clock"], optional = true } time = { version = "^0.3", optional = true } futures = { version = "^0.3" } futures-util = { version = "^0.3" } log = { version = "^0.4" } tracing = { version = "^0.1", features = ["log"] } rust_decimal = { version = "^1", optional = true } -sea-orm-macros = { version = "^0.9.1", path = "sea-orm-macros", optional = true } -sea-query = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.27", features = ["thread-safe"] } -sea-query-binder = { git = "https://github.com/ikrivosheev/sea-query", version = "^0.1", optional = true } +sea-orm-macros = { version = "^0.10.0", path = "sea-orm-macros", optional = true } +sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "master", version = "^0.27", features = ["thread-safe"] } +sea-query-binder = { git = "https://github.com/SeaQL/sea-query", branch = "master", version = "^0.1", optional = true } sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1.0", optional = true } diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index c1526c697..c970a2c3b 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -216,9 +216,9 @@ impl From for ExecResult { } pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, MySql, SqlxValues> { - if let Some(values) = &stmt.values { - sqlx::query_with(&stmt.sql, SqlxValues(values.clone())) - } else { - sqlx::query_with(&stmt.sql, SqlxValues(Values(Vec::new()))) - } + let values = stmt + .values + .as_ref() + .map_or(Values(Vec::new()), |values| values.clone()); + sqlx::query_with(&stmt.sql, SqlxValues(values)) } diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 11150fb98..7737c08fe 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -216,9 +216,9 @@ impl From for ExecResult { } pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Postgres, SqlxValues> { - if let Some(values) = &stmt.values { - sqlx::query_with(&stmt.sql, SqlxValues(values.clone())) - } else { - sqlx::query_with(&stmt.sql, SqlxValues(Values(Vec::new()))) - } + let values = stmt + .values + .as_ref() + .map_or(Values(Vec::new()), |values| values.clone()); + sqlx::query_with(&stmt.sql, SqlxValues(values)) } diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 985430168..3898661af 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -223,9 +223,9 @@ impl From for ExecResult { } pub(crate) fn sqlx_query(stmt: &Statement) -> sqlx::query::Query<'_, Sqlite, SqlxValues> { - if let Some(values) = &stmt.values { - sqlx::query_with(&stmt.sql, SqlxValues(values.clone())) - } else { - sqlx::query_with(&stmt.sql, SqlxValues(Values(Vec::new()))) - } + let values = stmt + .values + .as_ref() + .map_or(Values(Vec::new()), |values| values.clone()); + sqlx::query_with(&stmt.sql, SqlxValues(values)) }