Messing with types

This commit is contained in:
Timothy Warren 2019-04-26 16:38:34 -04:00
parent 9d71dc641d
commit f9569a6340
4 changed files with 56 additions and 10 deletions

View File

@ -79,7 +79,7 @@ pub trait DatabaseDriver {
if identifier.contains(",") { if identifier.contains(",") {
// This was the only way I could figure to get // This was the only way I could figure to get
// around mutable string reference scope hell // around mutable string reference scope hell
let func = |part| self.quote_identifier(part.trim()); let func = |part: &str| self.quote_identifier(part.trim());
identifier.replace_range(.., &split_map_join(identifier, ",", func)); identifier.replace_range(.., &split_map_join(identifier, ",", func));
} }
@ -110,7 +110,10 @@ pub trait DatabaseDriver {
} }
// Runs a prepared statement on the database // Runs a prepared statement on the database
// fn execute(&self, sql: &str, ?) -> Result<?,?> // fn execute(&self, sql: &str, ?) -> Result<?,?>;
// Prepares and executes an sql query
// fn prepare_execute(&self, sql: &str, params: &[?]) -> Result<?, ?>;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// ! Driver-specific SQL methods // ! Driver-specific SQL methods
@ -124,7 +127,7 @@ pub trait DatabaseDriver {
sql += &format!("\nLIMIT {}", limit.unwrap()); sql += &format!("\nLIMIT {}", limit.unwrap());
} }
if offset.is_some() { if limit.is_some() && offset.is_some() {
sql += &format!(" OFFSET {}", offset.unwrap()); sql += &format!(" OFFSET {}", offset.unwrap());
} }

View File

@ -7,7 +7,7 @@
//! use stringqb::prelude::*; //! use stringqb::prelude::*;
//! //!
//! // Create a QueryBuilder object, with the chosen database driver //! // Create a QueryBuilder object, with the chosen database driver
//! let qb = QueryBuilder::new(PostgresDriver::new("postgresql://user@localhost")); //! let mut qb = QueryBuilder::new(PostgresDriver::new("postgresql://user@localhost"));
//! //!
//! ``` //! ```
//! //!

View File

@ -329,7 +329,6 @@ impl QueryBuilder {
/// Set a key and value for an insert or update query /// Set a key and value for an insert or update query
pub fn set(&mut self, key: &str, value: impl Any) -> &mut Self { pub fn set(&mut self, key: &str, value: impl Any) -> &mut Self {
// @TODO figure a way to make this easier to use
let key = self.driver.quote_identifier(key); let key = self.driver.quote_identifier(key);
self.state self.state
.append_set_array_keys(&key) .append_set_array_keys(&key)

View File

@ -42,6 +42,7 @@
//! //!
use std::any::Any; use std::any::Any;
use std::borrow::Cow; use std::borrow::Cow;
use std::error::Error;
/// Empty struct to represent Null values /// Empty struct to represent Null values
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -77,15 +78,58 @@ pub enum ValueRef<'a> {
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum ToSqlOutput<'a> { pub enum ToDriverOutput<'a> {
Borrowed(ValueRef<'a>), Borrowed(ValueRef<'a>),
Owned(Value), Owned(Value),
} }
/// Types that can be converted to SQL // Generically allow any type that can be converted into a ValueRef
//pub trait ToSql { // to be converted into a ToSqlOutput as well.
// fn to_sql(&self) -> Result<ToSqlOutput<'_>>; impl<'a, T: ?Sized> From<&'a T> for ToDriverOutput<'a>
where
&'a T: Into<ValueRef<'a>>,
{
fn from(t: &'a T) -> Self {
ToDriverOutput::Borrowed(t.into())
}
}
// We cannot also generically allow any type that can be converted
// into a Value to be converted into a ToDriverOutput because of
// coherence rules (https://github.com/rust-lang/rust/pull/46192),
// so we'll manually implement it for all the types we know can
// be converted into Values.
//macro_rules! from_value(
// ($t:ty) => (
// impl From<$t> for ToDriverOutput<'_> {
// fn from(t: $t) -> Self { ToDriverOutput::Owned(t.into())}
// } // }
// )
//);
//from_value!(String);
//from_value!(Null);
//from_value!(bool);
//from_value!(i8);
//from_value!(i16);
//from_value!(i32);
//from_value!(i64);
//from_value!(isize);
//from_value!(u8);
//from_value!(u16);
//from_value!(u32);
//from_value!(f64);
//from_value!(Vec<u8>);
/// Types that can be converted to a type that the driver understands
pub trait ToDriver {
fn to_driver(&self) -> Result<ToDriverOutput<'_>, ()>;
}
/// A trait for types that can be created from the result of a query on the driver
pub trait FromDriver: Sized {
}
/// Enum struct for mapping between database and Rust types /// Enum struct for mapping between database and Rust types
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]