sea-orm/src/query/select.rs
Chris Tsang f54683d365
Refactor/fix clippy errors (#2056) (#2057)
* chore: add clippy config file

* refactor: fix clippy errors and wornings of runtime-async-std-native-tls,sqlx-all

* refactor: fix clippy errors and wornings of sqlx-sqlite, sqlx-mysql, sqlx-postgres

* chore: format

* refactor: fix clippy

* fix: import path

* refactor: fix clippy errors and wornings of sqlx-sqlite, sqlx-mysql, sqlx-postgres

* fix: revert some space and comma removal

* fix: revert some space and comma removal

* refactor: add feature flag

* fix: import path

* test: remove mismatch feature flag

* test: remove mismatch feature flag

* chore: add proper feature flag

* chore: remove feature flag

* refactor: remove clippy.toml file

* fix: re-export driver

* fix: re-export JoinType

* fix: remove feature flag

* chore: add #[allow(unused_imports)] for driver

Co-authored-by: Shogo Nakano <61229807+shogo-nakano-desu@users.noreply.github.com>
2024-01-11 00:21:22 +08:00

177 lines
3.9 KiB
Rust

use crate::{ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySelect, QueryTrait};
use core::fmt::Debug;
use core::marker::PhantomData;
use sea_query::{Expr, IntoColumnRef, SelectStatement, SimpleExpr};
/// Defines a structure to perform select operations
#[derive(Clone, Debug)]
pub struct Select<E>
where
E: EntityTrait,
{
pub(crate) query: SelectStatement,
pub(crate) entity: PhantomData<E>,
}
/// Defines a structure to perform a SELECT operation on two Models
#[derive(Clone, Debug)]
pub struct SelectTwo<E, F>
where
E: EntityTrait,
F: EntityTrait,
{
pub(crate) query: SelectStatement,
pub(crate) entity: PhantomData<(E, F)>,
}
/// Defines a structure to perform a SELECT operation on many Models
#[derive(Clone, Debug)]
pub struct SelectTwoMany<E, F>
where
E: EntityTrait,
F: EntityTrait,
{
pub(crate) query: SelectStatement,
pub(crate) entity: PhantomData<(E, F)>,
}
/// Performs a conversion to [SimpleExpr]
pub trait IntoSimpleExpr {
/// Method to perform the conversion
fn into_simple_expr(self) -> SimpleExpr;
}
macro_rules! impl_trait {
( $trait: ident ) => {
impl<E> $trait for Select<E>
where
E: EntityTrait,
{
type QueryStatement = SelectStatement;
fn query(&mut self) -> &mut SelectStatement {
&mut self.query
}
}
impl<E, F> $trait for SelectTwo<E, F>
where
E: EntityTrait,
F: EntityTrait,
{
type QueryStatement = SelectStatement;
fn query(&mut self) -> &mut SelectStatement {
&mut self.query
}
}
impl<E, F> $trait for SelectTwoMany<E, F>
where
E: EntityTrait,
F: EntityTrait,
{
type QueryStatement = SelectStatement;
fn query(&mut self) -> &mut SelectStatement {
&mut self.query
}
}
};
}
impl_trait!(QuerySelect);
impl_trait!(QueryFilter);
impl_trait!(QueryOrder);
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 Expr {
fn into_simple_expr(self) -> SimpleExpr {
self.into()
}
}
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.exprs(self.column_list());
self
}
fn column_list(&self) -> Vec<SimpleExpr> {
E::Column::iter()
.map(|col| col.select_as(col.into_expr()))
.collect()
}
fn prepare_from(mut self) -> Self {
self.query.from(E::default().table_ref());
self
}
}
impl<E> QueryTrait for Select<E>
where
E: EntityTrait,
{
type QueryStatement = SelectStatement;
fn query(&mut self) -> &mut SelectStatement {
&mut self.query
}
fn as_query(&self) -> &SelectStatement {
&self.query
}
fn into_query(self) -> SelectStatement {
self.query
}
}
macro_rules! select_two {
( $selector: ident ) => {
impl<E, F> QueryTrait for $selector<E, F>
where
E: EntityTrait,
F: EntityTrait,
{
type QueryStatement = SelectStatement;
fn query(&mut self) -> &mut SelectStatement {
&mut self.query
}
fn as_query(&self) -> &SelectStatement {
&self.query
}
fn into_query(self) -> SelectStatement {
self.query
}
}
};
}
select_two!(SelectTwo);
select_two!(SelectTwoMany);