shard-crystal-sqlite3/src/sqlite3/result_set.cr
2015-08-20 12:29:20 +02:00

71 lines
1.5 KiB
Crystal
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# The ResultSet object encapsulates the enumerability of a querys output.
# It is a simple cursor over the data that the query returns.
#
# Typical usage is:
#
# ```
# require "sqlite3"
#
# db = SQLite3::Database.new("foo.db")
# stmt = db.prepare("select * from person")
# result_set = stmt.execute
# while result_set.next
# p result_set.to_a
# end
# stmt.close
# db.close
# ```
class SQLite3::ResultSet
# :nodoc:
def initialize(@statement)
end
# Returns the number of columns.
def column_count
@statement.column_count
end
# Returns the value of a column by index or name.
def [](index_or_name)
@statement[index_or_name]
end
# Returns the types of the columns, an `Array(Type)`.
def types
@statement.types
end
# Returns the names of the columns, an `Array(String)`.
def columns
@statement.columns
end
# Advances to the next row. Returns `true` if there's a next row,
# `false` otherwise. Must be called at least once to advance to the first
# row.
def next
case @statement.step
when LibSQLite3::Code::ROW
true
when LibSQLite3::Code::DONE
false
else
raise Exception.new(@db)
end
end
# Closes this result set, closing the associated statement.
def close
@statement.close
end
# Returns `true` if the associated statement is closed.
def closed?
@statement.closed?
end
# Return the current row's value as an `Array(Value)`.
def to_a
Array(Value).new(column_count) { |i| self[i] }
end
end