Struct sql_query_builder::Update
source · pub struct Update { /* private fields */ }
Expand description
Builder to contruct a Update command
Implementations§
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
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 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_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 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, 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 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 orders
sourcepub 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.login
sourcepub 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.login
sourcepub 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