Skip to content

Commit 9784573

Browse files
author
Brian J. Cardiff
committed
Add Bool support
* change specs to not use #scalar since it does not have expected type information. * refactor Time spec
1 parent 46709ea commit 9784573

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ DB.open "sqlite3://./data.db" do |db|
4343
end
4444
end
4545
```
46+
47+
### DB::Any
48+
49+
* `Time` is implemented as `TEXT` column using `SQLite3::DATE_FORMAT` format.
50+
* `Bool` is implemented as `INT` column mapping `0`/`1` values.

spec/driver_spec.cr

+19-11
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ def sql(s : String)
44
"#{s.inspect}"
55
end
66

7+
def sql(s : Bool)
8+
"#{s ? 1 : 0}"
9+
end
10+
711
def sql(s)
812
"#{s}"
913
end
1014

1115
def sqlite_type_for(v)
1216
case v
13-
when String ; "text"
14-
when Int32, Int64 ; "int"
15-
when Float32, Float64; "float"
16-
when Time ; "text"
17+
when String ; "text"
18+
when Bool, Int32, Int64; "int"
19+
when Float32, Float64 ; "float"
20+
when Time ; "text"
1721
else
1822
raise "not implemented for #{typeof(v)}"
1923
end
@@ -143,12 +147,20 @@ describe Driver do
143147
end
144148
end
145149

146-
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
150+
{% for value in [true, false, 1, 1_i64, "hello", 1.5, 1.5_f32] %}
147151
it "insert/get value {{value.id}} from table" do
148152
with_db do |db|
149153
db.exec "create table table1 (col1 #{sqlite_type_for({{value}})})"
150154
db.exec %(insert into table1 values (#{sql({{value}})}))
151-
db.scalar("select col1 from table1").should eq({{value}})
155+
db.query_one("select col1 from table1", as: typeof({{value}})).should eq({{value}})
156+
end
157+
end
158+
159+
it "insert/get value {{value.id}} using bind" do
160+
with_db do |db|
161+
db.exec "create table table1 (col1 #{sqlite_type_for({{value}})})"
162+
db.exec %(insert into table1 (col1) values (?)), {{value}}
163+
db.query_one("select col1 from table1", as: typeof({{value}})).should eq({{value}})
152164
end
153165
end
154166
{% end %}
@@ -170,11 +182,7 @@ describe Driver do
170182
value = Time.new(2016, 7, 22, 15, 0, 0, 0)
171183
db.exec "create table table1 (col1 #{sqlite_type_for(value)})"
172184
db.exec %(insert into table1 values (?)), value
173-
174-
db.query "select col1 from table1" do |rs|
175-
rs.move_next
176-
rs.read(Time).should eq(value)
177-
end
185+
db.query_one("select col1 from table1", as: Time).should eq(value)
178186
end
179187
end
180188

src/sqlite3/result_set.cr

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ class SQLite3::ResultSet < DB::ResultSet
5959
Time.parse read(String), SQLite3::DATE_FORMAT
6060
end
6161

62+
def read(t : Bool.class) : Bool
63+
read(Int64) != 0
64+
end
65+
6266
def column_count
6367
LibSQLite3.column_count(self)
6468
end

src/sqlite3/statement.cr

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class SQLite3::Statement < DB::Statement
3737
check LibSQLite3.bind_null(self, index)
3838
end
3939

40+
private def bind_arg(index, value : Bool)
41+
check LibSQLite3.bind_int(self, index, value ? 1 : 0)
42+
end
43+
4044
private def bind_arg(index, value : Int32)
4145
check LibSQLite3.bind_int(self, index, value)
4246
end

0 commit comments

Comments
 (0)