DBX::Query
Query executor. See also: https://crystal-lang.github.io/crystal-db/api/latest/DB/QueryMethods.html
Constructors
Instance methods
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 an DB::ExecResult.
Returns nil instead of raising DB::NoResultsError.
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.
Executes current built query that is expected to return one result.
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.
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.
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