Rework driver specific stuff a bit
This commit is contained in:
parent
3865fd56b2
commit
f11b55b73d
@ -29,6 +29,12 @@ struct DriverBase {
|
|||||||
///
|
///
|
||||||
/// Interface between the database connection library and the query builder
|
/// Interface between the database connection library and the query builder
|
||||||
pub trait DatabaseDriver: fmt::Debug {
|
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`
|
/// Vector version of `quote_identifier`
|
||||||
fn quote_identifiers(&self, identifiers: Vec<String>) -> Vec<String> {
|
fn quote_identifiers(&self, identifiers: Vec<String>) -> Vec<String> {
|
||||||
let mut output: Vec<String> = vec![];
|
let mut output: Vec<String> = vec![];
|
||||||
@ -42,7 +48,9 @@ pub trait DatabaseDriver: fmt::Debug {
|
|||||||
|
|
||||||
/// Quote the identifiers passed, so the database does not
|
/// Quote the identifiers passed, so the database does not
|
||||||
/// normalize the identifiers (eg, table, column, etc.)
|
/// 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
|
/// Runs a basic sql query on the database
|
||||||
fn query(&self, query: &str) -> Result<(), ()>;
|
fn query(&self, query: &str) -> Result<(), ()>;
|
||||||
|
@ -4,8 +4,10 @@ use super::*;
|
|||||||
pub struct MySQL;
|
pub struct MySQL;
|
||||||
|
|
||||||
impl DatabaseDriver for MySQL {
|
impl DatabaseDriver for MySQL {
|
||||||
fn quote_identifier(&self, identifier: &str) -> String {
|
/// Get which characters are used to delimit identifiers
|
||||||
String::from(identifier)
|
/// such as tables, and columns
|
||||||
|
fn _quotes(&self) -> (char, char) {
|
||||||
|
('`','`')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query(&self, _query: &str) -> Result<(), ()> {
|
fn query(&self, _query: &str) -> Result<(), ()> {
|
||||||
|
@ -4,10 +4,6 @@ use super::*;
|
|||||||
pub struct Postgres;
|
pub struct Postgres;
|
||||||
|
|
||||||
impl DatabaseDriver for Postgres {
|
impl DatabaseDriver for Postgres {
|
||||||
fn quote_identifier(&self, identifier: &str) -> String {
|
|
||||||
String::from(identifier)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn query(&self, _query: &str) -> Result<(), ()> {
|
fn query(&self, _query: &str) -> Result<(), ()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ fn main() {
|
|||||||
.set("buzz", Box::new((1, 2.0, true, 'q')));
|
.set("buzz", Box::new((1, 2.0, true, 'q')));
|
||||||
|
|
||||||
// This just makes me sad
|
// This just makes me sad
|
||||||
qb.r#where("foo", Box::new(2));
|
qb.r#where("foo", "<>", Box::new(2));
|
||||||
|
|
||||||
println!("QueryBuilder object: {:#?}", &qb);
|
println!("QueryBuilder object: {:#?}", &qb);
|
||||||
|
|
||||||
|
@ -155,11 +155,17 @@ impl QueryBuilder {
|
|||||||
// ! Select Queries
|
// ! 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 {
|
pub fn select(&mut self, fields: &str) -> &mut Self {
|
||||||
unimplemented!();
|
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
|
/// Adds the `distinct` keyword to a query
|
||||||
pub fn distinct(&mut self) -> &mut Self {
|
pub fn distinct(&mut self) -> &mut Self {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
@ -231,13 +237,18 @@ impl QueryBuilder {
|
|||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Specify a condition for the `where` clause of the query
|
/// 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
|
// @TODO actually implement setting the keys for the where
|
||||||
self.state.where_values.push(value);
|
self.state.where_values.push(value);
|
||||||
|
|
||||||
self
|
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`
|
/// 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 {
|
pub fn or_where(&mut self, key: &str, value: Box<dyn Any>) -> &mut Self {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
|
Loading…
Reference in New Issue
Block a user