pub trait QuerySelect: Sized {
type QueryStatement;
Show 24 methods
// Required method
fn query(&mut self) -> &mut SelectStatement;
// Provided methods
fn select_only(self) -> Self { ... }
fn column<C>(self, col: C) -> Self
where C: ColumnTrait { ... }
fn column_as<C, I>(self, col: C, alias: I) -> Self
where C: IntoSimpleExpr,
I: IntoIdentity { ... }
fn columns<C, I>(self, cols: I) -> Self
where C: ColumnTrait,
I: IntoIterator<Item = C> { ... }
fn offset<T>(self, offset: T) -> Self
where T: Into<Option<u64>> { ... }
fn limit<T>(self, limit: T) -> Self
where T: Into<Option<u64>> { ... }
fn group_by<C>(self, col: C) -> Self
where C: IntoSimpleExpr { ... }
fn having<F>(self, filter: F) -> Self
where F: IntoCondition { ... }
fn distinct(self) -> Self { ... }
fn distinct_on<T, I>(self, cols: I) -> Self
where T: IntoColumnRef,
I: IntoIterator<Item = T> { ... }
fn join(self, join: JoinType, rel: RelationDef) -> Self { ... }
fn join_rev(self, join: JoinType, rel: RelationDef) -> Self { ... }
fn join_as<I>(self, join: JoinType, rel: RelationDef, alias: I) -> Self
where I: IntoIden { ... }
fn join_as_rev<I>(self, join: JoinType, rel: RelationDef, alias: I) -> Self
where I: IntoIden { ... }
fn lock(self, lock_type: LockType) -> Self { ... }
fn lock_shared(self) -> Self { ... }
fn lock_exclusive(self) -> Self { ... }
fn lock_with_behavior(self, type: LockType, behavior: LockBehavior) -> Self { ... }
fn expr<T>(self, expr: T) -> Self
where T: Into<SelectExpr> { ... }
fn exprs<T, I>(self, exprs: I) -> Self
where T: Into<SelectExpr>,
I: IntoIterator<Item = T> { ... }
fn expr_as<T, A>(self, expr: T, alias: A) -> Self
where T: Into<SimpleExpr>,
A: IntoIdentity { ... }
fn expr_as_<T, A>(self, expr: T, alias: A) -> Self
where T: Into<SimpleExpr>,
A: IntoIdentity { ... }
fn tbl_col_as<T, C, A>(self, (tbl, col): (T, C), alias: A) -> Self
where T: IntoIden + 'static,
C: IntoIden + 'static,
A: IntoIdentity { ... }
}
Expand description
Abstract API for performing queries
Required Associated Typesยง
type QueryStatement
Required Methodsยง
Sourcefn query(&mut self) -> &mut SelectStatement
fn query(&mut self) -> &mut SelectStatement
Add the select SQL statement
Provided Methodsยง
Sourcefn select_only(self) -> Self
fn select_only(self) -> Self
Clear the selection list
Sourcefn column<C>(self, col: C) -> Selfwhere
C: ColumnTrait,
fn column<C>(self, col: C) -> Selfwhere
C: ColumnTrait,
Add a select column
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.select_only()
.column(cake::Column::Name)
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."name" FROM "cake""#
);
Enum column will be casted into text (PostgreSQL only)
use sea_orm::{entity::*, query::*, tests_cfg::lunch_set, DbBackend};
assert_eq!(
lunch_set::Entity::find()
.select_only()
.column(lunch_set::Column::Tea)
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT CAST("lunch_set"."tea" AS text) FROM "lunch_set""#
);
assert_eq!(
lunch_set::Entity::find()
.select_only()
.column(lunch_set::Column::Tea)
.build(DbBackend::MySql)
.to_string(),
r#"SELECT `lunch_set`.`tea` FROM `lunch_set`"#
);
Sourcefn column_as<C, I>(self, col: C, alias: I) -> Selfwhere
C: IntoSimpleExpr,
I: IntoIdentity,
fn column_as<C, I>(self, col: C, alias: I) -> Selfwhere
C: IntoSimpleExpr,
I: IntoIdentity,
Add a select column with alias
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.select_only()
.column_as(cake::Column::Id.count(), "count")
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT COUNT("cake"."id") AS "count" FROM "cake""#
);
Sourcefn columns<C, I>(self, cols: I) -> Selfwhere
C: ColumnTrait,
I: IntoIterator<Item = C>,
fn columns<C, I>(self, cols: I) -> Selfwhere
C: ColumnTrait,
I: IntoIterator<Item = C>,
Select columns
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.select_only()
.columns([cake::Column::Id, cake::Column::Name])
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."id", "cake"."name" FROM "cake""#
);
Conditionally select all columns expect a specific column
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.select_only()
.columns(cake::Column::iter().filter(|col| match col {
cake::Column::Id => false,
_ => true,
}))
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."name" FROM "cake""#
);
Enum column will be casted into text (PostgreSQL only)
use sea_orm::{entity::*, query::*, tests_cfg::lunch_set, DbBackend};
assert_eq!(
lunch_set::Entity::find()
.select_only()
.columns([lunch_set::Column::Name, lunch_set::Column::Tea])
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "lunch_set"."name", CAST("lunch_set"."tea" AS text) FROM "lunch_set""#
);
assert_eq!(
lunch_set::Entity::find()
.select_only()
.columns([lunch_set::Column::Name, lunch_set::Column::Tea])
.build(DbBackend::MySql)
.to_string(),
r#"SELECT `lunch_set`.`name`, `lunch_set`.`tea` FROM `lunch_set`"#
);
Sourcefn offset<T>(self, offset: T) -> Self
fn offset<T>(self, offset: T) -> Self
Add an offset expression. Passing in None would remove the offset.
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.offset(10)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` OFFSET 10"
);
assert_eq!(
cake::Entity::find()
.offset(Some(10))
.offset(Some(20))
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` OFFSET 20"
);
assert_eq!(
cake::Entity::find()
.offset(10)
.offset(None)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`"
);
Sourcefn limit<T>(self, limit: T) -> Self
fn limit<T>(self, limit: T) -> Self
Add a limit expression. Passing in None would remove the limit.
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.limit(10)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` LIMIT 10"
);
assert_eq!(
cake::Entity::find()
.limit(Some(10))
.limit(Some(20))
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` LIMIT 20"
);
assert_eq!(
cake::Entity::find()
.limit(10)
.limit(None)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`"
);
Sourcefn group_by<C>(self, col: C) -> Selfwhere
C: IntoSimpleExpr,
fn group_by<C>(self, col: C) -> Selfwhere
C: IntoSimpleExpr,
Add a group by column
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.select_only()
.column(cake::Column::Name)
.group_by(cake::Column::Name)
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."name" FROM "cake" GROUP BY "cake"."name""#
);
assert_eq!(
cake::Entity::find()
.select_only()
.column_as(cake::Column::Id.count(), "count")
.column_as(cake::Column::Id.sum(), "sum_of_id")
.group_by(cake::Column::Name)
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT COUNT("cake"."id") AS "count", SUM("cake"."id") AS "sum_of_id" FROM "cake" GROUP BY "cake"."name""#
);
Sourcefn having<F>(self, filter: F) -> Selfwhere
F: IntoCondition,
fn having<F>(self, filter: F) -> Selfwhere
F: IntoCondition,
Add an AND HAVING expression
use sea_orm::{sea_query::{Alias, Expr}, entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.having(cake::Column::Id.eq(4))
.having(cake::Column::Id.eq(5))
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` HAVING `cake`.`id` = 4 AND `cake`.`id` = 5"
);
assert_eq!(
cake::Entity::find()
.select_only()
.column_as(cake::Column::Id.count(), "count")
.column_as(cake::Column::Id.sum(), "sum_of_id")
.group_by(cake::Column::Name)
.having(Expr::col(Alias::new("count")).gt(6))
.build(DbBackend::MySql)
.to_string(),
"SELECT COUNT(`cake`.`id`) AS `count`, SUM(`cake`.`id`) AS `sum_of_id` FROM `cake` GROUP BY `cake`.`name` HAVING `count` > 6"
);
Sourcefn distinct(self) -> Self
fn distinct(self) -> Self
Add a DISTINCT expression
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
struct Input {
name: Option<String>,
}
let input = Input {
name: Some("cheese".to_owned()),
};
assert_eq!(
cake::Entity::find()
.filter(
Condition::all().add_option(input.name.map(|n| cake::Column::Name.contains(&n)))
)
.distinct()
.build(DbBackend::MySql)
.to_string(),
"SELECT DISTINCT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'"
);
Sourcefn distinct_on<T, I>(self, cols: I) -> Selfwhere
T: IntoColumnRef,
I: IntoIterator<Item = T>,
fn distinct_on<T, I>(self, cols: I) -> Selfwhere
T: IntoColumnRef,
I: IntoIterator<Item = T>,
Add a DISTINCT ON expression
NOTE: this function is only supported by sqlx-postgres
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
struct Input {
name: Option<String>,
}
let input = Input {
name: Some("cheese".to_owned()),
};
assert_eq!(
cake::Entity::find()
.filter(
Condition::all().add_option(input.name.map(|n| cake::Column::Name.contains(&n)))
)
.distinct_on([(cake::Entity, cake::Column::Name)])
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT DISTINCT ON ("cake"."name") "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."name" LIKE '%cheese%'"#
);
Sourcefn join(self, join: JoinType, rel: RelationDef) -> Self
fn join(self, join: JoinType, rel: RelationDef) -> Self
Join via RelationDef
.
Sourcefn join_rev(self, join: JoinType, rel: RelationDef) -> Self
fn join_rev(self, join: JoinType, rel: RelationDef) -> Self
Join via RelationDef
but in reverse direction.
Assume when there exist a relation A to B.
You can reverse join B from A.
Sourcefn join_as<I>(self, join: JoinType, rel: RelationDef, alias: I) -> Selfwhere
I: IntoIden,
fn join_as<I>(self, join: JoinType, rel: RelationDef, alias: I) -> Selfwhere
I: IntoIden,
Join via RelationDef
with table alias.
Sourcefn join_as_rev<I>(self, join: JoinType, rel: RelationDef, alias: I) -> Selfwhere
I: IntoIden,
fn join_as_rev<I>(self, join: JoinType, rel: RelationDef, alias: I) -> Selfwhere
I: IntoIden,
Join via RelationDef
with table alias but in reverse direction.
Assume when there exist a relation A to B.
You can reverse join B from A.
Select lock shared
Sourcefn lock_exclusive(self) -> Self
fn lock_exclusive(self) -> Self
Select lock exclusive
Sourcefn lock_with_behavior(self, type: LockType, behavior: LockBehavior) -> Self
fn lock_with_behavior(self, type: LockType, behavior: LockBehavior) -> Self
Row locking with behavior (if supported).
Sourcefn expr<T>(self, expr: T) -> Selfwhere
T: Into<SelectExpr>,
fn expr<T>(self, expr: T) -> Selfwhere
T: Into<SelectExpr>,
Add an expression to the select expression list.
use sea_orm::sea_query::Expr;
use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait};
assert_eq!(
cake::Entity::find()
.select_only()
.expr(Expr::col((cake::Entity, cake::Column::Id)))
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id` FROM `cake`"
);
Sourcefn exprs<T, I>(self, exprs: I) -> Self
fn exprs<T, I>(self, exprs: I) -> Self
Add select expressions from vector of SelectExpr
.
use sea_orm::sea_query::Expr;
use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait};
assert_eq!(
cake::Entity::find()
.select_only()
.exprs([
Expr::col((cake::Entity, cake::Column::Id)),
Expr::col((cake::Entity, cake::Column::Name)),
])
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`"
);
Sourcefn expr_as<T, A>(self, expr: T, alias: A) -> Self
fn expr_as<T, A>(self, expr: T, alias: A) -> Self
Select column.
use sea_orm::sea_query::{Alias, Expr, Func};
use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait};
assert_eq!(
cake::Entity::find()
.expr_as(
Func::upper(Expr::col((cake::Entity, cake::Column::Name))),
"name_upper"
)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name`, UPPER(`cake`.`name`) AS `name_upper` FROM `cake`"
);
Sourcefn expr_as_<T, A>(self, expr: T, alias: A) -> Self
fn expr_as_<T, A>(self, expr: T, alias: A) -> Self
Same as expr_as
. Here for legacy reasons.
Select column.
use sea_orm::sea_query::{Alias, Expr, Func};
use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait};
assert_eq!(
cake::Entity::find()
.expr_as(
Func::upper(Expr::col((cake::Entity, cake::Column::Name))),
"name_upper"
)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name`, UPPER(`cake`.`name`) AS `name_upper` FROM `cake`"
);
Sourcefn tbl_col_as<T, C, A>(self, (tbl, col): (T, C), alias: A) -> Self
fn tbl_col_as<T, C, A>(self, (tbl, col): (T, C), alias: A) -> Self
Shorthand of expr_as(Expr::col((T, C)), A)
.
use sea_orm::sea_query::{Alias, Expr, Func};
use sea_orm::{entity::*, tests_cfg::cake, DbBackend, QuerySelect, QueryTrait};
assert_eq!(
cake::Entity::find()
.select_only()
.tbl_col_as((cake::Entity, cake::Column::Name), "cake_name")
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`name` AS `cake_name` FROM `cake`"
);
Dyn Compatibilityยง
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.