Struct sql_query_builder::Update
source · [−]pub struct Update<'a> { /* private fields */ }Expand description
Builder to contruct a Update command
Implementations
sourceimpl<'a> Update<'a>
impl<'a> Update<'a>
sourcepub fn and(self, condition: &'a str) -> Self
pub fn and(self, condition: &'a str) -> Self
The same as where_clause method, useful to write more idiomatic SQL query
Examples
use sql_query_builder as sql;
let update = sql::Update::new()
.update("users")
.set("name = $1")
.where_clause("login = $2")
.and("active = true");sourcepub fn as_string(&self) -> String
pub fn as_string(&self) -> String
Gets the current state of the Update and returns it as string
Examples
use sql_query_builder as sql;
let query = sql::Update::new()
.update("users")
.set("login = 'foo'")
.as_string();Output
UPDATE users SET login = 'foo'sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
Prints the current state of the Update into console output in a more ease to read version. This method is useful to debug complex queries or just to print the generated SQL while you type
Examples
use sql_query_builder as sql;
let update_query = sql::Update::new()
.update("users")
.set("login = 'foo'")
.debug()
.set("name = 'Foo'")
.as_string();Output
UPDATE users
SET login = 'foo'sourcepub fn from(self, tables: &'a str) -> Self
pub fn from(self, tables: &'a str) -> Self
The from clause, this method can be used enabling the feature flag postgresql
sourcepub fn print(self) -> Self
pub fn print(self) -> Self
Prints the current state of the Update into console output similar to debug method, the difference is that this method prints in one line.
sourcepub fn raw(self, raw_sql: &'a str) -> Self
pub fn raw(self, raw_sql: &'a str) -> Self
Adds at the beginning a raw SQL query.
Examples
use sql_query_builder as sql;
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: &'a str) -> Self
pub fn raw_after(self, clause: UpdateClause, raw_sql: &'a str) -> Self
Adds a raw SQL query after a specified clause.
Examples
use sql_query_builder as sql;
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: &'a str) -> Self
pub fn raw_before(self, clause: UpdateClause, raw_sql: &'a str) -> Self
Adds a raw SQL query before a specified clause.
Examples
use sql_query_builder as sql;
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 returning(self, output_name: &'a str) -> Self
pub fn returning(self, output_name: &'a str) -> Self
The returning clause, this method can be used enabling the feature flag postgresql
sourcepub fn update(self, table_name: &'a str) -> Self
pub fn update(self, table_name: &'a str) -> Self
The update clause. This method overrides the previous value
Examples
use sql_query_builder as sql;
let update = sql::Update::new()
.update("orders");
let update = sql::Update::new()
.update("address")
.update("orders");sourcepub fn where_clause(self, condition: &'a str) -> Self
pub fn where_clause(self, condition: &'a str) -> Self
The where clause
Examples
use sql_query_builder as sql;
let update = sql::Update::new()
.update("users")
.set("name = $1")
.where_clause("login = $2");sourcepub fn with(self, name: &'a str, query: impl WithQuery + 'static) -> Self
pub fn with(self, name: &'a str, query: impl WithQuery + 'static) -> Self
The with clause, this method can be used enabling the feature flag postgresql
Examples
use sql_query_builder as sql;
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();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)Trait Implementations
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Update<'a>
impl<'a> !Send for Update<'a>
impl<'a> !Sync for Update<'a>
impl<'a> Unpin for Update<'a>
impl<'a> !UnwindSafe for Update<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more