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

@ -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