diff --git a/README.md b/README.md index 863d9884a..4626a80a6 100644 --- a/README.md +++ b/README.md @@ -106,10 +106,10 @@ assert_eq!( r#"SELECT "image" FROM "glyph" WHERE "image" LIKE $1 AND "id" IN ($2, $3, $4)"# .to_owned(), Values(vec![ - Value::String(Some(Box::new("A".to_owned()))), - Value::Int(Some(1)), - Value::Int(Some(2)), - Value::Int(Some(3)) + Value::string("A".to_owned()), + Value::int(1), + Value::int(2), + Value::int(3) ]) ) ); @@ -505,7 +505,7 @@ let table = Table::create() .col(ColumnDef::new(Char::Character).string().not_null()) .col(ColumnDef::new(Char::SizeW).integer().not_null()) .col(ColumnDef::new(Char::SizeH).integer().not_null()) - .col(ColumnDef::new(Char::FontId).integer().default(Value::Int(None))) + .col(ColumnDef::new(Char::FontId).integer().default(Value::int(None))) .foreign_key( ForeignKey::create() .name("FK_2e303c3a712662f1fc2a4d0aad6") diff --git a/src/lib.rs b/src/lib.rs index a8f29de34..bc5b1c7ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,10 +111,10 @@ //! r#"SELECT "image" FROM "glyph" WHERE "image" LIKE $1 AND "id" IN ($2, $3, $4)"# //! .to_owned(), //! Values(vec![ -//! Value::String(Some("A".to_owned())), -//! Value::Int(Some(1)), -//! Value::Int(Some(2)), -//! Value::Int(Some(3)) +//! Value::string("A".to_owned()), +//! Value::int(1), +//! Value::int(2), +//! Value::int(3) //! ]) //! ) //! ); @@ -534,7 +534,7 @@ //! .col(ColumnDef::new(Char::Character).string().not_null()) //! .col(ColumnDef::new(Char::SizeW).integer().not_null()) //! .col(ColumnDef::new(Char::SizeH).integer().not_null()) -//! .col(ColumnDef::new(Char::FontId).integer().default(Value::Int(None))) +//! .col(ColumnDef::new(Char::FontId).integer().default(Value::int(None))) //! .foreign_key( //! ForeignKey::create() //! .name("FK_2e303c3a712662f1fc2a4d0aad6") diff --git a/src/query/traits.rs b/src/query/traits.rs index 3e854f09c..521169114 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -77,7 +77,7 @@ pub trait QueryStatementWriter: QueryStatementBuilder { /// ); /// assert_eq!( /// params, - /// Values(vec![Value::Int(Some(0)), Value::Int(Some(2))]) + /// Values(vec![Value::int(0), Value::int(2)]) /// ); /// ``` fn build(&self, query_builder: T) -> (String, Values) { @@ -118,7 +118,7 @@ pub trait QueryStatementWriter: QueryStatementBuilder { /// let (sql, values) = sql.into_parts(); /// assert_eq!( /// values, - /// Values(vec![Value::Int(Some(0)), Value::Int(Some(2))]) + /// Values(vec![Value::int(0), Value::int(2)]) /// ); /// ``` fn build_collect(&self, query_builder: T, sql: &mut dyn SqlWriter) -> String { diff --git a/src/query/update.rs b/src/query/update.rs index b22011923..604e90abc 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -212,7 +212,7 @@ impl UpdateStatement { /// /// let query = Query::update() /// .table(Glyph::Table) - /// .value(Glyph::Aspect, Expr::value(Value::Int(None))) + /// .value(Glyph::Aspect, Expr::value(Value::int(None))) /// .to_owned(); /// /// assert_eq!( diff --git a/src/table/create.rs b/src/table/create.rs index 6bd50738e..a8e6f12e7 100644 --- a/src/table/create.rs +++ b/src/table/create.rs @@ -21,7 +21,7 @@ use crate::{ /// .col(ColumnDef::new(Char::Character).string().not_null()) /// .col(ColumnDef::new(Char::SizeW).integer().not_null()) /// .col(ColumnDef::new(Char::SizeH).integer().not_null()) -/// .col(ColumnDef::new(Char::FontId).integer().default(Value::Int(None))) +/// .col(ColumnDef::new(Char::FontId).integer().default(Value::int(None))) /// .foreign_key( /// ForeignKey::create() /// .name("FK_2e303c3a712662f1fc2a4d0aad6") diff --git a/src/value.rs b/src/value.rs index 4ebc7e052..9646c928b 100644 --- a/src/value.rs +++ b/src/value.rs @@ -332,6 +332,79 @@ const fn check_value_size() -> usize { std::mem::size_of::() } +// Constructors +impl Value { + #[inline] + pub fn bool>>(value: T) -> Self { + Self::Bool(value.into()) + } + + #[inline] + pub fn tiny_int>>(value: T) -> Self { + Self::TinyInt(value.into()) + } + + #[inline] + pub fn small_int>>(value: T) -> Self { + Self::SmallInt(value.into()) + } + + #[inline] + pub fn int>>(value: T) -> Self { + Self::Int(value.into()) + } + + #[inline] + pub fn big_int>>(value: T) -> Self { + Self::BigInt(value.into()) + } + + #[inline] + pub fn tiny_unsigned>>(value: T) -> Self { + Self::TinyUnsigned(value.into()) + } + + #[inline] + pub fn small_unsigned>>(value: T) -> Self { + Self::SmallUnsigned(value.into()) + } + + #[inline] + pub fn unsigned>>(value: T) -> Self { + Self::Unsigned(value.into()) + } + + #[inline] + pub fn big_unsigned>>(value: T) -> Self { + Self::BigUnsigned(value.into()) + } + + #[inline] + pub fn float>>(value: T) -> Self { + Self::Float(value.into()) + } + + #[inline] + pub fn double>>(value: T) -> Self { + Self::Double(value.into()) + } + + #[inline] + pub fn string>>(value: T) -> Self { + Self::String(value.into()) + } + + #[inline] + pub fn char>>(value: T) -> Self { + Self::Char(value.into()) + } + + #[inline] + pub fn bytes>>>(value: T) -> Self { + Self::Bytes(value.into()) + } +} + impl Value { pub fn unwrap(self) -> T where @@ -352,13 +425,13 @@ impl Value { /// ``` /// use sea_query::Value; /// - /// let v = Value::Int(Some(2)); + /// let v = Value::int(2); /// let n = v.as_null(); /// - /// assert_eq!(n, Value::Int(None)); + /// assert_eq!(n, Value::int(None)); /// /// // one liner: - /// assert_eq!(Into::::into(2.2).as_null(), Value::Double(None)); + /// assert_eq!(Into::::into(2.2).as_null(), Value::double(None)); /// ``` pub fn as_null(&self) -> Self { match self { @@ -476,9 +549,9 @@ impl Value { /// ``` /// use sea_query::Value; /// - /// let v = Value::Int(None); + /// let v = Value::int(None); /// let n = v.dummy_value(); - /// assert_eq!(n, Value::Int(Some(0))); + /// assert_eq!(n, Value::int(0)); /// ``` pub fn dummy_value(&self) -> Self { match self { @@ -602,19 +675,19 @@ impl Value { impl From<&[u8]> for Value { fn from(x: &[u8]) -> Value { - Value::Bytes(Some(x.into())) + Value::bytes(x.to_vec()) } } impl From<&str> for Value { fn from(x: &str) -> Value { - Value::String(Some(x.to_owned())) + Value::string(x.to_owned()) } } impl From<&String> for Value { fn from(x: &String) -> Value { - Value::String(Some(x.clone())) + Value::string(x.clone()) } } diff --git a/src/value/hashable_value.rs b/src/value/hashable_value.rs index e51a9234d..107b2005f 100644 --- a/src/value/hashable_value.rs +++ b/src/value/hashable_value.rs @@ -258,29 +258,29 @@ mod tests { #[test] fn test_hash_value_0() { let hash_set: std::collections::HashSet = [ - Value::Int(None), - Value::Int(None), - Value::BigInt(None), - Value::BigInt(None), - Value::Float(None), - Value::Float(None), // Null is not NaN - Value::Float(Some(f32::NAN)), // NaN considered equal - Value::Float(Some(f32::NAN)), - Value::Double(None), - Value::Double(None), - Value::Double(Some(f64::NAN)), - Value::Double(Some(f64::NAN)), + Value::int(None), + Value::int(None), + Value::big_int(None), + Value::big_int(None), + Value::float(None), + Value::float(None), // Null is not NaN + Value::float(f32::NAN), // NaN considered equal + Value::float(f32::NAN), + Value::double(None), + Value::double(None), + Value::double(f64::NAN), + Value::double(f64::NAN), ] .into_iter() .collect(); let unique: std::collections::HashSet = [ - Value::Int(None), - Value::BigInt(None), - Value::Float(None), - Value::Double(None), - Value::Float(Some(f32::NAN)), - Value::Double(Some(f64::NAN)), + Value::int(None), + Value::big_int(None), + Value::float(None), + Value::double(None), + Value::float(f32::NAN), + Value::double(f64::NAN), ] .into_iter() .collect(); @@ -291,27 +291,27 @@ mod tests { #[test] fn test_hash_value_1() { let hash_set: std::collections::HashSet = [ - Value::Int(None), - Value::Int(Some(1)), - Value::Int(Some(1)), - Value::BigInt(Some(2)), - Value::BigInt(Some(2)), - Value::Float(Some(3.0)), - Value::Float(Some(3.0)), - Value::Double(Some(3.0)), - Value::Double(Some(3.0)), - Value::BigInt(Some(5)), + Value::int(None), + Value::int(1), + Value::int(1), + Value::big_int(2), + Value::big_int(2), + Value::float(3.0), + Value::float(3.0), + Value::double(3.0), + Value::double(3.0), + Value::big_int(5), ] .into_iter() .collect(); let unique: std::collections::HashSet = [ - Value::BigInt(Some(5)), - Value::Double(Some(3.0)), - Value::Float(Some(3.0)), - Value::BigInt(Some(2)), - Value::Int(Some(1)), - Value::Int(None), + Value::big_int(5), + Value::double(3.0), + Value::float(3.0), + Value::big_int(2), + Value::int(1), + Value::int(None), ] .into_iter() .collect(); @@ -322,30 +322,14 @@ mod tests { #[cfg(feature = "postgres-array")] #[test] fn test_hash_value_array() { - use crate::ArrayType; - assert_eq!( Into::::into(vec![0i32, 1, 2]), - Value::Array( - ArrayType::Int, - Some(Box::new(vec![ - Value::Int(Some(0)), - Value::Int(Some(1)), - Value::Int(Some(2)) - ])) - ) + Value::array(vec![0i32, 1, 2]) ); assert_eq!( Into::::into(vec![0f32, 1.0, 2.0]), - Value::Array( - ArrayType::Float, - Some(Box::new(vec![ - Value::Float(Some(0f32)), - Value::Float(Some(1.0)), - Value::Float(Some(2.0)) - ])) - ) + Value::array(vec![0f32, 1.0, 2.0]) ); let hash_set: std::collections::HashSet = [ diff --git a/src/value/tests.rs b/src/value/tests.rs index f62569705..af1d7b832 100644 --- a/src/value/tests.rs +++ b/src/value/tests.rs @@ -73,54 +73,51 @@ fn test_box_value() { #[test] fn test_value_tuple() { - assert_eq!( - 1i32.into_value_tuple(), - ValueTuple::One(Value::Int(Some(1))) - ); + assert_eq!(1i32.into_value_tuple(), ValueTuple::One(Value::int(1))); assert_eq!( "b".into_value_tuple(), - ValueTuple::One(Value::String(Some("b".to_owned()))) + ValueTuple::One(Value::string("b".to_owned())) ); assert_eq!( (1i32, "b").into_value_tuple(), - ValueTuple::Two(Value::Int(Some(1)), Value::String(Some("b".to_owned()))) + ValueTuple::Two(Value::int(1), Value::string("b".to_owned())) ); assert_eq!( (1i32, 2.4f64, "b").into_value_tuple(), ValueTuple::Three( - Value::Int(Some(1)), - Value::Double(Some(2.4)), - Value::String(Some("b".to_owned())) + Value::int(1), + Value::double(2.4), + Value::string("b".to_owned()) ) ); assert_eq!( (1i32, 2.4f64, "b", 123u8).into_value_tuple(), ValueTuple::Many(vec![ - Value::Int(Some(1)), - Value::Double(Some(2.4)), - Value::String(Some("b".to_owned())), - Value::TinyUnsigned(Some(123)) + Value::int(1), + Value::double(2.4), + Value::string("b".to_owned()), + Value::tiny_unsigned(123) ]) ); assert_eq!( (1i32, 2.4f64, "b", 123u8, 456u16).into_value_tuple(), ValueTuple::Many(vec![ - Value::Int(Some(1)), - Value::Double(Some(2.4)), - Value::String(Some("b".to_owned())), - Value::TinyUnsigned(Some(123)), - Value::SmallUnsigned(Some(456)) + Value::int(1), + Value::double(2.4), + Value::string("b".to_owned()), + Value::tiny_unsigned(123), + Value::small_unsigned(456) ]) ); assert_eq!( (1i32, 2.4f64, "b", 123u8, 456u16, 789u32).into_value_tuple(), ValueTuple::Many(vec![ - Value::Int(Some(1)), - Value::Double(Some(2.4)), - Value::String(Some("b".to_owned())), - Value::TinyUnsigned(Some(123)), - Value::SmallUnsigned(Some(456)), - Value::Unsigned(Some(789)) + Value::int(1), + Value::double(2.4), + Value::string("b".to_owned()), + Value::tiny_unsigned(123), + Value::small_unsigned(456), + Value::unsigned(789) ]) ); } @@ -167,46 +164,46 @@ fn test_from_value_tuple() { #[test] fn test_value_tuple_iter() { let mut iter = (1i32).into_value_tuple().into_iter(); - assert_eq!(iter.next().unwrap(), Value::Int(Some(1))); + assert_eq!(iter.next().unwrap(), Value::int(1)); assert_eq!(iter.next(), None); let mut iter = (1i32, 2.4f64).into_value_tuple().into_iter(); - assert_eq!(iter.next().unwrap(), Value::Int(Some(1))); - assert_eq!(iter.next().unwrap(), Value::Double(Some(2.4))); + assert_eq!(iter.next().unwrap(), Value::int(1)); + assert_eq!(iter.next().unwrap(), Value::double(2.4)); assert_eq!(iter.next(), None); let mut iter = (1i32, 2.4f64, "b").into_value_tuple().into_iter(); - assert_eq!(iter.next().unwrap(), Value::Int(Some(1))); - assert_eq!(iter.next().unwrap(), Value::Double(Some(2.4))); - assert_eq!(iter.next().unwrap(), Value::String(Some("b".to_owned()))); + assert_eq!(iter.next().unwrap(), Value::int(1)); + assert_eq!(iter.next().unwrap(), Value::double(2.4)); + assert_eq!(iter.next().unwrap(), Value::string("b".to_owned())); assert_eq!(iter.next(), None); let mut iter = (1i32, 2.4f64, "b", 123u8).into_value_tuple().into_iter(); - assert_eq!(iter.next().unwrap(), Value::Int(Some(1))); - assert_eq!(iter.next().unwrap(), Value::Double(Some(2.4))); - assert_eq!(iter.next().unwrap(), Value::String(Some("b".to_owned()))); - assert_eq!(iter.next().unwrap(), Value::TinyUnsigned(Some(123))); + assert_eq!(iter.next().unwrap(), Value::int(1)); + assert_eq!(iter.next().unwrap(), Value::double(2.4)); + assert_eq!(iter.next().unwrap(), Value::string("b".to_owned())); + assert_eq!(iter.next().unwrap(), Value::tiny_unsigned(123)); assert_eq!(iter.next(), None); let mut iter = (1i32, 2.4f64, "b", 123u8, 456u16) .into_value_tuple() .into_iter(); - assert_eq!(iter.next().unwrap(), Value::Int(Some(1))); - assert_eq!(iter.next().unwrap(), Value::Double(Some(2.4))); - assert_eq!(iter.next().unwrap(), Value::String(Some("b".to_owned()))); - assert_eq!(iter.next().unwrap(), Value::TinyUnsigned(Some(123))); - assert_eq!(iter.next().unwrap(), Value::SmallUnsigned(Some(456))); + assert_eq!(iter.next().unwrap(), Value::int(1)); + assert_eq!(iter.next().unwrap(), Value::double(2.4)); + assert_eq!(iter.next().unwrap(), Value::string("b".to_owned())); + assert_eq!(iter.next().unwrap(), Value::tiny_unsigned(123)); + assert_eq!(iter.next().unwrap(), Value::small_unsigned(456)); assert_eq!(iter.next(), None); let mut iter = (1i32, 2.4f64, "b", 123u8, 456u16, 789u32) .into_value_tuple() .into_iter(); - assert_eq!(iter.next().unwrap(), Value::Int(Some(1))); - assert_eq!(iter.next().unwrap(), Value::Double(Some(2.4))); - assert_eq!(iter.next().unwrap(), Value::String(Some("b".to_owned()))); - assert_eq!(iter.next().unwrap(), Value::TinyUnsigned(Some(123))); - assert_eq!(iter.next().unwrap(), Value::SmallUnsigned(Some(456))); - assert_eq!(iter.next().unwrap(), Value::Unsigned(Some(789))); + assert_eq!(iter.next().unwrap(), Value::int(1)); + assert_eq!(iter.next().unwrap(), Value::double(2.4)); + assert_eq!(iter.next().unwrap(), Value::string("b".to_owned())); + assert_eq!(iter.next().unwrap(), Value::tiny_unsigned(123)); + assert_eq!(iter.next().unwrap(), Value::small_unsigned(456)); + assert_eq!(iter.next().unwrap(), Value::unsigned(789)); assert_eq!(iter.next(), None); } @@ -413,14 +410,21 @@ fn test_decimal_value() { fn test_array_value() { let array = vec![1, 2, 3, 4, 5]; let v: Value = array.into(); + assert_eq!(v, Value::array(vec![1, 2, 3, 4, 5])); let out: Vec = v.unwrap(); assert_eq!(out, vec![1, 2, 3, 4, 5]); + + let array = vec!["1".to_owned(), "2".to_owned()]; + let v: Value = array.clone().into(); + assert_eq!(v, Value::array(array)); } #[test] #[cfg(feature = "postgres-array")] fn test_option_array_value() { - let v: Value = Value::Array(ArrayType::Int, None); + let v: Value = Option::>::None.into(); + assert_eq!(v, Value::array_null::()); + assert!(matches!(v, Value::Array(_, _))); let out: Option> = v.unwrap(); assert_eq!(out, None); } diff --git a/src/value/with_array.rs b/src/value/with_array.rs index ef198974f..f960d1f2c 100644 --- a/src/value/with_array.rs +++ b/src/value/with_array.rs @@ -81,10 +81,7 @@ where T: Into + NotU8 + ValueType, { fn from(x: Vec) -> Value { - Value::Array( - T::array_type(), - Some(Box::new(x.into_iter().map(|e| e.into()).collect())), - ) + Value::array(x) } } @@ -93,7 +90,7 @@ where T: Into + NotU8 + ValueType, { fn null() -> Value { - Value::Array(T::array_type(), None) + Value::array_null::() } } @@ -125,6 +122,25 @@ where } impl Value { + #[inline] + pub fn array + NotU8 + ValueType, I: IntoIterator>(values: I) -> Self { + Self::Array( + T::array_type(), + Some( + values + .into_iter() + .map(|v| v.into()) + .collect::>() + .into(), + ), + ) + } + + #[inline] + pub fn array_null + NotU8 + ValueType>() -> Self { + Self::Array(T::array_type(), None) + } + pub fn is_array(&self) -> bool { matches!(self, Self::Array(_, _)) } diff --git a/src/value/with_bigdecimal.rs b/src/value/with_bigdecimal.rs index 9a7ee4264..8fbb8df3e 100644 --- a/src/value/with_bigdecimal.rs +++ b/src/value/with_bigdecimal.rs @@ -3,6 +3,11 @@ use super::*; type_to_box_value!(BigDecimal, BigDecimal, Decimal(None)); impl Value { + #[inline] + pub fn big_decimal>>(value: T) -> Self { + Self::BigDecimal(value.into().map(Box::new)) + } + pub fn is_big_decimal(&self) -> bool { matches!(self, Self::BigDecimal(_)) } diff --git a/src/value/with_chrono.rs b/src/value/with_chrono.rs index 9e95f5c76..5cb6c7b13 100644 --- a/src/value/with_chrono.rs +++ b/src/value/with_chrono.rs @@ -5,28 +5,62 @@ type_to_value!(NaiveDate, ChronoDate, Date); type_to_value!(NaiveTime, ChronoTime, Time); type_to_value!(NaiveDateTime, ChronoDateTime, DateTime); +impl Value { + #[inline] + pub fn chrono_date>>(value: T) -> Self { + Self::ChronoDate(value.into()) + } + + #[inline] + pub fn chrono_time>>(value: T) -> Self { + Self::ChronoTime(value.into()) + } + + #[inline] + pub fn chrono_date_time>>(value: T) -> Self { + Self::ChronoDateTime(value.into()) + } + + #[inline] + pub fn chrono_date_time_utc>>>(value: T) -> Self { + Self::ChronoDateTimeUtc(value.into()) + } + + #[inline] + pub fn chrono_date_time_local>>>(value: T) -> Self { + Self::ChronoDateTimeLocal(value.into()) + } + + #[inline] + pub fn chrono_date_time_with_time_zone>>>( + value: T, + ) -> Self { + Self::ChronoDateTimeWithTimeZone(value.into()) + } +} + impl From> for Value { fn from(v: DateTime) -> Value { - Value::ChronoDateTimeUtc(Some(v)) + Value::chrono_date_time_utc(v) } } impl From> for Value { fn from(v: DateTime) -> Value { - Value::ChronoDateTimeLocal(Some(v)) + Value::chrono_date_time_local(v) } } impl From> for Value { fn from(x: DateTime) -> Value { let v = DateTime::::from_naive_utc_and_offset(x.naive_utc(), x.offset().fix()); - Value::ChronoDateTimeWithTimeZone(Some(v)) + Value::chrono_date_time_with_time_zone(v) } } impl Nullable for DateTime { fn null() -> Value { - Value::ChronoDateTimeUtc(None) + Value::chrono_date_time_utc(None) } } @@ -53,7 +87,7 @@ impl ValueType for DateTime { impl Nullable for DateTime { fn null() -> Value { - Value::ChronoDateTimeLocal(None) + Value::chrono_date_time_local(None) } } @@ -80,7 +114,7 @@ impl ValueType for DateTime { impl Nullable for DateTime { fn null() -> Value { - Value::ChronoDateTimeWithTimeZone(None) + Value::chrono_date_time_with_time_zone(None) } } diff --git a/src/value/with_ipnetwork.rs b/src/value/with_ipnetwork.rs index 42eaffed0..e18c47217 100644 --- a/src/value/with_ipnetwork.rs +++ b/src/value/with_ipnetwork.rs @@ -3,6 +3,11 @@ use super::*; type_to_value!(IpNetwork, IpNetwork, Inet); impl Value { + #[inline] + pub fn ip_network>>(value: T) -> Self { + Self::IpNetwork(value.into()) + } + pub fn is_ipnetwork(&self) -> bool { matches!(self, Self::IpNetwork(_)) } diff --git a/src/value/with_json.rs b/src/value/with_json.rs index 7b4cf4ba6..13b47a3be 100644 --- a/src/value/with_json.rs +++ b/src/value/with_json.rs @@ -3,10 +3,17 @@ use super::*; type_to_value!(Json, Json, Json); impl Value { + #[inline] + pub fn json>>(value: T) -> Self { + Self::Json(value.into()) + } + + #[inline] pub fn is_json(&self) -> bool { matches!(self, Self::Json(_)) } + #[inline] pub fn as_ref_json(&self) -> Option<&Json> { match self { Self::Json(v) => v.as_ref(), diff --git a/src/value/with_mac_address.rs b/src/value/with_mac_address.rs index 834695b31..015e5a39c 100644 --- a/src/value/with_mac_address.rs +++ b/src/value/with_mac_address.rs @@ -3,6 +3,11 @@ use super::*; type_to_value!(MacAddress, MacAddress, MacAddr); impl Value { + #[inline] + pub fn mac_address>>(value: T) -> Self { + Self::MacAddress(value.into()) + } + pub fn is_mac_address(&self) -> bool { matches!(self, Self::MacAddress(_)) } diff --git a/src/value/with_pgvector.rs b/src/value/with_pgvector.rs index 257a8afaf..e77e483bd 100644 --- a/src/value/with_pgvector.rs +++ b/src/value/with_pgvector.rs @@ -1,14 +1,21 @@ use super::*; +impl Value { + #[inline] + pub fn vector>>(value: T) -> Self { + Self::Vector(value.into()) + } +} + impl From for Value { fn from(x: pgvector::Vector) -> Value { - Value::Vector(Some(x)) + Value::vector(x) } } impl Nullable for pgvector::Vector { fn null() -> Value { - Value::Vector(None) + Value::vector(None) } } diff --git a/src/value/with_rust_decimal.rs b/src/value/with_rust_decimal.rs index 7733a8ebd..b200588a9 100644 --- a/src/value/with_rust_decimal.rs +++ b/src/value/with_rust_decimal.rs @@ -3,6 +3,11 @@ use super::*; type_to_value!(Decimal, Decimal, Decimal(None)); impl Value { + #[inline] + pub fn decimal>>(value: T) -> Self { + Self::Decimal(value.into()) + } + pub fn is_decimal(&self) -> bool { matches!(self, Self::Decimal(_)) } diff --git a/src/value/with_time.rs b/src/value/with_time.rs index 5ea29c6f1..6c0e7cc4c 100644 --- a/src/value/with_time.rs +++ b/src/value/with_time.rs @@ -4,15 +4,37 @@ type_to_value!(time::Date, TimeDate, Date); type_to_value!(time::Time, TimeTime, Time); type_to_value!(PrimitiveDateTime, TimeDateTime, DateTime); +impl Value { + #[inline] + pub fn time_date>>(value: T) -> Self { + Self::TimeDate(value.into()) + } + + #[inline] + pub fn time_time>>(value: T) -> Self { + Self::TimeTime(value.into()) + } + + #[inline] + pub fn time_date_time>>(value: T) -> Self { + Self::TimeDateTime(value.into()) + } + + #[inline] + pub fn time_date_time_with_time_zone>>(value: T) -> Self { + Self::TimeDateTimeWithTimeZone(value.into()) + } +} + impl From for Value { fn from(v: OffsetDateTime) -> Value { - Value::TimeDateTimeWithTimeZone(Some(v)) + Value::time_date_time_with_time_zone(v) } } impl Nullable for OffsetDateTime { fn null() -> Value { - Value::TimeDateTimeWithTimeZone(None) + Value::time_date_time_with_time_zone(None) } } diff --git a/src/value/with_uuid.rs b/src/value/with_uuid.rs index c12a8b102..d118593ea 100644 --- a/src/value/with_uuid.rs +++ b/src/value/with_uuid.rs @@ -6,13 +6,13 @@ macro_rules! fmt_uuid_to_box_value { ( $type: ty, $conversion_fn: ident ) => { impl From<$type> for Value { fn from(x: $type) -> Value { - Value::Uuid(Some(x.into_uuid())) + Value::uuid(x.into_uuid()) } } impl Nullable for $type { fn null() -> Value { - Value::Uuid(None) + Value::uuid(None) } } @@ -45,6 +45,11 @@ fmt_uuid_to_box_value!(uuid::fmt::Simple, simple); fmt_uuid_to_box_value!(uuid::fmt::Urn, urn); impl Value { + #[inline] + pub fn uuid>>(value: T) -> Self { + Self::Uuid(value.into()) + } + pub fn is_uuid(&self) -> bool { matches!(self, Self::Uuid(_)) } diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index cf6ca40ff..48cc3df84 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -1082,7 +1082,7 @@ fn insert_3() { "04108048005887010020060000204E0180400400".into(), 3.1415.into(), ]) - .values_panic([Value::String(None).into(), 2.1345.into(),]) + .values_panic([Value::string(None).into(), 2.1345.into(),]) .to_string(MysqlQueryBuilder), "INSERT INTO `glyph` (`image`, `aspect`) VALUES ('04108048005887010020060000204E0180400400', 3.1415), (NULL, 2.1345)" ); diff --git a/tests/mysql/table.rs b/tests/mysql/table.rs index bb37efbb9..8c1d946a1 100644 --- a/tests/mysql/table.rs +++ b/tests/mysql/table.rs @@ -81,7 +81,7 @@ fn create_3() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)) + .default(Value::int(None)) ) .col( ColumnDef::new(Char::CreatedAt) @@ -284,7 +284,7 @@ fn create_12() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)), + .default(Value::int(None)), ) .col( ColumnDef::new(Char::CreatedAt) diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index bdd77c613..cee915de9 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -1016,10 +1016,10 @@ fn select_56() { .order_by( Glyph::Id, Order::Field(Values(vec![ - Value::Int(Some(4)), - Value::Int(Some(5)), - Value::Int(Some(1)), - Value::Int(Some(3)) + Value::int(4), + Value::int(5), + Value::int(1), + Value::int(3) ])) ) .order_by((Glyph::Table, Glyph::Aspect), Order::Asc) @@ -1051,10 +1051,10 @@ fn select_57() { .order_by( Glyph::Id, Order::Field(Values(vec![ - Value::Int(Some(4)), - Value::Int(Some(5)), - Value::Int(Some(1)), - Value::Int(Some(3)) + Value::int(4), + Value::int(5), + Value::int(1), + Value::int(3) ])) ) .to_string(PostgresQueryBuilder), @@ -1256,7 +1256,7 @@ fn insert_3() { "04108048005887010020060000204E0180400400".into(), 3.1415.into(), ]) - .values_panic([Value::String(None).into(), 2.1345.into()]) + .values_panic([Value::string(None).into(), 2.1345.into()]) .to_string(PostgresQueryBuilder), r#"INSERT INTO "glyph" ("image", "aspect") VALUES ('04108048005887010020060000204E0180400400', 3.1415), (NULL, 2.1345)"# ); diff --git a/tests/postgres/table.rs b/tests/postgres/table.rs index cd75d2cb7..52209c972 100644 --- a/tests/postgres/table.rs +++ b/tests/postgres/table.rs @@ -75,7 +75,7 @@ fn create_3() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)) + .default(Value::int(None)) ) .foreign_key( ForeignKey::create() @@ -682,7 +682,7 @@ fn create_19() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)), + .default(Value::int(None)), ) .index( Index::create() diff --git a/tests/sqlite/query.rs b/tests/sqlite/query.rs index 3f6af32c7..259c359c1 100644 --- a/tests/sqlite/query.rs +++ b/tests/sqlite/query.rs @@ -899,10 +899,10 @@ fn select_55() { .order_by( Glyph::Id, Order::Field(Values(vec![ - Value::Int(Some(4)), - Value::Int(Some(5)), - Value::Int(Some(1)), - Value::Int(Some(3)) + Value::int(4), + Value::int(5), + Value::int(1), + Value::int(3) ])) ) .order_by((Glyph::Table, Glyph::Aspect), Order::Asc) @@ -934,10 +934,10 @@ fn select_56() { .order_by( Glyph::Id, Order::Field(Values(vec![ - Value::Int(Some(4)), - Value::Int(Some(5)), - Value::Int(Some(1)), - Value::Int(Some(3)) + Value::int(4), + Value::int(5), + Value::int(1), + Value::int(3) ])) ) .to_string(SqliteQueryBuilder), @@ -1081,7 +1081,7 @@ fn insert_3() { "04108048005887010020060000204E0180400400".into(), 3.1415.into(), ]) - .values_panic([Value::Double(None).into(), 2.1345.into()]) + .values_panic([Value::double(None).into(), 2.1345.into()]) .to_string(SqliteQueryBuilder), r#"INSERT INTO "glyph" ("image", "aspect") VALUES ('04108048005887010020060000204E0180400400', 3.1415), (NULL, 2.1345)"# ); diff --git a/tests/sqlite/table.rs b/tests/sqlite/table.rs index 659ca3123..422c2c034 100644 --- a/tests/sqlite/table.rs +++ b/tests/sqlite/table.rs @@ -75,7 +75,7 @@ fn create_3() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)) + .default(Value::int(None)) ) .foreign_key( ForeignKey::create() @@ -229,7 +229,7 @@ fn create_with_unique_index() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)) + .default(Value::int(None)) ) .foreign_key( ForeignKey::create() @@ -274,7 +274,7 @@ fn create_with_primary_unique_index() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)) + .default(Value::int(None)) ) .foreign_key( ForeignKey::create() @@ -321,7 +321,7 @@ fn create_with_unique_index_constraint() { .col( ColumnDef::new(Char::FontId) .integer() - .default(Value::Int(None)), + .default(Value::int(None)), ) .foreign_key( ForeignKey::create()