Skip to content

Commit f3da928

Browse files
authored
Ensure bit integer values are returned as Int64 (#234)
* Ensure bit integer values are returned as Int64 Fixes #207. The issue here was in our translation from sqlite type to Julia type, we were using `Int`, though in the `sqlitevalue` function that actually retrieved the value from the database, we used `sqlite3_column_int64`. This change just ensures that even in 32-bit systems, we'll return values as Int64, avoiding issues like the `RANDOM()` sqlite function, which returns an Int64 value. * Make test more 32-bit friendly
1 parent 24b69db commit f3da928

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/SQLite.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ end
366366

367367
# convert SQLite stored type into Julia equivalent
368368
juliatype(x::Integer) =
369-
x == SQLITE_INTEGER ? Int :
369+
x == SQLITE_INTEGER ? Int64 :
370370
x == SQLITE_FLOAT ? Float64 :
371371
x == SQLITE_TEXT ? String :
372372
Any
@@ -377,7 +377,7 @@ function juliatype(decl_typestr::AbstractString,
377377
default::Type = Any)
378378
typeuc = uppercase(decl_typestr)
379379
if typeuc in ("INTEGER", "INT")
380-
return Int
380+
return Int64
381381
elseif typeuc in ("NUMERIC", "REAL", "FLOAT")
382382
return Float64
383383
elseif typeuc == "TEXT"

test/runtests.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ end
4646

4747
db = SQLite.DB(dbfile2)
4848
db = DBInterface.connect(SQLite.DB, dbfile2)
49+
50+
# https://github.com/JuliaDatabases/SQLite.jl/issues/207
51+
ds = DBInterface.execute(db, "SELECT RANDOM() as a FROM Track LIMIT 1") |> columntable
52+
@test ds.a[1] isa Int64
53+
4954
# regular SQLite tests
5055

5156
@test_throws SQLiteException DBInterface.execute(db, "just some syntax error")
@@ -151,9 +156,9 @@ r = DBInterface.execute(db, "SELECT * FROM temp WHERE Title LIKE ?", ["%time%"])
151156
@test r[1] == [76, 111, 187]
152157
DBInterface.execute(db, "INSERT INTO temp VALUES (?1, ?3, ?2)", [0,0, "Test Album"])
153158
r = DBInterface.execute(db, "SELECT * FROM temp WHERE AlbumId = 0") |> columntable
154-
@test r[1][1] === 0
159+
@test r[1][1] == 0
155160
@test r[2][1] == "Test Album"
156-
@test r[3][1] === 0
161+
@test r[3][1] == 0
157162
SQLite.drop!(db, "temp")
158163

159164
DBInterface.execute(db, "CREATE TABLE temp AS SELECT * FROM Album")
@@ -189,10 +194,10 @@ s = DBInterface.execute(db, "SELECT AlbumId FROM Album") |> columntable
189194
@test r[1][1] == s[1][1] + 4
190195

191196
SQLite.@register db mult
192-
r = DBInterface.execute(db, "SELECT Milliseconds, Bytes FROM Track") |> columntable
193-
s = DBInterface.execute(db, "SELECT mult(Milliseconds, Bytes) FROM Track") |> columntable
197+
r = DBInterface.execute(db, "SELECT GenreId, UnitPrice FROM Track") |> columntable
198+
s = DBInterface.execute(db, "SELECT mult(GenreId, UnitPrice) FROM Track") |> columntable
194199
@test (r[1][1] * r[2][1]) == s[1][1]
195-
t = DBInterface.execute(db, "SELECT mult(Milliseconds, Bytes, 3, 4) FROM Track") |> columntable
200+
t = DBInterface.execute(db, "SELECT mult(GenreId, UnitPrice, 3, 4) FROM Track") |> columntable
196201
@test (r[1][1] * r[2][1] * 3 * 4) == t[1][1]
197202

198203
SQLite.@register db sin
@@ -291,16 +296,16 @@ SQLite.clear!(stmt)
291296
rr = (;) # just to have the var declared
292297
@test_logs(
293298
(:warn, "Unsupported SQLite declared type UNKNOWN1, falling back to String type"),
294-
(:warn, "Unsupported SQLite declared type UNKNOWN2, falling back to $(Int) type"),
299+
(:warn, "Unsupported SQLite declared type UNKNOWN2, falling back to $(Int64) type"),
295300
rr = DBInterface.execute(rowtable, binddb, "SELECT * FROM temp"))
296301
@test length(rr) == 1
297302
r = first(rr)
298-
@test typeof.(Tuple(r)) == (Missing, Int, Int,
299-
Float64, Float64, Int,
303+
@test typeof.(Tuple(r)) == (Missing, Int64, Int64,
304+
Float64, Float64, Int64,
300305
String, String, String, String,
301306
String, String,
302307
Base.CodeUnits{UInt8, String},
303-
String, Int)
308+
String, Int64)
304309
end
305310

306311
############################################

0 commit comments

Comments
 (0)