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 ColumnType unsigned integer types #211

Merged
merged 2 commits into from
Feb 20, 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
34 changes: 23 additions & 11 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ impl TableBuilder for MysqlQueryBuilder {
None => "varchar(255)".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger(length) => match length {
Some(length) => format!("tinyint({})", length),
None => "tinyint".into(),
},
ColumnType::SmallInteger(length) => match length {
Some(length) => format!("smallint({})", length),
None => "smallint".into(),
},
ColumnType::Integer(length) => match length {
ColumnType::TinyInteger(length) | ColumnType::TinyUnsigned(length) =>
match length {
Some(length) => format!("tinyint({})", length),
None => "tinyint".into(),
},
ColumnType::SmallInteger(length) | ColumnType::SmallUnsigned(length) =>
match length {
Some(length) => format!("smallint({})", length),
None => "smallint".into(),
},
ColumnType::Integer(length) | ColumnType::Unsigned(length) => match length {
Some(length) => format!("int({})", length),
None => "int".into(),
},
ColumnType::BigInteger(length) => match length {
ColumnType::BigInteger(length) | ColumnType::BigUnsigned(length) => match length {
Some(length) => format!("bigint({})", length),
None => "bigint".into(),
},
Expand Down Expand Up @@ -92,7 +94,17 @@ impl TableBuilder for MysqlQueryBuilder {
ColumnType::Array(_) => unimplemented!("Array is not available in MySQL."),
}
)
.unwrap()
.unwrap();
if matches!(
column_type,
ColumnType::TinyUnsigned(_)
| ColumnType::SmallUnsigned(_)
| ColumnType::Unsigned(_)
| ColumnType::BigUnsigned(_)
) {
write!(sql, " ").unwrap();
write!(sql, "UNSIGNED").unwrap();
}
}

fn prepare_column_spec(&self, column_spec: &ColumnSpec, sql: &mut SqlWriter) {
Expand Down
22 changes: 12 additions & 10 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ impl TableBuilder for PostgresQueryBuilder {
None => "varchar".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger(length) => match length {
Some(length) => format!("smallint({})", length),
None => "smallint".into(),
},
ColumnType::SmallInteger(length) => match length {
Some(length) => format!("smallint({})", length),
None => "smallint".into(),
},
ColumnType::Integer(length) => match length {
ColumnType::TinyInteger(length) | ColumnType::TinyUnsigned(length) =>
match length {
Some(length) => format!("smallint({})", length),
None => "smallint".into(),
},
ColumnType::SmallInteger(length) | ColumnType::SmallUnsigned(length) =>
match length {
Some(length) => format!("smallint({})", length),
None => "smallint".into(),
},
ColumnType::Integer(length) | ColumnType::Unsigned(length) => match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
ColumnType::BigInteger(length) => match length {
ColumnType::BigInteger(length) | ColumnType::BigUnsigned(length) => match length {
Some(length) => format!("bigint({})", length),
None => "bigint".into(),
},
Expand Down
22 changes: 12 additions & 10 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,21 @@ impl TableBuilder for SqliteQueryBuilder {
None => "text".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger(length) => match length {
ColumnType::TinyInteger(length) | ColumnType::TinyUnsigned(length) =>
match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
ColumnType::SmallInteger(length) | ColumnType::SmallUnsigned(length) =>
match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
ColumnType::Integer(length) | ColumnType::Unsigned(length) => match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
ColumnType::SmallInteger(length) => match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
ColumnType::Integer(length) => match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
ColumnType::BigInteger(length) => match length {
ColumnType::BigInteger(length) | ColumnType::BigUnsigned(length) => match length {
Some(length) => format!("integer({})", length),
None => "integer".into(),
},
Expand Down
52 changes: 52 additions & 0 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub enum ColumnType {
SmallInteger(Option<u32>),
Integer(Option<u32>),
BigInteger(Option<u32>),
TinyUnsigned(Option<u32>),
SmallUnsigned(Option<u32>),
Unsigned(Option<u32>),
BigUnsigned(Option<u32>),
Float(Option<u32>),
Double(Option<u32>),
Decimal(Option<(u32, u32)>),
Expand Down Expand Up @@ -230,6 +234,54 @@ impl ColumnDef {
self
}

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

/// Set column type as tiny_unsigned
pub fn tiny_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::TinyUnsigned(None));
self
}

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

/// Set column type as small_unsigned
pub fn small_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::SmallUnsigned(None));
self
}

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

/// Set column type as unsigned
pub fn unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::Unsigned(None));
self
}

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

/// Set column type as big_unsigned
pub fn big_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::BigUnsigned(None));
self
}

/// Set column type as float with custom precision
pub fn float_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Float(Some(precision)));
Expand Down
10 changes: 4 additions & 6 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,10 @@ type_to_value!(i8, TinyInt, TinyInteger(None));
type_to_value!(i16, SmallInt, SmallInteger(None));
type_to_value!(i32, Int, Integer(None));
type_to_value!(i64, BigInt, BigInteger(None));

