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

Remap SQLite types #735

Merged
merged 5 commits into from
Jan 29, 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
8 changes: 2 additions & 6 deletions sea-query-binder/src/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,12 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues {
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(decimal) => {
use rust_decimal::prelude::ToPrimitive;
args.add(
decimal.map(|d| d.to_f64().expect("Fail to convert rust_decimal as f64")),
);
args.add(decimal.map(|d| d.to_string()));
}
#[cfg(feature = "with-bigdecimal")]
Value::BigDecimal(big_decimal) => {
use bigdecimal::ToPrimitive;
args.add(
big_decimal.map(|d| d.to_f64().expect("Fail to convert bigdecimal as f64")),
);
args.add(big_decimal.map(|d| d.to_string()));
}
#[cfg(feature = "with-json")]
Value::Json(j) => {
Expand Down
71 changes: 43 additions & 28 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl TableBuilder for SqliteQueryBuilder {
}

fn prepare_table_drop_opt(&self, _drop_opt: &TableDropOpt, _sql: &mut dyn SqlWriter) {
// SQLite does not support table drop options
// Sqlite does not support table drop options
}

fn prepare_table_truncate_statement(
Expand Down Expand Up @@ -131,53 +131,60 @@ impl SqliteQueryBuilder {
"{}",
match column_type {
ColumnType::Char(length) => match length {
Some(length) => format!("text({length})"),
None => "text".into(),
Some(length) => format!("char({length})"),
None => "char".into(),
},
ColumnType::String(length) => match length {
Some(length) => format!("text({length})"),
None => "text".into(),
Some(length) => format!("varchar({length})"),
None => "varchar".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger | ColumnType::TinyUnsigned => "integer".into(),
ColumnType::SmallInteger | ColumnType::SmallUnsigned => "integer".into(),
ColumnType::TinyInteger | ColumnType::TinyUnsigned => integer("tinyint").into(),
ColumnType::SmallInteger | ColumnType::SmallUnsigned => integer("smallint").into(),
ColumnType::Integer | ColumnType::Unsigned => "integer".into(),
#[allow(clippy::if_same_then_else)]
ColumnType::BigInteger | ColumnType::BigUnsigned => if is_auto_increment {
"integer"
} else if cfg!(feature = "option-sqlite-exact-column-type") {
"integer"
} else {
"bigint"
integer("bigint")
}
.into(),
ColumnType::Float => "real".into(),
ColumnType::Double => "real".into(),
ColumnType::Float => "float".into(),
ColumnType::Double => "double".into(),
ColumnType::Decimal(precision) => match precision {
Some((precision, scale)) => format!("real({precision}, {scale})"),
Some((precision, scale)) => {
if precision > &16 {
panic!("precision cannot be larger than 16");
}
format!("real({precision}, {scale})")
}
None => "real".into(),
},
ColumnType::DateTime => "text".into(),
ColumnType::Timestamp => "text".into(),
ColumnType::TimestampWithTimeZone => "text".into(),
ColumnType::Time => "text".into(),
ColumnType::Date => "text".into(),
ColumnType::Interval(_, _) => "unsupported".into(),
ColumnType::DateTime => "datetime_text".into(),
ColumnType::Timestamp => "timestamp_text".into(),
ColumnType::TimestampWithTimeZone => "timestamp_with_timezone_text".into(),
ColumnType::Time => "time_text".into(),
ColumnType::Date => "date_text".into(),
ColumnType::Interval(_, _) =>
unimplemented!("Interval is not available in Sqlite."),
ColumnType::Binary(blob_size) => match blob_size {
BlobSize::Blob(Some(length)) => format!("binary({length})"),
_ => "blob".into(),
BlobSize::Tiny => "tinyblob".into(),
BlobSize::Blob(Some(length)) => format!("blob({length})"),
BlobSize::Blob(None) => "blob".into(),
BlobSize::Medium => "mediumblob".into(),
BlobSize::Long => "longblob".into(),
},
ColumnType::VarBinary(length) => format!("binary({length})"),
ColumnType::VarBinary(length) => format!("varbinary_blob({length})"),
ColumnType::Boolean => "boolean".into(),
ColumnType::Money(precision) => match precision {
Some((precision, scale)) => format!("integer({precision}, {scale})"),
None => "integer".into(),
Some((precision, scale)) => format!("money({precision}, {scale})"),
None => "money".into(),
},
ColumnType::Json => "text".into(),
ColumnType::JsonBinary => "text".into(),
ColumnType::Uuid => "text(36)".into(),
ColumnType::Json => "json_text".into(),
ColumnType::JsonBinary => "jsonb_text".into(),
ColumnType::Uuid => "uuid_text".into(),
ColumnType::Custom(iden) => iden.to_string(),
ColumnType::Enum { .. } => "text".into(),
ColumnType::Enum { .. } => "enum_text".into(),
ColumnType::Array(_) => unimplemented!("Array is not available in Sqlite."),
ColumnType::Cidr => unimplemented!("Cidr is not available in Sqlite."),
ColumnType::Inet => unimplemented!("Inet is not available in Sqlite."),
Expand All @@ -191,3 +198,11 @@ impl SqliteQueryBuilder {
.unwrap()
}
}

fn integer(ty: &str) -> &str {
if cfg!(feature = "option-sqlite-exact-column-type") {
"integer"
} else {
ty
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@
//! r#"CREATE TABLE IF NOT EXISTS "character" ("#,
//! r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
//! r#""font_size" integer NOT NULL,"#,
//! r#""character" text NOT NULL,"#,
//! r#""character" varchar NOT NULL,"#,
//! r#""size_w" integer NOT NULL,"#,
//! r#""size_h" integer NOT NULL,"#,
//! r#""font_id" integer DEFAULT NULL,"#,
Expand Down
29 changes: 18 additions & 11 deletions src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ where

#[cfg(test)]
#[cfg(feature = "backend-mysql")]
mod tests {
mod tests_mysql {
use super::*;
use pretty_assertions::assert_eq;

Expand Down Expand Up @@ -151,6 +151,21 @@ mod tests {

#[test]
fn inject_parameters_4() {
assert_eq!(
inject_parameters("?", [vec![0xABu8, 0xCD, 0xEF].into()], &MysqlQueryBuilder),
"x'ABCDEF'"
);
}
}

#[cfg(test)]
#[cfg(feature = "backend-postgres")]
mod tests_postgres {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn inject_parameters_5() {
assert_eq!(
inject_parameters(
"WHERE A = $1 AND C = $2",
Expand All @@ -162,7 +177,7 @@ mod tests {
}

#[test]
fn inject_parameters_5() {
fn inject_parameters_6() {
assert_eq!(
inject_parameters(
"WHERE A = $2 AND C = $1",
Expand All @@ -174,18 +189,10 @@ mod tests {
}

#[test]
fn inject_parameters_6() {
fn inject_parameters_7() {
assert_eq!(
inject_parameters("WHERE A = $1", ["B'C".into()], &PostgresQueryBuilder),
"WHERE A = E'B\\'C'"
);
}

#[test]
fn inject_parameters_7() {
assert_eq!(
inject_parameters("?", [vec![0xABu8, 0xCD, 0xEF].into()], &MysqlQueryBuilder),
"x'ABCDEF'"
);
}
}
4 changes: 2 additions & 2 deletions src/table/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use crate::{
/// r#"CREATE TABLE IF NOT EXISTS "character" ("#,
/// r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
/// r#""font_size" integer NOT NULL,"#,
/// r#""character" text NOT NULL,"#,
/// r#""character" varchar NOT NULL,"#,
/// r#""size_w" integer NOT NULL,"#,
/// r#""size_h" integer NOT NULL,"#,
/// r#""font_id" integer DEFAULT NULL,"#,
Expand Down Expand Up @@ -215,7 +215,7 @@ impl TableCreateStatement {
/// [
/// r#"CREATE TABLE "glyph" ("#,
/// r#""id" integer NOT NULL,"#,
/// r#""image" text NOT NULL,"#,
/// r#""image" varchar NOT NULL,"#,
/// r#"PRIMARY KEY ("id", "image")"#,
/// r#")"#,
/// ]
Expand Down
30 changes: 15 additions & 15 deletions tests/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn create_1() {
[
r#"CREATE TABLE "glyph" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""aspect" real NOT NULL,"#,
r#""aspect" double NOT NULL,"#,
r#""image" text"#,
r#")"#,
]
Expand All @@ -46,9 +46,9 @@ fn create_2() {
[
r#"CREATE TABLE "font" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""name" text NOT NULL,"#,
r#""variant" text NOT NULL,"#,
r#""language" text NOT NULL"#,
r#""name" varchar NOT NULL,"#,
r#""variant" varchar NOT NULL,"#,
r#""language" varchar NOT NULL"#,
r#")"#,
]
.join(" ")
Expand Down Expand Up @@ -89,7 +89,7 @@ fn create_3() {
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" text NOT NULL,"#,
r#""character" varchar NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
Expand All @@ -115,13 +115,13 @@ fn create_4() {
.to_string(SqliteQueryBuilder),
[
r#"CREATE TABLE "binary_type" ("#,
r#""binlen" binary(32),"#,
r#""binlen" blob(32),"#,
r#""bin" blob,"#,
r#""defb" binary(32),"#,
r#""tb" blob,"#,
r#""defb" blob(32),"#,
r#""tb" tinyblob,"#,
r#""b" blob,"#,
r#""mb" blob,"#,
r#""lb" blob"#,
r#""mb" mediumblob,"#,
r#""lb" longblob"#,
r#")"#,
]
.join(" ")
Expand All @@ -140,8 +140,8 @@ fn create_5() {
[
r#"CREATE TABLE "character" ("#,
r#""character" blob,"#,
r#""font_size" binary(10),"#,
r#""size_w" binary(10)"#,
r#""font_size" blob(10),"#,
r#""size_w" varbinary_blob(10)"#,
r#")"#,
]
.join(" ")
Expand Down Expand Up @@ -229,7 +229,7 @@ fn create_with_unique_index() {
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" text NOT NULL,"#,
r#""character" varchar NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
Expand Down Expand Up @@ -274,7 +274,7 @@ fn create_with_primary_unique_index() {
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" text NOT NULL,"#,
r#""character" varchar NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
Expand Down Expand Up @@ -328,7 +328,7 @@ fn create_with_unique_index_constraint() {
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
r#""font_size" integer NOT NULL,"#,
r#""character" text NOT NULL,"#,
r#""character" varchar NOT NULL,"#,
r#""size_w" integer NOT NULL,"#,
r#""size_h" integer NOT NULL,"#,
r#""font_id" integer DEFAULT NULL,"#,
Expand Down
Loading