mod conversions;
mod drivers;
mod insert;
pub mod owned;
mod update;
pub use insert::*;
pub use owned::*;
pub use update::*;
use crate::traits::FromSQLiteValue;
use drizzle_core::{dialect::Dialect, error::DrizzleError, sql::SQL, traits::SQLParam};
use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub enum SQLiteValue<'a> {
Integer(i64),
Real(f64),
Text(Cow<'a, str>),
Blob(Cow<'a, [u8]>),
#[default]
Null,
}
impl<'a> SQLiteValue<'a> {
#[inline]
pub const fn is_null(&self) -> bool {
matches!(self, SQLiteValue::Null)
}
#[inline]
pub const fn as_i64(&self) -> Option<i64> {
match self {
SQLiteValue::Integer(value) => Some(*value),
_ => None,
}
}
#[inline]
pub const fn as_f64(&self) -> Option<f64> {
match self {
SQLiteValue::Real(value) => Some(*value),
_ => None,
}
}
#[inline]
pub fn as_str(&self) -> Option<&str> {
match self {
SQLiteValue::Text(value) => Some(value.as_ref()),
_ => None,
}
}
#[inline]
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
SQLiteValue::Blob(value) => Some(value.as_ref()),
_ => None,
}
}
#[inline]
pub fn into_owned(self) -> OwnedSQLiteValue {
self.into()
}
pub fn convert<T: FromSQLiteValue>(self) -> Result<T, DrizzleError> {
match self {
SQLiteValue::Integer(i) => T::from_sqlite_integer(i),
SQLiteValue::Text(s) => T::from_sqlite_text(&s),
SQLiteValue::Real(r) => T::from_sqlite_real(r),
SQLiteValue::Blob(b) => T::from_sqlite_blob(&b),
SQLiteValue::Null => T::from_sqlite_null(),
}
}
pub fn convert_ref<T: FromSQLiteValue>(&self) -> Result<T, DrizzleError> {
match self {
SQLiteValue::Integer(i) => T::from_sqlite_integer(*i),
SQLiteValue::Text(s) => T::from_sqlite_text(s),
SQLiteValue::Real(r) => T::from_sqlite_real(*r),
SQLiteValue::Blob(b) => T::from_sqlite_blob(b),
SQLiteValue::Null => T::from_sqlite_null(),
}
}
}
impl<'a> std::fmt::Display for SQLiteValue<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let value = match self {
SQLiteValue::Integer(i) => i.to_string(),
SQLiteValue::Real(r) => r.to_string(),
SQLiteValue::Text(cow) => cow.to_string(),
SQLiteValue::Blob(cow) => String::from_utf8_lossy(cow).to_string(),
SQLiteValue::Null => String::new(),
};
write!(f, "{value}")
}
}
impl<'a> SQLParam for SQLiteValue<'a> {
const DIALECT: Dialect = Dialect::SQLite;
}
impl<'a> From<SQLiteValue<'a>> for SQL<'a, SQLiteValue<'a>> {
fn from(value: SQLiteValue<'a>) -> Self {
SQL::param(value)
}
}
impl<'a> FromIterator<OwnedSQLiteValue> for Vec<SQLiteValue<'a>> {
fn from_iter<T: IntoIterator<Item = OwnedSQLiteValue>>(iter: T) -> Self {
iter.into_iter().map(SQLiteValue::from).collect()
}
}
impl<'a> FromIterator<&'a OwnedSQLiteValue> for Vec<SQLiteValue<'a>> {
fn from_iter<T: IntoIterator<Item = &'a OwnedSQLiteValue>>(iter: T) -> Self {
iter.into_iter().map(SQLiteValue::from).collect()
}
}