@@ -187,6 +187,13 @@ function execute!(db::DB,sql::AbstractString)
187
187
return execute! (stmt)
188
188
end
189
189
190
+ " Escape SQLite identifiers (e.g. column, table or index names). Can be either
191
+ a string, or a vector of strings (note does not check for null characters).
192
+ A vector of identifiers will be separated by commas."
193
+ esc_id (x:: AbstractString ) = " \" " * replace (x," \" " ," \"\" " )* " \" "
194
+ esc_id {S<:AbstractString} (X:: AbstractVector{S} ) = join (map (esc_id,X),' ,' )
195
+
196
+
190
197
# Transaction-based commands
191
198
"""
192
199
Begin a transaction in the spedified `mode`, default = "DEFERRED".
@@ -232,43 +239,46 @@ rollback(db) = execute!(db, "ROLLBACK TRANSACTION;")
232
239
rollback (db, name) = execute! (db, " ROLLBACK TRANSACTION TO SAVEPOINT $(name) ;" )
233
240
234
241
" drop the SQLite table `table` from the database `db`; `ifexists=true` will prevent an error being thrown if `table` doesn't exist"
235
- function drop! (db:: DB ,table:: AbstractString ;ifexists:: Bool = false )
236
- exists = ifexists ? " if exists " : " "
242
+ function drop! (db:: DB , table:: AbstractString ; ifexists:: Bool = false )
243
+ exists = ifexists ? " IF EXISTS " : " "
237
244
transaction (db) do
238
- execute! (db," drop table $exists $table " )
245
+ execute! (db," DROP TABLE $exists $( esc_id ( table)) " )
239
246
end
240
- execute! (db," vacuum " )
247
+ execute! (db," VACUUM " )
241
248
return
242
249
end
250
+
243
251
" drop the SQLite index `index` from the database `db`; `ifexists=true` will not return an error if `index` doesn't exist"
244
252
function dropindex! (db:: DB ,index:: AbstractString ;ifexists:: Bool = false )
245
- exists = ifexists ? " if exists " : " "
253
+ exists = ifexists ? " IF EXISTS " : " "
246
254
transaction (db) do
247
- execute! (db," drop index $exists $index " )
255
+ execute! (db," DROP INDEX $exists $( esc_id ( index)) " )
248
256
end
249
257
return
250
258
end
259
+
251
260
"""
252
- create the SQLite index `index` on the table `table` using `cols`, which may be a single column or comma-delimited list of columns.
261
+ create the SQLite index `index` on the table `table` using `cols`, which may be a single column or vector of columns.
253
262
`unique` specifies whether the index will be unique or not.
254
263
`ifnotexists=true` will not throw an error if the index already exists
255
264
"""
256
- function createindex! (db:: DB ,table:: AbstractString ,index:: AbstractString ,cols
257
- ;unique:: Bool = true ,ifnotexists:: Bool = false )
258
- u = unique ? " unique " : " "
259
- exists = ifnotexists ? " if not exists " : " "
265
+ function createindex! {S<:AbstractString} (db:: DB ,table:: AbstractString ,index:: AbstractString ,cols:: Union{S,AbstractVector{S}}
266
+ ;unique:: Bool = true ,ifnotexists:: Bool = false )
267
+ u = unique ? " UNIQUE " : " "
268
+ exists = ifnotexists ? " IF NOT EXISTS " : " "
260
269
transaction (db) do
261
- execute! (db," create $u index $exists $index on $ table ($cols )" )
270
+ execute! (db," CREATE $u INDEX $exists $( esc_id ( index)) ON $( esc_id ( table)) ($( esc_id ( cols) ) " )
262
271
end
263
- execute! (db," analyze $index " )
272
+ execute! (db," ANALYZE $index " )
264
273
return
265
274
end
275
+
266
276
" removes duplicate rows from `table` based on the values in `cols` which may be a single column or comma-delimited list of columns"
267
277
function removeduplicates! (db,table:: AbstractString ,cols:: AbstractString )
268
278
transaction (db) do
269
- execute! (db," delete from $ table where rowid not in (select max(rowid ) from $table group by $ cols );" )
279
+ execute! (db," DELETE FROM $( esc_id ( table)) WHERE _ROWID_ NOT IN (SELECT max(_ROWID_ ) from $( esc_id ( table)) GROUP BY $( esc_id ( cols)) );" )
270
280
end
271
- execute! (db," analyze $table " )
281
+ execute! (db," ANALYZE $table " )
272
282
return
273
283
end
274
284
0 commit comments