Skip to content

Commit

Permalink
Cont. PR #117 & #122 (#124)
Browse files Browse the repository at this point in the history
* Rework SQLite type mapping (#117)

* Rework SQLite type mapping

* Update SeaQuery dep

* sqlite: decimal_text

* clippy

* fix test cases

* sqlite: decimal -> real

---------

Co-authored-by: Billy Chan <[email protected]>

* Bump SeaQuery and update blob data types (#122)

* Rework SQLite type mapping

* Update SeaQuery dep

* sqlite: decimal_text

* clippy

* fix test cases

* sqlite: decimal -> real

* Bump SeaQuery and update blob data types

* fixup

---------

Co-authored-by: Chris Tsang <[email protected]>

* revert

* real_money

* mysql: write `bit` and `year` column types

* Bump sea-query dependencies

* postgres: write `bit` and `varbit` column types

---------

Co-authored-by: Chris Tsang <[email protected]>
  • Loading branch information
billy1624 and tyt2y3 authored Jan 31, 2024
1 parent b46dbd6 commit b91799d
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 225 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ path = "src/lib.rs"
[dependencies]
futures = { version = "0.3", default-features = false, optional = true, features = ["alloc"] }
sea-schema-derive = { version = "0.2.0", path = "sea-schema-derive", default-features = false }
sea-query = { version = "0.30.0", default-features = false, features = ["derive"] }
sea-query-binder = { version = "0.5.0", default-features = false, optional = true }
sea-query = { version = "0.31.0-rc.1", default-features = false, features = ["derive"] }
sea-query-binder = { version = "0.6.0-rc.1", default-features = false, optional = true }
serde = { version = "1", default-features = false, optional = true, features = ["derive"] }
sqlx = { version = "0.7", default-features = false, optional = true }
log = { version = "0.4", default-features = false, optional = true }
Expand Down
24 changes: 11 additions & 13 deletions src/mysql/writer/column.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::mysql::def::{CharSet, ColumnDefault, ColumnInfo, NumericAttr, StringAttr, Type};
use sea_query::{
Alias, BlobSize, ColumnDef, DynIden, EscapeBuilder, Expr, Iden, IntoIden, Keyword,
MysqlQueryBuilder, SimpleExpr,
extension::mysql::MySqlType, Alias, ColumnDef, DynIden, EscapeBuilder, Expr, Iden, IntoIden,
Keyword, MysqlQueryBuilder, SimpleExpr,
};
use std::fmt::Write;

Expand Down Expand Up @@ -55,9 +55,8 @@ impl ColumnInfo {
.auto_increment()
.unique_key();
}
Type::Bit(_) => {
// FIXME: Unresolved type mapping
col_def.custom(self.col_type.clone());
Type::Bit(num_attr) => {
col_def.bit(num_attr.maximum);
}
Type::TinyInt(num_attr) => {
match num_attr.unsigned {
Expand Down Expand Up @@ -122,8 +121,7 @@ impl ColumnInfo {
col_def.timestamp();
}
Type::Year => {
// FIXME: Unresolved type mapping
col_def.custom(self.col_type.clone());
col_def.year(None);
}
Type::Char(str_attr) => {
match str_attr.length {
Expand Down Expand Up @@ -156,14 +154,14 @@ impl ColumnInfo {
Type::Binary(str_attr) => {
match str_attr.length {
Some(length) => col_def.binary_len(length),
_ => col_def.binary(),
None => col_def.custom(MySqlType::Blob),
};
col_def = self.write_str_attr(col_def, str_attr);
}
Type::Varbinary(str_attr) => {
match str_attr.length {
Some(length) => col_def.var_binary(length),
None => col_def.binary(),
None => col_def.custom(MySqlType::Blob),
};
}
Type::Text(str_attr) => {
Expand All @@ -185,17 +183,17 @@ impl ColumnInfo {
Type::Blob(blob_attr) => {
match blob_attr.length {
Some(length) => col_def.binary_len(length),
None => col_def.binary(),
None => col_def.custom(MySqlType::Blob),
};
}
Type::TinyBlob => {
col_def.blob(BlobSize::Tiny);
col_def.custom(MySqlType::TinyBlob);
}
Type::MediumBlob => {
col_def.blob(BlobSize::Medium);
col_def.custom(MySqlType::MediumBlob);
}
Type::LongBlob => {
col_def.blob(BlobSize::Long);
col_def.custom(MySqlType::LongBlob);
}
Type::Enum(enum_attr) => {
let name = Alias::new(&self.name);
Expand Down
8 changes: 6 additions & 2 deletions src/postgres/def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub enum Type {
/// Fixed length bit string
Bit(BitAttr),

/// Variable length bit string
VarBit(BitAttr),

// Text search types
/// A sorted list of distinct lexemes which are words that have been normalized to merge different
/// variants of the same word
Expand Down Expand Up @@ -171,7 +174,7 @@ impl Type {
"time" | "time without time zone" => Type::Time(TimeAttr::default()),
"time with time zone" => Type::TimeWithTimeZone(TimeAttr::default()),
"interval" => Type::Interval(IntervalAttr::default()),
"boolean" => Type::Boolean,
"boolean" | "bool" => Type::Boolean,
"point" => Type::Point,
"line" => Type::Line,
"lseg" => Type::Lseg,
Expand All @@ -184,6 +187,7 @@ impl Type {
"macaddr" => Type::MacAddr,
"macaddr8" => Type::MacAddr8,
"bit" => Type::Bit(BitAttr::default()),
"bit varying" | "varbit" => Type::VarBit(BitAttr::default()),
"tsvector" => Type::TsVector,
"tsquery" => Type::TsQuery,
"uuid" => Type::Uuid,
Expand Down Expand Up @@ -288,7 +292,7 @@ impl Type {
}

pub fn has_bit_attr(&self) -> bool {
matches!(self, Type::Bit(_))
matches!(self, Type::Bit(_) | Type::VarBit(_))
}

pub fn has_enum_attr(&self) -> bool {
Expand Down
9 changes: 9 additions & 0 deletions src/postgres/parser/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ pub fn parse_bit_attributes(
},
};
}
Type::VarBit(ref mut attr) => {
attr.length = match character_maximum_length {
None => None,
Some(num) => match u16::try_from(num) {
Ok(num) => Some(num),
Err(_) => None,
},
};
}
_ => panic!("parse_bit_attributes(_) received a type that does not have BitAttr"),
};

Expand Down
25 changes: 8 additions & 17 deletions src/postgres/writer/column.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::postgres::def::{ColumnInfo, Type};
use sea_query::{Alias, BlobSize, ColumnDef, ColumnType, DynIden, IntoIden, PgInterval, RcOrArc};
use sea_query::{Alias, ColumnDef, ColumnType, DynIden, IntoIden, PgInterval, RcOrArc, StringLen};
use std::{convert::TryFrom, fmt::Write};

impl ColumnInfo {
Expand Down Expand Up @@ -72,12 +72,13 @@ impl ColumnInfo {
Type::Serial => ColumnType::Integer,
Type::BigSerial => ColumnType::BigInteger,
Type::Money => ColumnType::Money(None),
Type::Varchar(string_attr) => {
ColumnType::String(string_attr.length.map(Into::into))
}
Type::Varchar(string_attr) => match string_attr.length {
Some(length) => ColumnType::String(StringLen::N(length.into())),
None => ColumnType::String(StringLen::None),
},
Type::Char(string_attr) => ColumnType::Char(string_attr.length.map(Into::into)),
Type::Text => ColumnType::Text,
Type::Bytea => ColumnType::Binary(BlobSize::Blob(None)),
Type::Bytea => ColumnType::VarBinary(StringLen::None),
// The SQL standard requires that writing just timestamp be equivalent to timestamp without time zone,
// and PostgreSQL honors that behavior. (https://www.postgresql.org/docs/current/datatype-datetime.html)
Type::Timestamp(_) => ColumnType::DateTime,
Expand Down Expand Up @@ -105,18 +106,8 @@ impl ColumnInfo {
Type::Inet => ColumnType::Custom(Alias::new("inet").into_iden()),
Type::MacAddr => ColumnType::Custom(Alias::new("macaddr").into_iden()),
Type::MacAddr8 => ColumnType::Custom(Alias::new("macaddr8").into_iden()),
Type::Bit(bit_attr) => {
let mut str = String::new();
write!(str, "bit").unwrap();
if bit_attr.length.is_some() {
write!(str, "(").unwrap();
if let Some(length) = bit_attr.length {
write!(str, "{}", length).unwrap();
}
write!(str, ")").unwrap();
}
ColumnType::Custom(Alias::new(&str).into_iden())
}
Type::Bit(bit_attr) => ColumnType::Bit(bit_attr.length.map(Into::into)),
Type::VarBit(bit_attr) => ColumnType::VarBit(bit_attr.length.unwrap_or(1).into()),
Type::TsVector => ColumnType::Custom(Alias::new("tsvector").into_iden()),
Type::TsQuery => ColumnType::Custom(Alias::new("tsquery").into_iden()),
Type::Uuid => ColumnType::Uuid,
Expand Down
9 changes: 5 additions & 4 deletions src/sqlite/def/column.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{DefaultType, Type};
use super::{parse_type, DefaultType};
use sea_query::{
foreign_key::ForeignKeyAction as SeaQueryForeignKeyAction, Alias, Index, IndexCreateStatement,
foreign_key::ForeignKeyAction as SeaQueryForeignKeyAction, Alias, ColumnType, Index,
IndexCreateStatement,
};
use std::num::ParseIntError;

Expand All @@ -12,7 +13,7 @@ use crate::sqlx_types::{sqlite::SqliteRow, Row};
pub struct ColumnInfo {
pub cid: i32,
pub name: String,
pub r#type: Type,
pub r#type: ColumnType,
pub not_null: bool,
pub default_value: DefaultType,
pub primary_key: bool,
Expand All @@ -28,7 +29,7 @@ impl ColumnInfo {
Ok(ColumnInfo {
cid: row.get(0),
name: row.get(1),
r#type: Type::to_type(row.get(2))?,
r#type: parse_type(row.get(2))?,
not_null: col_not_null != 0,
default_value: if default_value == "NULL" {
DefaultType::Null
Expand Down
5 changes: 2 additions & 3 deletions src/sqlite/def/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ impl TableDef {
new_table.table(Alias::new(&self.name));

self.columns.iter().for_each(|column_info| {
let mut new_column = ColumnDef::new(Alias::new(&column_info.name));
let mut new_column =
ColumnDef::new_with_type(Alias::new(&column_info.name), column_info.r#type.clone());
if column_info.not_null {
new_column.not_null();
}
Expand All @@ -235,8 +236,6 @@ impl TableDef {
primary_keys.push(column_info.name.clone());
}

column_info.r#type.write_type(&mut new_column);

match &column_info.default_value {
DefaultType::Integer(integer_value) => {
new_column.default(Value::Int(Some(*integer_value)));
Expand Down
Loading

0 comments on commit b91799d

Please sign in to comment.