2016-02-04 00:52:45 +00:00
|
|
|
class SQLite3::ResultSet < DB::ResultSet
|
2016-01-29 19:15:28 +00:00
|
|
|
@column_index = 0
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
protected def do_close
|
|
|
|
super
|
|
|
|
LibSQLite3.reset(self)
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
# 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.
|
2019-07-29 13:25:46 +00:00
|
|
|
def move_next : Bool
|
2016-01-29 19:15:28 +00:00
|
|
|
@column_index = 0
|
|
|
|
|
|
|
|
case step
|
|
|
|
when LibSQLite3::Code::ROW
|
|
|
|
true
|
|
|
|
when LibSQLite3::Code::DONE
|
|
|
|
false
|
|
|
|
else
|
2016-07-14 18:21:59 +00:00
|
|
|
raise Exception.new(sqlite3_statement.sqlite3_connection)
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-28 17:04:27 +00:00
|
|
|
def read
|
|
|
|
col = @column_index
|
|
|
|
value =
|
|
|
|
case LibSQLite3.column_type(self, col)
|
|
|
|
when Type::INTEGER
|
|
|
|
LibSQLite3.column_int64(self, col)
|
|
|
|
when Type::FLOAT
|
|
|
|
LibSQLite3.column_double(self, col)
|
|
|
|
when Type::BLOB
|
|
|
|
blob = LibSQLite3.column_blob(self, col)
|
|
|
|
bytes = LibSQLite3.column_bytes(self, col)
|
|
|
|
ptr = Pointer(UInt8).malloc(bytes)
|
|
|
|
ptr.copy_from(blob, bytes)
|
|
|
|
Bytes.new(ptr, bytes)
|
|
|
|
when Type::TEXT
|
|
|
|
String.new(LibSQLite3.column_text(self, col))
|
|
|
|
when Type::NULL
|
|
|
|
nil
|
2016-01-29 19:15:28 +00:00
|
|
|
else
|
2016-07-14 18:21:59 +00:00
|
|
|
raise Exception.new(sqlite3_statement.sqlite3_connection)
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
2016-06-28 17:04:27 +00:00
|
|
|
@column_index += 1
|
|
|
|
value
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read(t : Int32.class) : Int32
|
|
|
|
read(Int64).to_i32
|
|
|
|
end
|
|
|
|
|
2017-04-05 19:37:06 +00:00
|
|
|
def read(type : Int32?.class) : Int32?
|
|
|
|
read(Int64?).try &.to_i32
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
def read(t : Float32.class) : Float32
|
|
|
|
read(Float64).to_f32
|
|
|
|
end
|
|
|
|
|
2017-04-05 19:37:06 +00:00
|
|
|
def read(type : Float32?.class) : Float32?
|
|
|
|
read(Float64?).try &.to_f32
|
|
|
|
end
|
|
|
|
|
2016-06-22 19:17:47 +00:00
|
|
|
def read(t : Time.class) : Time
|
2018-06-18 08:26:32 +00:00
|
|
|
Time.parse read(String), SQLite3::DATE_FORMAT, location: SQLite3::TIME_ZONE
|
2016-06-22 19:17:47 +00:00
|
|
|
end
|
|
|
|
|
2017-04-05 19:37:06 +00:00
|
|
|
def read(t : Time?.class) : Time?
|
2018-06-18 08:26:32 +00:00
|
|
|
read(String?).try { |v| Time.parse(v, SQLite3::DATE_FORMAT, location: SQLite3::TIME_ZONE) }
|
2017-04-05 19:37:06 +00:00
|
|
|
end
|
|
|
|
|
2016-12-14 16:52:12 +00:00
|
|
|
def read(t : Bool.class) : Bool
|
|
|
|
read(Int64) != 0
|
|
|
|
end
|
|
|
|
|
2017-04-05 19:37:06 +00:00
|
|
|
def read(t : Bool?.class) : Bool?
|
|
|
|
read(Int64?).try &.!=(0)
|
|
|
|
end
|
|
|
|
|
2019-07-29 13:25:46 +00:00
|
|
|
def column_count : Int32
|
2016-01-30 00:58:04 +00:00
|
|
|
LibSQLite3.column_count(self)
|
|
|
|
end
|
|
|
|
|
2019-07-29 13:25:46 +00:00
|
|
|
def column_name(index) : String
|
2016-01-30 00:58:04 +00:00
|
|
|
String.new LibSQLite3.column_name(self, index)
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
def to_unsafe
|
2016-07-14 18:21:59 +00:00
|
|
|
sqlite3_statement.to_unsafe
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
private def step
|
2016-07-14 18:21:59 +00:00
|
|
|
LibSQLite3::Code.new LibSQLite3.step(sqlite3_statement)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected def sqlite3_statement
|
|
|
|
@statement.as(Statement)
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private def moving_column
|
|
|
|
res = yield @column_index
|
|
|
|
@column_index += 1
|
|
|
|
res
|
|
|
|
end
|
|
|
|
end
|