Skip to content

Commit c6c943f

Browse files
committed
Merge pull request #68 from simonbyrne/fix04
get working on 0.4
2 parents 83be523 + 17ce653 commit c6c943f

File tree

6 files changed

+48
-62
lines changed

6 files changed

+48
-62
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ A Julia interface to the SQLite library and support for operations on DataFrames
5454
* `execute(stmt::SQLiteStmt)`
5555
`execute(db::SQLiteDB, sql::String)`
5656

57-
Used to execute prepared `SQLiteStmt`. The 2nd method is a convenience method to pass in an SQL statement as a string which gets prepared and executed in one call. This method does not check for or return any results, hence it is only useful for database manipulation methods (i.e. ALTER, CREATE, UPDATE, DROP). To return results, see `query` below. Also consider the `create`, `drop`, and `append` methods for manipulation statements as further SQLite performance tricks are incorporated automatically.
57+
Used to execute prepared `SQLiteStmt`. The 2nd method is a convenience method to pass in an SQL statement as a string which gets prepared and executed in one call. This method does not check for or return any results, hence it is only useful for database manipulation methods (i.e. ALTER, CREATE, UPDATE, DROP). To return results, see `query` below. Also consider the `create`, `droptable`, and `append` methods for manipulation statements as further SQLite performance tricks are incorporated automatically.
5858

5959
* `query(db::SQLiteDB, sql::String, values=[])`
6060

@@ -73,9 +73,9 @@ A Julia interface to the SQLite library and support for operations on DataFrames
7373

7474
Takes the values in `table` and appends (by repeated inserts) to the SQLite table `name`. No column checking is done to ensure correct types, so care should be taken as SQLite is "typeless" in that it allows items of any type to be stored in columns. Transaction handling is automatic as well as performance enhancements.
7575

76-
* `drop(db::SQLiteDB,table::String)`
76+
* `droptable(db::SQLiteDB,table::String)`
7777

78-
`drop` is pretty self-explanatory. It's really just a convenience wrapper around `query` to execute a DROP TABLE command, while also calling "VACUUM" to clean out freed memory from the database.
78+
`droptable` is pretty self-explanatory. It's really just a convenience wrapper around `query` to execute a DROP TABLE command, while also calling "VACUUM" to clean out freed memory from the database.
7979

8080
* `register(db::SQLiteDB, func::Function; nargs::Int=-1, name::AbstractString=string(func), isdeterm::Bool=true)`
8181
* `register(db::SQLiteDB, init, step::Function, final::Function=identity; nargs::Int=-1, name::AbstractString=string(final), isdeterm::Bool=true)`

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
julia 0.3-
22
BinDeps
3+
Compat
34
@osx Homebrew
45
@windows WinRPM

src/SQLite.jl

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
module SQLite
22

3+
using Compat
4+
35
export NULL, SQLiteDB, SQLiteStmt, ResultSet,
4-
execute, query, tables, indices, columns, drop, dropindex,
6+
execute, query, tables, indices, columns, droptable, dropindex,
57
create, createindex, append, deleteduplicates
68

7-
if VERSION < v"0.4.0-dev"
8-
const AbstractString = String
9-
const UInt8 = Uint8
10-
const UInt16 = Uint16
11-
const UInt32 = Uint32
12-
const UInt64 = Uint64
13-
const UInt128 = Uint128
14-
const UInt = Uint
15-
end
9+
import Base: ==, show, convert, bind, close
1610

1711
type SQLiteException <: Exception
1812
msg::AbstractString
@@ -24,7 +18,7 @@ include("api.jl")
2418
# Custom NULL type
2519
immutable NullType end
2620
const NULL = NullType()
27-
Base.show(io::IO,::NullType) = print(io,"NULL")
21+
show(io::IO,::NullType) = print(io,"NULL")
2822

