class CQL::Update

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

Included Modules

Defined in:

update.cr

Constructors

Instance Method Summary

Instance methods inherited from module CQL::Pinnable

executor : Executor executor, schema : Schema schema, using(connection : DB::Connection) : self
using(executor : Executor) : self
using

Constructor Detail

def self.new(schema : Schema) #

[View source]

Instance Method Detail

def back(*columns : Symbol) #

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]}

[View source]
def build #

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

[View source]
def commit #

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

[View source]
def schema : Schema #

[View source]
def set(setters : Hash(Symbol, DB::Any)) #

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

[View source]
def set(**fields) #

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

[View source]
def table(table : Symbol) #

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)

[View source]
def to_sql(gen : Expression::Generator | Nil = nil) #

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]}

[View source]
def where(&) #

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

[View source]
def where(attr : Hash(Symbol, DB::Any)) #

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

[View source]
def where(__sql sql : String, *positional, **named) #

A raw SQL fragment with bound ? / :name values (sql must be a literal).


[View source]
def where(**fields) #

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]}

[View source]
def with_optimistic_lock(version : Int32, column : Symbol | Nil = nil) #

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

[View source]