Skip to content

Commit

Permalink
Added ColumnType::Blob (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored May 3, 2024
1 parent 33c0dae commit 12fe34b
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ assert_eq!(
.col(ColumnDef::new(Alias::new("boolean_col")).boolean())
.col(ColumnDef::new(Alias::new("binary2")).binary_len(1024))
.col(ColumnDef::new(Alias::new("binary3")).var_binary(1024))
.col(ColumnDef::new(Alias::new("binary4")).blob())
.to_string(SqliteQueryBuilder),
[
r#"CREATE TABLE "strange" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
Expand All @@ -77,7 +78,8 @@ assert_eq!(
r#""datetime_col" datetime_text,"#,
r#""boolean_col" boolean,"#,
r#""binary2" blob(1024),"#,
r#""binary3" varbinary_blob(1024)"#,
r#""binary3" varbinary_blob(1024),"#,
r#""binary4" blob"#,
r#")"#,
]
.join(" ")
Expand All @@ -91,7 +93,7 @@ assert_eq!(
.table(BinaryType::Table)
.col(ColumnDef::new(BinaryType::BinaryLen).binary_len(32))
.col(ColumnDef::new(BinaryType::Binary).binary())
.col(ColumnDef::new(BinaryType::Blob).custom(MySqlType::Blob))
.col(ColumnDef::new(BinaryType::Blob).blob())
.col(ColumnDef::new(BinaryType::TinyBlob).custom(MySqlType::TinyBlob))
.col(ColumnDef::new(BinaryType::MediumBlob).custom(MySqlType::MediumBlob))
.col(ColumnDef::new(BinaryType::LongBlob).custom(MySqlType::LongBlob))
Expand Down
1 change: 1 addition & 0 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl TableBuilder for MysqlQueryBuilder {
StringLen::None => "varbinary(255)".into(),
StringLen::Max => "varbinary(65535)".into(),
},
ColumnType::Blob => "blob".into(),
ColumnType::Bit(length) => {
match length {
Some(length) => format!("bit({length})"),
Expand Down
3 changes: 2 additions & 1 deletion src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl TableBuilder for PostgresQueryBuilder {
}
typ
}
ColumnType::Binary(_) | ColumnType::VarBinary(_) => "bytea".into(),
ColumnType::Binary(_) | ColumnType::VarBinary(_) | ColumnType::Blob =>
"bytea".into(),
ColumnType::Bit(length) => {
match length {
Some(length) => format!("bit({length})"),
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 @@ -172,6 +172,7 @@ impl SqliteQueryBuilder {
StringLen::N(length) => format!("varbinary_blob({length})"),
_ => "varbinary_blob".into(),
},
ColumnType::Blob => "blob".into(),
ColumnType::Boolean => "boolean".into(),
ColumnType::Money(precision) => match precision {
Some((precision, scale)) => format!("real_money({precision}, {scale})"),
Expand Down
2 changes: 0 additions & 2 deletions src/extension/mysql/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::Iden;

#[derive(Debug, Copy, Clone)]
pub enum MySqlType {
Blob,
TinyBlob,
MediumBlob,
LongBlob,
Expand All @@ -11,7 +10,6 @@ pub enum MySqlType {
impl Iden for MySqlType {
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
let ty = match self {
Self::Blob => "blob",
Self::TinyBlob => "tinyblob",
Self::MediumBlob => "mediumblob",
Self::LongBlob => "longblob",
Expand Down
8 changes: 5 additions & 3 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub trait IntoColumnDef {
/// | Date | date | date | date_text |
/// | Year | year | N/A | N/A |
/// | Interval | N/A | interval | N/A |
/// | Blob | blob | bytea | blob |
/// | Binary | binary | bytea | blob |
/// | VarBinary | varbinary | bytea | varbinary_blob |
/// | Bit | bit | bit | N/A |
Expand All @@ -59,6 +60,7 @@ pub enum ColumnType {
Char(Option<u32>),
String(StringLen),
Text,
Blob,
TinyInteger,
SmallInteger,
Integer,
Expand Down Expand Up @@ -512,10 +514,10 @@ impl ColumnDef {
self
}

#[cfg(feature = "backend-mysql")]
/// Set column type as blob. MySQL only.
/// Set column type as blob
pub fn blob(&mut self) -> &mut Self {
self.custom(crate::extension::mysql::MySqlType::Blob)
self.types = Some(ColumnType::Blob);
self
}

/// Set column type as boolean
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn create_7() {
assert_eq!(
Table::create()
.table(Char::Table)
.col(ColumnDef::new(BinaryType::Blob).custom(MySqlType::Blob))
.col(ColumnDef::new(BinaryType::Blob).blob())
.col(ColumnDef::new(Char::Character).binary())
.col(ColumnDef::new(Char::FontSize).binary_len(10))
.col(ColumnDef::new(Char::SizeW).var_binary(10))
Expand Down

0 comments on commit 12fe34b

Please sign in to comment.