From b39e87698c3dfcd21b6a26fd74ffd0a168834205 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 24 Apr 2019 16:12:07 -0400 Subject: [PATCH] Experimenting with type conversion --- Cargo.toml | 2 +- src/drivers.rs | 5 +++ src/drivers/sqlite.rs | 27 +++++++++++--- src/lib.rs | 1 + src/types.rs | 82 +++++++++++++++++++++++++++++++++++++++---- 5 files changed, 105 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb710c8..5612785 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ optional=true package="postgres" [dependencies.slite] -version="0.17.0" +version="0.18.0" features=["chrono","serde_json","url"] optional=true package="rusqlite" diff --git a/src/drivers.rs b/src/drivers.rs index 08aeb43..2501e09 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -110,6 +110,11 @@ pub trait DatabaseDriver { // Runs a basic sql query on the database // fn query(&self, sql: &str) -> Result; + // Prepares an sql statement for the database + fn prepare(&self, sql: &str) -> Result<(), ()> { + Ok(()) + } + // Runs a prepared statement on the database // fn execute(&self, sql: &str, ?) -> Result diff --git a/src/drivers/sqlite.rs b/src/drivers/sqlite.rs index 68e1895..c045c71 100644 --- a/src/drivers/sqlite.rs +++ b/src/drivers/sqlite.rs @@ -5,7 +5,8 @@ //! Contains database-specific query data use super::*; -use slite::{Connection}; +use slite::{params, Connection, Result}; +use slite::NO_PARAMS; use std::cell::RefCell; /// The struct implementing the `DatabaseDriver` trait @@ -16,11 +17,29 @@ pub struct SQLiteDriver { impl SQLiteDriver { /// Create an SQLiteDriver driver - pub fn new() -> Self { - SQLiteDriver { + pub fn new(dsn: &str) -> Self { + let mut driver = SQLiteDriver { connection: RefCell::new(None) - } + }; + + driver.connect(dsn); + + driver } + + fn connect(&mut self, dsn: &str) { + let connection = if dsn == ":memory:" { + Connection::open_in_memory().unwrap() + } else { + Connection::open(dsn).unwrap() + }; + + self.connection = RefCell::new(Some(connection)); + } + +// pub fn query(&self, sql: &str) -> Result { +// +// } } impl DatabaseDriver for SQLiteDriver { diff --git a/src/lib.rs b/src/lib.rs index 8dfef52..69204d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,7 @@ extern crate lazy_static; pub mod drivers; pub mod fns; pub mod query_builder; +pub mod types; pub mod prelude { //! Re-exports important traits and types. diff --git a/src/types.rs b/src/types.rs index f6d0338..54b5b28 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,8 +1,76 @@ //! Shared Types for different Database drivers +//! +//! ## Postgres Type Mappings +//! +//! | Rust type | Postgres type(s) | +//! |-------------------------------|-----------------------------------------------| +//! | `bool` | BOOL | +//! | `i8` | "char" | +//! | `i16` | SMALLINT, SMALLSERIAL | +//! | `i32` | INT, SERIAL | +//! | `u32` | OID | +//! | `i64` | BIGINT, BIGSERIAL | +//! | `f32` | REAL | +//! | `f64` | DOUBLE PRECISION | +//! | `&str`/`String` | VARCHAR, CHAR(n), TEXT, CITEXT, NAME, UNKNOWN | +//! | `&[u8]`/`Vec` | BYTEA | +//! +//! ## SQLite Type Mappings +//! +//! | Rust type(s) | SQLite type(s) | +//! |-------------------------------|-----------------------------------------------| +//! | `i8`/`i16`/`i32`/`i64` | INTEGER | +//! | `f64` | REAL | +//! | `&str`/`String` | TEXT | +//! | `&[u8]`/`Vec` | BLOB | +//! +//! use std::any::Any; +use std::borrow::Cow; -#[derive(Debug)] -struct Type(pub Box); +/// Empty struct to represent Null values +#[derive(Copy, Clone)] +pub struct Null; + +#[derive(Clone, Debug, PartialEq)] +pub enum Type { + Null, + Integer, + Real, + Text, + Blob, +} + +/// An owned value +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + Null, + Integer(i64), + Real(f64), + Text(String), + Blob(Vec), +} + +/// A borrowed value +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum ValueRef<'a> { + Null, + Integer(i64), + Real(f64), + Text(&'a str), + Blob(&'a [u8]), +} + +#[derive(Clone, Debug, PartialEq)] +pub enum ToSqlOutput<'a> { + Borrowed(ValueRef<'a>), + Owned(Value), +} + +/// Types that can be converted to SQL +//pub trait ToSql { +// fn to_sql(&self) -> Result>; +//} /// Enum struct for mapping between database and Rust types #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] @@ -10,22 +78,22 @@ struct SQLType { value: Option, } -fn is_str(s: &(dyn Any)) { +fn is_str(s: &(dyn Any)) -> bool { s.is::<&str>() } -fn is_string(s: &(dyn Any)) { +fn is_string(s: &(dyn Any)) -> bool { s.is::() } -fn is_bool(v: &(dyn Any)) { +fn is_bool(v: &(dyn Any)) -> bool { v.is::() } impl SQLType { pub fn is_some(&self) -> bool { - match *self { - SQLType::None => false, + match self.value { + None => false, _ => true, } }