class
CQL::Update
- CQL::Update
- Reference
- Object
Overview
The CQL::Update class represents an SQL UPDATE statement.
All update operations are automatically tracked by CQL::Performance when enabled.
Example
AcmeDB.update
.table(:users)
.set(name: "John", age: 30)
.where { users.id == 1 }
.commit
Usage
initialize(schema : Schema)- Initializes a new instance ofCQL::Updatewith the given schema (schema.updatedoes the same).commit : DB::ExecResult- Executes the update query and returns the result.to_sql(gen : Expression::Generator? = nil) : {String, Array(DB::Any)}- Generates the SQL query and parameters.table(table : Symbol) : self- Sets the table to update.set(setters : Hash(Symbol, DB::Any)) : self- Sets the column values to update using a hash.set(**fields) : self- Sets the column values to update using keyword arguments.where(&block) : self- Sets the WHERE clause using a block.where(**fields) : self- Sets the WHERE clause using keyword arguments.where(attr : Hash(Symbol, DB::Any)) : self- Sets the WHERE clause using a hash.with_optimistic_lock(version : Int32, column : Symbol? = nil) : self- Checks and bumps a version column.back(*columns : Symbol) : self- Sets the columns to return after the update.build : Expression::Update- Builds theExpression::Updateobject.
Included Modules
Defined in:
update.crConstructors
Instance Method Summary
-
#back(*columns : Symbol)
Sets the columns to return after the update (
RETURNING; SQLite raises when SQL is generated). -
#build
Builds the
Expression::Updateobject. -
#commit
Executes the update query and returns the result.
- #schema : Schema
-
#set(setters : Hash(Symbol, DB::Any))
Sets the column values to update using a hash.
-
#set(**fields)
Sets the column values to update using keyword arguments.
-
#table(table : Symbol)
Sets the table to update.
-
#to_sql(gen : Expression::Generator | Nil = nil)
Generates the SQL query and parameters.
-
#where(&)
Sets the WHERE clause using a block evaluated on an
Expression::FilterBuilder, where the table is reachable by name. -
#where(attr : Hash(Symbol, DB::Any))
Sets the WHERE clause using a hash of column => value conditions, ANDed together.
-
#where(__sql sql : String, *positional, **named)
A raw SQL fragment with bound
?/:namevalues (sqlmust be a literal). -
#where(**fields)
Sets the WHERE clause using keyword arguments.
-
#with_optimistic_lock(version : Int32, column : Symbol | Nil = nil)
Enables optimistic locking for this update: the row must still hold version, which is incremented;
#commitraisesCQL::OptimisticLockErrorwhen no row matched.
Instance methods inherited from module CQL::Pinnable
executor : Executor
executor,
schema : Schema
schema,
using(connection : DB::Connection) : selfusing(executor : Executor) : self using
Constructor Detail
Instance Method Detail
Sets the columns to return after the update (RETURNING; SQLite raises when SQL is generated).
- @param columns [Symbol*] the columns to return
- @return [self] the current instance
Example
AcmeDB.update
.table(:users)
.set(name: "John", age: 30)
.where { users.id == 1 }
.back(:name, :age)
.to_sql(Expression::Generator.new(CQL::Adapter::Postgres))
# => {"UPDATE users SET name = $1, age = $2 WHERE users.id = $3 RETURNING users.name, users.age", ["John", 30, 1]}
Builds the Expression::Update object.
- @return [Expression::Update] the update expression
- @raise [Exception] if the table is not set
Example
AcmeDB.update.table(:users).set(name: "John").where(id: 1).build
Executes the update query and returns the result. The query execution is automatically tracked by CQL::Performance when enabled.
- @return [DB::ExecResult] the result of the query
Example
result = AcmeDB.update
.table(:users)
.set(name: "John", age: 30)
.where { users.id == 1 }
.commit
result.rows_affected
Sets the column values to update using a hash.
- @param setters [Hash(Symbol, DB::Any)] the column values to update
- @return [self] the current instance
Example
values = {:name => "John", :age => 30} of Symbol => DB::Any
AcmeDB.update.table(:users).set(values).where(id: 1).commit
Sets the column values to update using keyword arguments.
- @param fields [NamedTuple] the column values to update
- @return [self] the current instance
Example
AcmeDB.update.table(:users).set(name: "John", age: 30).where(id: 1).commit
Sets the table to update.
- @param table [Symbol] the name of the table
- @return [self] the current instance
- @raise [ArgumentError] if the table does not exist
Example
AcmeDB.update.table(:users)
Generates the SQL query and parameters.
- @param gen [Expression::Generator] the generator to use
- @return [{String, Array(DB::Any)}] the query and parameters
Example
AcmeDB.update
.table(:users)
.set(name: "John", age: 30)
.where { users.id == 1 }
.to_sql
# => {"UPDATE users SET name = ?, age = ? WHERE users.id = ?", ["John", 30, 1]}
Sets the WHERE clause using a block evaluated on an Expression::FilterBuilder,
where the table is reachable by name.
- @return [self] the current instance
Example
AcmeDB.update.table(:users).set(active: false).where { users.age < 18 }.commit
Sets the WHERE clause using a hash of column => value conditions, ANDed together.
- @param attr [Hash(Symbol, DB::Any)] the conditions
- @return [self] the current instance
- @raise [ArgumentError] if a column does not exist
Example
conditions = {:id => 1} of Symbol => DB::Any
AcmeDB.update.table(:users).set(name: "John").where(conditions).commit
A raw SQL fragment with bound ? / :name values (sql must be a literal).
Sets the WHERE clause using keyword arguments.
- @param fields [NamedTuple] the conditions, ANDed together
- @return [self] the current instance
- @raise [Exception] if the column does not exist
- @raise [Exception] if the value is invalid
Example
AcmeDB.update.table(:users).set(name: "John").where(id: 1).to_sql
# => {"UPDATE users SET name = ? WHERE users.id = ?", ["John", 1]}
Enables optimistic locking for this update: the row must still hold version,
which is incremented; #commit raises CQL::OptimisticLockError when no row matched.
- @param version [Int32] the current version value to check against
- @param column [Symbol?] the name of the version column (default: the table's version column, or
version) - @return [self] the current instance
Example Using optimistic locking (the table needs a version column)
AcmeDB.update
.table(:users)
.set(name: "John", age: 30)
.where(id: 1)
.with_optimistic_lock(version: 5)
.commit