// FIXME: edit this mapping after we added unsigned column types
type_to_value!(u8, TinyUnsigned, TinyInteger(None));
type_to_value!(u16, SmallUnsigned, SmallInteger(None));
type_to_value!(u32, Unsigned, Integer(None));
type_to_value!(u64, BigUnsigned, BigInteger(None));
type_to_value!(u8, TinyUnsigned, TinyUnsigned(None));
type_to_value!(u16, SmallUnsigned, SmallUnsigned(None));
type_to_value!(u32, Unsigned, Unsigned(None));
type_to_value!(u64, BigUnsigned, BigUnsigned(None));
type_to_value!(f32, Float, Float(None));
type_to_value!(f64, Double, Double(None));

Expand Down
8 changes: 4 additions & 4 deletions tests/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ fn create_3() {
)
.col(ColumnDef::new(Char::FontSize).integer_len(11).not_null())
.col(ColumnDef::new(Char::Character).string_len(255).not_null())
.col(ColumnDef::new(Char::SizeW).integer_len(11).not_null())
.col(ColumnDef::new(Char::SizeH).integer_len(11).not_null())
.col(ColumnDef::new(Char::SizeW).unsigned_len(11).not_null())
.col(ColumnDef::new(Char::SizeH).unsigned_len(11).not_null())
.col(
ColumnDef::new(Char::FontId)
.integer_len(11)
Expand All @@ -99,8 +99,8 @@ fn create_3() {
"`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,",
"`font_size` int(11) NOT NULL,",
"`character` varchar(255) NOT NULL,",
"`size_w` int(11) NOT NULL,",
"`size_h` int(11) NOT NULL,",
"`size_w` int(11) UNSIGNED NOT NULL,",
"`size_h` int(11) UNSIGNED NOT NULL,",
"`font_id` int(11) DEFAULT NULL,",
"CONSTRAINT `FK_2e303c3a712662f1fc2a4d0aad6`",
"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`)",
Expand Down
4 changes: 2 additions & 2 deletions tests/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ fn create_3() {
)
.col(ColumnDef::new(Char::FontSize).integer().not_null())
.col(ColumnDef::new(Char::Character).string_len(255).not_null())
.col(ColumnDef::new(Char::SizeW).integer().not_null())
.col(ColumnDef::new(Char::SizeH).integer().not_null())
.col(ColumnDef::new(Char::SizeW).unsigned().not_null())
.col(ColumnDef::new(Char::SizeH).unsigned().not_null())
.col(
ColumnDef::new(Char::FontId)
.integer()
Expand Down
4 changes: 2 additions & 2 deletions tests/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ fn create_3() {
)
.col(ColumnDef::new(Char::FontSize).integer().not_null())
.col(ColumnDef::new(Char::Character).string().not_null())
.col(ColumnDef::new(Char::SizeW).integer().not_null())
.col(ColumnDef::new(Char::SizeH).integer().not_null())
.col(ColumnDef::new(Char::SizeW).unsigned().not_null())
.col(ColumnDef::new(Char::SizeH).unsigned().not_null())
.col(
ColumnDef::new(Char::FontId)
.integer()
Expand Down