diff --git a/sea-query-binder/src/sqlx_sqlite.rs b/sea-query-binder/src/sqlx_sqlite.rs index d100ef6d2..1865ba6f6 100644 --- a/sea-query-binder/src/sqlx_sqlite.rs +++ b/sea-query-binder/src/sqlx_sqlite.rs @@ -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) => { diff --git a/src/backend/sqlite/table.rs b/src/backend/sqlite/table.rs index 3a5a32712..e32741bea 100644 --- a/src/backend/sqlite/table.rs +++ b/src/backend/sqlite/table.rs @@ -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( @@ -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."), @@ -191,3 +198,11 @@ impl SqliteQueryBuilder { .unwrap() } } + +fn integer(ty: &str) -> &str { + if cfg!(feature = "option-sqlite-exact-column-type") { + "integer" + } else { + ty + } +} diff --git a/src/lib.rs b/src/lib.rs index 17987430c..8ae26b945 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,"#, diff --git a/src/table/create.rs b/src/table/create.rs index 022e194f6..77739f98c 100644 --- a/src/table/create.rs +++ b/src/table/create.rs @@ -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,"#, @@ -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#")"#, /// ] diff --git a/tests/sqlite/table.rs b/tests/sqlite/table.rs index b79af8530..aa377359d 100644 --- a/tests/sqlite/table.rs +++ b/tests/sqlite/table.rs @@ -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#")"#, ] @@ -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(" ") @@ -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,"#, @@ -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(" ") @@ -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(" ") @@ -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,"#, @@ -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,"#, @@ -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,"#,