Skip to content

Commit 2a7dee7

Browse files
committed
Add ifexists and ifnotexists keywords to create/createindex and drop/dropindex. Fixes #52.
1 parent f8b3a9c commit 2a7dee7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/SQLite.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,19 @@ commit(db, name) = execute(db, "RELEASE SAVEPOINT $(name);")
292292
rollback(db) = execute(db, "ROLLBACK TRANSACTION;")
293293
rollback(db, name) = execute(db, "ROLLBACK TRANSACTION TO SAVEPOINT $(name);")
294294

295-
function drop(db::SQLiteDB,table::AbstractString)
295+
function drop(db::SQLiteDB,table::AbstractString;ifexists::Bool=false)
296+
exists = ifexists ? "if exists" : ""
296297
transaction(db) do
297-
execute(db,"drop table $table")
298+
execute(db,"drop table $exists $table")
298299
end
299300
execute(db,"vacuum")
300301
return changes(db)
301302
end
302303

303-
function dropindex(db::SQLiteDB,index::AbstractString)
304+
function dropindex(db::SQLiteDB,index::AbstractString;ifexists::Bool=false)
305+
exists = ifexists ? "if exists" : ""
304306
transaction(db) do
305-
execute(db,"drop index $index")
307+
execute(db,"drop index $exists $index")
306308
end
307309
return changes(db)
308310
end
@@ -314,7 +316,9 @@ gettype(::Type) = " BLOB"
314316
gettype(::Type{NullType}) = " NULL"
315317

316318
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)
318322
N, M = size(table)
319323
colnames = isempty(colnames) ? ["x$i" for i=1:M] : colnames
320324
coltypes = isempty(coltypes) ? [typeof(table[1,i]) for i=1:M] : coltypes
@@ -323,7 +327,8 @@ function create(db::SQLiteDB,name::AbstractString,table,
323327
transaction(db) do
324328
# create table statement
325329
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,',')))")
327332
# insert statements
328333
params = chop(repeat("?,",M))
329334
stmt = SQLiteStmt(db,"insert into $name values ($params)")
@@ -340,10 +345,12 @@ function create(db::SQLiteDB,name::AbstractString,table,
340345
return changes(db)
341346
end
342347

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)
344350
u = unique ? "unique" : ""
351+
exists = ifnotexists ? "if not exists" : ""
345352
transaction(db) do
346-
execute(db,"create $u index $index on $table ($cols)")
353+
execute(db,"create $u index $exists $index on $table ($cols)")
347354
end
348355
execute(db,"analyze $index")
349356
return changes(db)

0 commit comments

Comments
 (0)