pub struct Update { /* private fields */ }Expand description
Builder of Update command.
Basic API
use sql_query_builder as sql;
let query = sql::Update::new()
.update("users")
.set("name = 'Bar'")
.where_clause("id = $1")
.as_string();
Output
UPDATE users SET name = 'Bar' WHERE id = $1Implementations§
Source§impl Update
impl Update
Sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
Prints the current state of the Update 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 update = sql::Update::new()
.update("users")
.set("login = 'foo'")
.set("name = 'Foo'")
.debug();
Prints to the standard output
-- ------------------------------------------------------------------------------
UPDATE users
SET login = 'foo', name = 'Foo'
-- ------------------------------------------------------------------------------Sourcepub fn print(self) -> Self
pub fn print(self) -> Self
Prints the current state of the Update 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 = "update users";
let update_query = sql::Update::new()
.raw(raw_query)
.set("login = 'foo'")
.as_string();
Output
update users
SET login = 'foo'Sourcepub fn raw_after(self, clause: UpdateClause, raw_sql: &str) -> Self
pub fn raw_after(self, clause: UpdateClause, raw_sql: &str) -> Self
Adds a raw SQL query after a specified clause.
§Example
let raw = "set name = 'Foo'";
let update_query = sql::Update::new()
.update("users")
.raw_after(sql::UpdateClause::Update, raw)
.as_string();
Output
UPDATE users
set name = 'Foo'Sourcepub fn raw_before(self, clause: UpdateClause, raw_sql: &str) -> Self
pub fn raw_before(self, clause: UpdateClause, raw_sql: &str) -> Self
Adds a raw SQL query before a specified clause.
§Example
let raw = "update users";
let update_query = sql::Update::new()
.raw_before(sql::UpdateClause::Set, raw)
.set("name = 'Bar'")
.as_string();
Output
update users
SET name = 'Bar'Sourcepub fn set(self, value: &str) -> Self
pub fn set(self, value: &str) -> Self
The set clause
§Example
let update_query = sql::Update::new()
.set("name = 'Bar'")
.as_string();
Output
SET name = 'Bar'Sourcepub fn where_and(self, condition: &str) -> Self
pub fn where_and(self, condition: &str) -> Self
The method will concatenate multiples calls using the and operator. This method is un alias of where_clause.
§Example
let update_query = sql::Update::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_dateSourcepub fn where_clause(self, condition: &str) -> Self
pub fn where_clause(self, condition: &str) -> Self
The where clause, this method will concatenate multiples calls using the and operator.
If you intended to use the or operator you should use the where_or method
§Example
let update_query = sql::Update::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 update_query = sql::Update::new()
.where_clause("login = 'foo'")
.where_or("login = 'bar'")
.as_string();
Output
WHERE
login = 'foo'
OR login = 'bar'Source§impl Update
impl Update
Sourcepub fn from(self, tables: &str) -> Self
Available on crate features postgresql and sqlite only.
pub fn from(self, tables: &str) -> Self
postgresql and sqlite only.The from clause
§Example
let update = sql::Update::new()
.update("users")
.set("users.status = 'active'")
.from("users_bk")
.where_clause("users_bk.status = 'active'")
.debug();
Prints to the standard output
-- ------------------------------------------------------------------------------
UPDATE users
SET users.status = 'active'
FROM users_bk
WHERE users_bk.status = 'active'
-- ------------------------------------------------------------------------------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 update_query = sql::Update::new()
.returning("name, login")
.as_string();
Output
RETURNING name, loginSourcepub fn with(
self,
name: &str,
query: impl WithQuery + 'static + Send + Sync,
) -> Self
Available on crate features postgresql and sqlite only.
pub fn with( self, name: &str, query: impl WithQuery + 'static + Send + Sync, ) -> Self
postgresql and sqlite only.The with clause
§Example
let user = sql::Insert::new()
.insert_into("users(login, name)")
.values("('foo', 'Foo')")
.returning("group_id");
let update = sql::Update::new()
.with("user", user)
.update("user_group")
.set("count = count + 1")
.where_clause("id = (select group_id from user)")
.debug();
Prints to the standard output
-- ------------------------------------------------------------------------------
WITH
user AS (
INSERT INTO users(login, name)
VALUES ('foo', 'Foo')
RETURNING group_id
)
UPDATE user_group
SET count = count + 1
WHERE id = (select group_id from user)
-- ------------------------------------------------------------------------------Source§impl Update
impl Update
Sourcepub fn cross_join(self, table: &str) -> Self
Available on crate feature sqlite only.
pub fn cross_join(self, table: &str) -> Self
sqlite only.The cross join clause
§Example
let update_query = sql::Update::new()
.cross_join("orders")
.as_string();
Output
CROSS JOIN ordersSourcepub fn inner_join(self, table: &str) -> Self
Available on crate feature sqlite only.
pub fn inner_join(self, table: &str) -> Self
sqlite only.The inner join clause
§Example
let update_query = sql::Update::new()
.inner_join("orders on orders.owner_login = users.login")
.as_string();
Output
INNER JOIN orders on orders.owner_login = users.loginSourcepub fn left_join(self, table: &str) -> Self
Available on crate feature sqlite only.
pub fn left_join(self, table: &str) -> Self
sqlite only.The left join clause
§Example
let update_query = sql::Update::new()
.left_join("orders on orders.owner_login = users.login")
.as_string();
Output
LEFT JOIN orders on orders.owner_login = users.loginSourcepub fn right_join(self, table: &str) -> Self
Available on crate feature sqlite only.
pub fn right_join(self, table: &str) -> Self
sqlite only.The right join clause
§Example
let update_query = sql::Update::new()
.right_join("orders on orders.owner_login = users.login")
.as_string();
Output
RIGHT JOIN orders on orders.owner_login = users.login