Rework driver specific stuff a bit

This commit is contained in:
Timothy Warren 2019-04-05 14:51:31 -04:00
parent 3865fd56b2
commit f11b55b73d
5 changed files with 27 additions and 10 deletions

View File

@ -29,6 +29,12 @@ struct DriverBase {
///
/// Interface between the database connection library and the query builder
pub trait DatabaseDriver: fmt::Debug {
/// Get which characters are used to delimit identifiers
/// such as tables, and columns
fn _quotes(&self) -> (char, char) {
('"','"')
}
/// Vector version of `quote_identifier`
fn quote_identifiers(&self, identifiers: Vec<String>) -> Vec<String> {
let mut output: Vec<String> = vec![];
@ -42,7 +48,9 @@ pub trait DatabaseDriver: fmt::Debug {
/// Quote the identifiers passed, so the database does not
/// normalize the identifiers (eg, table, column, etc.)
fn quote_identifier(&self, identifier: &str) -> String;
fn quote_identifier(&self, identifier: &str) -> String {
identifier.to_string()
}
/// Runs a basic sql query on the database
fn query(&self, query: &str) -> Result<(), ()>;

View File

@ -4,8 +4,10 @@ use super::*;
pub struct MySQL;
impl DatabaseDriver for MySQL {
fn quote_identifier(&self, identifier: &str) -> String {
String::from(identifier)
/// Get which characters are used to delimit identifiers
/// such as tables, and columns
fn _quotes(&self) -> (char, char) {
('`','`')
}
fn query(&self, _query: &str) -> Result<(), ()> {

View File

@ -4,10 +4,6 @@ use super::*;
pub struct Postgres;
impl DatabaseDriver for Postgres {
fn quote_identifier(&self, identifier: &str) -> String {
String::from(identifier)
}
fn query(&self, _query: &str) -> Result<(), ()> {
Ok(())
}

View File

@ -12,7 +12,7 @@ fn main() {
.set("buzz", Box::new((1, 2.0, true, 'q')));
// This just makes me sad
qb.r#where("foo", Box::new(2));
qb.r#where("foo", "<>", Box::new(2));
println!("QueryBuilder object: {:#?}", &qb);

View File

@ -155,11 +155,17 @@ impl QueryBuilder {
// ! Select Queries
// --------------------------------------------------------------------------
/// Set the fields to select from the database
/// Set the fields to select from the database as a string
pub fn select(&mut self, fields: &str) -> &mut Self {
unimplemented!();
}
/// Set the fields to select from the database as a Vector
pub fn select_vec(&mut self, fields: Vec<&str>) -> &mut Self {
let fields = fields.join(",");
self.select(&fields)
}
/// Adds the `distinct` keyword to a query
pub fn distinct(&mut self) -> &mut Self {
unimplemented!();
@ -231,13 +237,18 @@ impl QueryBuilder {
// --------------------------------------------------------------------------
/// Specify a condition for the `where` clause of the query
pub fn r#where(&mut self, key: &str, value: Box<dyn Any>) -> &mut Self {
pub fn r#where(&mut self, key: &str, op: &str, value: Box<dyn Any>) -> &mut Self {
// @TODO actually implement setting the keys for the where
self.state.where_values.push(value);
self
}
// Specify a condition for a `where` clause where a column has a value
pub fn where_eq(&mut self, key: &str, value: Box<dyn Any>) -> &mut Self {
self.r#where(key, "=", value)
}
/// Specify a condition for the `where` clause of the query, prefixed with `or`
pub fn or_where(&mut self, key: &str, value: Box<dyn Any>) -> &mut Self {
unimplemented!();