2923
# internal wrapper type to, in-effect, mark something which has been serialized
3024
immutable Serialization
@@ -37,7 +31,7 @@ type ResultSet
3731
end
3832
==(a::ResultSet,b::ResultSet) = a.colnames == b.colnames && a.values == b.values
3933
include("show.jl")
40-
Base.convert(::Type{Matrix},a::ResultSet) = [a[i,j] for i=1:size(a,1), j=1:size(a,2)]
34+
convert(::Type{Matrix},a::ResultSet) = [a[i,j] for i=1:size(a,1), j=1:size(a,2)]
4135

4236
type SQLiteDB{T<:AbstractString}
4337
file::T
@@ -79,7 +73,7 @@ function SQLiteDB(file::AbstractString="";UTF16::Bool=false)
7973
end
8074
end
8175

82-
function Base.close{T}(db::SQLiteDB{T})
76+
function close{T}(db::SQLiteDB{T})
8377
db.handle == C_NULL && return
8478
# ensure SQLiteStmts are finalised
8579
gc()
@@ -107,23 +101,23 @@ function SQLiteStmt{T}(db::SQLiteDB{T},sql::AbstractString)
107101
return stmt
108102
end
109103

110-
function Base.close(stmt::SQLiteStmt)
104+
function close(stmt::SQLiteStmt)
111105
stmt.handle == C_NULL && return
112106
@CHECK stmt.db sqlite3_finalize(stmt.handle)
113107
stmt.handle = C_NULL
114108
return
115109
end
116110

117111
# bind a row to nameless parameters
118-
function Base.bind(stmt::SQLiteStmt, values::Vector)
112+
function bind(stmt::SQLiteStmt, values::Vector)
119113
nparams = sqlite3_bind_parameter_count(stmt.handle)
120114
@assert nparams == length(values) "you must provide values for all placeholders"
121115
for i in 1:nparams
122116
@inbounds bind(stmt, i, values[i])
123117
end
124118
end
125119
# bind a row to named parameters
126-
function Base.bind{V}(stmt::SQLiteStmt, values::Dict{Symbol, V})
120+
function bind{V}(stmt::SQLiteStmt, values::Dict{Symbol, V})
127121
nparams = sqlite3_bind_parameter_count(stmt.handle)
128122
@assert nparams == length(values) "you must provide values for all placeholders"
129123
for i in 1:nparams
@@ -135,22 +129,22 @@ function Base.bind{V}(stmt::SQLiteStmt, values::Dict{Symbol, V})
135129
end
136130
end
137131
# Binding parameters to SQL statements
138-
function Base.bind(stmt::SQLiteStmt,name::AbstractString,val)
132+
function bind(stmt::SQLiteStmt,name::AbstractString,val)
139133
i = sqlite3_bind_parameter_index(stmt.handle,name)
140134
if i == 0
141135
throw(SQLiteException("SQL parameter $name not found in $stmt"))
142136
end
143137
return bind(stmt,i,val)
144138
end
145-
Base.bind(stmt::SQLiteStmt,i::Int,val::FloatingPoint) = @CHECK stmt.db sqlite3_bind_double(stmt.handle,i,float64(val))
146-
Base.bind(stmt::SQLiteStmt,i::Int,val::Int32) = @CHECK stmt.db sqlite3_bind_int(stmt.handle,i,val)
147-
Base.bind(stmt::SQLiteStmt,i::Int,val::Int64) = @CHECK stmt.db sqlite3_bind_int64(stmt.handle,i,val)
148-
Base.bind(stmt::SQLiteStmt,i::Int,val::NullType) = @CHECK stmt.db sqlite3_bind_null(stmt.handle,i)
149-
Base.bind(stmt::SQLiteStmt,i::Int,val::AbstractString) = @CHECK stmt.db sqlite3_bind_text(stmt.handle,i,val)
150-
Base.bind(stmt::SQLiteStmt,i::Int,val::UTF16String) = @CHECK stmt.db sqlite3_bind_text16(stmt.handle,i,val)
139+
bind(stmt::SQLiteStmt,i::Int,val::FloatingPoint) = @CHECK stmt.db sqlite3_bind_double(stmt.handle,i,@compat Float64(val))
140+
bind(stmt::SQLiteStmt,i::Int,val::Int32) = @CHECK stmt.db sqlite3_bind_int(stmt.handle,i,val)
141+
bind(stmt::SQLiteStmt,i::Int,val::Int64) = @CHECK stmt.db sqlite3_bind_int64(stmt.handle,i,val)
142+
bind(stmt::SQLiteStmt,i::Int,val::NullType) = @CHECK stmt.db sqlite3_bind_null(stmt.handle,i)
143+
bind(stmt::SQLiteStmt,i::Int,val::AbstractString) = @CHECK stmt.db sqlite3_bind_text(stmt.handle,i,val)
144+
bind(stmt::SQLiteStmt,i::Int,val::UTF16String) = @CHECK stmt.db sqlite3_bind_text16(stmt.handle,i,val)
151145
# We may want to track the new ByteVec type proposed at https://github.com/JuliaLang/julia/pull/8964
152146
# as the "official" bytes type instead of Vector{UInt8}
153-
Base.bind(stmt::SQLiteStmt,i::Int,val::Vector{UInt8}) = @CHECK stmt.db sqlite3_bind_blob(stmt.handle,i,val)
147+
bind(stmt::SQLiteStmt,i::Int,val::Vector{UInt8}) = @CHECK stmt.db sqlite3_bind_blob(stmt.handle,i,val)
154148
# Fallback is BLOB and defaults to serializing the julia value
155149
function sqlserialize(x)
156150
t = IOBuffer()
@@ -161,7 +155,7 @@ function sqlserialize(x)
161155
serialize(t,s)
162156
return takebuf_array(t)
163157
end
164-
Base.bind(stmt::SQLiteStmt,i::Int,val) = bind(stmt,i,sqlserialize(val))
158+
bind(stmt::SQLiteStmt,i::Int,val) = bind(stmt,i,sqlserialize(val))
165159
#TODO:
166160
#int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
167161
#int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
@@ -293,7 +287,7 @@ commit(db, name) = execute(db, "RELEASE SAVEPOINT $(name);")
293287
rollback(db) = execute(db, "ROLLBACK TRANSACTION;")
294288
rollback(db, name) = execute(db, "ROLLBACK TRANSACTION TO SAVEPOINT $(name);")
295289

