Struct grafbase_sql_ast::ast::Update
source · pub struct Update<'a> { /* private fields */ }
Expand description
A builder for an UPDATE
statement.
Implementations§
source§impl<'a> Update<'a>
impl<'a> Update<'a>
sourcepub fn table<T>(table: T) -> Selfwhere
T: Into<Table<'a>>,
pub fn table<T>(table: T) -> Selfwhere T: Into<Table<'a>>,
Creates the basis for an UPDATE
statement to the given table.
sourcepub fn set<K, V>(self, column: K, value: V) -> Update<'a>where
K: Into<Column<'a>>,
V: Into<Expression<'a>>,
pub fn set<K, V>(self, column: K, value: V) -> Update<'a>where K: Into<Column<'a>>, V: Into<Expression<'a>>,
Add another column value assignment to the query
let query = Update::table("users").set("foo", 10).set("bar", false);
let (sql, params) = renderer::Postgres::build(query);
assert_eq!(r#"UPDATE "users" SET "foo" = $1, "bar" = $2"#, sql);
assert_eq!(
vec![
Value::from(10),
Value::from(false),
],
params,
);
sourcepub fn so_that<T>(self, conditions: T) -> Selfwhere
T: Into<ConditionTree<'a>>,
pub fn so_that<T>(self, conditions: T) -> Selfwhere T: Into<ConditionTree<'a>>,
Adds WHERE
conditions to the query. See
Comparable for more examples.
let query = Update::table("users").set("foo", 1).so_that("bar".equals(false));
let (sql, params) = renderer::Postgres::build(query);
assert_eq!(r#"UPDATE "users" SET "foo" = $1 WHERE "bar" = $2"#, sql);
assert_eq!(
vec![
Value::from(1),
Value::from(false),
],
params,
);
We can also use a nested SELECT
in the conditions.
let mut select = Select::from_table("bars");
select.column("id");
select.so_that("uniq_val".equals(3));
let query = Update::table("users").set("foo", 1).so_that("bar".equals(select));
let (sql, params) = renderer::Postgres::build(query);
assert_eq!(
r#"UPDATE "users" SET "foo" = $1 WHERE "bar" = (SELECT "id" FROM "bars" WHERE "uniq_val" = $2)"#,
sql
);
assert_eq!(
vec![
Value::from(1),
Value::from(3),
],
params,
);
sourcepub fn returning<K, I>(self, columns: I) -> Selfwhere
K: Into<Column<'a>>,
I: IntoIterator<Item = K>,
pub fn returning<K, I>(self, columns: I) -> Selfwhere K: Into<Column<'a>>, I: IntoIterator<Item = K>,
Sets the returned columns.
let update = Update::table("users").set("foo", 10);
let update = update.returning(vec!["id"]);
let (sql, _) = renderer::Postgres::build(update);
assert_eq!(r#"UPDATE "users" SET "foo" = $1 RETURNING "id""#, sql);
Trait Implementations§
source§impl<'a> PartialEq<Update<'a>> for Update<'a>
impl<'a> PartialEq<Update<'a>> for Update<'a>
impl<'a> StructuralPartialEq for Update<'a>
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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more