Struct sql_query_builder::Update

source ·
pub struct Update { /* private fields */ }
Expand description

Builder to contruct a Update command

Implementations§

source§

impl Update

source

pub fn as_string(&self) -> String

Gets the current state of the Update and returns it as string

§Example
let update_query = sql::Update::new()
  .update("users")
  .set("login = 'foo'")
  .as_string();

Output

 UPDATE users SET login = 'foo'
source

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'
-- ------------------------------------------------------------------------------
source

pub fn new() -> Self

Creates instance of the Update command

source

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.

source

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'
source

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'
source

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'
source

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'
source

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
source

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'
source

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

source

pub fn from(self, tables: &str) -> Self

Available on crate features 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'
-- ------------------------------------------------------------------------------
source

pub fn returning(self, output_name: &str) -> Self

Available on crate features postgresql and sqlite only.

The returning clause

§Example
let update_query = sql::Update::new()
  .returning("name, login")
  .as_string();

Output

RETURNING name, login
source

pub fn with(self, name: &str, query: impl WithQuery + 'static) -> Self

Available on crate features 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

source

pub fn cross_join(self, table: &str) -> Self

Available on crate feature sqlite only.

The cross join clause

§Example
let update_query = sql::Update::new()
  .cross_join("orders")
  .as_string();

Output

CROSS JOIN orders
source

pub fn inner_join(self, table: &str) -> Self

Available on crate feature 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
source

pub fn left_join(self, table: &str) -> Self

Available on crate feature 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
source

pub fn right_join(self, table: &str) -> Self

Available on crate feature 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
source

pub fn update(self, table_name: &str) -> Self

Available on crate feature sqlite only.

The update clause, this method overrides the previous value

§Example
let update_query = sql::Update::new()
  .update("orders")
  .as_string();
source

pub fn update_or(self, expression: &str) -> Self

Available on crate feature sqlite only.

The update or <keyword> clause, this method overrides the previous value

§Example
let update_query = sql::Update::new()
  .update_or("ABORT orders")
  .as_string();

Output

UPDATE OR ABORT orders

Trait Implementations§

source§

impl Clone for Update

source§

fn clone(&self) -> Update

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Update

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Update

source§

fn default() -> Update

Returns the “default value” for a type. Read more
source§

impl Display for Update

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Update

§

impl !RefUnwindSafe for Update

§

impl !Send for Update

§

impl !Sync for Update

§

impl Unpin for Update

§

impl !UnwindSafe for Update

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.