Struct sql_query_builder::Delete
source · pub struct Delete { /* private fields */ }
Expand description
Builder to contruct a Delete command
Implementations§
source§impl Delete
impl Delete
sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
Prints the current state of the Delete to the standard output in a more ease to read version. This method is useful to debug complex queries or just print the generated SQL while you type
§Example
let delete_query = sql::Delete::new()
.delete_from("users")
.where_clause("login = 'foo'")
.debug()
.where_clause("name = 'Foo'")
.as_string();
Prints to the standard output
-- ------------------------------------------------------------------------------
DELETE FROM users
WHERE login = 'foo'
-- ------------------------------------------------------------------------------
sourcepub fn delete_from(self, table_name: &str) -> Self
pub fn delete_from(self, table_name: &str) -> Self
The delete
clause. This method overrides the previous value
§Example
let delete = sql::Delete::new()
.delete_from("orders");
let delete = sql::Delete::new()
.delete_from("addresses")
.delete_from("orders");
sourcepub fn print(self) -> Self
pub fn print(self) -> Self
Prints the current state of the Delete to the standard output similar to debug method, the difference is that this method prints in one line.
sourcepub fn raw(self, raw_sql: &str) -> Self
pub fn raw(self, raw_sql: &str) -> Self
Adds at the beginning a raw SQL query.
§Example
let raw_query = "delete from users";
let delete_query = sql::Delete::new()
.raw(raw_query)
.where_clause("login = 'foo'")
.as_string();
Output
delete from users WHERE login = 'foo'
sourcepub fn raw_after(self, clause: DeleteClause, raw_sql: &str) -> Self
pub fn raw_after(self, clause: DeleteClause, raw_sql: &str) -> Self
Adds a raw SQL query after a specified clause.
§Example
let raw = "where name = 'Foo'";
let delete_query = sql::Delete::new()
.delete_from("users")
.raw_after(sql::DeleteClause::DeleteFrom, raw)
.as_string();
Output
DELETE FROM users where name = 'Foo'
sourcepub fn raw_before(self, clause: DeleteClause, raw_sql: &str) -> Self
pub fn raw_before(self, clause: DeleteClause, raw_sql: &str) -> Self
Adds a raw SQL query before a specified clause.
§Example
let raw = "delete from users";
let delete_query = sql::Delete::new()
.raw_before(sql::DeleteClause::Where, raw)
.where_clause("name = 'Bar'")
.as_string();
Output
delete from users WHERE name = 'Bar'
sourcepub fn where_and(self, condition: &str) -> Self
pub fn where_and(self, condition: &str) -> Self
This method is un alias of where_clause
. The where_and
will concatenate mulltiples calls using the and
operator.
The intention is to enable more idiomatic concatenation of conditions.
§Example
let delete_query = sql::Delete::new()
.where_clause("login = $1")
.where_and("product_id = $2")
.where_and("created_at >= current_date")
.as_string();
Outputs
WHERE
login = $1
AND product_id = $2
AND created_at >= current_date
sourcepub fn where_clause(self, condition: &str) -> Self
pub fn where_clause(self, condition: &str) -> Self
The where
clause, this method will concatenate mulltiples calls using the and
operator.
If you intended to use the or
operator you should use the where_or method
§Example
let delete_query = sql::Delete::new()
.where_clause("login = $1")
.where_clause("status = 'deactivated'")
.as_string();
Outputs
WHERE
login = $1
AND status = 'deactivated'
sourcepub fn where_or(self, condition: &str) -> Self
pub fn where_or(self, condition: &str) -> Self
The where
clause that concatenate multiples calls using the OR operator.
If you intended to use the and
operator you should use the where_clause method
§Example
let delete_query = sql::Delete::new()
.where_clause("login = 'foo'")
.where_or("login = 'bar'")
.as_string();
Output
WHERE
login = 'foo'
OR login = 'bar'
source§impl Delete
impl Delete
sourcepub fn returning(self, output_name: &str) -> Self
Available on crate features postgresql
and sqlite
only.
pub fn returning(self, output_name: &str) -> Self
postgresql
and sqlite
only.The returning
clause
§Example
let delete = sql::Delete::new()
.delete_from("users")
.returning("id")
.returning("login");
Output
DELETE FROM users RETURNING id, login
sourcepub fn with(self, name: &str, query: impl WithQuery + 'static) -> Self
Available on crate features postgresql
and sqlite
only.
pub fn with(self, name: &str, query: impl WithQuery + 'static) -> Self
postgresql
and sqlite
only.The with
clause
§Example
let deactivated_users = sql::Select::new().select("id").from("users").where_clause("ative = false");
let delete = sql::Delete::new()
.with("deactivated_users", deactivated_users)
.delete_from("users")
.where_clause("id in (select * from deactivated_users)")
.debug();
WITH deactivated_users AS (\
SELECT id \
FROM users \
WHERE ative = false\
) \
DELETE FROM users \
WHERE id in (select * from deactivated_users)\
";
Output
WITH deactivated_users AS (
SELECT id
FROM users
WHERE ative = false
)
DELETE FROM users
WHERE id in (select * from deactivated_users)