use crate::table::{Table, TableQueryable};
use crate::types::bind::{Binder, Binding};
use crate::ToSql;
pub mod delete;
use delete::Delete;
pub mod filter;
use filter::Filter;
pub use filter::Filterable;
pub mod insert;
pub use insert::Insertable;
use insert::{InsertSelect, InsertStatement, Values};
pub mod predicate;
pub use predicate::Predicate;
use predicate::{And, Or};
pub mod prepare;
use prepare::Prepare;
pub mod select;
use select::queryable::{Count, WildCard, WriteQueryable};
use select::{GroupBy, GroupOrder, Limit, Order, OrderBy, SelectStatement, Selectable};
pub use select::{Join, Joined, Queryable, Select};
pub mod update;
use update::{Update, UpdateSet};
pub trait Query: Sized {
fn prepare<F, S>(name: &str, f: F) -> Prepare<Self, S>
where
Self: Binding,
F: FnOnce(Self::Bindings) -> S,
S: ToSql,
{
let bindings = Self::bindings(&mut Binder::default());
Prepare::new(name, f(bindings))
}
fn select(self) -> SelectStatement<Self, WildCard>
where
Self: Selectable,
{
self.query(WildCard)
}
fn query<Q>(self, query: Q) -> SelectStatement<Self, Q>
where
Self: Selectable,
Q: WriteQueryable,
{
SelectStatement::new(self, query)
}
fn count<F, T>(self, f: F) -> SelectStatement<Self, Count<T>>
where
Self: Selectable,
F: FnOnce(Self::Fields) -> T,
Count<T>: WriteQueryable,
{
self.query(Count::new(f(Default::default())))
}
fn insert<I>(self, value: I) -> InsertStatement<Self::Table, I>
where
Self: TableQueryable,
I: Insertable,
{
InsertStatement::new(value)
}
fn insert_values<I>(self, values: I) -> InsertStatement<Self::Table, Values<I>>
where
Self: TableQueryable,
I: IntoIterator + Clone,
I::Item: Insertable,
{
InsertStatement::new(Values::new(values))
}
fn insert_select<S, I>(self, select: S) -> InsertStatement<Self::Table, InsertSelect<S, I>>
where
Self: TableQueryable,
S: Select,
I: Insertable,
{
InsertStatement::new(InsertSelect::new(select))
}
fn update<F, S>(self, f: F) -> Update<Self::Table, S>
where
Self: TableQueryable,
F: FnOnce(<Self::Table as Table>::Fields) -> S,
S: UpdateSet,
{
Update::new(f(Default::default()))
}
fn delete(self) -> Delete<Self::Table>
where
Self: TableQueryable,
{
Delete::new()
}
fn filter<F, P>(self, f: F) -> Filter<Self, P>
where
Self: Filterable,
F: FnOnce(Self::Fields) -> P,
{
Filter::new(self, f(Default::default()))
}
fn and<P>(self, predicate: P) -> And<Self, P>
where
Self: Predicate,
P: Predicate,
{
And {
head: self,
tail: predicate,
}
}
fn or<P>(self, predicate: P) -> Or<Self, P>
where
Self: Predicate,
P: Predicate,
{
Or {
head: self,
tail: predicate,
}
}
fn group_by<F, O>(self, f: F) -> GroupBy<Self, O>
where
Self: Select,
F: FnOnce(<Self::Selectable as Selectable>::Fields) -> O,
O: GroupOrder,
{
GroupBy::new(self, f(Default::default()))
}
fn order_by<F, O>(self, f: F) -> OrderBy<Self, O>
where
Self: Select,
F: FnOnce(<Self::Selectable as Selectable>::Fields) -> O,
O: Order,
{
OrderBy::new(self, f(Default::default()))
}
fn limit(self, limit: usize) -> Limit<Self>
where
Self: Select,
{
Limit::new(self, limit)
}
}
impl<T> Query for T {}