296-
function drop(db::SQLiteDB,table::AbstractString;ifexists::Bool=false)
290+
function droptable(db::SQLiteDB,table::AbstractString;ifexists::Bool=false)
297291
exists = ifexists ? "if exists" : ""
298292
transaction(db) do
299293
execute(db,"drop table $exists $table")
@@ -384,4 +378,4 @@ function deleteduplicates(db,table::AbstractString,cols::AbstractString)
384378
return changes(db)
385379
end
386380

387-
end #SQLite module
381+
end #SQLite module

src/UDF.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ sqlreturn(context, val::UTF16String) = sqlite3_result_text16(context, val)
3232
sqlreturn(context, val::AbstractString) = sqlite3_result_text(context, val)
3333
sqlreturn(context, val::Vector{UInt8}) = sqlite3_result_blob(context, val)
3434

35-
sqlreturn(context, val::Bool) = sqlreturn(context, int(val))
35+
sqlreturn(context, val::Bool) = sqlreturn(context, @compat Int(val))
3636
sqlreturn(context, val) = sqlreturn(context, sqlserialize(val))
3737

3838
# Internal method for generating an SQLite scalar function from
@@ -88,7 +88,7 @@ function stepfunc(init, func, fsym=symbol(string(func)*"_step"))
8888
acval = $(init)
8989
valsize = 256
9090
# avoid the garbage collector using malloc
91-
valptr = convert(Ptr{UInt8}, c_malloc(valsize))
91+
valptr = convert(Ptr{UInt8}, Libc.malloc(valsize))
9292
valptr == C_NULL && throw(SQLiteException("memory error"))
9393
else
9494
# size of serialized value is first sizeof(Int) bytes
@@ -107,16 +107,16 @@ function stepfunc(init, func, fsym=symbol(string(func)*"_step"))
107107
try
108108
funcret = sqlserialize($(func)(acval, args...))
109109
catch
110-
c_free(valptr)
110+
Libc.free(valptr)
111111
rethrow()
112112
end
113113

