Ugly progress commit

This commit is contained in:
Timothy Warren 2019-04-02 16:35:52 -04:00
parent f2d993cbf0
commit d77e25b800
9 changed files with 81 additions and 35 deletions

2
.gitignore vendored
View File

@ -130,3 +130,5 @@ Cargo.lock
**/*.rs.bk
# End of https://www.gitignore.io/api/rust,macos,jetbrains+all
.env

View File

@ -6,14 +6,22 @@ edition = "2018"
categories = ["database"]
[dependencies]
dotenv = "0.13.0"
serde_json = "1.0.39"
[dependencies.postgres]
[dependencies.pg]
version="0.15.2"
features=["with-serde_json"]
optional=true
package="postgres"
[dependencies.rusqlite]
[dependencies.slite]
version="0.17.0"
features=["chrono","serde_json","url"]
optional=true
package="rusqlite"
[features]
default=['postgres']
postgres=['pg']
sqlite=['slite']

View File

@ -1,20 +0,0 @@
pub trait DatabaseDriver {
fn query(&self, query: &str) -> Result<(), ()>;
}
pub struct Postgres;
pub struct SQLite;
impl DatabaseDriver for Postgres {
fn query(&self, _query: &str) -> Result<(), ()> {
Ok(())
}
}
impl DatabaseDriver for SQLite {
fn query(&self, _query: &str) -> Result<(), ()> {
Ok(())
}
}

26
src/drivers/mod.rs Normal file
View File

@ -0,0 +1,26 @@
//! Drivers
//!
//! Drivers represent a connection to a specific type of database engine
use std::fmt;
#[cfg(feature="pg")]
mod postgres;
#[cfg(feature="sqlite")]
mod sqlite;
#[derive(Debug)]
struct Connection;
#[derive(Debug)]
struct QueryResult;
/// Database Driver Trait
///
/// Interface between the database connection library and the query builder
pub trait DatabaseDriver: fmt::Debug {
/// Runs a basic sql query on the database
fn query(&self, query: &str) -> Result<(), ()>;
}

13
src/drivers/postgres.rs Normal file
View File

@ -0,0 +1,13 @@
use super::*;
use pg;
#[derive(Debug)]
pub struct Postgres;
#[cfg(feature="pg")]
impl DatabaseDriver for Postgres {
fn query(&self, _query: &str) -> Result<(), ()> {
Ok(())
}
}

7
src/drivers/sqlite.rs Normal file
View File

@ -0,0 +1,7 @@
pub struct SQLite;
impl DatabaseDriver for SQLite {
fn query(&self, _query: &str) -> Result<(), ()> {
Ok(())
}
}

View File

@ -1,6 +1,9 @@
/// # StringQB
///
/// A query builder using mostly strings, with methods following common SQL syntax
//! # StringQB
//!
//! A query builder using mostly strings, with methods following common SQL syntax
#![warn(missing_docs)]
pub mod drivers;
pub mod query_builder;
pub mod types;
mod drivers;
mod query_builder;

View File

@ -1,4 +1,7 @@
use std::any::Any;
//! QueryBuilder
//!
//! The QueryBuilder creates sql queries from chained methods
use std::any::{Any, TypeId};
use std::collections::HashMap;
use crate::drivers::DatabaseDriver;
@ -39,10 +42,10 @@ struct QueryState {
group_array: HashMap<String, String>,
// Values to apply to prepared statements
pub values: Vec<String>,
pub values: Vec<Box<dyn Any>>,
// Values to apply to where clauses in prepared statements
pub where_values: Vec<String>,
pub where_values: Vec<Box<dyn Any>>,
limit: u32,
@ -61,13 +64,15 @@ impl QueryState {
}
}
/// The struct representing a query builder
#[derive(Default, Debug)]
pub struct QueryBuilder {
state: QueryState,
driver: Option<DatabaseDriver>,
driver: Option<Box<dyn DatabaseDriver>>,
}
impl QueryBuilder {
/// Creates a new QueryBuilder instance
pub fn new() -> QueryBuilder {
QueryBuilder {
state: QueryState::new(),
@ -75,7 +80,8 @@ impl QueryBuilder {
}
}
pub fn set(mut self, key: String, value: String) -> Self {
/// Set a key and value for an insert or update query
pub fn set(mut self, key: String, value: Box<dyn Any>) -> Self {
self.state.set_array_keys.push(key);
self.state.values.push(value);
@ -90,8 +96,8 @@ mod tests {
#[test]
fn set_key_value() {
let builder = QueryBuilder::new();
let builder = builder.set("foo".to_string(), "bar".to_string());
let builder = builder.set("foo".to_string(), Box::new("bar".to_string()));
assert_eq!(builder.state.values, vec!["bar"]);
assert!(builder.state.values[0].is::<String>());
}
}

1
src/types.rs Normal file
View File

@ -0,0 +1 @@
//! Shared Types for different Database drivers