Struct sea_query::query::DeleteStatement
source · [−]pub struct DeleteStatement { /* private fields */ }
Expand description
Delete existing rows from the table
Examples
use sea_query::{tests_cfg::*, *};
let query = Query::delete()
.from_table(Glyph::Table)
.or_where(Expr::col(Glyph::Id).lt(1))
.or_where(Expr::col(Glyph::Id).gt(10))
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"DELETE FROM `glyph` WHERE `id` < 1 OR `id` > 10"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" < 1 OR "id" > 10"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" < 1 OR "id" > 10"#
);
Implementations
sourceimpl DeleteStatement
impl DeleteStatement
sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new DeleteStatement
sourcepub fn from_table<T>(&mut self, tbl_ref: T) -> &mut Self where
T: IntoTableRef,
pub fn from_table<T>(&mut self, tbl_ref: T) -> &mut Self where
T: IntoTableRef,
Specify which table to delete from.
Examples
use sea_query::{tests_cfg::*, *};
let query = Query::delete()
.from_table(Glyph::Table)
.and_where(Expr::col(Glyph::Id).eq(1))
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"DELETE FROM `glyph` WHERE `id` = 1"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" = 1"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" = 1"#
);
sourcepub fn returning(&mut self, select: SelectStatement) -> &mut Self
pub fn returning(&mut self, select: SelectStatement) -> &mut Self
RETURNING expressions.
Note:
Works on
- PostgreSQL
- SQLite
- SQLite version >= 3.35.0
- Note that sea-query won’t try to enforce either of these constraints
use sea_query::{tests_cfg::*, *};
let query = Query::delete()
.from_table(Glyph::Table)
.and_where(Expr::col(Glyph::Id).eq(1))
.returning(Query::select().column(Glyph::Id).take())
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"DELETE FROM `glyph` WHERE `id` = 1"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
);
sourcepub fn returning_col<C>(&mut self, col: C) -> &mut Self where
C: IntoIden,
pub fn returning_col<C>(&mut self, col: C) -> &mut Self where
C: IntoIden,
RETURNING a column after delete.
Wrapper over DeleteStatement::returning()
.
Note:
Works on
- PostgreSQL
- SQLite
- SQLite version >= 3.35.0
- Note that sea-query won’t try to enforce either of these constraints
use sea_query::{tests_cfg::*, *};
let query = Query::delete()
.from_table(Glyph::Table)
.and_where(Expr::col(Glyph::Id).eq(1))
.returning_col(Glyph::Id)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"DELETE FROM `glyph` WHERE `id` = 1"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"DELETE FROM "glyph" WHERE "id" = 1 RETURNING "id""#
);
sourcepub fn with(self, clause: WithClause) -> WithQuery
pub fn with(self, clause: WithClause) -> WithQuery
Create a WithQuery by specifying a WithClause to execute this query with.
Examples
use sea_query::{*, IntoCondition, IntoIden, tests_cfg::*};
let select = SelectStatement::new()
.columns([Glyph::Id])
.from(Glyph::Table)
.and_where(Expr::col(Glyph::Image).like("0%"))
.to_owned();
let cte = CommonTableExpression::new()
.query(select)
.column(Glyph::Id)
.table_name(Alias::new("cte"))
.to_owned();
let with_clause = WithClause::new().cte(cte).to_owned();
let update = DeleteStatement::new()
.from_table(Glyph::Table)
.and_where(Expr::col(Glyph::Id).in_subquery(SelectStatement::new().column(Glyph::Id).from(Alias::new("cte")).to_owned()))
.to_owned();
let query = update.with(with_clause);
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"WITH `cte` (`id`) AS (SELECT `id` FROM `glyph` WHERE `image` LIKE '0%') DELETE FROM `glyph` WHERE `id` IN (SELECT `id` FROM `cte`)"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"WITH "cte" ("id") AS (SELECT "id" FROM "glyph" WHERE "image" LIKE '0%') DELETE FROM "glyph" WHERE "id" IN (SELECT "id" FROM "cte")"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"WITH "cte" ("id") AS (SELECT "id" FROM "glyph" WHERE "image" LIKE '0%') DELETE FROM "glyph" WHERE "id" IN (SELECT "id" FROM "cte")"#
);
sourceimpl DeleteStatement
impl DeleteStatement
pub fn order_by<T>(&mut self, col: T, order: Order) -> &mut Self where
T: IntoColumnRef,
pub fn order_by_tbl<T, C>(
&mut self,
table: T,
col: C,
order: Order
) -> &mut Self where
T: IntoIden,
C: IntoIden,
Please use the [OrderedStatement::order_by
] with a tuple as [ColumnRef
]
pub fn order_by_expr(&mut self, expr: SimpleExpr, order: Order) -> &mut Self
pub fn order_by_customs<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: ToString,
pub fn order_by_columns<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: IntoColumnRef,
pub fn order_by_table_columns<T, C>(
&mut self,
cols: Vec<(T, C, Order)>
) -> &mut Self where
T: IntoIden,
C: IntoIden,
Please use the [OrderedStatement::order_by_columns
] with a tuple as [ColumnRef
]
sourceimpl DeleteStatement
impl DeleteStatement
pub fn and_where(&mut self, other: SimpleExpr) -> &mut Self
pub fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self
pub fn or_where(&mut self, other: SimpleExpr) -> &mut Self
Please use [ConditionalStatement::cond_where
]. Calling or_where
after and_where
will panic.
pub fn cond_where<C>(&mut self, condition: C) -> &mut Self where
C: IntoCondition,
Trait Implementations
sourceimpl Clone for DeleteStatement
impl Clone for DeleteStatement
sourcefn clone(&self) -> DeleteStatement
fn clone(&self) -> DeleteStatement
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl ConditionalStatement for DeleteStatement
impl ConditionalStatement for DeleteStatement
sourcefn cond_where<C>(&mut self, condition: C) -> &mut Self where
C: IntoCondition,
fn cond_where<C>(&mut self, condition: C) -> &mut Self where
C: IntoCondition,
Where condition, expressed with any
and all
.
Calling cond_where
multiple times will conjoin them.
Calling or_where
after cond_where
will panic. Read more
sourcefn and_where(&mut self, other: SimpleExpr) -> &mut Self
fn and_where(&mut self, other: SimpleExpr) -> &mut Self
And where condition. This cannot be mixed with ConditionalStatement::or_where
.
Calling or_where
after and_where
will panic. Read more
sourcefn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self
fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self
Optional and where, short hand for if c.is_some() q.and_where(c)
. Read more
sourcefn or_where(&mut self, other: SimpleExpr) -> &mut Self
fn or_where(&mut self, other: SimpleExpr) -> &mut Self
Please use [ConditionalStatement::cond_where
]. Calling or_where
after and_where
will panic.
Or where condition. This cannot be mixed with ConditionalStatement::and_where
.
Calling or_where
after and_where
will panic. Read more
sourceimpl Debug for DeleteStatement
impl Debug for DeleteStatement
sourceimpl Default for DeleteStatement
impl Default for DeleteStatement
sourceimpl OrderedStatement for DeleteStatement
impl OrderedStatement for DeleteStatement
sourcefn order_by<T>(&mut self, col: T, order: Order) -> &mut Self where
T: IntoColumnRef,
fn order_by<T>(&mut self, col: T, order: Order) -> &mut Self where
T: IntoColumnRef,
Order by column. Read more
sourcefn order_by_tbl<T, C>(&mut self, table: T, col: C, order: Order) -> &mut Self where
T: IntoIden,
C: IntoIden,
fn order_by_tbl<T, C>(&mut self, table: T, col: C, order: Order) -> &mut Self where
T: IntoIden,
C: IntoIden,
Please use the [OrderedStatement::order_by
] with a tuple as [ColumnRef
]
sourcefn order_by_expr(&mut self, expr: SimpleExpr, order: Order) -> &mut Self
fn order_by_expr(&mut self, expr: SimpleExpr, order: Order) -> &mut Self
Order by SimpleExpr
.
sourcefn order_by_customs<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: ToString,
fn order_by_customs<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: ToString,
Order by custom string.
sourcefn order_by_columns<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: IntoColumnRef,
fn order_by_columns<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: IntoColumnRef,
Order by vector of columns.
sourcefn order_by_table_columns<T, C>(
&mut self,
cols: Vec<(T, C, Order)>
) -> &mut Self where
T: IntoIden,
C: IntoIden,
fn order_by_table_columns<T, C>(
&mut self,
cols: Vec<(T, C, Order)>
) -> &mut Self where
T: IntoIden,
C: IntoIden,
Please use the [OrderedStatement::order_by_columns
] with a tuple as [ColumnRef
]
sourcefn order_by_with_nulls<T>(
&mut self,
col: T,
order: Order,
nulls: NullOrdering
) -> &mut Self where
T: IntoColumnRef,
fn order_by_with_nulls<T>(
&mut self,
col: T,
order: Order,
nulls: NullOrdering
) -> &mut Self where
T: IntoColumnRef,
Order by column with nulls order option. Read more
sourcefn order_by_expr_with_nulls(
&mut self,
expr: SimpleExpr,
order: Order,
nulls: NullOrdering
) -> &mut Self
fn order_by_expr_with_nulls(
&mut self,
expr: SimpleExpr,
order: Order,
nulls: NullOrdering
) -> &mut Self
Order by SimpleExpr
with nulls order option.
sourcefn order_by_customs_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: ToString,
fn order_by_customs_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: ToString,
Order by custom string with nulls order option.
sourcefn order_by_columns_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: IntoColumnRef,
fn order_by_columns_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: IntoColumnRef,
Order by vector of columns with nulls order option.
sourceimpl QueryStatementBuilder for DeleteStatement
impl QueryStatementBuilder for DeleteStatement
sourcefn build_collect_any_into(
&self,
query_builder: &dyn QueryBuilder,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value)
)
fn build_collect_any_into(
&self,
query_builder: &dyn QueryBuilder,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value)
)
Build corresponding SQL statement into the SqlWriter for certain database backend and collect query parameters
fn into_sub_query_statement(self) -> SubQueryStatement
sourcefn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)
fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)
Build corresponding SQL statement for certain database backend and collect query parameters into a vector
sourcefn build_collect_any(
&self,
query_builder: &dyn QueryBuilder,
collector: &mut dyn FnMut(Value)
) -> String
fn build_collect_any(
&self,
query_builder: &dyn QueryBuilder,
collector: &mut dyn FnMut(Value)
) -> String
Build corresponding SQL statement for certain database backend and collect query parameters
sourceimpl QueryStatementWriter for DeleteStatement
impl QueryStatementWriter for DeleteStatement
sourcefn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value)
) -> String
fn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value)
) -> String
Build corresponding SQL statement for certain database backend and collect query parameters
Examples
use sea_query::{tests_cfg::*, *};
let query = Query::delete()
.from_table(Glyph::Table)
.and_where(Expr::col(Glyph::Id).eq(1))
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"DELETE FROM `glyph` WHERE `id` = 1"#
);
let mut params = Vec::new();
let mut collector = |v| params.push(v);
assert_eq!(
query.build_collect(MysqlQueryBuilder, &mut collector),
r#"DELETE FROM `glyph` WHERE `id` = ?"#
);
assert_eq!(params, vec![Value::Int(Some(1)),]);
sourcefn to_string<T: QueryBuilder>(&self, query_builder: T) -> String
fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String
Build corresponding SQL statement for certain database backend and return SQL string Read more
Auto Trait Implementations
impl !RefUnwindSafe for DeleteStatement
impl Send for DeleteStatement
impl Sync for DeleteStatement
impl Unpin for DeleteStatement
impl !UnwindSafe for DeleteStatement
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more