Skip to content
Open
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
])
)
);
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//! ])
//! )
//! );
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/query/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: QueryBuilder>(&self, query_builder: T) -> (String, Values) {
Expand Down Expand Up @@ -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<T: QueryBuilder>(&self, query_builder: T, sql: &mut dyn SqlWriter) -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion src/table/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
89 changes: 81 additions & 8 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,79 @@ const fn check_value_size() -> usize {
std::mem::size_of::<Value>()
}

// Constructors
impl Value {
#[inline]
pub fn bool<T: Into<Option<bool>>>(value: T) -> Self {
Self::Bool(value.into())
}

#[inline]
pub fn tiny_int<T: Into<Option<i8>>>(value: T) -> Self {
Self::TinyInt(value.into())
}

#[inline]
pub fn small_int<T: Into<Option<i16>>>(value: T) -> Self {
Self::SmallInt(value.into())
}

#[inline]
pub fn int<T: Into<Option<i32>>>(value: T) -> Self {
Self::Int(value.into())
}

#[inline]
pub fn big_int<T: Into<Option<i64>>>(value: T) -> Self {
Self::BigInt(value.into())
}

#[inline]
pub fn tiny_unsigned<T: Into<Option<u8>>>(value: T) -> Self {
Self::TinyUnsigned(value.into())
}

#[inline]
pub fn small_unsigned<T: Into<Option<u16>>>(value: T) -> Self {
Self::SmallUnsigned(value.into())
}

#[inline]
pub fn unsigned<T: Into<Option<u32>>>(value: T) -> Self {
Self::Unsigned(value.into())
}

#[inline]
pub fn big_unsigned<T: Into<Option<u64>>>(value: T) -> Self {
Self::BigUnsigned(value.into())
}

#[inline]
pub fn float<T: Into<Option<f32>>>(value: T) -> Self {
Self::Float(value.into())
}

#[inline]
pub fn double<T: Into<Option<f64>>>(value: T) -> Self {
Self::Double(value.into())
}

#[inline]
pub fn string<T: Into<Option<String>>>(value: T) -> Self {
Self::String(value.into())
}

#[inline]
pub fn char<T: Into<Option<char>>>(value: T) -> Self {
Self::Char(value.into())
}

#[inline]
pub fn bytes<T: Into<Option<Vec<u8>>>>(value: T) -> Self {
Self::Bytes(value.into())
}
}

impl Value {
pub fn unwrap<T>(self) -> T
where
Expand All @@ -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::<Value>::into(2.2).as_null(), Value::Double(None));
/// assert_eq!(Into::<Value>::into(2.2).as_null(), Value::double(None));
/// ```
pub fn as_null(&self) -> Self {
match self {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}

Expand Down
88 changes: 36 additions & 52 deletions src/value/hashable_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,29 +258,29 @@ mod tests {
#[test]
fn test_hash_value_0() {
let hash_set: std::collections::HashSet<Value> = [
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> = [
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();
Expand All @@ -291,27 +291,27 @@ mod tests {
#[test]
fn test_hash_value_1() {
let hash_set: std::collections::HashSet<Value> = [
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> = [
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();
Expand All @@ -322,30 +322,14 @@ mod tests {
#[cfg(feature = "postgres-array")]
#[test]
fn test_hash_value_array() {
use crate::ArrayType;

assert_eq!(
Into::<Value>::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::<Value>::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<Value> = [
Expand Down
Loading