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

issues-336 Array support #413

Draft
wants to merge 2 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
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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 }
chrono = { version = "^0.4", default-features = false, features = ["clock"], optional = true }
postgres-types = { version = "^0", optional = true }
rust_decimal = { version = "^1", optional = true }
bigdecimal = { version = "^0.3", optional = true }
uuid = { version = "^1", optional = true }
Expand All @@ -40,6 +39,7 @@ quote = { version = "^1", optional = true }
time = { version = "^0.3", optional = true, features = ["macros", "formatting"] }
ipnetwork = { version = "^0.19", optional = true }
mac_address = { version = "^1.1", optional = true }
bytes = { version = "^1", optional = true }

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
Expand All @@ -52,7 +52,6 @@ backend-sqlite = []
default = ["derive", "backend-mysql", "backend-postgres", "backend-sqlite"]
derive = ["sea-query-derive"]
attr = ["sea-query-attr"]
postgres-array = []
postgres-interval = ["proc-macro2", "quote"]
thread-safe = []
with-chrono = ["chrono"]
Expand Down
1 change: 0 additions & 1 deletion examples/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ sea-query-postgres = { path = "../../sea-query-postgres", features = [
"with-chrono",
"with-json",
"with-time",
"postgres-array",
"with-rust_decimal"
] }
# NOTE: if you are copying this example into your own project, use the following line instead:
Expand Down
1 change: 0 additions & 1 deletion sea-query-binder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ with-uuid = ["sqlx/uuid", "sea-query/with-uuid"]
with-time = ["sqlx/time", "sea-query/with-time"]
with-ipnetwork = ["sqlx/ipnetwork", "sea-query/with-ipnetwork"]
with-mac_address = ["sqlx/mac_address", "sea-query/with-mac_address"]
postgres-array = ["sea-query/postgres-array"]
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"]
Expand Down
3 changes: 1 addition & 2 deletions sea-query-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rust-version = "1.60"
[lib]

[dependencies]
sea-query = { version = "^0", path = ".." }
sea-query = { version = "^0", path = "..", features = ["thread-safe"] }
postgres-types = { version = "^0.2" }
bytes = { version = "^1" }
rust_decimal = { version = "^1", optional = true }
Expand All @@ -29,6 +29,5 @@ with-rust_decimal = ["sea-query/with-rust_decimal", "rust_decimal/db-postgres"]
with-bigdecimal = ["sea-query/with-bigdecimal"]
with-uuid = ["postgres-types/with-uuid-1", "sea-query/with-uuid"]
with-time = ["postgres-types/with-time-0_3", "sea-query/with-time"]
postgres-array = ["postgres-types/array-impls", "sea-query/postgres-array"]
with-ipnetwork = ["sea-query/with-ipnetwork"]
with-mac_address = ["sea-query/with-mac_address"]
117 changes: 58 additions & 59 deletions sea-query-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::error::Error;
use bytes::BytesMut;
use postgres_types::{to_sql_checked, IsNull, ToSql, Type};

use sea_query::{query::*, QueryBuilder, Value};
use sea_query::BinOper::Is;
use sea_query::{query::*, QueryBuilder, Value, ValueType};

#[derive(Clone, Debug, PartialEq)]
pub struct PostgresValue(pub sea_query::Value);
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
pub struct PostgresValue(pub Value);
#[derive(Clone, Debug)]
pub struct PostgresValues(pub Vec<PostgresValue>);

