class Cql::Update

Overview

The Cql::Update class represents an SQL UPDATE statement.

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

Usage

Defined in:

update.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(schema : Schema) #

[View source]

Instance Method Detail

def back(*columns : Symbol) #

Sets the columns to return after the update.

  • @param columns [Array(Symbol)] the columns to return
  • @return [self] the current instance

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .back(:name, :age)
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3 RETURNING name, 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

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

[View source]
def commit #

Executes the update query and returns the result.

  • @return [DB::Result] the result of the query

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

[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

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

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

Sets the column values to update using keyword arguments.

  • @param fields [Hash(Symbol, DB::Any)] the column values to update
  • @return [self] the current instance

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

[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 [Exception] if the table does not exist

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

[View source]
def to_sql(gen = @schema.gen) #

Generates the SQL query and parameters.

  • @param gen [Expression::Generator] the generator to use
  • @return [{String, Array(DB::Any)}] the query and parameters

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .to_sql

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

[View source]
def where(&) #

Sets the WHERE clause using a block.

  • @block w [Expression::FilterBuilder] the filter builder
  • @return [self] the current instance
  • @raise [Exception] if the block is not provided
  • @raise [Exception] if the block does not return an expression

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

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

Sets the columns to return after the update.

  • @param columns [Array(Symbol)] the columns to return
  • @return [self] the current instance
  • @raise [Exception] if the column does not exist
  • @raise [Exception] if the column is not part of the table

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where { |w| w.id == 1 }
  .back(:name, :age)
  .commit

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

Sets the WHERE clause using keyword arguments.

  • @param fields [Hash(Symbol, DB::Any)] the conditions
  • @return [self] the current instance
  • @raise [Exception] if the column does not exist
  • @raise [Exception] if the value is invalid

Example

update = Cql::Update.new(schema)
  .table(:users)
  .set(name: "John", age: 30)
  .where(id: 1)
  .commit

=> {"UPDATE users SET name = $1, age = $2 WHERE id = $3", ["John", 30, 1]}

[View source]