Skip to content

Commit

Permalink
feat: add mysql support for diesel integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Jul 14, 2023
1 parent 0b24022 commit 32ab118
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
15 changes: 8 additions & 7 deletions sea-query-diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ bigdecimal = { version = "0.3", default-features = false, optional = true }
rust_decimal = { version = "1", default-features = false, optional = true }

[features]
default = ["sqlite", "extras"]
default = ["sqlite", "mysql", "extras"]
extras = [
"with-chrono",
"with-json",
"with-rust_decimal",
"with-rust_decimal-mysql",
"with-bigdecimal",
"with-uuid",
"with-time",
Expand All @@ -40,12 +40,13 @@ mysql = ["diesel/mysql", "sea-query/backend-mysql"]
sqlite = ["diesel/sqlite", "sea-query/backend-sqlite"]
with-chrono = ["diesel/chrono", "sea-query/with-chrono"]
with-json = ["diesel/serde_json", "sea-query/with-json"]
with-rust_decimal = ["sea-query/with-rust_decimal", "rust_decimal/diesel2"]
with-bigdecimal = [
"diesel/bigdecimal",
"sea-query/with-bigdecimal",
"bigdecimal",
with-rust_decimal = ["sea-query/with-rust_decimal"]
with-rust_decimal-mysql = ["with-rust_decimal", "rust_decimal/db-diesel2-mysql"]
with-rust_decimal-postgres = [
"with-rust_decimal",
"rust_decimal/db-diesel2-postgres",
]
with-bigdecimal = ["diesel/numeric", "sea-query/with-bigdecimal", "bigdecimal"]
with-uuid = ["diesel/uuid", "sea-query/with-uuid"]
with-time = ["diesel/time", "sea-query/with-time"]
with-ipnetwork = ["diesel/network-address", "sea-query/with-ipnetwork"]
Expand Down
2 changes: 1 addition & 1 deletion sea-query-diesel/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod macros {
}

macro_rules! build {
($type: ident, $value: expr) => {
($type: ty, $value: expr) => {
$crate::value::SeaValue::<::diesel::sql_types::Nullable<$type>, _>::build($value)
};
}
Expand Down
74 changes: 74 additions & 0 deletions sea-query-diesel/src/backend/mysql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use diesel::mysql::sql_types::*;
use diesel::mysql::Mysql;
use diesel::query_builder::QueryFragment;
use diesel::result::QueryResult;
use diesel::sql_types::*;
use sea_query::{MysqlQueryBuilder, Value};

use super::macros::{bail, build};
use super::{ExtractBuilder, TransformValue};

impl ExtractBuilder for Mysql {
type Builder = MysqlQueryBuilder;

fn builder() -> Self::Builder {
MysqlQueryBuilder
}
}

impl TransformValue for Mysql {
fn transform_value(value: Value) -> QueryResult<Box<dyn QueryFragment<Self> + Send>> {
let transformed = match value {
Value::Bool(v) => build!(Bool, v),
Value::TinyInt(v) => build!(TinyInt, v),
Value::SmallInt(v) => build!(SmallInt, v),
Value::Int(v) => build!(Integer, v),
Value::BigInt(v) => build!(BigInt, v),
Value::TinyUnsigned(v) => build!(Unsigned<TinyInt>, v),
Value::SmallUnsigned(v) => build!(Unsigned<SmallInt>, v),
Value::Unsigned(v) => build!(Unsigned<Integer>, v),
Value::BigUnsigned(v) => build!(Unsigned<BigInt>, v),
Value::Float(v) => build!(Float, v),
Value::Double(v) => build!(Double, v),
Value::String(v) => build!(Text, v.map(|v| *v)),
Value::Char(v) => build!(Text, v.map(|v| v.to_string())),
Value::Bytes(v) => build!(Blob, v.map(|v| *v)),
#[cfg(feature = "with-chrono")]
Value::ChronoDate(v) => build!(Date, v.map(|v| *v)),
#[cfg(feature = "with-chrono")]
Value::ChronoTime(v) => build!(Time, v.map(|v| *v)),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTime(v) => build!(Timestamp, v.map(|v| *v)),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeUtc(v) => build!(Timestamp, v.map(|v| v.naive_utc())),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeLocal(v) => build!(Timestamp, v.map(|v| v.naive_utc())),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeWithTimeZone(v) => build!(Timestamp, v.map(|v| v.naive_utc())),
#[cfg(feature = "with-time")]
Value::TimeDate(v) => build!(Date, v.map(|v| *v)),
#[cfg(feature = "with-time")]
Value::TimeTime(v) => build!(Time, v.map(|v| *v)),
#[cfg(feature = "with-time")]
Value::TimeDateTime(v) => build!(Timestamp, v.map(|v| *v)),
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(v) => build!(Timestamp, v.map(|v| *v)),
#[cfg(feature = "with-uuid")]
// UUID are generally stored as VARCHAR(36)
Value::Uuid(v) => build!(Text, v.map(|v| v.to_string())),
#[cfg(feature = "with-rust_decimal-mysql")]
Value::Decimal(v) => build!(Numeric, v.map(|v| *v)),
#[cfg(feature = "with-bigdecimal")]
Value::BigDecimal(v) => build!(Numeric, v.map(|v| *v)),
#[cfg(feature = "with-json")]
Value::Json(v) => build!(Json, v.map(|v| *v)),
#[cfg(feature = "with-ipnetwork")]
Value::IpNetwork(_) => bail!("Mysql doesn't support IpNetwork arguments"),
#[cfg(feature = "with-mac_address")]
Value::MacAddress(_) => bail!("Mysql doesn't support MacAddress arguments"),
#[cfg(feature = "postgres-array")]
Value::Array(_, _) => bail!("Mysql doesn't support array arguments"),
};
Ok(transformed)
}
}

0 comments on commit 32ab118

Please sign in to comment.