@@ -16,27 +16,22 @@ sqliteerror(db) = throw(SQLiteException(unsafe_string(sqlite3_errmsg(db.handle))
16
16
sqliteexception (db) = SQLiteException (unsafe_string (sqlite3_errmsg (db. handle)))
17
17
18
18
"""
19
- Represents an SQLite database, either backed by an on-disk file or in-memory
19
+ `SQLite.DB()` => in-memory SQLite database
20
+ `SQLite.DB(file)` => file-based SQLite database
20
21
21
- Constructors:
22
+ Constructors for a representation of an sqlite database, either backed by an on-disk file or in-memory.
22
23
23
- * `SQLite.DB()` => in-memory SQLite database
24
- * `SQLite.DB(file)` => file-based SQLite database
25
-
26
- `SQLite.DB(file::AbstractString)`
27
-
28
- `SQLite.DB` requires the `file` string argument
24
+ `SQLite.DB` requires the `file` string argument in the 2nd definition
29
25
as the name of either a pre-defined SQLite database to be opened,
30
26
or if the file doesn't exist, a database will be created.
31
27
Note that only sqlite 3.x version files are supported.
32
28
33
- The `SQLite.DB` object
34
- represents a single connection to an SQLite database.
29
+ The `SQLite.DB` object represents a single connection to an SQLite database.
35
30
All other SQLite.jl functions take an `SQLite.DB` as the first argument as context.
36
31
37
32
To create an in-memory temporary database, call `SQLite.DB()`.
38
33
39
- The `SQLite.DB` will automatically closed/shutdown when it goes out of scope
34
+ The `SQLite.DB` will be automatically closed/shutdown when it goes out of scope
40
35
(i.e. the end of the Julia session, end of a function call wherein it was created, etc.)
41
36
"""
42
37
mutable struct DB <: DBInterface.Connection
71
66
Base. show (io:: IO , db:: SQLite.DB ) = print (io, string (" SQLite.DB(" , " \" $(db. file) \" " , " )" ))
72
67
73
68
"""
69
+ SQLite.Stmt(db, sql) => SQL.Stmt
70
+
74
71
Constructs and prepares (compiled by the SQLite library)
75
72
an SQL statement in the context of the provided `db`.
76
73
Note the SQL statement is not actually executed,
@@ -88,7 +85,7 @@ mutable struct Stmt <: DBInterface.Statement
88
85
params:: Dict{Int, Any}
89
86
status:: Int
90
87
91
- function Stmt (db:: DB ,sql:: AbstractString )
88
+ function Stmt (db:: DB , sql:: AbstractString )
92
89
handle = Ref {Ptr{Cvoid}} ()
93
90
sqliteprepare (db, sql, handle, Ref {Ptr{Cvoid}} ())
94
91
stmt = new (db, handle[], Dict {Int, Any} (), 0 )
@@ -111,9 +108,9 @@ include("UDF.jl")
111
108
export @sr_str
112
109
113
110
"""
114
- ` SQLite.clear!(stmt::SQLite.Stmt)`
111
+ SQLite.clear!(stmt::SQLite.Stmt)
115
112
116
- clears any bound values to a prepared SQL statement.
113
+ Clears any bound values to a prepared SQL statement
117
114
"""
118
115
function clear! (stmt:: Stmt )
119
116
sqlite3_clear_bindings (stmt. handle)
@@ -122,12 +119,12 @@ function clear!(stmt::Stmt)
122
119
end
123
120
124
121
"""
125
- ` SQLite.bind!(stmt::SQLite.Stmt, values)`
122
+ SQLite.bind!(stmt::SQLite.Stmt, values)
126
123
127
124
bind `values` to parameters in a prepared [`SQLite.Stmt`](@ref). Values can be:
128
125
129
- * `Vector`; where each element will be bound to an SQL parameter by index order
130
- * `Dict`; where dict values will be bound to named SQL parameters by the dict key
126
+ * `Vector` or `Tuple`: where each element will be bound to an SQL parameter by index order
127
+ * `Dict` or `NamedTuple` ; where values will be bound to named SQL parameters by the `Dict`/`NamedTuple` key
131
128
132
129
Additional methods exist for working individual SQL parameters:
133
130
@@ -342,7 +339,7 @@ julia> db = SQLite.DB(mktemp()[1]);
342
339
343
340
julia> tbl |> SQLite.load!(db, "temp");
344
341
345
- julia> SQLite.Query (db,"SELECT * FROM temp WHERE label IN ('a','b','c')") |> DataFrame
342
+ julia> DBInterface.execute (db,"SELECT * FROM temp WHERE label IN ('a','b','c')") |> DataFrame
346
343
4×2 DataFrame
347
344
│ Row │ label │ value │
348
345
│ │ String⍰ │ Float64⍰ │
@@ -354,7 +351,7 @@ julia> SQLite.Query(db,"SELECT * FROM temp WHERE label IN ('a','b','c')") |> Dat
354
351
355
352
julia> q = ['a','b','c'];
356
353
357
- julia> SQLite.Query (db,"SELECT * FROM temp WHERE label IN (\$ (SQLite.esc_id(q)))")
354
+ julia> DBInterface.execute (db,"SELECT * FROM temp WHERE label IN (\$ (SQLite.esc_id(q)))") |> DataFrame
358
355
4×2 DataFrame
359
356
│ Row │ label │ value │
360
357
│ │ String⍰ │ Float64⍰ │
@@ -372,10 +369,8 @@ esc_id(X::AbstractVector{S}) where {S <: AbstractString} = join(map(esc_id, X),
372
369
373
370
# Transaction-based commands
374
371
"""
375
- `SQLite.transaction(db, mode="DEFERRED")`
376
-
377
- `SQLite.transaction(func, db)`
378
-
372
+ SQLite.transaction(db, mode="DEFERRED")
373
+ SQLite.transaction(func, db)
379
374
380
375
Begin a transaction in the specified `mode`, default = "DEFERRED".
381
376
414
409
end
415
410
416
411
"""
417
- `SQLite.commit(db)`
418
-
419
- `SQLite.commit(db, name)`
420
-
412
+ SQLite.commit(db)
413
+ SQLite.commit(db, name)
421
414
422
415
commit a transaction or named savepoint
423
416
"""
@@ -427,10 +420,8 @@ commit(db) = execute(db, "COMMIT TRANSACTION;")
427
420
commit (db, name) = execute (db, " RELEASE SAVEPOINT $(name) ;" )
428
421
429
422
"""
430
- `SQLite.rollback(db)`
431
-
432
- `SQLite.rollback(db, name)`
433
-
423
+ SQLite.rollback(db)
424
+ SQLite.rollback(db, name)
434
425
435
426
rollback transaction or named savepoint
436
427
"""
@@ -440,7 +431,7 @@ rollback(db) = execute(db, "ROLLBACK TRANSACTION;")
440
431
rollback (db, name) = execute (db, " ROLLBACK TRANSACTION TO SAVEPOINT $(name) ;" )
441
432
442
433
"""
443
- ` SQLite.drop!(db, table; ifexists::Bool=true)`
434
+ SQLite.drop!(db, table; ifexists::Bool=true)
444
435
445
436
drop the SQLite table `table` from the database `db`; `ifexists=true` will prevent an error being thrown if `table` doesn't exist
446
437
"""
@@ -454,7 +445,7 @@ function drop!(db::DB, table::AbstractString; ifexists::Bool=false)
454
445
end
455
446
456
447
"""
457
- ` SQLite.dropindex!(db, index; ifexists::Bool=true)`
448
+ SQLite.dropindex!(db, index; ifexists::Bool=true)
458
449
459
450
drop the SQLite index `index` from the database `db`; `ifexists=true` will not return an error if `index` doesn't exist
460
451
"""
@@ -467,6 +458,8 @@ function dropindex!(db::DB, index::AbstractString; ifexists::Bool=false)
467
458
end
468
459
469
460
"""
461
+ SQLite.createindex!(db, table, index, cols; unique=true, ifnotexists=false)
462
+
470
463
create the SQLite index `index` on the table `table` using `cols`,
471
464
which may be a single column or vector of columns.
472
465
`unique` specifies whether the index will be unique or not.
@@ -484,8 +477,9 @@ function createindex!(db::DB, table::AbstractString, index::AbstractString, cols
484
477
end
485
478
486
479
"""
487
- Removes duplicate rows from `table`
488
- based on the values in `cols`, which is an array of column names.
480
+ SQLite.removeduplicates!(db, table, cols)
481
+
482
+ Removes duplicate rows from `table` based on the values in `cols`, which is an array of column names.
489
483
490
484
A convenience method for the common task of removing duplicate
491
485
rows in a dataset according to some subset of columns that make up a "primary key".
@@ -506,35 +500,35 @@ function removeduplicates!(db, table::AbstractString, cols::AbstractArray{T}) wh
506
500
include (" tables.jl" )
507
501
508
502
"""
509
- ` SQLite.tables(db, sink=columntable)`
503
+ SQLite.tables(db, sink=columntable)
510
504
511
505
returns a list of tables in `db`
512
506
"""
513
507
tables (db:: DB , sink= columntable) = DBInterface. execute (db, " SELECT name FROM sqlite_master WHERE type='table';" ) |> sink
514
508
515
509
"""
516
- ` SQLite.indices(db, sink=columntable)`
510
+ SQLite.indices(db, sink=columntable)
517
511
518
512
returns a list of indices in `db`
519
513
"""
520
514
indices (db:: DB , sink= columntable) = DBInterface. execute (db, " SELECT name FROM sqlite_master WHERE type='index';" ) |> sink
521
515
522
516
"""
523
- ` SQLite.columns(db, table, sink=columntable)`
517
+ SQLite.columns(db, table, sink=columntable)
524
518
525
519
returns a list of columns in `table`
526
520
"""
527
521
columns (db:: DB , table:: AbstractString , sink= columntable) = DBInterface. execute (db, " PRAGMA table_info($(esc_id (table)) )" ) |> sink
528
522
529
523
"""
530
- ` SQLite.last_insert_rowid(db)`
524
+ SQLite.last_insert_rowid(db)
531
525
532
526
returns the auto increment id of the last row
533
527
"""
534
528
last_insert_rowid (db:: DB ) = sqlite3_last_insert_rowid (db. handle)
535
529
536
530
"""
537
- ` SQLite.enable_load_extension(db, enable::Bool=true)`
531
+ SQLite.enable_load_extension(db, enable::Bool=true)
538
532
539
533
Enables extension loading (off by default) on the sqlite database `db`. Pass `false` as the second argument to disable.
540
534
"""
0 commit comments