Skip to content

Commit 6d045a2

Browse files
committed
Doc updates for 1.0
1 parent a510e57 commit 6d045a2

File tree

4 files changed

+63
-75
lines changed

4 files changed

+63
-75
lines changed

docs/src/index.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,16 @@ SQLite.@sr_str
3030
SQLite.sqlreturn
3131
```
3232

33-
* `SQLite.sqlreturn(contex, val)`
34-
35-
This function should never be called explicitly. Instead it is exported so that it can be overloaded when necessary, see below.
36-
37-
3833
## User Defined Functions
3934

4035
### [SQLite Regular Expressions](@id regex)
4136

4237
SQLite provides syntax for calling
4338
the [`regexp` function](http://sqlite.org/lang_expr.html#regexp)
44-
from inside `WHERE` clauses.
45-
Unfortunately, however,
46-
SQLite does not provide a default implementation of the `regexp` function,
47-
so SQLite.jl creates one automatically when you open a database.
39+
from inside `WHERE` clauses. Unfortunately, however, sqlite does not provide
40+
a default implementation of the `regexp` function. It can be easily added,
41+
however, by calling `SQLite.@register db SQLite.regexp`
42+
4843
The function can be called in the following ways
4944
(examples using the [Chinook Database](http://chinookdatabase.codeplex.com/))
5045

@@ -55,15 +50,15 @@ julia> db = SQLite.DB("Chinook_Sqlite.sqlite")
5550

5651
julia> # using SQLite's in-built syntax
5752

58-
julia> SQLite.Query(db, "SELECT FirstName, LastName FROM Employee WHERE LastName REGEXP 'e(?=a)'") |> DataFrame
53+
julia> DBInterface.execute(db, "SELECT FirstName, LastName FROM Employee WHERE LastName REGEXP 'e(?=a)'") |> DataFrame
5954
1x2 ResultSet
6055
| Row | "FirstName" | "LastName" |
6156
|-----|-------------|------------|
6257
| 1 | "Jane" | "Peacock" |
6358

6459
julia> # explicitly calling the regexp() function
6560

66-
julia> SQLite.Query(db, "SELECT * FROM Genre WHERE regexp('e[trs]', Name)") |> DataFrame
61+
julia> DBInterface.execute(db, "SELECT * FROM Genre WHERE regexp('e[trs]', Name)") |> DataFrame
6762
6x2 ResultSet
6863
| Row | "GenreId" | "Name" |
6964
|-----|-----------|----------------------|
@@ -76,20 +71,20 @@ julia> SQLite.Query(db, "SELECT * FROM Genre WHERE regexp('e[trs]', Name)") |> D
7671

7772
julia> # you can even do strange things like this if you really want
7873

79-
julia> SQLite.Query(db, "SELECT * FROM Genre ORDER BY GenreId LIMIT 2") |> DataFrame
74+
julia> DBInterface.execute(db, "SELECT * FROM Genre ORDER BY GenreId LIMIT 2") |> DataFrame
8075
2x2 ResultSet
8176
| Row | "GenreId" | "Name" |
8277
|-----|-----------|--------|
8378
| 1 | 1 | "Rock" |
8479
| 2 | 2 | "Jazz" |
8580

86-
julia> SQLite.Query(db, "INSERT INTO Genre VALUES (regexp('^word', 'this is a string'), 'My Genre')") |> DataFrame
81+
julia> DBInterface.execute(db, "INSERT INTO Genre VALUES (regexp('^word', 'this is a string'), 'My Genre')") |> DataFrame
8782
1x1 ResultSet
8883
| Row | "Rows Affected" |
8984
|-----|-----------------|
9085
| 1 | 0 |
9186

92-
julia> SQLite.Query(db, "SELECT * FROM Genre ORDER BY GenreId LIMIT 2") |> DataFrame
87+
julia> DBInterface.execute(db, "SELECT * FROM Genre ORDER BY GenreId LIMIT 2") |> DataFrame
9388
2x2 ResultSet
9489
| Row | "GenreId" | "Name" |
9590
|-----|-----------|------------|
@@ -103,13 +98,13 @@ for example `"\y"` simply becomes `"y"`.
10398
For example, the following two queries are identical:
10499

105100
```julia
106-
julia> SQLite.Query(db, "SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
101+
julia> DBInterface.execute(db, "SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
107102
1x1 ResultSet
108103
| Row | "Rows Affected" |
109104
|-----|-----------------|
110105
| 1 | 0 |
111106

112-
julia> SQLite.Query(db, "SELECT * FROM MediaType WHERE Name REGEXP '-d'") |> DataFrame
107+
julia> DBInterface.execute(db, "SELECT * FROM MediaType WHERE Name REGEXP '-d'") |> DataFrame
113108
1x1 ResultSet
114109
| Row | "Rows Affected" |
115110
|-----|-----------------|
@@ -124,15 +119,15 @@ The previous query can then successfully be run like so:
124119
```julia
125120
julia> # manually escaping backslashes
126121

127-
julia> SQLite.Query(db, "SELECT * FROM MediaType WHERE Name REGEXP '-\\d'") |> DataFrame
122+
julia> DBInterface.execute(db, "SELECT * FROM MediaType WHERE Name REGEXP '-\\d'") |> DataFrame
128123
1x2 ResultSet
129124
| Row | "MediaTypeId" | "Name" |
130125
|-----|---------------|-------------------------------|
131126
| 1 | 3 | "Protected MPEG-4 video file" |
132127

133128
julia> # using sr"..."
134129

135-
julia> SQLite.Query(db, sr"SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
130+
julia> DBInterface.execute(db, sr"SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
136131
1x2 ResultSet
137132
| Row | "MediaTypeId" | "Name" |
138133
|-----|---------------|-------------------------------|
@@ -147,27 +142,27 @@ but it may be changed in the future to escape only characters which are part of
147142

148143
SQLite.jl also provides a way
149144
that you can implement your own [Scalar Functions](https://www.sqlite.org/lang_corefunc.html).
150-
This is done using the [`register`](@ref) function and macro.
145+
This is done using the [`SQLite.register`](@ref) function and macro.
151146

152-
[`@register`](@ref) takes a [`SQLite.DB`](@ref) and a function.
147+
[`SQLite.@register`](@ref) takes a [`SQLite.DB`](@ref) and a function.
153148
The function can be in block syntax:
154149

155150
```julia
156-
julia> @register db function add3(x)
151+
julia> SQLite.@register db function add3(x)
157152
x + 3
158153
end
159154
```
160155

161156
inline function syntax:
162157

163158
```julia
164-
julia> @register db mult3(x) = 3 * x
159+
julia> SQLite.@register db mult3(x) = 3 * x
165160
```
166161

167162
and previously defined functions:
168163

169164
```julia
170-
julia> @register db sin
165+
julia> SQLite.@register db sin
171166
```
172167

173168
The [`SQLite.register`](@ref) function takes optional arguments;
@@ -213,8 +208,7 @@ For this reason the following method was defined:
213208
sqlreturn(context, val::Bool) = sqlreturn(context, int(val))
214209
```
215210

216-
Any new method defined for `sqlreturn`
217-
must take two arguments
211+
Any new method defined for `sqlreturn` must take two arguments
218212
and must pass the first argument straight through as the first argument.
219213

220214
### Custom Aggregate Functions

src/SQLite.jl

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,22 @@ sqliteerror(db) = throw(SQLiteException(unsafe_string(sqlite3_errmsg(db.handle))
1616
sqliteexception(db) = SQLiteException(unsafe_string(sqlite3_errmsg(db.handle)))
1717

1818
"""
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
2021
21-
Constructors:
22+
Constructors for a representation of an sqlite database, either backed by an on-disk file or in-memory.
2223
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
2925
as the name of either a pre-defined SQLite database to be opened,
3026
or if the file doesn't exist, a database will be created.
3127
Note that only sqlite 3.x version files are supported.
3228
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.
3530
All other SQLite.jl functions take an `SQLite.DB` as the first argument as context.
3631
3732
To create an in-memory temporary database, call `SQLite.DB()`.
3833
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
4035
(i.e. the end of the Julia session, end of a function call wherein it was created, etc.)
4136
"""
4237
mutable struct DB <: DBInterface.Connection
@@ -71,6 +66,8 @@ end
7166
Base.show(io::IO, db::SQLite.DB) = print(io, string("SQLite.DB(", "\"$(db.file)\"", ")"))
7267

7368
"""
69+
SQLite.Stmt(db, sql) => SQL.Stmt
70+
7471
Constructs and prepares (compiled by the SQLite library)
7572
an SQL statement in the context of the provided `db`.
7673
Note the SQL statement is not actually executed,
@@ -88,7 +85,7 @@ mutable struct Stmt <: DBInterface.Statement
8885
params::Dict{Int, Any}
8986
status::Int
9087

91-
function Stmt(db::DB,sql::AbstractString)
88+
function Stmt(db::DB, sql::AbstractString)
9289
handle = Ref{Ptr{Cvoid}}()
9390
sqliteprepare(db, sql, handle, Ref{Ptr{Cvoid}}())
9491
stmt = new(db, handle[], Dict{Int, Any}(), 0)
@@ -111,9 +108,9 @@ include("UDF.jl")
111108
export @sr_str
112109

113110
"""
114-
`SQLite.clear!(stmt::SQLite.Stmt)`
111+
SQLite.clear!(stmt::SQLite.Stmt)
115112
116-
clears any bound values to a prepared SQL statement.
113+
Clears any bound values to a prepared SQL statement
117114
"""
118115
function clear!(stmt::Stmt)
119116
sqlite3_clear_bindings(stmt.handle)
@@ -122,12 +119,12 @@ function clear!(stmt::Stmt)
122119
end
123120

124121
"""
125-
`SQLite.bind!(stmt::SQLite.Stmt, values)`
122+
SQLite.bind!(stmt::SQLite.Stmt, values)
126123
127124
bind `values` to parameters in a prepared [`SQLite.Stmt`](@ref). Values can be:
128125
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
131128
132129
Additional methods exist for working individual SQL parameters:
133130
@@ -342,7 +339,7 @@ julia> db = SQLite.DB(mktemp()[1]);
342339
343340
julia> tbl |> SQLite.load!(db, "temp");
344341
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
346343
4×2 DataFrame
347344
│ Row │ label │ value │
348345
│ │ String⍰ │ Float64⍰ │
@@ -354,7 +351,7 @@ julia> SQLite.Query(db,"SELECT * FROM temp WHERE label IN ('a','b','c')") |> Dat
354351
355352
julia> q = ['a','b','c'];
356353
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
358355
4×2 DataFrame
359356
│ Row │ label │ value │
360357
│ │ String⍰ │ Float64⍰ │
@@ -372,10 +369,8 @@ esc_id(X::AbstractVector{S}) where {S <: AbstractString} = join(map(esc_id, X),
372369

373370
# Transaction-based commands
374371
"""
375-
`SQLite.transaction(db, mode="DEFERRED")`
376-
377-
`SQLite.transaction(func, db)`
378-
372+
SQLite.transaction(db, mode="DEFERRED")
373+
SQLite.transaction(func, db)
379374
380375
Begin a transaction in the specified `mode`, default = "DEFERRED".
381376
@@ -414,10 +409,8 @@ end
414409
end
415410

416411
"""
417-
`SQLite.commit(db)`
418-
419-
`SQLite.commit(db, name)`
420-
412+
SQLite.commit(db)
413+
SQLite.commit(db, name)
421414
422415
commit a transaction or named savepoint
423416
"""
@@ -427,10 +420,8 @@ commit(db) = execute(db, "COMMIT TRANSACTION;")
427420
commit(db, name) = execute(db, "RELEASE SAVEPOINT $(name);")
428421

429422
"""
430-
`SQLite.rollback(db)`
431-
432-
`SQLite.rollback(db, name)`
433-
423+
SQLite.rollback(db)
424+
SQLite.rollback(db, name)
434425
435426
rollback transaction or named savepoint
436427
"""
@@ -440,7 +431,7 @@ rollback(db) = execute(db, "ROLLBACK TRANSACTION;")
440431
rollback(db, name) = execute(db, "ROLLBACK TRANSACTION TO SAVEPOINT $(name);")
441432

442433
"""
443-
`SQLite.drop!(db, table; ifexists::Bool=true)`
434+
SQLite.drop!(db, table; ifexists::Bool=true)
444435
445436
drop the SQLite table `table` from the database `db`; `ifexists=true` will prevent an error being thrown if `table` doesn't exist
446437
"""
@@ -454,7 +445,7 @@ function drop!(db::DB, table::AbstractString; ifexists::Bool=false)
454445
end
455446

456447
"""
457-
`SQLite.dropindex!(db, index; ifexists::Bool=true)`
448+
SQLite.dropindex!(db, index; ifexists::Bool=true)
458449
459450
drop the SQLite index `index` from the database `db`; `ifexists=true` will not return an error if `index` doesn't exist
460451
"""
@@ -467,6 +458,8 @@ function dropindex!(db::DB, index::AbstractString; ifexists::Bool=false)
467458
end
468459

469460
"""
461+
SQLite.createindex!(db, table, index, cols; unique=true, ifnotexists=false)
462+
470463
create the SQLite index `index` on the table `table` using `cols`,
471464
which may be a single column or vector of columns.
472465
`unique` specifies whether the index will be unique or not.
@@ -484,8 +477,9 @@ function createindex!(db::DB, table::AbstractString, index::AbstractString, cols
484477
end
485478

486479
"""
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.
489483
490484
A convenience method for the common task of removing duplicate
491485
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
506500
include("tables.jl")
507501

508502
"""
509-
`SQLite.tables(db, sink=columntable)`
503+
SQLite.tables(db, sink=columntable)
510504
511505
returns a list of tables in `db`
512506
"""
513507
tables(db::DB, sink=columntable) = DBInterface.execute(db, "SELECT name FROM sqlite_master WHERE type='table';") |> sink
514508

515509
"""
516-
`SQLite.indices(db, sink=columntable)`
510+
SQLite.indices(db, sink=columntable)
517511
518512
returns a list of indices in `db`
519513
"""
520514
indices(db::DB, sink=columntable) = DBInterface.execute(db, "SELECT name FROM sqlite_master WHERE type='index';") |> sink
521515

522516
"""
523-
`SQLite.columns(db, table, sink=columntable)`
517+
SQLite.columns(db, table, sink=columntable)
524518
525519
returns a list of columns in `table`
526520
"""
527521
columns(db::DB, table::AbstractString, sink=columntable) = DBInterface.execute(db, "PRAGMA table_info($(esc_id(table)))") |> sink
528522

529523
"""
530-
`SQLite.last_insert_rowid(db)`
524+
SQLite.last_insert_rowid(db)
531525
532526
returns the auto increment id of the last row
533527
"""
534528
last_insert_rowid(db::DB) = sqlite3_last_insert_rowid(db.handle)
535529

536530
"""
537-
`SQLite.enable_load_extension(db, enable::Bool=true)`
531+
SQLite.enable_load_extension(db, enable::Bool=true)
538532
539533
Enables extension loading (off by default) on the sqlite database `db`. Pass `false` as the second argument to disable.
540534
"""

src/UDF.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function finalfunc(init, func, fsym=Symbol(string(func)*"_final"))
189189
end
190190

191191
"""
192-
@register db function
192+
SQLite.@register db function
193193
194194
User-facing macro for convenience in registering a simple function
195195
with no configurations needed
@@ -199,6 +199,9 @@ macro register(db, func)
199199
end
200200

201201
"""
202+
SQLite.register(db, func)
203+
SQLite.register(db, init, step_func, final_func; nargs=-1, name=string(step), isdeterm=true)
204+
202205
Register a scalar (first method) or aggregate (second method) function
203206
with a [`SQLite.DB`](@ref).
204207
"""

0 commit comments

Comments
 (0)