From 73701fef9aa1074e1ac6967e71bda209a26528cb Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 14 Mar 2022 12:40:02 +0800 Subject: [PATCH] Codegen Unsigned Integer - 2 (#397) * feat: codegen unsigned integer * feat: apply alias on `ColumnRef::SchemaTableColumn` * Update SQLite test cases, quote identifier with double quotes * Bump sea-query version to 0.22 * Add dummy line * Tests [cli] and [issues] * update sea-schema Co-authored-by: Chris Tsang --- Cargo.toml | 2 +- sea-orm-cli/Cargo.toml | 2 +- sea-orm-codegen/Cargo.toml | 2 +- sea-orm-codegen/src/entity/column.rs | 30 +++++++++- sea-orm-codegen/src/entity/writer.rs | 14 ++--- sea-orm-codegen/tests/compact/rust_keyword.rs | 14 ++--- .../tests/expanded/rust_keyword.rs | 28 +++++----- sea-orm-macros/src/derives/entity_model.rs | 12 ++-- src/entity/column.rs | 55 +++++++++++++++++-- 9 files changed, 117 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90313632..7165b8e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ futures-util = { version = "^0.3" } tracing = { version = "0.1", features = ["log"] } rust_decimal = { version = "^1", optional = true } sea-orm-macros = { version = "^0.6.0", path = "sea-orm-macros", optional = true } -sea-query = { version = "^0.21.0", features = ["thread-safe"] } +sea-query = { version = "^0.22.0", features = ["thread-safe"] } sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1", optional = true } diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index 3e0a96e9..c683dc3a 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -22,7 +22,7 @@ clap = { version = "^2.33.3" } dotenv = { version = "^0.15" } async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] } sea-orm-codegen = { version = "^0.6.0", path = "../sea-orm-codegen" } -sea-schema = { version = "^0.5.0", default-features = false, features = [ +sea-schema = { version = "^0.6.0", default-features = false, features = [ "debug-print", "sqlx-mysql", "sqlx-sqlite", diff --git a/sea-orm-codegen/Cargo.toml b/sea-orm-codegen/Cargo.toml index 2a90badb..c2c13f97 100644 --- a/sea-orm-codegen/Cargo.toml +++ b/sea-orm-codegen/Cargo.toml @@ -15,7 +15,7 @@ name = "sea_orm_codegen" path = "src/lib.rs" [dependencies] -sea-query = { version = "0.21.0" } +sea-query = { version = "0.22.0" } syn = { version = "^1", default-features = false, features = [ "derive", "parsing", diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index 3b374b02..125e671c 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -37,6 +37,10 @@ impl Column { ColumnType::SmallInteger(_) => "i16".to_owned(), ColumnType::Integer(_) => "i32".to_owned(), ColumnType::BigInteger(_) => "i64".to_owned(), + ColumnType::TinyUnsigned(_) => "u8".to_owned(), + ColumnType::SmallUnsigned(_) => "u16".to_owned(), + ColumnType::Unsigned(_) => "u32".to_owned(), + ColumnType::BigUnsigned(_) => "u64".to_owned(), ColumnType::Float(_) => "f32".to_owned(), ColumnType::Double(_) => "f64".to_owned(), ColumnType::Json | ColumnType::JsonBinary => "Json".to_owned(), @@ -90,6 +94,10 @@ impl Column { ColumnType::SmallInteger(_) => quote! { ColumnType::SmallInteger.def() }, ColumnType::Integer(_) => quote! { ColumnType::Integer.def() }, ColumnType::BigInteger(_) => quote! { ColumnType::BigInteger.def() }, + ColumnType::TinyUnsigned(_) => quote! { ColumnType::TinyUnsigned.def() }, + ColumnType::SmallUnsigned(_) => quote! { ColumnType::SmallUnsigned.def() }, + ColumnType::Unsigned(_) => quote! { ColumnType::Unsigned.def() }, + ColumnType::BigUnsigned(_) => quote! { ColumnType::BigUnsigned.def() }, ColumnType::Float(_) => quote! { ColumnType::Float.def() }, ColumnType::Double(_) => quote! { ColumnType::Double.def() }, ColumnType::Decimal(s) => match s { @@ -194,9 +202,13 @@ mod tests { ColumnType::Custom(SeaRc::new(Alias::new("cus_col"))) ), make_col!("CakeId", ColumnType::TinyInteger(None)), + make_col!("CakeId", ColumnType::TinyUnsigned(Some(9))), make_col!("CakeId", ColumnType::SmallInteger(None)), - make_col!("CakeId", ColumnType::Integer(Some(11))), + make_col!("CakeId", ColumnType::SmallUnsigned(Some(10))), + make_col!("CakeId", ColumnType::Integer(None)), + make_col!("CakeId", ColumnType::Unsigned(Some(11))), make_col!("CakeFillingId", ColumnType::BigInteger(None)), + make_col!("CakeFillingId", ColumnType::BigUnsigned(Some(12))), make_col!("cake-filling-id", ColumnType::Float(None)), make_col!("CAKE_FILLING_ID", ColumnType::Double(None)), make_col!("CAKE-FILLING-ID", ColumnType::Binary(None)), @@ -218,6 +230,10 @@ mod tests { "cake_id", "cake_id", "cake_id", + "cake_id", + "cake_id", + "cake_id", + "cake_filling_id", "cake_filling_id", "cake_filling_id", "cake_filling_id", @@ -243,6 +259,10 @@ mod tests { "CakeId", "CakeId", "CakeId", + "CakeId", + "CakeId", + "CakeId", + "CakeFillingId", "CakeFillingId", "CakeFillingId", "CakeFillingId", @@ -266,9 +286,13 @@ mod tests { "String", "String", "i8", + "u8", "i16", + "u16", "i32", + "u32", "i64", + "u64", "f32", "f64", "Vec", @@ -300,9 +324,13 @@ mod tests { "ColumnType::String(Some(255u32)).def()", "ColumnType::Custom(\"cus_col\".to_owned()).def()", "ColumnType::TinyInteger.def()", + "ColumnType::TinyUnsigned.def()", "ColumnType::SmallInteger.def()", + "ColumnType::SmallUnsigned.def()", "ColumnType::Integer.def()", + "ColumnType::Unsigned.def()", "ColumnType::BigInteger.def()", + "ColumnType::BigUnsigned.def()", "ColumnType::Float.def()", "ColumnType::Double.def()", "ColumnType::Binary.def()", diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 21269cb9..a5c4af0d 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -778,28 +778,28 @@ mod tests { }, Column { name: "testing".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::TinyInteger(Some(11)), auto_increment: false, not_null: true, unique: false, }, Column { name: "rust".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::TinyUnsigned(Some(11)), auto_increment: false, not_null: true, unique: false, }, Column { name: "keywords".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::SmallInteger(Some(11)), auto_increment: false, not_null: true, unique: false, }, Column { name: "type".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::SmallUnsigned(Some(11)), auto_increment: false, not_null: true, unique: false, @@ -813,21 +813,21 @@ mod tests { }, Column { name: "crate".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::Unsigned(Some(11)), auto_increment: false, not_null: true, unique: false, }, Column { name: "self".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::BigInteger(Some(11)), auto_increment: false, not_null: true, unique: false, }, Column { name: "self_id1".to_owned(), - col_type: ColumnType::Integer(Some(11)), + col_type: ColumnType::BigUnsigned(Some(11)), auto_increment: false, not_null: true, unique: false, diff --git a/sea-orm-codegen/tests/compact/rust_keyword.rs b/sea-orm-codegen/tests/compact/rust_keyword.rs index 8b76eaa1..c5d46f50 100644 --- a/sea-orm-codegen/tests/compact/rust_keyword.rs +++ b/sea-orm-codegen/tests/compact/rust_keyword.rs @@ -7,14 +7,14 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key)] pub id: i32, - pub testing: i32, - pub rust: i32, - pub keywords: i32, - pub r#type: i32, + pub testing: i8, + pub rust: u8, + pub keywords: i16, + pub r#type: u16, pub r#typeof: i32, - pub crate_: i32, - pub self_: i32, - pub self_id1: i32, + pub crate_: u32, + pub self_: i64, + pub self_id1: u64, pub self_id2: i32, pub fruit_id1: i32, pub fruit_id2: i32, diff --git a/sea-orm-codegen/tests/expanded/rust_keyword.rs b/sea-orm-codegen/tests/expanded/rust_keyword.rs index e0677897..1e96f791 100644 --- a/sea-orm-codegen/tests/expanded/rust_keyword.rs +++ b/sea-orm-codegen/tests/expanded/rust_keyword.rs @@ -14,14 +14,14 @@ impl EntityName for Entity { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, - pub testing: i32, - pub rust: i32, - pub keywords: i32, - pub r#type: i32, + pub testing: i8, + pub rust: u8, + pub keywords: i16, + pub r#type: u16, pub r#typeof: i32, - pub crate_: i32, - pub self_: i32, - pub self_id1: i32, + pub crate_: u32, + pub self_: i64, + pub self_id1: u64, pub self_id2: i32, pub fruit_id1: i32, pub fruit_id2: i32, @@ -73,14 +73,14 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Testing => ColumnType::Integer.def(), - Self::Rust => ColumnType::Integer.def(), - Self::Keywords => ColumnType::Integer.def(), - Self::Type => ColumnType::Integer.def(), + Self::Testing => ColumnType::TinyInteger.def(), + Self::Rust => ColumnType::TinyUnsigned.def(), + Self::Keywords => ColumnType::SmallInteger.def(), + Self::Type => ColumnType::SmallUnsigned.def(), Self::Typeof => ColumnType::Integer.def(), - Self::Crate => ColumnType::Integer.def(), - Self::Self_ => ColumnType::Integer.def(), - Self::SelfId1 => ColumnType::Integer.def(), + Self::Crate => ColumnType::Unsigned.def(), + Self::Self_ => ColumnType::BigInteger.def(), + Self::SelfId1 => ColumnType::BigUnsigned.def(), Self::SelfId2 => ColumnType::Integer.def(), Self::FruitId1 => ColumnType::Integer.def(), Self::FruitId2 => ColumnType::Integer.def(), diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 0bbd8bf1..25ea2727 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -230,10 +230,14 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res let col_type = match temp { "char" => quote! { Char(None) }, "String" | "&str" => quote! { String(None) }, - "u8" | "i8" => quote! { TinyInteger }, - "u16" | "i16" => quote! { SmallInteger }, - "u32" | "i32" => quote! { Integer }, - "u64" | "i64" => quote! { BigInteger }, + "i8" => quote! { TinyInteger }, + "u8" => quote! { TinyUnsigned }, + "i16" => quote! { SmallInteger }, + "u16" => quote! { SmallUnsigned }, + "i32" => quote! { Integer }, + "u32" => quote! { Unsigned }, + "i64" => quote! { BigInteger }, + "u64" => quote! { BigUnsigned }, "f32" => quote! { Float }, "f64" => quote! { Double }, "bool" => quote! { Boolean }, diff --git a/src/entity/column.rs b/src/entity/column.rs index c4ea545f..cbf51475 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -30,6 +30,14 @@ pub enum ColumnType { /// `BIGINT` is a 64-bit representation of an integer taking up 8 bytes of storage and /// ranging from -2^63 (-9,223,372,036,854,775,808) to 2^63 (9,223,372,036,854,775,807). BigInteger, + /// `TINYINT UNSIGNED` data type + TinyUnsigned, + /// `SMALLINT UNSIGNED` data type + SmallUnsigned, + /// `INTEGER UNSIGNED` data type + Unsigned, + /// `BIGINT UNSIGNED` data type + BigUnsigned, /// `FLOAT` an approximate-number data type, where values range cannot be represented exactly. Float, /// `DOUBLE` is a normal-size floating point number where the @@ -362,6 +370,10 @@ impl From for sea_query::ColumnType { ColumnType::SmallInteger => sea_query::ColumnType::SmallInteger(None), ColumnType::Integer => sea_query::ColumnType::Integer(None), ColumnType::BigInteger => sea_query::ColumnType::BigInteger(None), + ColumnType::TinyUnsigned => sea_query::ColumnType::TinyUnsigned(None), + ColumnType::SmallUnsigned => sea_query::ColumnType::SmallUnsigned(None), + ColumnType::Unsigned => sea_query::ColumnType::Unsigned(None), + ColumnType::BigUnsigned => sea_query::ColumnType::BigUnsigned(None), ColumnType::Float => sea_query::ColumnType::Float(None), ColumnType::Double => sea_query::ColumnType::Double(None), ColumnType::Decimal(s) => sea_query::ColumnType::Decimal(s), @@ -395,6 +407,10 @@ impl From for ColumnType { sea_query::ColumnType::SmallInteger(_) => Self::SmallInteger, sea_query::ColumnType::Integer(_) => Self::Integer, sea_query::ColumnType::BigInteger(_) => Self::BigInteger, + sea_query::ColumnType::TinyUnsigned(_) => Self::TinyUnsigned, + sea_query::ColumnType::SmallUnsigned(_) => Self::SmallUnsigned, + sea_query::ColumnType::Unsigned(_) => Self::Unsigned, + sea_query::ColumnType::BigUnsigned(_) => Self::BigUnsigned, sea_query::ColumnType::Float(_) => Self::Float, sea_query::ColumnType::Double(_) => Self::Double, sea_query::ColumnType::Decimal(s) => Self::Decimal(s), @@ -513,13 +529,21 @@ mod tests { pub id: i32, pub one: i32, #[sea_orm(unique)] - pub two: i32, + pub two: i8, #[sea_orm(indexed)] - pub three: i32, + pub three: i16, #[sea_orm(nullable)] pub four: i32, #[sea_orm(unique, indexed, nullable)] - pub five: i32, + pub five: i64, + #[sea_orm(unique)] + pub six: u8, + #[sea_orm(indexed)] + pub seven: u16, + #[sea_orm(nullable)] + pub eight: u32, + #[sea_orm(unique, indexed, nullable)] + pub nine: u64, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -529,10 +553,13 @@ mod tests { } assert_eq!(hello::Column::One.def(), ColumnType::Integer.def()); - assert_eq!(hello::Column::Two.def(), ColumnType::Integer.def().unique()); + assert_eq!( + hello::Column::Two.def(), + ColumnType::TinyInteger.def().unique() + ); assert_eq!( hello::Column::Three.def(), - ColumnType::Integer.def().indexed() + ColumnType::SmallInteger.def().indexed() ); assert_eq!( hello::Column::Four.def(), @@ -540,7 +567,23 @@ mod tests { ); assert_eq!( hello::Column::Five.def(), - ColumnType::Integer.def().unique().indexed().nullable() + ColumnType::BigInteger.def().unique().indexed().nullable() + ); + assert_eq!( + hello::Column::Six.def(), + ColumnType::TinyUnsigned.def().unique() + ); + assert_eq!( + hello::Column::Seven.def(), + ColumnType::SmallUnsigned.def().indexed() + ); + assert_eq!( + hello::Column::Eight.def(), + ColumnType::Unsigned.def().nullable() + ); + assert_eq!( + hello::Column::Nine.def(), + ColumnType::BigUnsigned.def().unique().indexed().nullable() ); }