114114
newsize = sizeof(funcret)
115115
if newsize > valsize
116116
# TODO: increase this in a cleverer way?
117-
tmp = convert(Ptr{UInt8}, c_realloc(valptr, newsize))
117+
tmp = convert(Ptr{UInt8}, Libc.realloc(valptr, newsize))
118118
if tmp == C_NULL
119-
c_free(valptr)
119+
Libc.free(valptr)
120120
throw(SQLiteException("memory error"))
121121
else
122122
valptr = tmp
@@ -171,7 +171,7 @@ function finalfunc(init, func, fsym=symbol(string(func)*"_final"))
171171
try
172172
ret = $(func)(acval)
173173
finally
174-
c_free(valptr)
174+
Libc.free(valptr)
175175
end
176176
sqlreturn(context, ret)
177177
end

src/show.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ begin
3939
return position(io)
4040
end
4141
ourstrwidth(x::AbstractString) = strwidth(x) + 2 # -> Int
42-
ourstrwidth(s::Symbol) = int(ccall(:u8_strwidth,
42+
ourstrwidth(s::Symbol) = @compat Int(ccall(:u8_strwidth,
4343
Csize_t,
4444
(Ptr{Uint8}, ),
45-
convert(Ptr{Uint8}, s)))
45+
string(s)))
4646
end
4747

4848
#' @description

test/runtests.jl

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
using Base.Test, SQLite
2-
3-
if VERSION < v"0.4.0-dev"
4-
const AbstractString = String
5-
const UInt8 = Uint8
6-
const UInt16 = Uint16
7-
const UInt32 = Uint32
8-
const UInt64 = Uint64
9-
const UInt128 = Uint128
10-
const UInt = Uint
11-
typealias AssertionError ErrorException
12-
end
1+
using Base.Test, SQLite, Compat
2+
3+
import Base: +, ==
134

145
a = SQLiteDB()
156
b = SQLiteDB(UTF16=true)
@@ -80,15 +71,15 @@ r = query(db,"select * from temp")
8071
@test all(r.values[1] .== 0.0)
8172
@test all([typeof(i) for i in r.values[1]] .== Float64)
8273
@test r.colnames == ["col1","col2","col3","col4","col5"]
83-
@test drop(db,"temp") == EMPTY_RESULTSET
74+
@test droptable(db,"temp") == EMPTY_RESULTSET
8475

8576
create(db,"temp",zeros(5,5))
8677
r = query(db,"select * from temp")
8778
@test size(r) == (5,5)
8879
@test all(r.values[1] .== 0.0)
8980
@test all([typeof(i) for i in r.values[1]] .== Float64)
9081
@test r.colnames == ["x1","x2","x3","x4","x5"]
91-
@test drop(db,"temp") == EMPTY_RESULTSET
82+
@test droptable(db,"temp") == EMPTY_RESULTSET
9283

9384
create(db,"temp",zeros(Int,5,5))
9485
r = query(db,"select * from temp")
@@ -102,7 +93,7 @@ r = query(db,"select * from temp")
10293
@test r.values[1] == Any[0,0,0,0,0,1,1,1,1,1]
10394
@test typeof(r[1,1]) == Int64
10495
@test r.colnames == ["x1","x2","x3","x4","x5"]
105-
@test drop(db,"temp") == EMPTY_RESULTSET
96+
@test droptable(db,"temp") == EMPTY_RESULTSET
10697

10798
if VERSION > v"0.4.0-"
10899
rng = Date(2013):Date(2013,1,5)
@@ -112,7 +103,7 @@ if VERSION > v"0.4.0-"
112103
@test all(r[:,1] .== rng)
113104
@test all([typeof(i) for i in r.values[1]] .== Date)
114105
@test r.colnames == ["x1","x2","x3","x4","x5"]
115-
@test drop(db,"temp") == EMPTY_RESULTSET
106+
@test droptable(db,"temp") == EMPTY_RESULTSET
116107
end
117108

