mirror of
https://github.com/fluencelabs/llamadb
synced 2025-05-02 18:22:15 +00:00
Add conversion methods to ColumnValueOps
This commit is contained in:
parent
ba3f05eb0e
commit
4b76b7fe81
@ -5,6 +5,14 @@ pub trait ColumnValueOps: Sized {
|
||||
fn from_string_literal(s: Cow<str>) -> Result<Self, Cow<str>>;
|
||||
fn from_number_literal(s: Cow<str>) -> Result<Self, Cow<str>>;
|
||||
|
||||
fn from_f64(value: f64) -> Self;
|
||||
fn to_f64(self) -> Result<f64, ()>;
|
||||
|
||||
fn from_u64(value: u64) -> Self;
|
||||
fn to_u64(self) -> Result<u64, ()>;
|
||||
|
||||
fn null() -> Self { ColumnValueOps::from_3vl(0) }
|
||||
|
||||
fn from_bytes(dbtype: DbType, bytes: Cow<[u8]>) -> Result<Self, ()>;
|
||||
fn to_bytes(self, dbtype: DbType) -> Result<Box<[u8]>, ()>;
|
||||
fn get_dbtype(&self) -> DbType;
|
||||
@ -25,6 +33,8 @@ pub trait ColumnValueOps: Sized {
|
||||
fn equals(&self, rhs: &Self) -> Self;
|
||||
fn not_equals(&self, rhs: &Self) -> Self;
|
||||
|
||||
fn is_null(&self) -> bool { self.to_3vl() == 0 }
|
||||
|
||||
fn not(&self) -> Self {
|
||||
ColumnValueOps::from_3vl(-self.to_3vl())
|
||||
}
|
||||
|
@ -55,6 +55,32 @@ impl ColumnValueOps for Variant {
|
||||
}
|
||||
}
|
||||
|
||||
fn from_f64(value: f64) -> Variant {
|
||||
Variant::Float(F64NoNaN::new(value).unwrap())
|
||||
}
|
||||
|
||||
fn to_f64(self) -> Result<f64, ()> {
|
||||
let num = self.cast(DbType::F64);
|
||||
if let Some(Variant::Float(float)) = num {
|
||||
Ok(*float)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
fn from_u64(value: u64) -> Variant {
|
||||
Variant::UnsignedInteger(value)
|
||||
}
|
||||
|
||||
fn to_u64(self) -> Result<u64, ()> {
|
||||
let num = self.cast(DbType::Integer { signed: false, bytes: 8 });
|
||||
if let Some(Variant::UnsignedInteger(i)) = num {
|
||||
Ok(i)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
fn from_bytes(dbtype: DbType, bytes: Cow<[u8]>) -> Result<Variant, ()> {
|
||||
match dbtype {
|
||||
DbType::Null => Ok(Variant::Null),
|
||||
|
Loading…
x
Reference in New Issue
Block a user