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