126 lines
2.6 KiB
Rust
126 lines
2.6 KiB
Rust
use crate::{ColumnTrait, EntityTrait, Iterable, QueryTrait, SelectHelper};
|
|
use core::fmt::Debug;
|
|
use core::marker::PhantomData;
|
|
pub use sea_query::JoinType;
|
|
use sea_query::{Iden, IntoColumnRef, IntoIden, SelectStatement, SimpleExpr};
|
|
use std::rc::Rc;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Select<E>
|
|
where
|
|
E: EntityTrait,
|
|
{
|
|
pub(crate) query: SelectStatement,
|
|
pub(crate) entity: PhantomData<E>,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct SelectTwo<E, F>
|
|
where
|
|
E: EntityTrait,
|
|
F: EntityTrait,
|
|
{
|
|
pub(crate) query: SelectStatement,
|
|
pub(crate) entity: PhantomData<(E, F)>,
|
|
}
|
|
|
|
pub trait IntoSimpleExpr {
|
|
fn into_simple_expr(self) -> SimpleExpr;
|
|
}
|
|
|
|
impl<E> SelectHelper for Select<E>
|
|
where
|
|
E: EntityTrait,
|
|
{
|
|
fn query(&mut self) -> &mut SelectStatement {
|
|
&mut self.query
|
|
}
|
|
}
|
|
|
|
impl<E, F> SelectHelper for SelectTwo<E, F>
|
|
where
|
|
E: EntityTrait,
|
|
F: EntityTrait,
|
|
{
|
|
fn query(&mut self) -> &mut SelectStatement {
|
|
&mut self.query
|
|
}
|
|
}
|
|
|
|
impl<C> IntoSimpleExpr for C
|
|
where
|
|
C: ColumnTrait,
|
|
{
|
|
fn into_simple_expr(self) -> SimpleExpr {
|
|
SimpleExpr::Column(self.as_column_ref().into_column_ref())
|
|
}
|
|
}
|
|
|
|
impl IntoSimpleExpr for SimpleExpr {
|
|
fn into_simple_expr(self) -> SimpleExpr {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<E> Select<E>
|
|
where
|
|
E: EntityTrait,
|
|
{
|
|
pub(crate) fn new() -> Self {
|
|
Self {
|
|
query: SelectStatement::new(),
|
|
entity: PhantomData,
|
|
}
|
|
.prepare_select()
|
|
.prepare_from()
|
|
}
|
|
|
|
fn prepare_select(mut self) -> Self {
|
|
self.query.columns(self.column_list());
|
|
self
|
|
}
|
|
|
|
fn column_list(&self) -> Vec<(Rc<dyn Iden>, E::Column)> {
|
|
let table = Rc::new(E::default()) as Rc<dyn Iden>;
|
|
E::Column::iter().map(|col| (table.clone(), col)).collect()
|
|
}
|
|
|
|
fn prepare_from(mut self) -> Self {
|
|
self.query.from(E::default().into_iden());
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<E> QueryTrait for Select<E>
|
|
where
|
|
E: EntityTrait,
|
|
{
|
|
type QueryStatementBuilder = SelectStatement;
|
|
fn query(&mut self) -> &mut SelectStatement {
|
|
&mut self.query
|
|
}
|
|
fn as_query(&self) -> &SelectStatement {
|
|
&self.query
|
|
}
|
|
fn into_query(self) -> SelectStatement {
|
|
self.query
|
|
}
|
|
}
|
|
|
|
impl<E, F> QueryTrait for SelectTwo<E, F>
|
|
where
|
|
E: EntityTrait,
|
|
F: EntityTrait,
|
|
{
|
|
type QueryStatementBuilder = SelectStatement;
|
|
fn query(&mut self) -> &mut SelectStatement {
|
|
&mut self.query
|
|
}
|
|
fn as_query(&self) -> &SelectStatement {
|
|
&self.query
|
|
}
|
|
fn into_query(self) -> SelectStatement {
|
|
self.query
|
|
}
|
|
}
|