@@ -292,17 +292,19 @@ commit(db, name) = execute(db, "RELEASE SAVEPOINT $(name);")
292
292
rollback (db) = execute (db, " ROLLBACK TRANSACTION;" )
293
293
rollback (db, name) = execute (db, " ROLLBACK TRANSACTION TO SAVEPOINT $(name) ;" )
294
294
295
- function drop (db:: SQLiteDB ,table:: AbstractString )
295
+ function drop (db:: SQLiteDB ,table:: AbstractString ;ifexists:: Bool = false )
296
+ exists = ifexists ? " if exists" : " "
296
297
transaction (db) do
297
- execute (db," drop table $table " )
298
+ execute (db," drop table $exists $ table" )
298
299
end
299
300
execute (db," vacuum" )
300
301
return changes (db)
301
302
end
302
303
303
- function dropindex (db:: SQLiteDB ,index:: AbstractString )
304
+ function dropindex (db:: SQLiteDB ,index:: AbstractString ;ifexists:: Bool = false )
305
+ exists = ifexists ? " if exists" : " "
304
306
transaction (db) do
305
- execute (db," drop index $index " )
307
+ execute (db," drop index $exists $ index" )
306
308
end
307
309
return changes (db)
308
310
end
@@ -314,7 +316,9 @@ gettype(::Type) = " BLOB"
314
316
gettype (:: Type{NullType} ) = " NULL"
315
317
316
318
function create (db:: SQLiteDB ,name:: AbstractString ,table,
317
- colnames= AbstractString[],coltypes= DataType[];temp:: Bool = false )
319
+ colnames= AbstractString[],
320
+ coltypes= DataType[]
321
+ ;temp:: Bool = false ,ifnotexists:: Bool = false )
318
322
N, M = size (table)
319
323
colnames = isempty (colnames) ? [" x$i " for i= 1 : M] : colnames
320
324
coltypes = isempty (coltypes) ? [typeof (table[1 ,i]) for i= 1 : M] : coltypes
@@ -323,7 +327,8 @@ function create(db::SQLiteDB,name::AbstractString,table,
323
327
transaction (db) do
324
328
# create table statement
325
329
t = temp ? " TEMP " : " "
326
- execute (db," CREATE $(t) TABLE $name ($(join (cols,' ,' )) )" )
330
+ exists = ifnotexists ? " if not exists" : " "
331
+ execute (db," CREATE $(t) TABLE $exists $name ($(join (cols,' ,' )) )" )
327
332
# insert statements
328
333
params = chop (repeat (" ?," ,M))
329
334
stmt = SQLiteStmt (db," insert into $name values ($params )" )
@@ -340,10 +345,12 @@ function create(db::SQLiteDB,name::AbstractString,table,
340
345
return changes (db)
341
346
end
342
347
343
- function createindex (db:: SQLiteDB ,table:: AbstractString ,index:: AbstractString ,cols;unique:: Bool = true )
348
+ function createindex (db:: SQLiteDB ,table:: AbstractString ,index:: AbstractString ,cols
349
+ ;unique:: Bool = true ,ifnotexists:: Bool = false )
344
350
u = unique ? " unique" : " "
351
+ exists = ifnotexists ? " if not exists" : " "
345
352
transaction (db) do
346
- execute (db," create $u index $index on $table ($cols )" )
353
+ execute (db," create $u index $exists $ index on $table ($cols )" )
347
354
end
348
355
execute (db," analyze $index " )
349
356
return changes (db)
0 commit comments