class Cql::Update
- Cql::Update
- Reference
- Object
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
initialize(schema : Schema)
- Initializes a new instance ofCql::Update
with the given schema.commit : DB::Result
- Executes the update query and returns the result.to_sql(gen = @schema.gen) : {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.back(*columns : Symbol) : self
- Sets the columns to return after the update.build : Expression::Update
- Builds theExpression::Update
object.
Defined in:
update.crConstructors
Instance Method Summary
-
#back(*columns : Symbol)
Sets the columns to return after the update.
-
#build
Builds the
Expression::Update
object. -
#commit
Executes the update query and returns the result.
-
#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 = @schema.gen)
Generates the SQL query and parameters.
-
#where(&)
Sets the WHERE clause using a block.
-
#where(attr : Hash(Symbol, DB::Any))
Sets the columns to return after the update.
-
#where(**fields)
Sets the WHERE clause using keyword arguments.
Constructor Detail
Instance Method Detail
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]}
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]}
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]}
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]}
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]}
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]}
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]}
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]}
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
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]}