Skip to content

Commit

Permalink
Remap SQLite types
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Jan 4, 2024
1 parent 45c1d30 commit 3cc6ad9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 47 deletions.
56 changes: 30 additions & 26 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,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 {
Expand All @@ -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."),
Expand Down
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
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
36 changes: 18 additions & 18 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#""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(" ")
Expand All @@ -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(" ")
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

0 comments on commit 3cc6ad9

Please sign in to comment.