extend sqlite with time (as text) support.

This commit is contained in:
Brian J. Cardiff 2016-06-22 16:17:47 -03:00
parent d65575cd77
commit 497379ff81
4 changed files with 38 additions and 1 deletions

View file

@ -25,6 +25,7 @@ def sqlite_type_for(v)
when String ; "text"
when Int32, Int64 ; "int"
when Float32, Float64; "float"
when Time ; "text"
else
raise "not implemented for #{typeof(v)}"
end
@ -200,6 +201,24 @@ describe Driver do
end
end
it "insert/get value date from table" do
with_db do |db|
value = Time.new(2016, 7, 22, 15, 0, 0, 0)
db.exec "create table table1 (col1 #{sqlite_type_for(value)})"
db.exec %(insert into table1 values (?)), value
db.query "select col1 from table1" do |rs|
rs.move_next
rs.read(Time).should eq(value)
end
db.query "select col1 from table1" do |rs|
rs.move_next
rs.read?(Time).should eq(value)
end
end
end
it "raises on unsupported param types" do
with_db do |db|
expect_raises Exception, "SQLite3::Statement does not support NotSupportedType params" do

View file

@ -1,2 +1,6 @@
require "db"
require "./sqlite3/**"
module SQLite3
DATE_FORMAT = "%F %H:%M:%S.%L"
end

View file

@ -22,7 +22,7 @@ class SQLite3::ResultSet < DB::ResultSet
end
end
{% for t in DB::TYPES %}
macro nilable_read_for(t)
def read?(t : {{t}}.class) : {{t}}?
if read_nil?
moving_column { nil }
@ -30,6 +30,10 @@ class SQLite3::ResultSet < DB::ResultSet
read(t)
end
end
end
{% for t in DB::TYPES %}
nilable_read_for({{t}})
{% end %}
def read(t : String.class) : String
@ -62,6 +66,12 @@ class SQLite3::ResultSet < DB::ResultSet
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

View file

@ -56,6 +56,10 @@ class SQLite3::Statement < DB::Statement
check LibSQLite3.bind_blob(self, index, value, value.size, nil)
end
private def bind_arg(index, value : Time)
bind_arg(index, value.to_s(SQLite3::DATE_FORMAT))
end
private def bind_arg(index, value)
raise "#{self.class} does not support #{value.class} params"
end