mirror of
https://gitea.invidious.io/iv-org/shard-crystal-sqlite3.git
synced 2024-08-15 00:53:26 +00:00
Add Bool support
* change specs to not use #scalar since it does not have expected type information. * refactor Time spec
This commit is contained in:
parent
46709eab00
commit
9784573152
4 changed files with 32 additions and 11 deletions
|
@ -43,3 +43,8 @@ DB.open "sqlite3://./data.db" do |db|
|
|||
end
|
||||
end
|
||||
```
|
||||
|
||||
### DB::Any
|
||||
|
||||
* `Time` is implemented as `TEXT` column using `SQLite3::DATE_FORMAT` format.
|
||||
* `Bool` is implemented as `INT` column mapping `0`/`1` values.
|
||||
|
|
|
@ -4,6 +4,10 @@ def sql(s : String)
|
|||
"#{s.inspect}"
|
||||
end
|
||||
|
||||
def sql(s : Bool)
|
||||
"#{s ? 1 : 0}"
|
||||
end
|
||||
|
||||
def sql(s)
|
||||
"#{s}"
|
||||
end
|
||||
|
@ -11,8 +15,8 @@ end
|
|||
def sqlite_type_for(v)
|
||||
case v
|
||||
when String ; "text"
|
||||
when Int32, Int64 ; "int"
|
||||
when Float32, Float64; "float"
|
||||
when Bool, Int32, Int64; "int"
|
||||
when Float32, Float64 ; "float"
|
||||
when Time ; "text"
|
||||
else
|
||||
raise "not implemented for #{typeof(v)}"
|
||||
|
@ -143,12 +147,20 @@ describe Driver do
|
|||
end
|
||||
end
|
||||
|
||||
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
||||
{% for value in [true, false, 1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
||||
it "insert/get value {{value.id}} from table" do
|
||||
with_db do |db|
|
||||
db.exec "create table table1 (col1 #{sqlite_type_for({{value}})})"
|
||||
db.exec %(insert into table1 values (#{sql({{value}})}))
|
||||
db.scalar("select col1 from table1").should eq({{value}})
|
||||
db.query_one("select col1 from table1", as: typeof({{value}})).should eq({{value}})
|
||||
end
|
||||
end
|
||||
|
||||
it "insert/get value {{value.id}} using bind" do
|
||||
with_db do |db|
|
||||
db.exec "create table table1 (col1 #{sqlite_type_for({{value}})})"
|
||||
db.exec %(insert into table1 (col1) values (?)), {{value}}
|
||||
db.query_one("select col1 from table1", as: typeof({{value}})).should eq({{value}})
|
||||
end
|
||||
end
|
||||
{% end %}
|
||||
|
@ -170,11 +182,7 @@ describe Driver do
|
|||
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_one("select col1 from table1", as: Time).should eq(value)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -59,6 +59,10 @@ class SQLite3::ResultSet < DB::ResultSet
|
|||
Time.parse read(String), SQLite3::DATE_FORMAT
|
||||
end
|
||||
|
||||
def read(t : Bool.class) : Bool
|
||||
read(Int64) != 0
|
||||
end
|
||||
|
||||
def column_count
|
||||
LibSQLite3.column_count(self)
|
||||
end
|
||||
|
|
|
@ -37,6 +37,10 @@ class SQLite3::Statement < DB::Statement
|
|||
check LibSQLite3.bind_null(self, index)
|
||||
end
|
||||
|
||||
private def bind_arg(index, value : Bool)
|
||||
check LibSQLite3.bind_int(self, index, value ? 1 : 0)
|
||||
end
|
||||
|
||||
private def bind_arg(index, value : Int32)
|
||||
check LibSQLite3.bind_int(self, index, value)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue