1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
use crate::{
backend::QueryBuilder,
prepare::*,
query::{condition::*, OrderedStatement},
types::*,
value::*,
QueryStatementBuilder,
};
/// 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"#
/// );
/// ```
#[derive(Debug, Clone)]
pub struct DeleteStatement {
pub(crate) table: Option<Box<TableRef>>,
pub(crate) wherei: ConditionHolder,
pub(crate) orders: Vec<OrderExpr>,
pub(crate) limit: Option<Value>,
}
impl Default for DeleteStatement {
fn default() -> Self {
Self::new()
}
}
impl DeleteStatement {
/// Construct a new [`DeleteStatement`]
pub fn new() -> Self {
Self {
table: None,
wherei: ConditionHolder::new(),
orders: Vec::new(),
limit: None,
}
}
/// 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"#
/// );
/// ```
#[allow(clippy::wrong_self_convention)]
pub fn from_table<T>(&mut self, tbl_ref: T) -> &mut Self
where
T: IntoTableRef,
{
self.table = Some(Box::new(tbl_ref.into_table_ref()));
self
}
/// Limit number of updated rows.
pub fn limit(&mut self, limit: u64) -> &mut Self {
self.limit = Some(Value::BigUnsigned(limit));
self
}
}
impl QueryStatementBuilder for DeleteStatement {
/// 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(1),
/// ]
/// );
/// ```
fn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value),
) -> String {
let mut sql = SqlWriter::new();
query_builder.prepare_delete_statement(self, &mut sql, collector);
sql.result()
}
fn build_collect_any(
&self,
query_builder: &dyn QueryBuilder,
collector: &mut dyn FnMut(Value),
) -> String {
let mut sql = SqlWriter::new();
query_builder.prepare_delete_statement(self, &mut sql, collector);
sql.result()
}
}
impl OrderedStatement for DeleteStatement {
fn add_order_by(&mut self, order: OrderExpr) -> &mut Self {
self.orders.push(order);
self
}
}
impl ConditionalStatement for DeleteStatement {
fn and_or_where(&mut self, condition: LogicalChainOper) -> &mut Self {
self.wherei.add_and_or(condition);
self
}
fn cond_where<C>(&mut self, condition: C) -> &mut Self
where
C: IntoCondition,
{
self.wherei.add_condition(condition.into_condition());
self
}
}