Skip to content

Commit

Permalink
mysql: sized blob data type moved to MySqlType & upstream StringLen
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jan 30, 2024
1 parent 82d0528 commit 3e97249
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 103 deletions.
21 changes: 8 additions & 13 deletions src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ impl TableBuilder for MysqlQueryBuilder {
None => "char".into(),
},
ColumnType::String(length) => match length {
Some(length) => format!("varchar({length})"),
None => "varchar(255)".into(),
StringLen::N(length) => format!("varchar({length})"),
StringLen::None => format!("varchar(255)"),
StringLen::Max => "varchar(65535)".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger | ColumnType::TinyUnsigned => "tinyint".into(),
Expand Down Expand Up @@ -63,18 +64,12 @@ impl TableBuilder for MysqlQueryBuilder {
}
}
ColumnType::Interval(_, _) => "unsupported".into(),
ColumnType::Binary(blob_size) => match blob_size {
BlobSize::Tiny => "tinyblob".into(),
BlobSize::Blob(length) => {
match length {
Some(length) => format!("binary({length})"),
None => "blob".into(),
}
}
BlobSize::Medium => "mediumblob".into(),
BlobSize::Long => "longblob".into(),
ColumnType::Binary(length) => format!("binary({length})"),
ColumnType::VarBinary(length) => match length {
StringLen::N(length) => format!("varbinary({length})"),
StringLen::None => format!("varbinary(255)"),
StringLen::Max => "varbinary(65535)".into(),
},
ColumnType::VarBinary(length) => format!("varbinary({length})"),
ColumnType::Bit(length) => {
match length {
Some(length) => format!("bit({length})"),
Expand Down
9 changes: 6 additions & 3 deletions src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl TableBuilder for PostgresQueryBuilder {
None => "char".into(),
},
ColumnType::String(length) => match length {
Some(length) => format!("varchar({length})"),
None => "varchar".into(),
StringLen::N(length) => format!("varchar({length})"),
_ => "varchar".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger | ColumnType::TinyUnsigned => "smallint".into(),
Expand Down Expand Up @@ -48,7 +48,10 @@ impl TableBuilder for PostgresQueryBuilder {
typ
}
ColumnType::Binary(_) => "bytea".into(),
ColumnType::VarBinary(length) => format!("bit varying({length})"),
ColumnType::VarBinary(length) => match length {
StringLen::N(length) => format!("bit varying({length})"),
_ => "bit varying".into(),
},
ColumnType::Bit(length) => {
match length {
Some(length) => format!("varbit({length})"),
Expand Down
15 changes: 6 additions & 9 deletions src/backend/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl SqliteQueryBuilder {
None => "char".into(),
},
ColumnType::String(length) => match length {
Some(length) => format!("varchar({length})"),
None => "varchar".into(),
StringLen::N(length) => format!("varchar({length})"),
_ => "varchar".into(),
},
ColumnType::Text => "text".into(),
ColumnType::TinyInteger | ColumnType::TinyUnsigned => integer("tinyint").into(),
Expand Down Expand Up @@ -167,14 +167,11 @@ impl SqliteQueryBuilder {
ColumnType::Date => "date_text".into(),
ColumnType::Interval(_, _) =>
unimplemented!("Interval is not available in Sqlite."),
ColumnType::Binary(blob_size) => match blob_size {
BlobSize::Tiny => "tinyblob".into(),
BlobSize::Blob(Some(length)) => format!("blob({length})"),
BlobSize::Blob(None) => "blob".into(),
BlobSize::Medium => "mediumblob".into(),
BlobSize::Long => "longblob".into(),
ColumnType::Binary(length) => format!("blob({length})"),
ColumnType::VarBinary(length) => match length {
StringLen::N(length) => format!("varbinary_blob({length})"),
_ => "varbinary_blob".into(),
},
ColumnType::VarBinary(length) => format!("varbinary_blob({length})"),
ColumnType::Boolean => "boolean".into(),
ColumnType::Money(precision) => match precision {
Some((precision, scale)) => format!("money({precision}, {scale})"),
Expand Down
21 changes: 21 additions & 0 deletions src/extension/mysql/column.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::Iden;

#[derive(Debug, Copy, Clone)]
pub enum MySqlType {
Blob,
TinyBlob,
MediumBlob,
LongBlob,
}

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",
};
write!(s, "{ty}").unwrap();
}
}
2 changes: 2 additions & 0 deletions src/extension/mysql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod column;
mod index;
mod select;

pub use column::*;
pub use index::*;
pub use select::*;
62 changes: 34 additions & 28 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct ColumnDef {
#[derive(Debug, Clone)]
pub enum ColumnType {
Char(Option<u32>),
String(Option<u32>),
String(StringLen),
Text,
TinyInteger,
SmallInteger,
Expand All @@ -34,8 +34,8 @@ pub enum ColumnType {
Date,
Year(Option<MySqlYear>),
Interval(Option<PgInterval>, Option<u32>),
Binary(BlobSize),
VarBinary(u32),
Binary(u32),
VarBinary(StringLen),
Bit(Option<u32>),
VarBit(u32),
Boolean,
Expand All @@ -55,6 +55,21 @@ pub enum ColumnType {
LTree,
}

/// Length for var-char/binary; default to 255
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StringLen {
/// String size
N(u32),
Max,
None,
}

impl Default for StringLen {
fn default() -> Self {
Self::None
}
}

impl PartialEq for ColumnType {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down Expand Up @@ -140,15 +155,6 @@ pub enum MySqlYear {
Four,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum BlobSize {
Tiny,
/// MySQL & SQLite support `binary(length)` column type
Blob(Option<u32>),
Medium,
Long,
}

impl ColumnDef {
/// Construct a table column
pub fn new<T>(name: T) -> Self
Expand Down Expand Up @@ -266,13 +272,19 @@ impl ColumnDef {

/// Set column type as string with custom length
pub fn string_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::String(Some(length)));
self.types = Some(ColumnType::String(StringLen::N(length)));
self
}

/// Set column type as string with maximum length
pub fn string_max(&mut self) -> &mut Self {
self.types = Some(ColumnType::String(StringLen::Max));
self
}

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

Expand Down Expand Up @@ -437,26 +449,20 @@ impl ColumnDef {
}

/// 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))));
self
}

/// Set column type as binary
pub fn binary(&mut self) -> &mut Self {
self.types = Some(ColumnType::Binary(BlobSize::Blob(None)));
pub fn binary(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Binary(length));
self
}

/// Set column type as blob, but when given BlobSize::Blob(size) argument, this column map to binary(size) type instead.
pub fn blob(&mut self, size: BlobSize) -> &mut Self {
self.types = Some(ColumnType::Binary(size));
/// Set column type as binary with variable length
pub fn var_binary(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::VarBinary(StringLen::N(length)));
self
}

/// Set column type as binary with variable length
pub fn var_binary(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::VarBinary(length));
/// Set column type as binary with maximum length
pub fn var_binary_max(&mut self) -> &mut Self {
self.types = Some(ColumnType::VarBinary(StringLen::Max));
self
}

Expand Down
8 changes: 4 additions & 4 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::net::IpAddr;
#[cfg(feature = "with-mac_address")]
use mac_address::MacAddress;

use crate::{BlobSize, ColumnType, CommonSqlQueryBuilder, QueryBuilder};
use crate::{ColumnType, CommonSqlQueryBuilder, QueryBuilder, StringLen};

/// [`Value`] types variant for Postgres array
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -495,12 +495,12 @@ impl ValueType for Cow<'_, str> {
}

fn column_type() -> ColumnType {
ColumnType::String(None)
ColumnType::String(StringLen::Max)
}
}

type_to_box_value!(Vec<u8>, Bytes, Binary(BlobSize::Blob(None)));
type_to_box_value!(String, String, String(None));
type_to_box_value!(Vec<u8>, Bytes, VarBinary(StringLen::Max));
type_to_box_value!(String, String, String(StringLen::Max));

#[cfg(feature = "with-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
Expand Down
1 change: 1 addition & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sea_query::Iden;
///
/// [`Iden`]: crate::types::Iden
#[derive(Debug)]
#[allow(dead_code)]
pub enum BinaryType {
Table,
BinaryLen,
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sea_query::{tests_cfg::*, *};
use sea_query::{extension::mysql::*, tests_cfg::*, *};

mod foreign_key;
mod index;
Expand Down
19 changes: 8 additions & 11 deletions tests/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,17 @@ fn create_6() {
assert_eq!(
Table::create()
.table(BinaryType::Table)
.col(ColumnDef::new(BinaryType::BinaryLen).binary_len(32))
.col(ColumnDef::new(BinaryType::Binary).binary())
.col(ColumnDef::new(BinaryType::BlobSize).blob(BlobSize::Blob(Some(32))))
.col(ColumnDef::new(BinaryType::TinyBlob).blob(BlobSize::Tiny))
.col(ColumnDef::new(BinaryType::Blob).blob(BlobSize::Blob(None)))
.col(ColumnDef::new(BinaryType::MediumBlob).blob(BlobSize::Medium))
.col(ColumnDef::new(BinaryType::LongBlob).blob(BlobSize::Long))
.col(ColumnDef::new(BinaryType::BinaryLen).binary(32))
.col(ColumnDef::new(BinaryType::Binary).custom(MySqlType::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))
.to_string(MysqlQueryBuilder),
[
"CREATE TABLE `binary_type` (",
"`binlen` binary(32),",
"`bin` blob, `defb` binary(32),",
"`bin` blob,",
"`tb` tinyblob,",
"`b` blob,",
"`mb` mediumblob,",
"`lb` longblob",
")",
Expand All @@ -190,8 +187,8 @@ fn create_7() {
assert_eq!(
Table::create()
.table(Char::Table)
.col(ColumnDef::new(Char::Character).binary())
.col(ColumnDef::new(Char::FontSize).binary_len(10))
.col(ColumnDef::new(Char::Character).custom(MySqlType::Blob))
.col(ColumnDef::new(Char::FontSize).binary(10))
.col(ColumnDef::new(Char::SizeW).var_binary(10))
.to_string(MysqlQueryBuilder),
[
Expand Down
20 changes: 3 additions & 17 deletions tests/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,11 @@ fn create_12() {
assert_eq!(
Table::create()
.table(BinaryType::Table)
.col(ColumnDef::new(BinaryType::BinaryLen).binary_len(32))
.col(ColumnDef::new(BinaryType::Binary).binary())
.col(ColumnDef::new(BinaryType::BlobSize).blob(BlobSize::Blob(Some(32))))
.col(ColumnDef::new(BinaryType::TinyBlob).blob(BlobSize::Tiny))
.col(ColumnDef::new(BinaryType::Blob).blob(BlobSize::Blob(None)))
.col(ColumnDef::new(BinaryType::MediumBlob).blob(BlobSize::Medium))
.col(ColumnDef::new(BinaryType::LongBlob).blob(BlobSize::Long))
.col(ColumnDef::new(BinaryType::BinaryLen).binary(32))
.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE "binary_type" ("#,
r#""binlen" bytea,"#,
r#""bin" bytea,"#,
r#""defb" bytea,"#,
r#""tb" bytea,"#,
r#""b" bytea,"#,
r#""mb" bytea,"#,
r#""lb" bytea"#,
r#""binlen" bytea"#,
r#")"#,
]
.join(" ")
Expand All @@ -286,13 +274,11 @@ fn create_13() {
assert_eq!(
Table::create()
.table(Char::Table)
.col(ColumnDef::new(Char::Character).binary())
.col(ColumnDef::new(Char::FontSize).binary_len(10))
.col(ColumnDef::new(Char::FontSize).binary(10))
.col(ColumnDef::new(Char::SizeW).var_binary(10))
.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE "character" ("#,
r#""character" bytea,"#,
r#""font_size" bytea,"#,
r#""size_w" bit varying(10)"#,
r#")"#,
Expand Down
20 changes: 3 additions & 17 deletions tests/sqlite/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,11 @@ fn create_4() {
assert_eq!(
Table::create()
.table(BinaryType::Table)
.col(ColumnDef::new(BinaryType::BinaryLen).binary_len(32))
.col(ColumnDef::new(BinaryType::Binary).binary())
.col(ColumnDef::new(BinaryType::BlobSize).blob(BlobSize::Blob(Some(32))))
.col(ColumnDef::new(BinaryType::TinyBlob).blob(BlobSize::Tiny))
.col(ColumnDef::new(BinaryType::Blob).blob(BlobSize::Blob(None)))
.col(ColumnDef::new(BinaryType::MediumBlob).blob(BlobSize::Medium))
.col(ColumnDef::new(BinaryType::LongBlob).blob(BlobSize::Long))
.col(ColumnDef::new(BinaryType::BinaryLen).binary(32))
.to_string(SqliteQueryBuilder),
[
r#"CREATE TABLE "binary_type" ("#,
r#""binlen" blob(32),"#,
r#""bin" blob,"#,
r#""defb" blob(32),"#,
r#""tb" tinyblob,"#,
r#""b" blob,"#,
r#""mb" mediumblob,"#,
r#""lb" longblob"#,
r#""binlen" blob(32)"#,
r#")"#,
]
.join(" ")
Expand All @@ -133,13 +121,11 @@ fn create_5() {
assert_eq!(
Table::create()
.table(Char::Table)
.col(ColumnDef::new(Char::Character).binary())
.col(ColumnDef::new(Char::FontSize).binary_len(10))
.col(ColumnDef::new(Char::FontSize).binary(10))
.col(ColumnDef::new(Char::SizeW).var_binary(10))
.to_string(SqliteQueryBuilder),
[
r#"CREATE TABLE "character" ("#,
r#""character" blob,"#,
r#""font_size" blob(10),"#,
r#""size_w" varbinary_blob(10)"#,
r#")"#,
Expand Down

0 comments on commit 3e97249

Please sign in to comment.