Skip to content

Commit

Permalink
Merge pull request #466 from karatakis/extra-types
Browse files Browse the repository at this point in the history
Add year, bit and varbit types
  • Loading branch information
ikrivosheev authored Oct 14, 2022
2 parents 678f310 + 4343b0f commit 122b325
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ impl TableBuilder for MysqlQueryBuilder {
None => "time".into(),
},
ColumnType::Date => "date".into(),
ColumnType::Year(length) => {
match length {
Some(length) => match length {
MySqlYear::Two => "year(2)".into(),
MySqlYear::Four => "year(4)".into(),
},
None => "year".into(),
}
}
ColumnType::Interval(_, _) => "unsupported".into(),
ColumnType::Binary(blob_size) => match blob_size {
BlobSize::Tiny => "tinyblob".into(),
Expand All @@ -89,6 +98,15 @@ impl TableBuilder for MysqlQueryBuilder {
BlobSize::Long => "longblob".into(),
},
ColumnType::VarBinary(length) => format!("varbinary({})", length),
ColumnType::Bit(length) => {
match length {
Some(length) => format!("bit({})", length),
None => "bit".into(),
}
}
ColumnType::VarBit(length) => {
format!("bit({})", length)
}
ColumnType::Boolean => "bool".into(),
ColumnType::Money(precision) => match precision {
Some((precision, scale)) => format!("money({}, {})", precision, scale),
Expand Down
10 changes: 10 additions & 0 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ impl TableBuilder for PostgresQueryBuilder {
}
ColumnType::Binary(_) => "bytea".into(),
ColumnType::VarBinary(length) => format!("bit varying({})", length),
ColumnType::Bit(length) => {
match length {
Some(length) => format!("varbit({})", length),
None => "bit".into(),
}
}
ColumnType::VarBit(length) => {
format!("varbit({})", length)
}
ColumnType::Boolean => "bool".into(),
ColumnType::Money(precision) => match precision {
Some((precision, scale)) => format!("money({}, {})", precision, scale),
Expand All @@ -102,6 +111,7 @@ impl TableBuilder for PostgresQueryBuilder {
ColumnType::Cidr => "cidr".into(),
ColumnType::Inet => "inet".into(),
ColumnType::MacAddr => "macaddr".into(),
ColumnType::Year(_) => unimplemented!("Year is not available in Postgres."),
}
)
.unwrap()
Expand Down
3 changes: 3 additions & 0 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ impl TableBuilder for SqliteQueryBuilder {
ColumnType::Cidr => unimplemented!("Cidr is not available in Sqlite."),
ColumnType::Inet => unimplemented!("Inet is not available in Sqlite."),
ColumnType::MacAddr => unimplemented!("MacAddr is not available in Sqlite."),
ColumnType::Year(_) => unimplemented!("Year is not available in Sqlite."),
ColumnType::Bit(_) => unimplemented!("Bit is not available in Sqlite."),
ColumnType::VarBit(_) => unimplemented!("VarBit is not available in Sqlite."),
}
)
.unwrap()
Expand Down
29 changes: 29 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ pub enum ColumnType {
TimestampWithTimeZone(Option<u32>),
Time(Option<u32>),
Date,
Year(Option<MySqlYear>),
Interval(Option<PgInterval>, Option<u32>),
Binary(BlobSize),
VarBinary(u32),
Bit(Option<u32>),
VarBit(u32),
Boolean,
Money(Option<(u32, u32)>),
Json,
Expand Down Expand Up @@ -81,6 +84,13 @@ pub enum PgInterval {
MinuteToSecond,
}

// All MySQL year type field length sizes
#[derive(Debug, Clone)]
pub enum MySqlYear {
Two,
Four,
}

#[derive(Debug, Clone)]
pub enum BlobSize {
Tiny,
Expand Down Expand Up @@ -439,6 +449,13 @@ impl ColumnDef {
self
}

/// Set column type as year
/// Only MySQL supports year
pub fn year(&mut self, length: Option<MySqlYear>) -> &mut Self {
self.types = Some(ColumnType::Year(length));
self
}

/// Set column type as binary with custom length
pub fn binary_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Binary(BlobSize::Blob(Some(length))));
Expand All @@ -463,6 +480,18 @@ impl ColumnDef {
self
}

/// Set column type as bit with variable length
pub fn bit(&mut self, length: Option<u32>) -> &mut Self {
self.types = Some(ColumnType::Bit(length));
self
}

/// Set column type as varbit with variable length
pub fn varbit(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::VarBit(length));
self
}

/// Set column type as boolean
pub fn boolean(&mut self) -> &mut Self {
self.types = Some(ColumnType::Boolean);
Expand Down
11 changes: 11 additions & 0 deletions tests/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ fn create_7() {
);
}

#[test]
fn create_8() {
assert_eq!(
Table::create()
.table(Font::Table)
.col(ColumnDef::new(Font::Variant).year(Some(MySqlYear::Four)))
.to_string(MysqlQueryBuilder),
["CREATE TABLE `font` (", "`variant` year(4)", ")",].join(" ")
);
}

#[test]
fn drop_1() {
assert_eq!(
Expand Down

0 comments on commit 122b325

Please sign in to comment.