Support subquery
This commit is contained in:
parent
571210748f
commit
6224bb6d5f
@ -1,5 +1,5 @@
|
|||||||
use crate::{EntityName, IdenStatic, Iterable};
|
use crate::{EntityName, IdenStatic, Iterable};
|
||||||
use sea_query::{DynIden, Expr, SeaRc, SimpleExpr, Value};
|
use sea_query::{DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ColumnDef {
|
pub struct ColumnDef {
|
||||||
@ -66,6 +66,15 @@ macro_rules! bind_vec_func {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! bind_subquery_func {
|
||||||
|
( $func: ident ) => {
|
||||||
|
#[allow(clippy::wrong_self_convention)]
|
||||||
|
fn $func(&self, s: SelectStatement) -> SimpleExpr {
|
||||||
|
Expr::tbl(self.entity_name(), *self).$func(s)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// LINT: when the operand value does not match column type
|
// LINT: when the operand value does not match column type
|
||||||
/// Wrapper of the identically named method in [`sea_query::Expr`]
|
/// Wrapper of the identically named method in [`sea_query::Expr`]
|
||||||
pub trait ColumnTrait: IdenStatic + Iterable {
|
pub trait ColumnTrait: IdenStatic + Iterable {
|
||||||
@ -216,6 +225,9 @@ pub trait ColumnTrait: IdenStatic + Iterable {
|
|||||||
|
|
||||||
bind_vec_func!(is_in);
|
bind_vec_func!(is_in);
|
||||||
bind_vec_func!(is_not_in);
|
bind_vec_func!(is_not_in);
|
||||||
|
|
||||||
|
bind_subquery_func!(in_subquery);
|
||||||
|
bind_subquery_func!(not_in_subquery);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ColumnType {
|
impl ColumnType {
|
||||||
@ -303,3 +315,35 @@ impl From<sea_query::ColumnType> for ColumnType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{
|
||||||
|
tests_cfg::*, ColumnTrait, Condition, DbBackend, EntityTrait, QueryFilter, QueryTrait,
|
||||||
|
};
|
||||||
|
use sea_query::Query;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_in_subquery() {
|
||||||
|
assert_eq!(
|
||||||
|
cake::Entity::find()
|
||||||
|
.filter(
|
||||||
|
Condition::any().add(
|
||||||
|
cake::Column::Id.in_subquery(
|
||||||
|
Query::select()
|
||||||
|
.expr(cake::Column::Id.max())
|
||||||
|
.from(cake::Entity)
|
||||||
|
.to_owned()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.build(DbBackend::MySql)
|
||||||
|
.to_string(),
|
||||||
|
[
|
||||||
|
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
|
||||||
|
"WHERE `cake`.`id` IN (SELECT MAX(`cake`.`id`) FROM `cake`)",
|
||||||
|
]
|
||||||
|
.join(" ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user