@@ -3,20 +3,7 @@ module SQLite
3
3
using Compat, NullableArrays, CSV, Libz, DataStreams
4
4
import CSV. PointerString
5
5
6
- # TODO
7
- # create old_ui.jl file w/ deprecations
8
- # make all function renames
9
- # make new types SQLite.DB/Stmt, get code from jq/updates
10
- # deprecate all old function names/types
11
- # create Source.jl, Sink.jl files
12
- # pull Source code from jq/updates
13
- # stream!(SQLite.Source,DataStream)
14
- # stream!(SQLite.Source,CSV.Sink)
15
- # stream!(DataStream,SQLite.Sink)
16
- # stream!(CSV.Source,SQLite.Sink)
17
- # rewrite tests
18
- # add new tests file for Source/Sink
19
-
6
+ # Deprecated exports
20
7
export NULL, SQLiteDB, SQLiteStmt, ResultSet,
21
8
execute, query, tables, indices, columns, droptable, dropindex,
22
9
create, createindex, append, deleteduplicates
@@ -35,7 +22,7 @@ immutable NullType end
35
22
const NULL = NullType ()
36
23
show (io:: IO ,:: NullType ) = print (io," #NULL" )
37
24
38
- # internal wrapper type to, in-effect, mark something which has been serialized
25
+ " internal wrapper type to, in-effect, mark something which has been serialized"
39
26
immutable Serialization
40
27
object
41
28
end
@@ -66,7 +53,9 @@ type DB
66
53
end
67
54
end
68
55
end
56
+ " `SQLite.DB(file::AbstractString)` opens or creates an SQLite database with `file`"
69
57
DB (f:: AbstractString ) = DB (utf8 (f))
58
+ " `SQLite.DB()` creates an in-memory SQLite database"
70
59
DB () = DB (" :memory:" )
71
60
72
61
function _close (db:: DB )
77
66
78
67
Base. show (io:: IO , db:: SQLite.DB ) = print (io, string (" SQLite.DB(" ,db. file == " :memory:" ? " in-memory" : " \" $(db. file) \" " ," )" ))
79
68
69
+ """
70
+ `SQLite.Stmt(db::DB, sql::AbstractString)` creates and prepares an SQLite statement
71
+ """
80
72
type Stmt
81
73
db:: DB
82
74
handle:: Ptr{Void}
@@ -110,15 +102,15 @@ include("UDF.jl")
110
102
include (" old_ui.jl" )
111
103
export @sr_str , @register , register
112
104
113
- # bind a row to nameless parameters
105
+ " bind a row (`values`) to nameless parameters by index "
114
106
function bind! (stmt:: Stmt , values:: Vector )
115
107
nparams = sqlite3_bind_parameter_count (stmt. handle)
116
108
@assert nparams == length (values) " you must provide values for all placeholders"
117
109
for i in 1 : nparams
118
110
@inbounds bind! (stmt, i, values[i])
119
111
end
120
112
end
121
- # bind a row to named parameters
113
+ " bind a row (`Dict(:key => value)`) to named parameters"
122
114
function bind! {V} (stmt:: Stmt , values:: Dict{Symbol, V} )
123
115
nparams = sqlite3_bind_parameter_count (stmt. handle)
124
116
@assert nparams == length (values) " you must provide values for all placeholders"
@@ -131,23 +123,24 @@ function bind!{V}(stmt::Stmt, values::Dict{Symbol, V})
131
123
end
132
124
end
133
125
# Binding parameters to SQL statements
126
+ " bind `val` to the named parameter `name`"
134
127
function bind! (stmt:: Stmt ,name:: AbstractString ,val)
135
128
i = sqlite3_bind_parameter_index (stmt. handle,name)
136
129
if i == 0
137
130
throw (SQLiteException (" SQL parameter $name not found in $stmt " ))
138
131
end
139
132
return bind! (stmt,i,val)
140
133
end
141
- bind! (stmt:: Stmt ,i:: Int ,val:: AbstractFloat ) = sqlite3_bind_double (stmt. handle,i,Float64 (val))
142
- bind! (stmt:: Stmt ,i:: Int ,val:: Int32 ) = sqlite3_bind_int (stmt. handle,i,val)
143
- bind! (stmt:: Stmt ,i:: Int ,val:: Int64 ) = sqlite3_bind_int64 (stmt. handle,i,val)
144
- bind! (stmt:: Stmt ,i:: Int ,val:: NullType ) = sqlite3_bind_null (stmt. handle,i)
145
- bind! (stmt:: Stmt ,i:: Int ,val:: AbstractString ) = sqlite3_bind_text (stmt. handle,i,val)
146
- bind! (stmt:: Stmt ,i:: Int ,val:: PointerString ) = sqlite3_bind_text (stmt. handle,i,val. ptr,val. len)
147
- bind! (stmt:: Stmt ,i:: Int ,val:: UTF16String ) = sqlite3_bind_text16 (stmt. handle,i,val)
134
+ bind! (stmt:: Stmt ,i:: Int ,val:: AbstractFloat ) = ( sqlite3_bind_double (stmt. handle,i,Float64 (val)); return nothing )
135
+ bind! (stmt:: Stmt ,i:: Int ,val:: Int32 ) = ( sqlite3_bind_int (stmt. handle,i,val); return nothing )
136
+ bind! (stmt:: Stmt ,i:: Int ,val:: Int64 ) = ( sqlite3_bind_int64 (stmt. handle,i,val); return nothing )
137
+ bind! (stmt:: Stmt ,i:: Int ,val:: NullType ) = ( sqlite3_bind_null (stmt. handle,i); return nothing )
138
+ bind! (stmt:: Stmt ,i:: Int ,val:: AbstractString ) = ( sqlite3_bind_text (stmt. handle,i,val); return nothing )
139
+ bind! (stmt:: Stmt ,i:: Int ,val:: PointerString ) = ( sqlite3_bind_text (stmt. handle,i,val. ptr,val. len); return nothing )
140
+ bind! (stmt:: Stmt ,i:: Int ,val:: UTF16String ) = ( sqlite3_bind_text16 (stmt. handle,i,val); return nothing )
148
141
# We may want to track the new ByteVec type proposed at https://github.com/JuliaLang/julia/pull/8964
149
142
# as the "official" bytes type instead of Vector{UInt8}
150
- bind! (stmt:: Stmt ,i:: Int ,val:: Vector{UInt8} ) = sqlite3_bind_blob (stmt. handle,i,val)
143
+ bind! (stmt:: Stmt ,i:: Int ,val:: Vector{UInt8} ) = ( sqlite3_bind_blob (stmt. handle,i,val); return nothing )
151
144
# Fallback is BLOB and defaults to serializing the julia value
152
145
function sqlserialize (x)
153
146
t = IOBuffer ()
@@ -158,12 +151,13 @@ function sqlserialize(x)
158
151
serialize (t,s)
159
152
return takebuf_array (t)
160
153
end
154
+ " bind `val` to the parameter at index `i`"
161
155
bind! (stmt:: Stmt ,i:: Int ,val) = bind! (stmt,i,sqlserialize (val))
162
156
# TODO :
163
157
# int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
164
158
# int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
165
159
166
- # Execute SQL statements
160
+ " Execute a prepared SQLite statement "
167
161
function execute! (stmt:: Stmt )
168
162
r = sqlite3_step (stmt. handle)
169
163
if r == SQLITE_DONE
@@ -173,6 +167,7 @@ function execute!(stmt::Stmt)
173
167
end
174
168
return r
175
169
end
170
+ " Prepare and execute an SQLite statement"
176
171
function execute! (db:: DB ,sql:: AbstractString )
177
172
stmt = Stmt (db,sql)
178
173
return execute! (stmt)
@@ -192,28 +187,26 @@ function sqldeserialize(r)
192
187
end
193
188
194
189
# Transaction-based commands
195
- function transaction (db, mode= " DEFERRED" )
196
- #=
197
- Begin a transaction in the spedified mode, default "DEFERRED".
190
+ """
191
+ Begin a transaction in the spedified `mode`, default = "DEFERRED".
198
192
199
- If mode is one of "", "DEFERRED", "IMMEDIATE" or "EXCLUSIVE" then a
200
- transaction of that (or the default) type is started. Otherwise a savepoint
201
- is created whose name is mode converted to AbstractString.
202
- =#
193
+ If `mode` is one of "", "DEFERRED", "IMMEDIATE" or "EXCLUSIVE" then a
194
+ transaction of that (or the default) type is started. Otherwise a savepoint
195
+ is created whose name is `mode` converted to AbstractString.
196
+ """
197
+ function transaction (db, mode= " DEFERRED" )
198
+ execute! (db," PRAGMA temp_store=MEMORY;" )
203
199
if uppercase (mode) in [" " , " DEFERRED" , " IMMEDIATE" , " EXCLUSIVE" ]
204
200
execute! (db, " BEGIN $(mode) TRANSACTION;" )
205
201
else
206
202
execute! (db, " SAVEPOINT $(mode) ;" )
207
203
end
208
204
end
209
-
205
+ " Execute the function `f` within a transaction. "
210
206
function transaction (f:: Function , db)
211
- #=
212
- Execute the function f within a transaction.
213
- =#
214
207
# generate a random name for the savepoint
215
208
name = string (" SQLITE" ,randstring (10 ))
216
- execute! (db," PRAGMA synchronous = OFF" )
209
+ execute! (db," PRAGMA synchronous = OFF; " )
217
210
transaction (db, name)
218
211
try
219
212
f ()
@@ -223,18 +216,21 @@ function transaction(f::Function, db)
223
216
finally
224
217
# savepoints are not released on rollback
225
218
commit (db, name)
226
- execute! (db," PRAGMA synchronous = ON" )
219
+ execute! (db," PRAGMA synchronous = ON; " )
227
220
end
228
221
end
229
222
230
- # commit a transaction or savepoint (if name is given)
223
+ " commit a transaction or named savepoint "
231
224
commit (db) = execute! (db, " COMMIT TRANSACTION;" )
225
+ " commit a transaction or named savepoint"
232
226
commit (db, name) = execute! (db, " RELEASE SAVEPOINT $(name) ;" )
233
227
234
- # rollback transaction or savepoint (if name is given)
228
+ " rollback transaction or named savepoint "
235
229
rollback (db) = execute! (db, " ROLLBACK TRANSACTION;" )
230
+ " rollback transaction or named savepoint"
236
231
rollback (db, name) = execute! (db, " ROLLBACK TRANSACTION TO SAVEPOINT $(name) ;" )
237
232
233
+ " drop the SQLite table `table` from the database `db`; `ifexists=true` will not return an error if `table` doesn't exist"
238
234
function drop! (db:: DB ,table:: AbstractString ;ifexists:: Bool = false )
239
235
exists = ifexists ? " if exists" : " "
240
236
transaction (db) do
@@ -243,15 +239,19 @@ function drop!(db::DB,table::AbstractString;ifexists::Bool=false)
243
239
execute! (db," vacuum" )
244
240
return
245
241
end
246
-
242
+ " drop the SQLite index `index` from the database `db`; `ifexists=true` will not return an error if `index` doesn't exist "
247
243
function dropindex! (db:: DB ,index:: AbstractString ;ifexists:: Bool = false )
248
244
exists = ifexists ? " if exists" : " "
249
245
transaction (db) do
250
246
execute! (db," drop index $exists $index " )
251
247
end
252
248
return
253
249
end
254
-
250
+ """
251
+ create the SQLite index `index` on the table `table` using `cols`, which may be a single column or comma-delimited list of columns.
252
+ `unique` specifies whether the index will be unique or not.
253
+ `ifnotexists=true` will not throw an error if the index already exists
254
+ """
255
255
function createindex! (db:: DB ,table:: AbstractString ,index:: AbstractString ,cols
256
256
;unique:: Bool = true ,ifnotexists:: Bool = false )
257
257
u = unique ? " unique" : " "
@@ -262,8 +262,8 @@ function createindex!(db::DB,table::AbstractString,index::AbstractString,cols
262
262
execute! (db," analyze $index " )
263
263
return
264
264
end
265
-
266
- function deleteduplicates ! (db,table:: AbstractString ,cols:: AbstractString )
265
+ " removes duplicate rows from `table` based on the values in `cols` which may be a single column or comma-delimited list of columns "
266
+ function removeduplicates ! (db,table:: AbstractString ,cols:: AbstractString )
267
267
transaction (db) do
268
268
execute! (db," delete from $table where rowid not in (select max(rowid) from $table group by $cols );" )
269
269
end
274
274
include (" Source.jl" )
275
275
include (" Sink.jl" )
276
276
277
- end # SQLite module
277
+ end # module
0 commit comments