Skip to content

Commit

Permalink
Merge pull request #188 from SeaQL/enum-column-type
Browse files Browse the repository at this point in the history
Add `ColumnType::Enum`
  • Loading branch information
tyt2y3 authored Nov 17, 2021
2 parents 82ab40a + 80e0d95 commit 1f6a61a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,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()
Expand Down
1 change: 1 addition & 0 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum ColumnType {
JsonBinary,
Uuid,
Custom(DynIden),
Enum(String, Vec<String>),
}

/// All column specification keywords
Expand Down Expand Up @@ -402,6 +403,20 @@ impl ColumnDef {
self
}

/// Set column type as enum.
pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Self
where
N: ToString,
S: ToString,
V: IntoIterator<Item = S>,
{
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));
Expand Down

0 comments on commit 1f6a61a

Please sign in to comment.