Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump SeaQuery and update blob data types #122

Merged
merged 9 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", git = "https://github.com/seaql/sea-query", branch = "sqlite-types", default-features = false, features = ["derive"] }
sea-query-binder = { version = "0.5.0", git = "https://github.com/seaql/sea-query", branch = "sqlite-types", default-features = false, optional = true }
sea-query = { version = "0.31.0", git = "https://github.com/seaql/sea-query", default-features = false, features = ["derive"] }
sea-query-binder = { version = "0.5.0", git = "https://github.com/seaql/sea-query", 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
16 changes: 8 additions & 8 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 @@ -156,14 +156,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 +185,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
11 changes: 6 additions & 5 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
7 changes: 4 additions & 3 deletions src/sqlite/def/column.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{parse_type, 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 Down
21 changes: 12 additions & 9 deletions src/sqlite/def/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use sea_query::{BlobSize, ColumnType};
use sea_query::{ColumnType, StringLen};
use std::num::ParseIntError;

pub type Type = ColumnType;

pub fn parse_type(data_type: &str) -> Result<ColumnType, ParseIntError> {
let mut type_name = data_type;
let mut parts: Vec<u32> = Vec::new();
Expand All @@ -20,7 +18,10 @@ pub fn parse_type(data_type: &str) -> Result<ColumnType, ParseIntError> {
}
Ok(match type_name.to_lowercase().as_str() {
"char" => ColumnType::Char(parts.into_iter().next()),
"varchar" => ColumnType::String(parts.into_iter().next()),
"varchar" => ColumnType::String(match parts.into_iter().next() {
Some(length) => StringLen::N(length),
None => StringLen::None,
}),
"text" => ColumnType::Text,
"tinyint" => ColumnType::TinyInteger,
"smallint" => ColumnType::SmallInteger,
Expand All @@ -38,11 +39,13 @@ pub fn parse_type(data_type: &str) -> Result<ColumnType, ParseIntError> {
"timestamp_with_timezone_text" => ColumnType::TimestampWithTimeZone,
"time_text" => ColumnType::Time,
"date_text" => ColumnType::Date,
"tinyblob" => ColumnType::Binary(BlobSize::Tiny),
"mediumblob" => ColumnType::Binary(BlobSize::Medium),
"longblob" => ColumnType::Binary(BlobSize::Long),
"blob" => ColumnType::Binary(BlobSize::Blob(parts.into_iter().next())),
"varbinary_blob" if parts.len() == 1 => ColumnType::VarBinary(parts[0]),
"blob" if parts.len() == 1 => ColumnType::Binary(parts[0]),
"varbinary_blob" if parts.len() == 1 => {
ColumnType::VarBinary(match parts.into_iter().next() {
Some(length) => StringLen::N(length),
None => StringLen::None,
})
}
"boolean" => ColumnType::Boolean,
"money" => ColumnType::Money(if parts.len() == 2 {
Some((parts[0], parts[1]))
Expand Down
8 changes: 2 additions & 6 deletions tests/live/sqlite/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use sqlx::SqlitePool;
use std::collections::HashMap;

use sea_schema::sea_query::{
Alias, BlobSize, ColumnDef, Expr, ForeignKey, ForeignKeyAction, ForeignKeyCreateStatement,
Index, Query, SqliteQueryBuilder, Table, TableCreateStatement, TableRef,
Alias, ColumnDef, Expr, ForeignKey, ForeignKeyAction, ForeignKeyCreateStatement, Index, Query,
SqliteQueryBuilder, Table, TableCreateStatement, TableRef,
};
use sea_schema::sqlite::{
def::TableDef,
Expand Down Expand Up @@ -580,11 +580,7 @@ fn create_strange_table() -> TableCreateStatement {
.col(ColumnDef::new(Alias::new("time_col")).time())
.col(ColumnDef::new(Alias::new("datetime_col")).date_time())
.col(ColumnDef::new(Alias::new("boolean_col")).boolean())
.col(ColumnDef::new(Alias::new("binary1")).binary())
.col(ColumnDef::new(Alias::new("binary2")).binary_len(1024))
.col(ColumnDef::new(Alias::new("binary3")).var_binary(1024))
.col(ColumnDef::new(Alias::new("binary4")).blob(BlobSize::Tiny))
.col(ColumnDef::new(Alias::new("binary5")).blob(BlobSize::Medium))
.col(ColumnDef::new(Alias::new("binary6")).blob(BlobSize::Long))
.to_owned()
}
Loading