class Cql::Insert

Overview

An insert statement builder class This class provides methods for building an insert statement It also provides methods for executing the statement

Example Inserting a record

insert
  .into(:users)
  .values(name: "John", age: 30)
  .last_insert_id

Example Inserting multiple records

insert
  .into(:users)
  .values(
    [
      {name: "John", age: 30},
      {name: "Jane", age: 25},
    ]
  ).commit

Example Inserting a record with a query

insert
  .into(:users)
  .query(
    select.from(:users).where(id: 1)
  ).commit

Defined in:

insert.cr

Constant Summary

Log = ::Log.for(self)

Constructors

Instance Method Summary

Constructor Detail

def self.new(schema : Schema) #

[View source]

Instance Method Detail

def back(*columns : Symbol) #

Set the columns to return

  • @param columns [Symbol*] The columns to return
  • @return [Insert] The insert object
  • @raise [Exception] If the column does not exist

Example Inserting a record

insert.into(:users).values(name: "John", age: 30).back(:id).commit

[View source]
def build #

Build the insert statement object @return [Expression::Insert] The insert statement

Example Building the insert statement

insert.into(:users).values(name: "John", age: 30).commit

[View source]
def commit #

Executes the insert statement and returns the result

  • @return [Int64] The last inserted ID

Example Inserting a record

insert
  .into(:users)
  .values(name: "John", age: 30)
  .commit

=> 1

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

Set the table to insert into

  • @param table [Symbol] The table to insert into
  • @return [Insert] The insert object

Example Inserting a record

insert
  .into(:users)
  .values(name: "John", age: 30)
  .commit

[View source]
def last_insert_id(as type : PrimaryKeyType = Int64) #

Inserts and gets the last inserted ID from the database Works with SQLite, PostgreSQL and MySQL.

  • @return [Int64] The last inserted ID

Example Getting the last inserted ID

insert.into(:users).values(name: "John", age: 30).last_insert_id

[View source]
def query(query : Query) #

Set the query to use for the insert

  • @param query [Query] The query to use
  • @return [Insert] The insert object

Example Inserting a record with a query

insert.into(:users).query(select.from(:users).where(id: 1)).commit

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

Convert the insert object to a SQL query

  • @param gen [Generator] The generator to use
  • @return [{String, Array(DB::Any)}] The query and parameters
  • @raise [Exception] If the table does not exist

Example Generating a SQL query

insert.into(:users).values(name: "John", age: 30).to_sql

[View source]
def values(values : Array(Hash(Symbol, DB::Any))) #

Set the columns to insert

  • @param columns [Array(Symbol)] The columns to insert
  • @return [Insert] The insert object

Example Inserting a record

insert
  .into(:users)
  .columns(:name, :age)
  .values("John", 30)
  .commit

[View source]
def values(hash : Hash(Symbol, DB::Any)) #

Set the values to insert

  • @param hash [Hash(Symbol, DB::Any)] The values to insert
  • @return [Insert] The insert object

Example Inserting a record

insert.into(:users).values(name: "John", age: 30).commit

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

Set the values to insert

  • @param fields [Hash(Symbol, DB::Any)] The values to insert
  • @return [Insert] The insert object

Example Inserting a record

insert.into(:users).values(name: "John", age: 30).commit

[View source]