From 3cc6ad9f8c05041c85d64063375ab8a4342ae1f0 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Thu, 4 Jan 2024 18:52:39 +0000 Subject: [PATCH] Remap SQLite types --- src/backend/sqlite/table.rs | 56 ++++++++++++++++++++----------------- src/lib.rs | 2 +- src/table/create.rs | 4 +-- tests/sqlite/table.rs | 36 ++++++++++++------------ 4 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/backend/sqlite/table.rs b/src/backend/sqlite/table.rs index 3a5a32712..76c4d917b 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,16 +131,16 @@ 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 => "tinyint".into(), + ColumnType::SmallInteger | ColumnType::SmallUnsigned => "smallint".into(), ColumnType::Integer | ColumnType::Unsigned => "integer".into(), #[allow(clippy::if_same_then_else)] ColumnType::BigInteger | ColumnType::BigUnsigned => if is_auto_increment { @@ -151,33 +151,37 @@ impl SqliteQueryBuilder { "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})"), - None => "real".into(), + Some((precision, scale)) => format!("decimal({precision}, {scale})"), + None => "decimal".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!("binary({length})[blob]"), + BlobSize::Blob(None) => format!("binary[blob]"), + BlobSize::Medium => "mediumblob".into(), + BlobSize::Long => "longblob".into(), }, - ColumnType::VarBinary(length) => format!("binary({length})"), + ColumnType::VarBinary(length) => format!("varbinary({length})[blob]"), 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."), 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..3ce8a0383 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#""bin" blob,"#, - r#""defb" binary(32),"#, - r#""tb" blob,"#, - r#""b" blob,"#, - r#""mb" blob,"#, - r#""lb" blob"#, + r#""binlen" binary(32)[blob],"#, + r#""bin" binary[blob],"#, + r#""defb" binary(32)[blob],"#, + r#""tb" tinyblob,"#, + r#""b" binary[blob],"#, + r#""mb" mediumblob,"#, + r#""lb" longblob"#, r#")"#, ] .join(" ") @@ -139,9 +139,9 @@ fn create_5() { .to_string(SqliteQueryBuilder), [ r#"CREATE TABLE "character" ("#, - r#""character" blob,"#, - r#""font_size" binary(10),"#, - r#""size_w" binary(10)"#, + r#""character" binary[blob],"#, + r#""font_size" binary(10)[blob],"#, + r#""size_w" varbinary(10)[blob]"#, 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,"#,