class DBX::Query

Overview

Query executor. See also: https://crystal-lang.github.io/crystal-db/api/latest/DB/QueryMethods.html

Direct Known Subclasses

Defined in:

query_builder/executor.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(adapter : DBX::Adapter::Base) #

[View source]

Instance Method Detail

def build : Tuple #

Builds current query and returns sql, args. See DBX::QueryBuilder#build method.


[View source]
def builder : DBX::QueryBuilder #

Returns DBX::QueryBuilder instance used in current Query instance.


[View source]
def create!(data : Hash | NamedTuple, as types, returning : DBX::QueryBuilder::OneOrMoreFieldsType = "*", pk_name : DBX::QueryBuilder::FieldType = :id, pk_type = ::Union(Int64, ::Nil)) #

Creates a new record and returns.

query.table(:tests).create!(
  {name: "Baby", about: "I'm a baby", age: 1},
  as: {String, Int32},
  returning: {:name, :age}
)
# => {"Baby", 1}

query.table(:tests).create!(
  {name: "Baby", about: "I'm a baby", age: 1},
  as: {name: String, age: Int32},
  returning: {:name, :age}
)
# => {name: "Baby", age: 1}

[View source]
def exec #

Executes current built query that is expected to return an DB::ExecResult.

Returns nil instead of raising DB::NoResultsError.


[View source]
def exec! #

Executes current built query that is expected to return an DB::ExecResult.


[View source]
def query #

Executes current built query that is expected to return one or more results.

tests = [] of Array(String | Int32)
rs = query.find(:tests).select(:name, :age).query

begin
  while rs.move_next
    name = rs.read(String)
    age = rs.read(Int32)
    tests << [name, age]
  end
ensure
  rs.close
end

[View source]
def query(&) #

Executes current built query and yields a DB::ResultSet with the results. The DB::ResultSet is closed automatically.

tests = [] of Array(String | Int32)
query.find(:tests).select(:name, :age).query do |rs|
  rs.each do
    name = rs.read(String)
    age = rs.read(Int32)
    tests << [name, age]
  end
end

[View source]
def query_all(as types) #

Executes current built query that is expected to return one result.


[View source]
def query_all(&) #

Executes current built query and yields a DB::ResultSet positioned at the beginning of each row, returning an Array of the values of the blocks.


[View source]
def query_each(&) #

Executes current built query and yields the DB::ResultSet once per each row.


[View source]
def query_one(as types) #

Executes current built query that is expected to return one result.

If no result found, this method returns nil instead of raising DB::NoResultsError.


[View source]
def query_one(&) #

Executes current built query that expects at most a single row and yields a DB::ResultSet positioned at that first row.

If no result found, this method returns nil instead of raising DB::NoResultsError.


[View source]
def query_one!(as types) #

Executes current built query that is expected to return one result.


[View source]
def query_one!(&) #

Executes current built query that expects at most a single row and yields a DB::ResultSet positioned at that first row.


[View source]
def raw_query(&) : Query #

[View source]
def scalar #

Executes current built query and returns a single scalar value.

If no result found, this method returns nil instead of raising DB::NoResultsError. So the type MUST be nillable:

query
  .find(:tests)
  .select(:name)
  .where(:name, "Terminator")
  .scalar
  .as(String?)
# => String | Nil

[View source]
def scalar! #

Executes current built query and returns a single scalar value.


[View source]
def to_a(as types) #

Shortcut, same as #query_all(types).


[View source]
def to_a(&) #

Shortcut, same as #query_all(&block).


[View source]
def to_o(as types) #

Shortcut, same as #query_one(types).


[View source]
def to_o(&) #

Shortcut, same as #query_one(&block).


[View source]
def to_o!(as types) #

Shortcut, same as #query_one!(types).


[View source]
def to_o!(&) #

Shortcut, same as #query_one!(&block).


[View source]

Macro Detail

macro method_missing(call) #

[View source]