class

DBX::Query

Inherits Reference / Object

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

Constructors

Instance methods

build

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

Source
builder

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

Source
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}
Source
exec

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

Returns nil instead of raising DB::NoResultsError.

Source
exec!

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

Source
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
Source
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
Source
query_all(as types)

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

Source
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.

Source
query_each

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

Source
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.

Source
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.

Source
query_one!(as types)

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

Source
query_one!

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

Source
raw_query

See DBX::QueryBuilder#query

Source
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
Source
scalar!

Executes current built query and returns a single scalar value.

Source
to_a(as types)

Shortcut, same as query_all(types).

Source
to_a

Shortcut, same as query_all(&block).

Source
to_o(as types)

Shortcut, same as query_one(types).

Source
to_o

Shortcut, same as query_one(&block).

Source
to_o!(as types)

Shortcut, same as query_one!(types).

Source
to_o!

Shortcut, same as query_one!(&block).

Source

Macros

method_missing(call)
Source