Skip to content

Commit 61628a2

Browse files
committed
Port to Missings
1 parent 3442e6d commit 61628a2

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

src/SQLite.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__precompile__(true)
22
module SQLite
33

4-
using Nulls, DataStreams, WeakRefStrings, LegacyStrings, DataFrames
4+
using Missings, DataStreams, WeakRefStrings, LegacyStrings, DataFrames
55
import LegacyStrings: UTF16String
66

77
export Data, DataFrame
@@ -144,7 +144,7 @@ end
144144
bind!(stmt::Stmt, i::Int, val::AbstractFloat) = (sqlite3_bind_double(stmt.handle, i ,Float64(val)); return nothing)
145145
bind!(stmt::Stmt, i::Int, val::Int32) = (sqlite3_bind_int(stmt.handle, i ,val); return nothing)
146146
bind!(stmt::Stmt, i::Int, val::Int64) = (sqlite3_bind_int64(stmt.handle, i ,val); return nothing)
147-
bind!(stmt::Stmt, i::Int, val::Null) = (sqlite3_bind_null(stmt.handle, i ); return nothing)
147+
bind!(stmt::Stmt, i::Int, val::Missing) = (sqlite3_bind_null(stmt.handle, i ); return nothing)
148148
bind!(stmt::Stmt, i::Int, val::AbstractString) = (sqlite3_bind_text(stmt.handle, i ,val); return nothing)
149149
bind!(stmt::Stmt, i::Int, val::WeakRefString{UInt8}) = (sqlite3_bind_text(stmt.handle, i, val.ptr, val.len); return nothing)
150150
bind!(stmt::Stmt, i::Int, val::WeakRefString{UInt16}) = (sqlite3_bind_text16(stmt.handle, i, val.ptr, val.len*2); return nothing)

src/Sink.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sqlitetype(::Type{T}) where {T<:Integer} = "INT"
22
sqlitetype(::Type{T}) where {T<:AbstractFloat} = "REAL"
33
sqlitetype(::Type{T}) where {T<:AbstractString} = "TEXT"
4-
sqlitetype(::Type{Null}) = "NULL"
4+
sqlitetype(::Type{Missing}) = "NULL"
55
sqlitetype(x) = "BLOB"
66

77
function createtable!(db::DB, name::AbstractString, schema::Data.Schema; temp::Bool=false, ifnotexists::Bool=true)

