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
use crate::ast::*;
use std::borrow::Cow;
#[derive(Debug, PartialEq, Clone)]
/// A builder for a `DELETE` statement.
pub struct Delete<'a> {
pub(crate) table: Table<'a>,
pub(crate) conditions: Option<ConditionTree<'a>>,
pub(crate) returning: Option<Vec<Column<'a>>>,
pub(crate) comment: Option<Cow<'a, str>>,
}
impl<'a> From<Delete<'a>> for Query<'a> {
fn from(delete: Delete<'a>) -> Self {
Query::Delete(Box::new(delete))
}
}
impl<'a> Delete<'a> {
/// Creates a new `DELETE` statement for the given table.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// # fn main() -> Result<(), quaint::error::Error> {
/// let query = Delete::from_table("users");
/// let (sql, _) = Sqlite::build(query)?;
///
/// assert_eq!("DELETE FROM `users`", sql);
/// # Ok(())
/// # }
/// ```
pub fn from_table<T>(table: T) -> Self
where
T: Into<Table<'a>>,
{
Self {
table: table.into(),
conditions: None,
returning: None,
comment: None,
}
}
/// Adds a comment to the delete.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// # fn main() -> Result<(), quaint::error::Error> {
/// let query = Delete::from_table("users").comment("trace_id='5bd66ef5095369c7b0d1f8f4bd33716a', parent_id='c532cb4098ac3dd2'");
/// let (sql, _) = Sqlite::build(query)?;
///
/// assert_eq!("DELETE FROM `users` /* trace_id='5bd66ef5095369c7b0d1f8f4bd33716a', parent_id='c532cb4098ac3dd2' */", sql);
/// # Ok(())
/// # }
/// ```
pub fn comment<C: Into<Cow<'a, str>>>(mut self, comment: C) -> Self {
self.comment = Some(comment.into());
self
}
/// Adds `WHERE` conditions to the query. See
/// [Comparable](trait.Comparable.html#required-methods) for more examples.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// # fn main() -> Result<(), quaint::error::Error> {
/// let query = Delete::from_table("users").so_that("bar".equals(false));
/// let (sql, params) = Sqlite::build(query)?;
///
/// assert_eq!("DELETE FROM `users` WHERE `bar` = ?", sql);
/// assert_eq!(vec![Value::boolean(false)], params);
/// # Ok(())
/// # }
/// ```
pub fn so_that<T>(mut self, conditions: T) -> Self
where
T: Into<ConditionTree<'a>>,
{
self.conditions = Some(conditions.into());
self
}
/// Sets the returned columns.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// # fn main() -> Result<(), quaint::error::Error> {
/// let query = Delete::from_table("users").returning(vec!["id"]);
/// let (sql, _) = Sqlite::build(query)?;
///
/// assert_eq!("DELETE FROM `users` RETURNING \"id\"", sql);
/// # Ok(())
/// # }
#[cfg(any(feature = "postgresql", feature = "mssql", feature = "sqlite"))]
pub fn returning<K, I>(mut self, columns: I) -> Self
where
K: Into<Column<'a>>,
I: IntoIterator<Item = K>,
{
self.returning = Some(columns.into_iter().map(|k| k.into()).collect());
self
}
}