Do you wish to register an account?
Browse Source

Ugly progress commit

master
Timothy Warren 1 year ago
parent
commit
d77e25b800
9 changed files with 81 additions and 35 deletions
  1. +2
    -0
      .gitignore
  2. +11
    -3
      Cargo.toml
  3. +0
    -20
      src/drivers.rs
  4. +26
    -0
      src/drivers/mod.rs
  5. +13
    -0
      src/drivers/postgres.rs
  6. +7
    -0
      src/drivers/sqlite.rs
  7. +8
    -5
      src/lib.rs
  8. +13
    -7
      src/query_builder.rs
  9. +1
    -0
      src/types.rs

+ 2
- 0
.gitignore View File

@@ -130,3 +130,5 @@ Cargo.lock
**/*.rs.bk

# End of https://www.gitignore.io/api/rust,macos,jetbrains+all

.env

+ 11
- 3
Cargo.toml 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
optional=true
package="rusqlite"

[features]
default=['postgres']
postgres=['pg']
sqlite=['slite']

+ 0
- 20
src/drivers.rs 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
- 0
src/drivers/mod.rs 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
- 0
src/drivers/postgres.rs 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
- 0
src/drivers/sqlite.rs View File

@@ -0,0 +1,7 @@
pub struct SQLite;

impl DatabaseDriver for SQLite {
fn query(&self, _query: &str) -> Result<(), ()> {
Ok(())
}
}

+ 8
- 5
src/lib.rs 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;

+ 13
- 7
src/query_builder.rs 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
- 0
src/types.rs View File

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

Loading…
Cancel
Save