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 <chris.2y3@outlook.com>
This commit is contained in:
Billy Chan 2022-03-14 12:40:02 +08:00 committed by GitHub
parent b2954d84af
commit 73701fef9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 117 additions and 42 deletions

View File

@ -30,7 +30,7 @@ futures-util = { version = "^0.3" }
tracing = { version = "0.1", features = ["log"] } tracing = { version = "0.1", features = ["log"] }
rust_decimal = { version = "^1", optional = true } rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.6.0", path = "sea-orm-macros", 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"] } sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] } serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1", optional = true } serde_json = { version = "^1", optional = true }

View File

@ -22,7 +22,7 @@ clap = { version = "^2.33.3" }
dotenv = { version = "^0.15" } dotenv = { version = "^0.15" }
async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] } async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] }
sea-orm-codegen = { version = "^0.6.0", path = "../sea-orm-codegen" } 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", "debug-print",
"sqlx-mysql", "sqlx-mysql",
"sqlx-sqlite", "sqlx-sqlite",

View File

@ -15,7 +15,7 @@ name = "sea_orm_codegen"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
sea-query = { version = "0.21.0" } sea-query = { version = "0.22.0" }
syn = { version = "^1", default-features = false, features = [ syn = { version = "^1", default-features = false, features = [
"derive", "derive",
"parsing", "parsing",

View File

@ -37,6 +37,10 @@ impl Column {
ColumnType::SmallInteger(_) => "i16".to_owned(), ColumnType::SmallInteger(_) => "i16".to_owned(),
ColumnType::Integer(_) => "i32".to_owned(), ColumnType::Integer(_) => "i32".to_owned(),
ColumnType::BigInteger(_) => "i64".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::Float(_) => "f32".to_owned(),
ColumnType::Double(_) => "f64".to_owned(), ColumnType::Double(_) => "f64".to_owned(),
ColumnType::Json | ColumnType::JsonBinary => "Json".to_owned(), ColumnType::Json | ColumnType::JsonBinary => "Json".to_owned(),
@ -90,6 +94,10 @@ impl Column {
ColumnType::SmallInteger(_) => quote! { ColumnType::SmallInteger.def() }, ColumnType::SmallInteger(_) => quote! { ColumnType::SmallInteger.def() },
ColumnType::Integer(_) => quote! { ColumnType::Integer.def() }, ColumnType::Integer(_) => quote! { ColumnType::Integer.def() },
ColumnType::BigInteger(_) => quote! { ColumnType::BigInteger.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::Float(_) => quote! { ColumnType::Float.def() },
ColumnType::Double(_) => quote! { ColumnType::Double.def() }, ColumnType::Double(_) => quote! { ColumnType::Double.def() },
ColumnType::Decimal(s) => match s { ColumnType::Decimal(s) => match s {
@ -194,9 +202,13 @@ mod tests {
ColumnType::Custom(SeaRc::new(Alias::new("cus_col"))) ColumnType::Custom(SeaRc::new(Alias::new("cus_col")))
), ),
make_col!("CakeId", ColumnType::TinyInteger(None)), make_col!("CakeId", ColumnType::TinyInteger(None)),
make_col!("CakeId", ColumnType::TinyUnsigned(Some(9))),
make_col!("CakeId", ColumnType::SmallInteger(None)), 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::BigInteger(None)),
make_col!("CakeFillingId", ColumnType::BigUnsigned(Some(12))),
make_col!("cake-filling-id", ColumnType::Float(None)), make_col!("cake-filling-id", ColumnType::Float(None)),
make_col!("CAKE_FILLING_ID", ColumnType::Double(None)), make_col!("CAKE_FILLING_ID", ColumnType::Double(None)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(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_id",
"cake_id",
"cake_id",
"cake_filling_id",
"cake_filling_id", "cake_filling_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",
"CakeId",
"CakeId",
"CakeId",
"CakeFillingId",
"CakeFillingId", "CakeFillingId",
"CakeFillingId", "CakeFillingId",
"CakeFillingId", "CakeFillingId",
@ -266,9 +286,13 @@ mod tests {
"String", "String",
"String", "String",
"i8", "i8",
"u8",
"i16", "i16",
"u16",
"i32", "i32",
"u32",
"i64", "i64",
"u64",
"f32", "f32",
"f64", "f64",
"Vec<u8>", "Vec<u8>",
@ -300,9 +324,13 @@ mod tests {
"ColumnType::String(Some(255u32)).def()", "ColumnType::String(Some(255u32)).def()",
"ColumnType::Custom(\"cus_col\".to_owned()).def()", "ColumnType::Custom(\"cus_col\".to_owned()).def()",
"ColumnType::TinyInteger.def()", "ColumnType::TinyInteger.def()",
"ColumnType::TinyUnsigned.def()",
"ColumnType::SmallInteger.def()", "ColumnType::SmallInteger.def()",
"ColumnType::SmallUnsigned.def()",
"ColumnType::Integer.def()", "ColumnType::Integer.def()",
"ColumnType::Unsigned.def()",
"ColumnType::BigInteger.def()", "ColumnType::BigInteger.def()",
"ColumnType::BigUnsigned.def()",
"ColumnType::Float.def()", "ColumnType::Float.def()",
"ColumnType::Double.def()", "ColumnType::Double.def()",
"ColumnType::Binary.def()", "ColumnType::Binary.def()",

View File

@ -778,28 +778,28 @@ mod tests {
}, },
Column { Column {
name: "testing".to_owned(), name: "testing".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::TinyInteger(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,
}, },
Column { Column {
name: "rust".to_owned(), name: "rust".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::TinyUnsigned(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,
}, },
Column { Column {
name: "keywords".to_owned(), name: "keywords".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::SmallInteger(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,
}, },
Column { Column {
name: "type".to_owned(), name: "type".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::SmallUnsigned(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,
@ -813,21 +813,21 @@ mod tests {
}, },
Column { Column {
name: "crate".to_owned(), name: "crate".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::Unsigned(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,
}, },
Column { Column {
name: "self".to_owned(), name: "self".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::BigInteger(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,
}, },
Column { Column {
name: "self_id1".to_owned(), name: "self_id1".to_owned(),
col_type: ColumnType::Integer(Some(11)), col_type: ColumnType::BigUnsigned(Some(11)),
auto_increment: false, auto_increment: false,
not_null: true, not_null: true,
unique: false, unique: false,

View File

@ -7,14 +7,14 @@ use sea_orm::entity::prelude::*;
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
pub testing: i32, pub testing: i8,
pub rust: i32, pub rust: u8,
pub keywords: i32, pub keywords: i16,
pub r#type: i32, pub r#type: u16,
pub r#typeof: i32, pub r#typeof: i32,
pub crate_: i32, pub crate_: u32,
pub self_: i32, pub self_: i64,
pub self_id1: i32, pub self_id1: u64,
pub self_id2: i32, pub self_id2: i32,
pub fruit_id1: i32, pub fruit_id1: i32,
pub fruit_id2: i32, pub fruit_id2: i32,

View File

@ -14,14 +14,14 @@ impl EntityName for Entity {
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub testing: i32, pub testing: i8,
pub rust: i32, pub rust: u8,
pub keywords: i32, pub keywords: i16,
pub r#type: i32, pub r#type: u16,
pub r#typeof: i32, pub r#typeof: i32,
pub crate_: i32, pub crate_: u32,
pub self_: i32, pub self_: i64,
pub self_id1: i32, pub self_id1: u64,
pub self_id2: i32, pub self_id2: i32,
pub fruit_id1: i32, pub fruit_id1: i32,
pub fruit_id2: i32, pub fruit_id2: i32,
@ -73,14 +73,14 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef { fn def(&self) -> ColumnDef {
match self { match self {
Self::Id => ColumnType::Integer.def(), Self::Id => ColumnType::Integer.def(),
Self::Testing => ColumnType::Integer.def(), Self::Testing => ColumnType::TinyInteger.def(),
Self::Rust => ColumnType::Integer.def(), Self::Rust => ColumnType::TinyUnsigned.def(),
Self::Keywords => ColumnType::Integer.def(), Self::Keywords => ColumnType::SmallInteger.def(),
Self::Type => ColumnType::Integer.def(), Self::Type => ColumnType::SmallUnsigned.def(),
Self::Typeof => ColumnType::Integer.def(), Self::Typeof => ColumnType::Integer.def(),
Self::Crate => ColumnType::Integer.def(), Self::Crate => ColumnType::Unsigned.def(),
Self::Self_ => ColumnType::Integer.def(), Self::Self_ => ColumnType::BigInteger.def(),
Self::SelfId1 => ColumnType::Integer.def(), Self::SelfId1 => ColumnType::BigUnsigned.def(),
Self::SelfId2 => ColumnType::Integer.def(), Self::SelfId2 => ColumnType::Integer.def(),
Self::FruitId1 => ColumnType::Integer.def(), Self::FruitId1 => ColumnType::Integer.def(),
Self::FruitId2 => ColumnType::Integer.def(), Self::FruitId2 => ColumnType::Integer.def(),

View File

@ -230,10 +230,14 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let col_type = match temp { let col_type = match temp {
"char" => quote! { Char(None) }, "char" => quote! { Char(None) },
"String" | "&str" => quote! { String(None) }, "String" | "&str" => quote! { String(None) },
"u8" | "i8" => quote! { TinyInteger }, "i8" => quote! { TinyInteger },
"u16" | "i16" => quote! { SmallInteger }, "u8" => quote! { TinyUnsigned },
"u32" | "i32" => quote! { Integer }, "i16" => quote! { SmallInteger },
"u64" | "i64" => quote! { BigInteger }, "u16" => quote! { SmallUnsigned },
"i32" => quote! { Integer },
"u32" => quote! { Unsigned },
"i64" => quote! { BigInteger },
"u64" => quote! { BigUnsigned },
"f32" => quote! { Float }, "f32" => quote! { Float },
"f64" => quote! { Double }, "f64" => quote! { Double },
"bool" => quote! { Boolean }, "bool" => quote! { Boolean },

View File

@ -30,6 +30,14 @@ pub enum ColumnType {
/// `BIGINT` is a 64-bit representation of an integer taking up 8 bytes of storage and /// `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). /// ranging from -2^63 (-9,223,372,036,854,775,808) to 2^63 (9,223,372,036,854,775,807).
BigInteger, 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` an approximate-number data type, where values range cannot be represented exactly.
Float, Float,
/// `DOUBLE` is a normal-size floating point number where the /// `DOUBLE` is a normal-size floating point number where the
@ -362,6 +370,10 @@ impl From<ColumnType> for sea_query::ColumnType {
ColumnType::SmallInteger => sea_query::ColumnType::SmallInteger(None), ColumnType::SmallInteger => sea_query::ColumnType::SmallInteger(None),
ColumnType::Integer => sea_query::ColumnType::Integer(None), ColumnType::Integer => sea_query::ColumnType::Integer(None),
ColumnType::BigInteger => sea_query::ColumnType::BigInteger(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::Float => sea_query::ColumnType::Float(None),
ColumnType::Double => sea_query::ColumnType::Double(None), ColumnType::Double => sea_query::ColumnType::Double(None),
ColumnType::Decimal(s) => sea_query::ColumnType::Decimal(s), ColumnType::Decimal(s) => sea_query::ColumnType::Decimal(s),
@ -395,6 +407,10 @@ impl From<sea_query::ColumnType> for ColumnType {
sea_query::ColumnType::SmallInteger(_) => Self::SmallInteger, sea_query::ColumnType::SmallInteger(_) => Self::SmallInteger,
sea_query::ColumnType::Integer(_) => Self::Integer, sea_query::ColumnType::Integer(_) => Self::Integer,
sea_query::ColumnType::BigInteger(_) => Self::BigInteger, 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::Float(_) => Self::Float,
sea_query::ColumnType::Double(_) => Self::Double, sea_query::ColumnType::Double(_) => Self::Double,
sea_query::ColumnType::Decimal(s) => Self::Decimal(s), sea_query::ColumnType::Decimal(s) => Self::Decimal(s),
@ -513,13 +529,21 @@ mod tests {
pub id: i32, pub id: i32,
pub one: i32, pub one: i32,
#[sea_orm(unique)] #[sea_orm(unique)]
pub two: i32, pub two: i8,
#[sea_orm(indexed)] #[sea_orm(indexed)]
pub three: i32, pub three: i16,
#[sea_orm(nullable)] #[sea_orm(nullable)]
pub four: i32, pub four: i32,
#[sea_orm(unique, indexed, nullable)] #[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)] #[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::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!( assert_eq!(
hello::Column::Three.def(), hello::Column::Three.def(),
ColumnType::Integer.def().indexed() ColumnType::SmallInteger.def().indexed()
); );
assert_eq!( assert_eq!(
hello::Column::Four.def(), hello::Column::Four.def(),
@ -540,7 +567,23 @@ mod tests {
); );
assert_eq!( assert_eq!(
hello::Column::Five.def(), 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()
); );
} }