From ff7b2436d1a6fd1b398b044b13f980e557861bcd Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 18:38:53 +0800 Subject: [PATCH 1/2] Add `ColumnType::Enum` --- src/backend/mysql/table.rs | 1 + src/backend/postgres/table.rs | 1 + src/backend/sqlite/table.rs | 1 + src/table/column.rs | 1 + 4 files changed, 4 insertions(+) diff --git a/src/backend/mysql/table.rs b/src/backend/mysql/table.rs index c72c038f2..663fd3192 100644 --- a/src/backend/mysql/table.rs +++ b/src/backend/mysql/table.rs @@ -88,6 +88,7 @@ impl TableBuilder for MysqlQueryBuilder { ColumnType::JsonBinary => "json".into(), ColumnType::Uuid => "binary(16)".into(), ColumnType::Custom(iden) => iden.to_string(), + ColumnType::Enum(_, variants) => format!("ENUM('{}')", variants.join("', '")), } ) .unwrap() diff --git a/src/backend/postgres/table.rs b/src/backend/postgres/table.rs index 8f35347c6..3ad85fa77 100644 --- a/src/backend/postgres/table.rs +++ b/src/backend/postgres/table.rs @@ -96,6 +96,7 @@ impl TableBuilder for PostgresQueryBuilder { ColumnType::JsonBinary => "jsonb".into(), ColumnType::Uuid => "uuid".into(), ColumnType::Custom(iden) => iden.to_string(), + ColumnType::Enum(name, _) => name.into(), } ) .unwrap() diff --git a/src/backend/sqlite/table.rs b/src/backend/sqlite/table.rs index f3840133f..be3031ceb 100644 --- a/src/backend/sqlite/table.rs +++ b/src/backend/sqlite/table.rs @@ -108,6 +108,7 @@ impl TableBuilder for SqliteQueryBuilder { ColumnType::JsonBinary => "text".into(), ColumnType::Uuid => "text(36)".into(), ColumnType::Custom(iden) => iden.to_string(), + ColumnType::Enum(_, _) => "text".into(), } ) .unwrap() diff --git a/src/table/column.rs b/src/table/column.rs index d319ee42c..64ad6a747 100644 --- a/src/table/column.rs +++ b/src/table/column.rs @@ -36,6 +36,7 @@ pub enum ColumnType { JsonBinary, Uuid, Custom(DynIden), + Enum(String, Vec), } /// All column specification keywords From 80e0d9504e8a04ff897c81c81b9660fbcfa314d2 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 17 Nov 2021 10:36:48 +0800 Subject: [PATCH 2/2] Add `enumeration` helper method --- src/table/column.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/table/column.rs b/src/table/column.rs index 64ad6a747..73af174dc 100644 --- a/src/table/column.rs +++ b/src/table/column.rs @@ -403,6 +403,20 @@ impl ColumnDef { self } + /// Set column type as enum. + pub fn enumeration(&mut self, name: N, variants: V) -> &mut Self + where + N: ToString, + S: ToString, + V: IntoIterator, + { + self.types = Some(ColumnType::Enum( + name.to_string(), + variants.into_iter().map(|v| v.to_string()).collect(), + )); + self + } + /// Some extra options in custom string pub fn extra(&mut self, string: String) -> &mut Self { self.spec.push(ColumnSpec::Extra(string));