Skip to content

Commit 3442e6d

Browse files
authored
Move to DataStreams 0.2.0 (#131)
1 parent c774148 commit 3442e6d

File tree

12 files changed

+365
-493
lines changed

12 files changed

+365
-493
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ os:
77
- osx
88

99
julia:
10-
- 0.5
10+
- 0.6
1111
- nightly
1212

1313
notifications:

REQUIRE

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
julia 0.5
1+
julia 0.6
22
BinDeps
33
@osx Homebrew
44
@windows WinRPM
5-
DataStreams 0.1.0
5+
DataStreams 0.2.0
66
DataFrames
7-
WeakRefStrings 0.1.3
7+
WeakRefStrings 0.3.0
88
LegacyStrings
9-
Compat 0.9.5

appveyor.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
environment:
22
matrix:
3-
- JULIAVERSION: "julialang/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIAVERSION: "julialang/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
5-
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
6-
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
3+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
4+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
5+
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
6+
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
77

88
branches:
99
only:
@@ -17,14 +17,15 @@ notifications:
1717
on_build_status_changed: false
1818

1919
install:
20+
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
2021
# if there's a newer build queued for the same PR, cancel this one
2122
- ps: if ($env:APPVEYOR_PULL_REQUEST_NUMBER -and $env:APPVEYOR_BUILD_NUMBER -ne ((Invoke-RestMethod `
2223
https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/history?recordsNumber=50).builds | `
2324
Where-Object pullRequestId -eq $env:APPVEYOR_PULL_REQUEST_NUMBER)[0].buildNumber) { `
2425
throw "There are newer queued builds for this pull request, failing early." }
2526
# Download most recent Julia Windows binary
2627
- ps: (new-object net.webclient).DownloadFile(
27-
$("http://s3.amazonaws.com/"+$env:JULIAVERSION),
28+
$env:JULIA_URL,
2829
"C:\projects\julia-binary.exe")
2930
# Run installer silently, output to C:\projects\julia
3031
- C:\projects\julia-binary.exe /S /D=C:\projects\julia
@@ -37,4 +38,4 @@ build_script:
3738
- where *sqlite3*
3839

3940
test_script:
40-
- C:\projects\julia\bin\julia -e "Pkg.test(\"SQLite\")"
41+
- C:\projects\julia\bin\julia -e "Pkg.test(\"SQLite\")"

src/SQLite.jl

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

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

77
export Data, DataFrame
88

9-
type SQLiteException <: Exception
9+
struct SQLiteException <: Exception
1010
msg::AbstractString
1111
end
1212

1313
include("consts.jl")
1414
include("api.jl")
1515

16-
immutable NullType end
17-
# custom NULL value for interacting with the SQLite database
18-
const NULL = NullType()
19-
show(io::IO,::NullType) = print(io,"#NULL")
20-
2116
#TODO: Support sqlite3_open_v2
2217
# Normal constructor from filename
23-
sqliteopen(file,handle) = sqlite3_open(file,handle)
24-
sqliteopen(file::UTF16String,handle) = sqlite3_open16(file,handle)
18+
sqliteopen(file, handle) = sqlite3_open(file, handle)
19+
sqliteopen(file::UTF16String, handle) = sqlite3_open16(file, handle)
2520
sqliteerror() = throw(SQLiteException(unsafe_string(sqlite3_errmsg())))
2621
sqliteerror(db) = throw(SQLiteException(unsafe_string(sqlite3_errmsg(db.handle))))
2722

@@ -33,7 +28,7 @@ Constructors:
3328
* `SQLite.DB()` => in-memory SQLite database
3429
* `SQLite.DB(file)` => file-based SQLite database
3530
"""
36-
type DB
31+
mutable struct DB
3732
file::String
3833
handle::Ptr{Void}
3934
changes::Int
@@ -59,19 +54,19 @@ function _close(db::DB)
5954
return
6055
end
6156

62-
Base.show(io::IO, db::SQLite.DB) = print(io, string("SQLite.DB(",db.file == ":memory:" ? "in-memory" : "\"$(db.file)\"",")"))
57+
Base.show(io::IO, db::SQLite.DB) = print(io, string("SQLite.DB(", db.file == ":memory:" ? "in-memory" : "\"$(db.file)\"", ")"))
6358

6459
"""
6560
`SQLite.Stmt(db::DB, sql::AbstractString)` creates and prepares an SQLite statement
6661
"""
67-
type Stmt
62+
mutable struct Stmt
6863
db::DB
6964
handle::Ptr{Void}
7065

7166
function Stmt(db::DB,sql::AbstractString)
7267
handle = Ref{Ptr{Void}}()
73-
sqliteprepare(db,sql,handle,Ref{Ptr{Void}}())
74-
stmt = new(db,handle[])
68+
sqliteprepare(db, sql, handle, Ref{Ptr{Void}}())
69+
stmt = new(db, handle[])
7570
finalizer(stmt, _close)
7671
return stmt
7772
end
@@ -83,7 +78,7 @@ function _close(stmt::Stmt)
8378
return
8479
end
8580

86-
sqliteprepare(db,sql,stmt,null) = @CHECK db sqlite3_prepare_v2(db.handle,sql,stmt,null)
81+
sqliteprepare(db, sql, stmt, null) = @CHECK db sqlite3_prepare_v2(db.handle, sql, stmt, null)
8782

8883
include("UDF.jl")
8984
export @sr_str, @register, register
@@ -127,7 +122,7 @@ function bind!(stmt::Stmt, values::Vector)
127122
@inbounds bind!(stmt, i, values[i])
128123
end
129124
end
130-
function bind!{V}(stmt::Stmt, values::Dict{Symbol, V})
125+
function bind!(stmt::Stmt, values::Dict{Symbol, V}) where {V}
131126
nparams = sqlite3_bind_parameter_count(stmt.handle)
132127
@assert nparams == length(values) "you must provide values for all placeholders"
133128
for i in 1:nparams
@@ -139,56 +134,58 @@ function bind!{V}(stmt::Stmt, values::Dict{Symbol, V})
139134
end
140135
end
141136
# Binding parameters to SQL statements
142-
function bind!(stmt::Stmt,name::AbstractString,val)
143-
i::Int = sqlite3_bind_parameter_index(stmt.handle,name)
137+
function bind!(stmt::Stmt,name::AbstractString, val)
138+
i::Int = sqlite3_bind_parameter_index(stmt.handle, name)
144139
if i == 0
145140
throw(SQLiteException("SQL parameter $name not found in $stmt"))
146141
end
147-
return bind!(stmt,i,val)
142+
return bind!(stmt, i, val)
148143
end
149-
bind!(stmt::Stmt,i::Int,val::AbstractFloat) = (sqlite3_bind_double(stmt.handle,i,Float64(val)); return nothing)
150-
bind!(stmt::Stmt,i::Int,val::Int32) = (sqlite3_bind_int(stmt.handle,i,val); return nothing)
151-
bind!(stmt::Stmt,i::Int,val::Int64) = (sqlite3_bind_int64(stmt.handle,i,val); return nothing)
152-
bind!(stmt::Stmt,i::Int,val::NullType) = (sqlite3_bind_null(stmt.handle,i); return nothing)
153-
bind!(stmt::Stmt,i::Int,val::AbstractString) = (sqlite3_bind_text(stmt.handle,i,val); return nothing)
154-
bind!(stmt::Stmt,i::Int,val::WeakRefString{UInt8}) = (sqlite3_bind_text(stmt.handle,i,val.ptr,val.len); return nothing)
155-
bind!(stmt::Stmt,i::Int,val::WeakRefString{UInt16}) = (sqlite3_bind_text16(stmt.handle,i,val.ptr,val.len*2); return nothing)
156-
bind!(stmt::Stmt,i::Int,val::UTF16String) = (sqlite3_bind_text16(stmt.handle,i,val); return nothing)
157-
function bind!(stmt::Stmt,i::Int,val::WeakRefString{UInt32})
144+
bind!(stmt::Stmt, i::Int, val::AbstractFloat) = (sqlite3_bind_double(stmt.handle, i ,Float64(val)); return nothing)
145+
bind!(stmt::Stmt, i::Int, val::Int32) = (sqlite3_bind_int(stmt.handle, i ,val); return nothing)
146+
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)
148+
bind!(stmt::Stmt, i::Int, val::AbstractString) = (sqlite3_bind_text(stmt.handle, i ,val); return nothing)
149+
bind!(stmt::Stmt, i::Int, val::WeakRefString{UInt8}) = (sqlite3_bind_text(stmt.handle, i, val.ptr, val.len); return nothing)
150+
bind!(stmt::Stmt, i::Int, val::WeakRefString{UInt16}) = (sqlite3_bind_text16(stmt.handle, i, val.ptr, val.len*2); return nothing)
151+
bind!(stmt::Stmt, i::Int, val::UTF16String) = (sqlite3_bind_text16(stmt.handle, i, val); return nothing)
152+
function bind!(stmt::Stmt, i::Int, val::WeakRefString{UInt32})
158153
A = UTF32String(pointer_to_array(val.ptr, val.len+1, false))
159-
return bind!(stmt, i, convert(String,A))
154+
return bind!(stmt, i, convert(String, A))
160155
end
161-
# We may want to track the new ByteVec type proposed at https://github.com/JuliaLang/julia/pull/8964
162-
# as the "official" bytes type instead of Vector{UInt8}
163-
bind!(stmt::Stmt,i::Int,val::Vector{UInt8}) = (sqlite3_bind_blob(stmt.handle,i,val); return nothing)
156+
# We may want to track the new ByteVec mutable struct proposed at https://github.com/JuliaLang/julia/pull/8964
157+
# as the "official" bytes mutable struct instead of Vector{UInt8}
158+
bind!(stmt::Stmt, i::Int, val::Vector{UInt8}) = (sqlite3_bind_blob(stmt.handle, i, val); return nothing)
164159
# Fallback is BLOB and defaults to serializing the julia value
165160

166-
# internal wrapper type to, in-effect, mark something which has been serialized
167-
immutable Serialization
161+
# internal wrapper mutable struct to, in-effect, mark something which has been serialized
162+
struct Serialization
168163
object
169164
end
170165

166+
const GLOBAL_BUF = IOBuffer()
171167
function sqlserialize(x)
172-
t = IOBuffer()
168+
seekstart(GLOBAL_BUF)
173169
# deserialize will sometimes return a random object when called on an array
174-
# which has not been previously serialized, we can use this type to check
170+
# which has not been previously serialized, we can use this mutable struct to check
175171
# that the array has been serialized
176172
s = Serialization(x)
177-
serialize(t,s)
178-
return take!(t)
173+
serialize(GLOBAL_BUF, s)
174+
return take!(GLOBAL_BUF)
179175
end
180176
# fallback method to bind arbitrary julia `val` to the parameter at index `i` (object is serialized)
181-
bind!(stmt::Stmt,i::Int,val) = bind!(stmt,i,sqlserialize(val))
177+
bind!(stmt::Stmt, i::Int, val) = bind!(stmt, i, sqlserialize(val))
182178

183-
immutable SerializeError <: Exception
179+
struct SerializeError <: Exception
184180
msg::String
185181
end
186182

187183
# magic bytes that indicate that a value is in fact a serialized julia value, instead of just a byte vector
188-
const SERIALIZATION = UInt8[0x11,0x01,0x02,0x0d,0x53,0x65,0x72,0x69,0x61,0x6c,0x69,0x7a,0x61,0x74,0x69,0x6f,0x6e,0x23]
184+
# const SERIALIZATION = UInt8[0x11,0x01,0x02,0x0d,0x53,0x65,0x72,0x69,0x61,0x6c,0x69,0x7a,0x61,0x74,0x69,0x6f,0x6e,0x23]
185+
const SERIALIZATION = UInt8[0x34,0x10,0x01,0x0d,0x53,0x65,0x72,0x69,0x61,0x6c,0x69,0x7a,0x61,0x74,0x69,0x6f,0x6e,0x1f]
189186
function sqldeserialize(r)
190-
ret = ccall(:memcmp, Int32, (Ptr{UInt8},Ptr{UInt8}, UInt),
191-
SERIALIZATION, r, min(18,length(r)))
187+
ret = ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt),
188+
SERIALIZATION, r, min(18, length(r)))
192189
if ret == 0
193190
try
194191
v = deserialize(IOBuffer(r))
@@ -223,8 +220,8 @@ function execute!(stmt::Stmt)
223220
end
224221
return r
225222
end
226-
function execute!(db::DB,sql::AbstractString)
227-
stmt = Stmt(db,sql)
223+
function execute!(db::DB, sql::AbstractString)
224+
stmt = Stmt(db, sql)
228225
return execute!(stmt)
229226
end
230227

@@ -237,8 +234,8 @@ A vector of identifiers will be separated by commas.
237234
"""
238235
function esc_id end
239236

240-
esc_id(x::AbstractString) = "\""*replace(x,"\"","\"\"")*"\""
241-
esc_id{S<:AbstractString}(X::AbstractVector{S}) = join(map(esc_id,X),',')
237+
esc_id(x::AbstractString) = "\"" * replace(x,"\"","\"\"") * "\""
238+
esc_id(X::AbstractVector{S}) where {S <: AbstractString} = join(map(esc_id, X), ',')
242239

243240

244241
# Transaction-based commands
@@ -251,15 +248,15 @@ esc_id{S<:AbstractString}(X::AbstractVector{S}) = join(map(esc_id,X),',')
251248
Begin a transaction in the specified `mode`, default = "DEFERRED".
252249
253250
If `mode` is one of "", "DEFERRED", "IMMEDIATE" or "EXCLUSIVE" then a
254-
transaction of that (or the default) type is started. Otherwise a savepoint
251+
transaction of that (or the default) mutable struct is started. Otherwise a savepoint
255252
is created whose name is `mode` converted to AbstractString.
256253
257254
In the second method, `func` is executed within a transaction (the transaction being committed upon successful execution)
258255
"""
259256
function transaction end
260257

261258
function transaction(db, mode="DEFERRED")
262-
execute!(db,"PRAGMA temp_store=MEMORY;")
259+
execute!(db, "PRAGMA temp_store=MEMORY;")
263260
if uppercase(mode) in ["", "DEFERRED", "IMMEDIATE", "EXCLUSIVE"]
264261
execute!(db, "BEGIN $(mode) TRANSACTION;")
265262
else
@@ -317,9 +314,9 @@ drop the SQLite table `table` from the database `db`; `ifexists=true` will preve
317314
function drop!(db::DB, table::AbstractString; ifexists::Bool=false)
318315
exists = ifexists ? "IF EXISTS" : ""
319316
transaction(db) do
320-
execute!(db,"DROP TABLE $exists $(esc_id(table))")
317+
execute!(db, "DROP TABLE $exists $(esc_id(table))")
321318
end
322-
execute!(db,"VACUUM")
319+
execute!(db, "VACUUM")
323320
return
324321
end
325322

@@ -328,10 +325,10 @@ end
328325
329326
drop the SQLite index `index` from the database `db`; `ifexists=true` will not return an error if `index` doesn't exist
330327
"""
331-
function dropindex!(db::DB,index::AbstractString;ifexists::Bool=false)
328+
function dropindex!(db::DB, index::AbstractString; ifexists::Bool=false)
332329
exists = ifexists ? "IF EXISTS" : ""
333330
transaction(db) do
334-
execute!(db,"DROP INDEX $exists $(esc_id(index))")
331+
execute!(db, "DROP INDEX $exists $(esc_id(index))")
335332
end
336333
return
337334
end
@@ -343,14 +340,14 @@ create the SQLite index `index` on the table `table` using `cols`, which may be
343340
`unique` specifies whether the index will be unique or not.
344341
`ifnotexists=true` will not throw an error if the index already exists
345342
"""
346-
function createindex!{S<:AbstractString}(db::DB,table::AbstractString,index::AbstractString,cols::Union{S,AbstractVector{S}}
347-
;unique::Bool=true,ifnotexists::Bool=false)
343+
function createindex!(db::DB, table::AbstractString, index::AbstractString, cols::Union{S, AbstractVector{S}};
344+
unique::Bool=true, ifnotexists::Bool=false) where {S <: AbstractString}
348345
u = unique ? "UNIQUE" : ""
349346
exists = ifnotexists ? "IF NOT EXISTS" : ""
350347
transaction(db) do
351-
execute!(db,"CREATE $u INDEX $exists $(esc_id(index)) ON $(esc_id(table)) ($(esc_id(cols)))")
348+
execute!(db, "CREATE $u INDEX $exists $(esc_id(index)) ON $(esc_id(table)) ($(esc_id(cols)))")
352349
end
353-
execute!(db,"ANALYZE $index")
350+
execute!(db, "ANALYZE $index")
354351
return
355352
end
356353

@@ -359,32 +356,33 @@ end
359356
360357
removes duplicate rows from `table` based on the values in `cols` which is an array of column names
361358
"""
362-
function removeduplicates!{T <: AbstractString}(db,table::AbstractString,cols::AbstractArray{T})
359+
function removeduplicates!(db, table::AbstractString, cols::AbstractArray{T}) where {T <: AbstractString}
363360
colsstr = ""
364361
for c in cols
365362
colsstr = colsstr * esc_id(c) * ","
366363
end
367364
colsstr = chop(colsstr)
368365
transaction(db) do
369-
execute!(db,"DELETE FROM $(esc_id(table)) WHERE _ROWID_ NOT IN (SELECT max(_ROWID_) from $(esc_id(table)) GROUP BY $(colsstr));")
366+
execute!(db, "DELETE FROM $(esc_id(table)) WHERE _ROWID_ NOT IN (SELECT max(_ROWID_) from $(esc_id(table)) GROUP BY $(colsstr));")
370367
end
371-
execute!(db,"ANALYZE $table")
368+
execute!(db, "ANALYZE $table")
372369
return
373370
end
374371

375372
"`SQLite.Source` implements the `Source` interface in the `DataStreams` framework"
376-
type Source <: Data.Source
373+
mutable struct Source <: Data.Source
377374
schema::Data.Schema
378375
stmt::Stmt
379376
status::Cint
380377
end
381378

382379
"SQLite.Sink implements the `Sink` interface in the `DataStreams` framework"
383-
type Sink <: Data.Sink
380+
mutable struct Sink <: Data.Sink
384381
db::DB
385382
tablename::String
386383
stmt::Stmt
387384
transaction::String
385+
cols::Int
388386
end
389387

390388
include("Source.jl")

0 commit comments

Comments
 (0)