class DBX::Query
- DBX::Query
- Reference
- Object
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.crConstructors
Instance Method Summary
-
#build : Tuple
Builds current query and returns
sql, args
. -
#builder : DBX::QueryBuilder
Returns
DBX::QueryBuilder
instance used in currentQuery
instance. -
#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.
-
#exec
:ditto: Returns
nil
instead of raisingDB::NoResultsError
. -
#exec!
Executes current built query that is expected to return an
DB::ExecResult
. -
#query
Executes current built query that is expected to return one or more results.
-
#query(&)
Executes current built query and yields a
DB::ResultSet
with the results. -
#query_all(as types)
Executes current built query that is expected to return one result.
-
#query_all(&)
Executes current built query and yields a
DB::ResultSet
positioned at the beginning of each row, returning anArray
of the values of the blocks. -
#query_each(&)
Executes current built query and yields the
DB::ResultSet
once per each row. -
#query_one(as types)
:ditto: If no result found, this method returns
nil
instead of raisingDB::NoResultsError
. -
#query_one(&)
:ditto: If no result found, this method returns
nil
instead of raisingDB::NoResultsError
. -
#query_one!(as types)
Executes current built query that is expected to return one result.
-
#query_one!(&)
Executes current built query that expects at most a single row and yields a
DB::ResultSet
positioned at that first row. - #raw_query(&) : Query
-
#scalar
:ditto: If no result found, this method returns
nil
instead of raisingDB::NoResultsError
. -
#scalar!
Executes current built query and returns a single scalar value.
-
#to_a(as types)
Shortcut, same as
#query_all(types)
. -
#to_a(&)
Shortcut, same as
#query_all(&block)
. -
#to_o(as types)
Shortcut, same as
#query_one(types)
. -
#to_o(&)
Shortcut, same as
#query_one(&block)
. -
#to_o!(as types)
Shortcut, same as
#query_one!(types)
. -
#to_o!(&)
Shortcut, same as
#query_one!(&block)
.
Macro Summary
Constructor Detail
Instance Method Detail
Returns DBX::QueryBuilder
instance used in current Query
instance.
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}
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
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
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.
:ditto:
If no result found, this method returns nil
instead of raising DB::NoResultsError
.
:ditto:
If no result found, this method returns nil
instead of raising DB::NoResultsError
.
Executes current built query that expects at most a single row and yields
a DB::ResultSet
positioned at that first row.
:ditto:
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