This commit is contained in:
Ary Borenszweig 2016-06-28 14:04:27 -03:00
parent 710ac1d170
commit 1e8de9a2b4
2 changed files with 22 additions and 84 deletions

View file

@ -22,56 +22,43 @@ class SQLite3::ResultSet < DB::ResultSet
end
end
macro nilable_read_for(t)
def read?(t : {{t}}.class) : {{t}}?
if read_nil?
moving_column { nil }
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
else
read(t)
raise Exception.new(@statement.connection)
end
end
end
{% for t in DB::TYPES %}
nilable_read_for({{t}})
{% end %}
def read(t : String.class) : String
moving_column { |col| String.new(LibSQLite3.column_text(self, col)) }
@column_index += 1
value
end
def read(t : Int32.class) : Int32
read(Int64).to_i32
end
def read(t : Int64.class) : Int64
moving_column { |col| LibSQLite3.column_int64(self, col) }
end
def read(t : Float32.class) : Float32
read(Float64).to_f32
end
def read(t : Float64.class) : Float64
moving_column { |col| LibSQLite3.column_double(self, col) }
end
def read(t : Bytes.class) : Bytes
moving_column do |col|
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)
end
end
def read(t : Time.class) : Time
Time.parse read(String), SQLite3::DATE_FORMAT
end
nilable_read_for Time
def column_count
LibSQLite3.column_count(self)
end
@ -80,30 +67,10 @@ class SQLite3::ResultSet < DB::ResultSet
String.new LibSQLite3.column_name(self, index)
end
def column_type(index : Int32)
case LibSQLite3.column_type(self, index)
when Type::INTEGER; Int64
when Type::FLOAT ; Float64
when Type::BLOB ; Bytes
when Type::TEXT ; String
when Type::NULL ; Nil
else
raise Exception.new(@statement.connection)
end
end
def to_unsafe
@statement.to_unsafe
end
private def read_nil?
column_sqlite_type == Type::NULL
end
private def column_sqlite_type
LibSQLite3.column_type(self, @column_index)
end
# :nodoc:
private def step
LibSQLite3::Code.new LibSQLite3.step(@statement)