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

ValueTrait for SQLx #477

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ name = "sea_query"
path = "src/lib.rs"

[dependencies]
sea-query-sqlx = { version = "^0.1", path = "sea-query-sqlx", optional = true }
sea-query-attr = { version = "^0.1.1", path = "sea-query-attr", optional = true }
sea-query-derive = { version = "^0.2.0", path = "sea-query-derive", optional = true }
serde_json = { version = "^1", optional = true }
Expand Down
9 changes: 5 additions & 4 deletions sea-query-binder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
# A separate workspace
members = ["."]

[package]
name = "sea-query-binder"
Expand All @@ -18,6 +18,7 @@ rust-version = "1.60"

[dependencies]
sea-query = { version = "^0", path = "..", features = ["thread-safe"] }
sea-query-sqlx = { version = "^0.1", path = "../sea-query-sqlx", optional = true }
sqlx = { version = "^0.6.1", optional = true }
serde_json = { version = "^1", optional = true }
chrono = { version = "^0.4", default-features = false, features = ["clock"], optional = true }
Expand All @@ -32,9 +33,9 @@ ipnetwork = { version = "^0.19", optional = true }
mac_address = { version = "^1.1", optional = true }

[features]
sqlx-mysql = ["sqlx/mysql"]
sqlx-postgres = ["sqlx/postgres"]
sqlx-sqlite = ["sqlx/sqlite"]
sqlx-mysql = ["sqlx/mysql", "sea-query-sqlx", "sea-query/sea-query-sqlx"]
sqlx-postgres = ["sqlx/postgres", "sea-query-sqlx", "sea-query/sea-query-sqlx"]
sqlx-sqlite = ["sqlx/sqlite", "sea-query-sqlx", "sea-query/sea-query-sqlx"]
sqlx-any = ["sqlx/any"]
with-chrono = ["sqlx/chrono", "sea-query/with-chrono", "chrono"]
with-json = ["sqlx/json", "sea-query/with-json", "serde_json"]
Expand Down
3 changes: 3 additions & 0 deletions sea-query-binder/src/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::mysql::MySql> for SqlxValues {
Value::MacAddress(_) => {
panic!("Mysql doesn't support MacAddress arguments");
}
Value::CustomSqlx(value) => {
value.0.add_mysql(&mut args);
}
}
}
args
Expand Down
3 changes: 3 additions & 0 deletions sea-query-binder/src/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::postgres::Postgres> for SqlxValues {
Value::MacAddress(mac) => {
args.add(mac.as_deref());
}
Value::CustomSqlx(value) => {
value.0.add_postgres(&mut args);
}
#[cfg(feature = "postgres-array")]
Value::Array(ty, v) => match ty {
ArrayType::Bool => {
Expand Down
3 changes: 3 additions & 0 deletions sea-query-binder/src/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ impl<'q> sqlx::IntoArguments<'q, sqlx::sqlite::Sqlite> for SqlxValues {
Value::Array(_, _) => {
panic!("Sqlite doesn't support array arguments");
}
Value::CustomSqlx(value) => {
value.0.add_sqlite(&mut args);
}
}
}
args
Expand Down
28 changes: 28 additions & 0 deletions sea-query-sqlx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "sea-query-sqlx"
version = "0.1.0"
authors = [ "Chris Tsang <[email protected]>" ]
edition = "2021"
description = "SQLx traits for working with custom types"
license = "MIT OR Apache-2.0"
documentation = "https://docs.rs/sea-query"
repository = "https://github.com/SeaQL/sea-query"
categories = [ "database" ]
keywords = [ "database", "sql", "mysql", "postgres", "sqlite" ]
rust-version = "1.60"

[lib]

[dependencies]
sqlx = { version = "^0.6" }

[features]
sqlx-mysql = ["sqlx/mysql"]
sqlx-postgres = ["sqlx/postgres"]
sqlx-sqlite = ["sqlx/sqlite"]
runtime-async-std-native-tls = ["sqlx/runtime-async-std-native-tls"]
runtime-async-std-rustls = ["sqlx/runtime-async-std-rustls", ]
runtime-actix-native-tls = ["sqlx/runtime-actix-native-tls"]
runtime-actix-rustls = ["sqlx/runtime-actix-rustls"]
runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls"]
runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls"]
12 changes: 12 additions & 0 deletions sea-query-sqlx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::fmt::Debug;

pub trait SqlxValueTrait: Debug + Send + Sync {
#[cfg(feature = "sqlx-mysql")]
fn add_mysql(&self, args: &mut sqlx::mysql::MySqlArguments) -> bool;
#[cfg(feature = "sqlx-postgres")]
fn add_postgres(&self, args: &mut sqlx::postgres::PgArguments) -> bool;
#[cfg(feature = "sqlx-sqlite")]
fn add_sqlite(&self, args: &mut sqlx::sqlite::SqliteArguments) -> bool;

fn format_sql(&self, s: &mut dyn std::fmt::Write) -> std::fmt::Result;
}
2 changes: 2 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,8 @@ pub trait QueryBuilder: QuotedBuilder + EscapeBuilder + TableRefBuilder {
Value::IpNetwork(Some(v)) => write!(s, "'{}'", v).unwrap(),
#[cfg(feature = "with-mac_address")]
Value::MacAddress(Some(v)) => write!(s, "'{}'", v).unwrap(),
#[cfg(feature = "sea-query-sqlx")]
Value::CustomSqlx(v) => v.0.format_sql(&mut s).unwrap(),
};
s
}
Expand Down
18 changes: 18 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use std::net::IpAddr;
#[cfg(feature = "with-mac_address")]
use mac_address::MacAddress;

#[cfg(feature = "sea-query-sqlx")]
use sea_query_sqlx::SqlxValueTrait;

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

/// [`Value`] types variant for Postgres array
Expand Down Expand Up @@ -203,6 +206,9 @@ pub enum Value {
#[cfg(feature = "with-mac_address")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-mac_address")))]
MacAddress(Option<Box<MacAddress>>),

#[cfg(feature = "sea-query-sqlx")]
CustomSqlx(CustomSqlxValue),
}

impl std::fmt::Display for Value {
Expand Down Expand Up @@ -1394,6 +1400,18 @@ impl IntoIterator for Values {
}
}

#[cfg(feature = "sea-query-sqlx")]
#[derive(Debug, Clone)]
pub struct CustomSqlxValue(pub crate::SeaRc<dyn SqlxValueTrait>);

#[cfg(feature = "sea-query-sqlx")]
impl PartialEq for CustomSqlxValue {
fn eq(&self, other: &Self) -> bool {
#[allow(clippy::vtable_address_comparisons)]
crate::SeaRc::ptr_eq(&self.0, &other.0)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down