Browse Source

Experimenting with type conversion

master
Timothy Warren 9 months ago
parent
commit
b39e87698c
5 changed files with 105 additions and 12 deletions
  1. +1
    -1
      Cargo.toml
  2. +5
    -0
      src/drivers.rs
  3. +23
    -4
      src/drivers/sqlite.rs
  4. +1
    -0
      src/lib.rs
  5. +75
    -7
      src/types.rs

+ 1
- 1
Cargo.toml View File

@@ -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"

+ 5
- 0
src/drivers.rs View File

@@ -110,6 +110,11 @@ pub trait DatabaseDriver {
// Runs a basic sql query on the database
// 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
// fn execute(&self, sql: &str, ?) -> Result<?,?>


+ 23
- 4
src/drivers/sqlite.rs View File

@@ -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<usize> {
//
// }
}

impl DatabaseDriver for SQLiteDriver {

+ 1
- 0
src/lib.rs View File

@@ -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.

+ 75
- 7
src/types.rs View File

@@ -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<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::borrow::Cow;

#[derive(Debug)]
struct Type(pub Box<dyn Any>);
/// 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<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
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
@@ -10,22 +78,22 @@ struct SQLType<T> {
value: Option<T>,
}

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::<String>()
}

fn is_bool(v: &(dyn Any)) {
fn is_bool(v: &(dyn Any)) -> bool {
v.is::<bool>()
}

impl<T> SQLType<T> {
pub fn is_some(&self) -> bool {
match *self {
SQLType::None => false,
match self.value {
None => false,
_ => true,
}
}

Loading…
Cancel
Save