impl<'a> PostgresValues {
Expand Down Expand Up @@ -54,62 +55,60 @@ impl ToSql for PostgresValue {
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
macro_rules! to_sql {
( $v: expr, $ty: ty ) => {
$v.map(|v| v as $ty).as_ref().to_sql(ty, out)
};
if self.0.is_null() {
return Ok(IsNull::Yes);
}
match &self.0 {
Value::Bool(v) => to_sql!(v, bool),
Value::TinyInt(v) => to_sql!(v, i8),
Value::SmallInt(v) => to_sql!(v, i16),
Value::Int(v) => to_sql!(v, i32),
Value::BigInt(v) => to_sql!(v, i64),
Value::TinyUnsigned(v) => to_sql!(v, u32),
Value::SmallUnsigned(v) => to_sql!(v, u32),
Value::Unsigned(v) => to_sql!(v, u32),
Value::BigUnsigned(v) => to_sql!(v, i64),
Value::Float(v) => to_sql!(v, f32),
Value::Double(v) => to_sql!(v, f64),
Value::String(v) => v.as_deref().to_sql(ty, out),
Value::Char(v) => v.map(|v| v.to_string()).to_sql(ty, out),
Value::Bytes(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-json")]
Value::Json(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-chrono")]
Value::ChronoDate(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-chrono")]
Value::ChronoTime(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTime(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeUtc(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeLocal(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeWithTimeZone(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-time")]
Value::TimeDate(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-time")]
Value::TimeTime(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-time")]
Value::TimeDateTime(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-time")]
Value::TimeDateTimeWithTimeZone(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "with-uuid")]
Value::Uuid(v) => v.as_deref().to_sql(ty, out),
#[cfg(feature = "postgres-array")]
Value::Array(Some(v)) => v
.iter()
.map(|v| PostgresValue(v.clone()))
.collect::<Vec<PostgresValue>>()
.to_sql(ty, out),
#[cfg(feature = "postgres-array")]
Value::Array(None) => Ok(IsNull::Yes),
#[allow(unreachable_patterns)]
_ => unimplemented!(),

match &self.0.ty() {
ValueType::Bool => {
let v = self.0.value::<bool>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::TinyInt => {
let v = self.0.value::<i8>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::SmallInt => {
let v = self.0.value::<i16>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::Int => {
let v = self.0.value::<i32>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::BigInt => {
let v = self.0.value::<i64>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::TinyUnsigned => {
let v = self.0.value::<u8>().unwrap();
(*v as i16).to_sql(ty, out)
}
ValueType::SmallUnsigned => {
let v = self.0.value::<u16>().unwrap();
(*v as i32).to_sql(ty, out)
}
ValueType::Unsigned => {
let v = self.0.value::<u32>().unwrap();
(*v as i64).to_sql(ty, out)
}
ValueType::BigUnsigned => {
let v = self.0.value::<u64>().unwrap();
(*v as i64).to_sql(ty, out)
}
ValueType::Float => {
let v = self.0.value::<f32>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::Double => {
let v = self.0.value::<f64>().unwrap();
(*v).to_sql(ty, out)
}
ValueType::Char => {
let v = self.0.value::<f64>().unwrap();
v.to_string().to_sql(ty, out)
}
_ => panic!(),
}
}

Expand Down
4 changes: 1 addition & 3 deletions sea-query-rusqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rust-version = "1.60"

[dependencies]
sea-query = { version = "^0", path = ".." }
rusqlite = { package = "rusqlite", version = "^0.28", features = ["bundled"] }
rusqlite = { version = "^0.28", features = ["bundled"] }

[features]
with-chrono = ["rusqlite/chrono", "sea-query/with-chrono"]
Expand All @@ -29,5 +29,3 @@ with-uuid = ["rusqlite/uuid", "sea-query/with-uuid"]
with-time = ["rusqlite/time", "sea-query/with-time"]
with-ipnetwork = ["sea-query/with-ipnetwork"]
with-mac_address = ["sea-query/with-mac_address"]
postgres-array = ["sea-query/postgres-array"]

81 changes: 1 addition & 80 deletions sea-query-rusqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,85 +50,6 @@ impl_rusqlite_binder!(DeleteStatement);

impl ToSql for RusqliteValue {
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
macro_rules! box_to_sql {
( $v: expr ) => {
match $v {
Some(v) => v.as_ref().to_sql(),
None => Null.to_sql(),
}
};
}

macro_rules! opt_string_to_sql {
( $v: expr ) => {
match $v {
Some(v) => Ok(ToSqlOutput::from(v)),
None => Null.to_sql(),
}
};
}

match &self.0 {
Value::Bool(v) => v.to_sql(),
Value::TinyInt(v) => v.to_sql(),
Value::SmallInt(v) => v.to_sql(),
Value::Int(v) => v.to_sql(),
Value::BigInt(v) => v.to_sql(),
Value::TinyUnsigned(v) => v.to_sql(),
Value::SmallUnsigned(v) => v.to_sql(),
Value::Unsigned(v) => v.to_sql(),
Value::BigUnsigned(v) => v.to_sql(),
Value::Float(v) => v.to_sql(),
Value::Double(v) => v.to_sql(),
Value::String(v) => box_to_sql!(v),
Value::Char(v) => opt_string_to_sql!(v.map(|v| v.to_string())),
Value::Bytes(v) => box_to_sql!(v),
#[cfg(feature = "with-chrono")]
Value::ChronoDate(v) => box_to_sql!(v),
#[cfg(feature = "with-chrono")]
Value::ChronoTime(v) => box_to_sql!(v),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTime(v) => box_to_sql!(v),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeUtc(v) => box_to_sql!(v),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeLocal(v) => box_to_sql!(v),
#[cfg(feature = "with-chrono")]
Value::ChronoDateTimeWithTimeZone(v) => box_to_sql!(v),
#[cfg(feature = "with-time")]
v @ Value::TimeDate(_) => opt_string_to_sql!(v.time_as_naive_utc_in_string()),
#[cfg(feature = "with-time")]
v @ Value::TimeTime(_) => opt_string_to_sql!(v.time_as_naive_utc_in_string()),
#[cfg(feature = "with-time")]
v @ Value::TimeDateTime(_) => opt_string_to_sql!(v.time_as_naive_utc_in_string()),
#[cfg(feature = "with-time")]
v @ Value::TimeDateTimeWithTimeZone(_) => {
opt_string_to_sql!(v.time_as_naive_utc_in_string())
}
#[cfg(feature = "with-uuid")]
Value::Uuid(v) => box_to_sql!(v),
#[cfg(feature = "with-json")]
Value::Json(j) => box_to_sql!(j),
#[cfg(feature = "with-rust_decimal")]
Value::Decimal(_) => {
panic!("Rusqlite doesn't support rust_decimal arguments");
}
#[cfg(feature = "with-bigdecimal")]
Value::BigDecimal(_) => {
panic!("Rusqlite doesn't support bigdecimal arguments");
}
#[cfg(feature = "with-ipnetwork")]
Value::IpNetwork(_) => {
panic!("Rusqlite doesn't support IpNetwork arguments");
}
#[cfg(feature = "with-mac_address")]
Value::MacAddress(_) => {
panic!("Rusqlite doesn't support MacAddress arguments");
}
#[cfg(feature = "postgres-array")]
Value::Array(_) => {
panic!("Rusqlite doesn't support Array arguments");
}
}
self.0.to_sql()
}
}
Loading