Skip to content

Commit 9ded4ea

Browse files
committed
Try to use sqlite3_column_decltype if possible when determining a resultset column type. Fixes #92
1 parent 3f15c07 commit 9ded4ea

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Source.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ constructs an SQLite.Source from an SQLite.Sink; selects all rows/columns from t
3535
"""
3636
Source(sink::SQLite.Sink, sql::AbstractString="select * from $(sink.tablename)") = Source(sink.db, sql::AbstractString)
3737

38-
function juliatype(handle,col)
38+
function juliatype(handle, col)
39+
t = SQLite.sqlite3_column_decltype(handle, col)
40+
if t != C_NULL
41+
T = juliatype(unsafe_string(t))
42+
T !== Any && return T
43+
end
3944
x = SQLite.sqlite3_column_type(handle, col)
4045
if x == SQLITE_BLOB
4146
val = sqlitevalue(Any,handle, col)
@@ -44,7 +49,8 @@ function juliatype(handle,col)
4449
return juliatype(x)
4550
end
4651
end
47-
juliatype(x) = x == SQLITE_INTEGER ? Int : x == SQLITE_FLOAT ? Float64 : x == SQLITE_TEXT ? String : Any
52+
juliatype(x::Integer) = x == SQLITE_INTEGER ? Int : x == SQLITE_FLOAT ? Float64 : x == SQLITE_TEXT ? String : Any
53+
juliatype(x::String) = x == "INTEGER" ? Int : x in ("NUMERIC","REAL") ? Float64 : x == "TEXT" ? String : Any
4854

4955
sqlitevalue{T<:Union{Signed,Unsigned}}(::Type{T}, handle, col) = convert(T, sqlite3_column_int64(handle, col))
5056
const FLOAT_TYPES = Union{Float16, Float32, Float64} # exclude BigFloat

src/api.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ end
226226
# SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
227227
# SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
228228

229+
function sqlite3_column_decltype(stmt::Ptr{Void},col::Int)
230+
return ccall( (:sqlite3_column_decltype, sqlite3_lib),
231+
Ptr{UInt8}, (Ptr{Void},Cint),
232+
stmt,col-1)
233+
end
229234
# SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
230235
# SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
231236

0 commit comments

Comments
 (0)