-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mysql support for diesel integration
- Loading branch information
Showing
3 changed files
with
83 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |