Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add year, bit and varbit types #466

Merged
merged 5 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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