118109
query(db,"CREATE TABLE temp AS SELECT * FROM Album")
@@ -123,11 +114,11 @@ r = query(db, "SELECT * FROM temp WHERE Title LIKE ?", ["%time%"])
123114
query(db, "INSERT INTO temp VALUES (?1, ?3, ?2)", [0,0,"Test Album"])
124115
r = query(db, "SELECT * FROM temp WHERE AlbumId = 0")
125116
@test r == ResultSet(Any["AlbumId", "Title", "ArtistId"], Any[Any[0], Any["Test Album"], Any[0]])
126-
drop(db, "temp")
117+
droptable(db, "temp")
127118

128119
binddb = SQLiteDB()
129120
query(binddb, "CREATE TABLE temp (n NULL, i6 INT, f REAL, s TEXT, a BLOB)")
130-
query(binddb, "INSERT INTO temp VALUES (?1, ?2, ?3, ?4, ?5)", Any[NULL, int64(6), 6.4, "some text", b"bytearray"])
121+
query(binddb, "INSERT INTO temp VALUES (?1, ?2, ?3, ?4, ?5)", Any[NULL, convert(Int64,6), 6.4, "some text", b"bytearray"])
131122
r = query(binddb, "SELECT * FROM temp")
132123
for (v, t) in zip(r.values, [SQLite.NullType, Int64, Float64, AbstractString, Vector{UInt8}])
133124
@test isa(v[1], t)
@@ -163,7 +154,7 @@ if VERSION > v"0.4.0-"
163154
query(db, "INSERT INTO temp VALUES (@lid, :title, \$rid)", Dict(:rid => 0, :lid => 0, :title => "Test Album"))
164155
r = query(db, "SELECT * FROM temp WHERE AlbumId = 0")
165156
@test r == ResultSet(Any["AlbumId", "Title", "ArtistId"], Any[Any[0], Any["Test Album"], Any[0]])
166-
drop(db, "temp")
157+
droptable(db, "temp")
167158
end
168159

169160
r = query(db, sr"SELECT LastName FROM Employee WHERE BirthDate REGEXP '^\d{4}-08'")
@@ -198,7 +189,7 @@ u = query(db, "select sin(milliseconds) from track limit 5")
198189

199190
SQLite.register(db, hypot; nargs=2, name="hypotenuse")
200191
v = query(db, "select hypotenuse(Milliseconds,bytes) from track limit 5")
201-
@test [int(i) for i in v[1]] == [11175621,5521062,3997652,4339106,6301714]
192+
@test [@compat round(Int,i) for i in v[1]] == [11175621,5521062,3997652,4339106,6301714]
202193

203194
SQLite.@register db str2arr(s) = convert(Array{UInt8}, s)
204195
r = query(db, "SELECT str2arr(LastName) FROM Employee LIMIT 2")
@@ -242,7 +233,7 @@ sumpoint(p::Point3D, x, y, z) = p + Point3D(x, y, z)
242233
register(db, Point3D(0, 0, 0), sumpoint)
243234
r = query(db, "SELECT sumpoint(x, y, z) FROM points")
244235
@test r[1][1] == Point3D(12, 15, 18)
245-
drop(db, "points")
236+
droptable(db, "points")
246237

247238
db2 = SQLiteDB()
248239
query(db2, "CREATE TABLE tab1 (r REAL, s INT)")
@@ -252,11 +243,11 @@ query(db2, "CREATE TABLE tab1 (r REAL, s INT)")
252243
create(db2, "tab1", [2.1 3; 3.4 8], ifnotexists=true)
253244
create(db2, "tab2", [2.1 3; 3.4 8])
254245

255-
@test_throws SQLite.SQLiteException drop(db2, "nonexistant")
246+
@test_throws SQLite.SQLiteException droptable(db2, "nonexistant")
256247
# should not throw anything
257-
drop(db2, "nonexistant", ifexists=true)
248+
droptable(db2, "nonexistant", ifexists=true)
258249
# should drop "tab2"
259-
drop(db2, "tab2", ifexists=true)
250+
droptable(db2, "tab2", ifexists=true)
260251
@test !in("tab2", tables(db2)[1])
261252

262253
close(db2)

0 commit comments

Comments
 (0)