src/Source.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Independently constructs an `SQLite.Source` in `db` with the SQL statement `sql`
55
Will bind `values` to any parameters in `sql`.
66
`rows` is used to indicate how many rows to return in the query result if known beforehand. `rows=0` (the default) will return all possible rows.
77
`stricttypes=false` will remove strict column typing in the result set, making each column effectively `Vector{Any}`. `nullable::Bool=true` indicates
8-
whether to allow null values when fetching results; if set to `false` and a null value is encountered, a `NullException` will be thrown.
8+
whether to allow missing values when fetching results; if set to `false` and a missing value is encountered, a `NullException` will be thrown.
99
1010
Note that no results are returned; `sql` is executed, and results are ready to be returned (i.e. streamed to an appropriate `Data.Sink` type)
1111
"""
12-
function Source(db::DB, sql::AbstractString, values=[]; rows::Union{Int, Null}=null, stricttypes::Bool=true, nullable::Bool=true)
12+
function Source(db::DB, sql::AbstractString, values=[]; rows::Union{Int, Missing}=missing, stricttypes::Bool=true, nullable::Bool=true)
1313
stmt = SQLite.Stmt(db, sql)
1414
bind!(stmt, values)
1515
status = SQLite.execute!(stmt)
@@ -19,7 +19,7 @@ function Source(db::DB, sql::AbstractString, values=[]; rows::Union{Int, Null}=n
1919
for i = 1:cols
2020
header[i] = unsafe_string(SQLite.sqlite3_column_name(stmt.handle, i))
2121
if nullable
22-
types[i] = stricttypes ? Union{SQLite.juliatype(stmt.handle, i), Null} : Any
22+
types[i] = stricttypes ? Union{SQLite.juliatype(stmt.handle, i), Missing} : Any
2323
else
2424
types[i] = stricttypes ? SQLite.juliatype(stmt.handle, i) : Any
2525
end
@@ -75,26 +75,26 @@ end
7575
Data.reset!(io::SQLite.Source) = (sqlite3_reset(io.stmt.handle); execute!(io.stmt))
7676
Data.streamtype(::Type{SQLite.Source}, ::Type{Data.Field}) = true
7777

78-
# `T` might be Int, Float64, String, WeakRefString, any Julia type, Any, Null
78+
# `T` might be Int, Float64, String, WeakRefString, any Julia type, Any, Missing
7979
# `t` (the actual type of the value we're returning), might be SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL
80-
# `SQLite.streamfrom` returns the next `Union{T, Null}` value from the `SQLite.Source`
81-
function Data.streamfrom(source::SQLite.Source, ::Type{Data.Field}, ::Type{Union{T, Null}}, row, col) where {T}
80+
# `SQLite.streamfrom` returns the next `Union{T, Missing}` value from the `SQLite.Source`
81+
function Data.streamfrom(source::SQLite.Source, ::Type{Data.Field}, ::Type{Union{T, Missing}}, row, col) where {T}
8282
handle = source.stmt.handle
8383
t = SQLite.sqlite3_column_type(handle, col)
8484
if t == SQLite.SQLITE_NULL
85-
val = null
85+
val = missing
8686
else
8787
TT = SQLite.juliatype(t) # native SQLite Int, Float, and Text types
8888
val = SQLite.sqlitevalue(ifelse(TT === Any && !isbits(T), T, TT), handle, col)
8989
end
9090
col == source.schema.cols && (source.status = sqlite3_step(handle))
91-
return val::Union{T, Null}
91+
return val::Union{T, Missing}
9292
end
9393
function Data.streamfrom(source::SQLite.Source, ::Type{Data.Field}, ::Type{T}, row, col) where {T}
9494
handle = source.stmt.handle
9595
t = SQLite.sqlite3_column_type(handle, col)
9696
if t == SQLite.SQLITE_NULL
97-
throw(Data.NullException("encountered null value in non-null typed column: row = $row, col = $col"))
97+
throw(Data.NullException("encountered missing value in non-missing typed column: row = $row, col = $col"))
9898
else
9999
TT = SQLite.juliatype(t) # native SQLite Int, Float, and Text types
100100
val::T = sqlitevalue(ifelse(TT === Any && !isbits(T), T, TT), handle, col)
@@ -112,13 +112,13 @@ Will bind `values` to any parameters in `sql`.
112112
`rows` is used to indicate how many rows to return in the query result if known beforehand. `rows=0` (the default) will return all possible rows.
113113
`stricttypes=false` will remove strict column typing in the result set, making each column effectively `Vector{Any}`
114114
"""
115-
function query(db::DB, sql::AbstractString, sink=DataFrame, args...; append::Bool=false, transforms::Dict=Dict{Int,Function}(), values=[], rows::Union{Int, Null}=null, stricttypes::Bool=true, nullable::Bool=true)
115+
function query(db::DB, sql::AbstractString, sink=DataFrame, args...; append::Bool=false, transforms::Dict=Dict{Int,Function}(), values=[], rows::Union{Int, Missing}=missing, stricttypes::Bool=true, nullable::Bool=true)
116116
source = Source(db, sql, values; rows=rows, stricttypes=stricttypes, nullable=nullable)
117117
sink = Data.stream!(source, sink; append=append, transforms=transforms, args...)
118118
return Data.close!(sink)
119119
end
120120

121-
function query(db::DB, sql::AbstractString, sink::T; append::Bool=false, transforms::Dict=Dict{Int,Function}(), values=[], rows::Union{Int, Null}=null, stricttypes::Bool=true, nullable::Bool=true) where {T}
121+
function query(db::DB, sql::AbstractString, sink::T; append::Bool=false, transforms::Dict=Dict{Int,Function}(), values=[], rows::Union{Int, Missing}=missing, stricttypes::Bool=true, nullable::Bool=true) where {T}
122122
source = Source(db, sql, values; rows=rows, stricttypes=stricttypes, nullable=nullable)
123123
sink = Data.stream!(source, sink; append=append, transforms=transforms)
124124
return Data.close!(sink)

src/UDF.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function sqlvalue(values, i)
2424
end
2525
end
2626

27-
sqlreturn(context, ::Null) = sqlite3_result_null(context)
27+
sqlreturn(context, ::Missing) = sqlite3_result_null(context)
2828
sqlreturn(context, val::Int32) = sqlite3_result_int(context, val)
2929
sqlreturn(context, val::Int64) = sqlite3_result_int64(context, val)
3030
sqlreturn(context, val::Float64) = sqlite3_result_double(context, val)

0 commit comments

Comments
 (0)