Skip to content

Commit

Permalink
issues-336 Init new Value type
Browse files Browse the repository at this point in the history
  • Loading branch information
ikrivosheev committed Oct 8, 2022
1 parent 7c418e8 commit e98f4eb
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 1,951 deletions.
7 changes: 5 additions & 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,9 @@ 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 }
postgres-types = { version = "^0", optional = true }
rusqlite = { version = "^0.28", features = ["bundled"], optional = true }

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
Expand All @@ -52,7 +54,8 @@ backend-sqlite = []
default = ["derive", "backend-mysql", "backend-postgres", "backend-sqlite"]
derive = ["sea-query-derive"]
attr = ["sea-query-attr"]
postgres-array = []
with-postgres = ["postgres-types", "bytes"]
with-rusqlite = ["rusqlite"]
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
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", "with-postgres"] }
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"]
66 changes: 5 additions & 61 deletions sea-query-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ 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::{query::*, QueryBuilder, Value, ValueTrait};

#[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,63 +54,7 @@ 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)
};
}
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!(),
}
self.0.to_sql(ty, out)
}

fn accepts(_ty: &Type) -> bool {
Expand Down
5 changes: 2 additions & 3 deletions sea-query-rusqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ rust-version = "1.60"
[lib]

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

[features]
with-chrono = ["rusqlite/chrono", "sea-query/with-chrono"]
Expand All @@ -29,5 +29,4 @@ 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

0 comments on commit e98f4eb

Please sign in to comment.