Experimenting with type conversion
This commit is contained in:
parent
6c5abec034
commit
b39e87698c
@ -17,7 +17,7 @@ optional=true
|
|||||||
package="postgres"
|
package="postgres"
|
||||||
|
|
||||||
[dependencies.slite]
|
[dependencies.slite]
|
||||||
version="0.17.0"
|
version="0.18.0"
|
||||||
features=["chrono","serde_json","url"]
|
features=["chrono","serde_json","url"]
|
||||||
optional=true
|
optional=true
|
||||||
package="rusqlite"
|
package="rusqlite"
|
||||||
|
@ -110,6 +110,11 @@ pub trait DatabaseDriver {
|
|||||||
// Runs a basic sql query on the database
|
// Runs a basic sql query on the database
|
||||||
// fn query(&self, sql: &str) -> Result<impl Any, impl Error>;
|
// fn query(&self, sql: &str) -> Result<impl Any, impl Error>;
|
||||||
|
|
||||||
|
// Prepares an sql statement for the database
|
||||||
|
fn prepare(&self, sql: &str) -> Result<(), ()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// Runs a prepared statement on the database
|
// Runs a prepared statement on the database
|
||||||
// fn execute(&self, sql: &str, ?) -> Result<?,?>
|
// fn execute(&self, sql: &str, ?) -> Result<?,?>
|
||||||
|
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
//! Contains database-specific query data
|
//! Contains database-specific query data
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use slite::{Connection};
|
use slite::{params, Connection, Result};
|
||||||
|
use slite::NO_PARAMS;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
|
||||||
/// The struct implementing the `DatabaseDriver` trait
|
/// The struct implementing the `DatabaseDriver` trait
|
||||||
@ -16,11 +17,29 @@ pub struct SQLiteDriver {
|
|||||||
|
|
||||||
impl SQLiteDriver {
|
impl SQLiteDriver {
|
||||||
/// Create an SQLiteDriver driver
|
/// Create an SQLiteDriver driver
|
||||||
pub fn new() -> Self {
|
pub fn new(dsn: &str) -> Self {
|
||||||
SQLiteDriver {
|
let mut driver = SQLiteDriver {
|
||||||
connection: RefCell::new(None)
|
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<usize> {
|
||||||
|
//
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DatabaseDriver for SQLiteDriver {
|
impl DatabaseDriver for SQLiteDriver {
|
||||||
|
@ -23,6 +23,7 @@ extern crate lazy_static;
|
|||||||
pub mod drivers;
|
pub mod drivers;
|
||||||
pub mod fns;
|
pub mod fns;
|
||||||
pub mod query_builder;
|
pub mod query_builder;
|
||||||
|
pub mod types;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
//! Re-exports important traits and types.
|
//! Re-exports important traits and types.
|
||||||
|
82
src/types.rs
82
src/types.rs
@ -1,8 +1,76 @@
|
|||||||
//! Shared Types for different Database drivers
|
//! 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<u8>` | BYTEA |
|
||||||
|
//!
|
||||||
|
//! ## SQLite Type Mappings
|
||||||
|
//!
|
||||||
|
//! | Rust type(s) | SQLite type(s) |
|
||||||
|
//! |-------------------------------|-----------------------------------------------|
|
||||||
|
//! | `i8`/`i16`/`i32`/`i64` | INTEGER |
|
||||||
|
//! | `f64` | REAL |
|
||||||
|
//! | `&str`/`String` | TEXT |
|
||||||
|
//! | `&[u8]`/`Vec<u8>` | BLOB |
|
||||||
|
//!
|
||||||
|
//!
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Debug)]
|
/// Empty struct to represent Null values
|
||||||
struct Type(pub Box<dyn Any>);
|
#[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<u8>),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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<ToSqlOutput<'_>>;
|
||||||
|
//}
|
||||||
|
|
||||||
/// Enum struct for mapping between database and Rust types
|
/// Enum struct for mapping between database and Rust types
|
||||||
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
||||||
@ -10,22 +78,22 @@ struct SQLType<T> {
|
|||||||
value: Option<T>,
|
value: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_str(s: &(dyn Any)) {
|
fn is_str(s: &(dyn Any)) -> bool {
|
||||||
s.is::<&str>()
|
s.is::<&str>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_string(s: &(dyn Any)) {
|
fn is_string(s: &(dyn Any)) -> bool {
|
||||||
s.is::<String>()
|
s.is::<String>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_bool(v: &(dyn Any)) {
|
fn is_bool(v: &(dyn Any)) -> bool {
|
||||||
v.is::<bool>()
|
v.is::<bool>()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SQLType<T> {
|
impl<T> SQLType<T> {
|
||||||
pub fn is_some(&self) -> bool {
|
pub fn is_some(&self) -> bool {
|
||||||
match *self {
|
match self.value {
|
||||||
SQLType::None => false,
|
None => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user