Skip to content

Commit adf8fc2

Browse files
kescoboquinnj
authored andcommitted
Docs cleanup (#195)
* fix esc_id signature * add doc string and example to docs * some other docs formatting * remove docs build * transition index.md to Documenter @docs blocks
1 parent b964d19 commit adf8fc2

File tree

8 files changed

+241
-245
lines changed

8 files changed

+241
-245
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ CMakeLists.txt.user
88
/deps/deps.jl
99
/test/test.sqlite
1010
/test/test2.sqlite
11+
/docs/build

docs/build/assets/Documenter.css

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/build/assets/mathjaxhelper.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

docs/build/index.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

docs/src/index.md

Lines changed: 81 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -10,124 +10,25 @@ SQLite.load!
1010
```
1111

1212
## Types/Functions
13-
* `SQLite.DB(file::AbstractString)`
14-
15-
`SQLite.DB` requires the `file` string argument as the name of either a pre-defined SQLite database to be opened, or if the file doesn't exist, a database will be created. Note that only sqlite 3.x version files are supported.
16-
17-
The `SQLite.DB` object represents a single connection to an SQLite database. All other SQLite.jl functions take an `SQLite.DB` as the first argument as context.
18-
19-
To create an in-memory temporary database, call `SQLite.DB()`.
20-
21-
The `SQLite.DB` will automatically closed/shutdown when it goes out of scope (i.e. the end of the Julia session, end of a function call wherein it was created, etc.)
22-
23-
24-
* `SQLite.Stmt(db::SQLite.DB, sql::String)`
25-
26-
Constructs and prepares (compiled by the SQLite library) an SQL statement in the context of the provided `db`. Note the SQL statement is not actually executed, but only compiled (mainly for usage where the same statement is repeated with different parameters bound as values. See `bind!` below).
27-
28-
The `SQLite.Stmt` will automatically closed/shutdown when it goes out of scope (i.e. the end of the Julia session, end of a function call wherein it was created, etc.)
29-
30-
31-
* `SQLite.bind!(stmt::SQLite.Stmt,index,value)`
32-
33-
Used to bind values to parameter placeholders in an prepared `SQLite.Stmt`. From the SQLite documentation:
34-
35-
> Usually, though, it is not useful to evaluate exactly the same SQL statement more than once. More often, one wants to evaluate similar statements. For example, you might want to evaluate an INSERT statement multiple times though with different values to insert. To accommodate this kind of flexibility, SQLite allows SQL statements to contain parameters which are "bound" to values prior to being evaluated. These values can later be changed and the same prepared statement can be evaluated a second time using the new values.
36-
37-
> In SQLite, wherever it is valid to include a string literal, one can use a parameter in one of the following forms:
38-
39-
> ?
40-
>
41-
> ?NNN
42-
>
43-
> :AAA
44-
>
45-
> $AAA
46-
>
47-
> @AAA
48-
49-
> In the examples above, NNN is an integer value and AAA is an identifier. A parameter initially has a value of NULL. Prior to calling sqlite3_step() for the first time or immediately after sqlite3_reset(), the application can invoke one of the sqlite3_bind() interfaces to attach values to the parameters. Each call to sqlite3_bind() overrides prior bindings on the same parameter.
50-
51-
52-
* `SQLite.execute!(stmt::SQLite.Stmt; values=[])`
53-
54-
`SQLite.execute!(db::SQLite.DB, sql::String)`
55-
56-
57-
Used to execute a prepared `SQLite.Stmt`. The 2nd method is a convenience method to pass in an SQL statement as a string which gets prepared and executed in one call. This method does not check for or return any results, hence it is only useful for database manipulation methods (i.e. ALTER, CREATE, UPDATE, DROP). To return results, see `SQLite.query` below. With a prepared `stmt`, you can also pass a `values` iterable or `Dict` that will bind to
58-
parameters in the prepared query.
59-
60-
61-
* `SQLite.Query(db::SQLite.DB, sql::String, values=[])`
62-
63-
Constructs a `SQLite.Query` object by executing the SQL query `sql` against the sqlite database `db` and querying
64-
the columns names and types of the result set, if any.
65-
66-
Will bind `values` to any parameters in `sql`.
67-
`stricttypes=false` will remove strict column typing in the result set, making each column effectively `Vector{Any}`; in sqlite, individual
68-
column values are only loosely associated with declared column types, and instead each carry their own type information. This can lead to
69-
type errors when trying to query columns when a single type is expected.
70-
`nullable` controls whether `NULL` (`missing` in Julia) values are expected in a column.
71-
72-
An `SQLite.Query` object will iterate NamedTuple rows by default, and also supports the Tables.jl interface for integrating with
73-
any other Tables.jl implementation. Due note however that iterating an sqlite result set is a forward-once-only operation. If you need
74-
to iterate over an `SQLite.Query` multiple times, but can't store the iterated NamedTuples, call `SQLite.reset!(q::SQLite.Query)` to
75-
re-execute the query and position the iterator back at the begining of the result set.
76-
77-
78-
* `SQLite.drop!(db::SQLite.DB,table::String;ifexists::Bool=false)`
79-
80-
`SQLite.dropindex!(db::SQLite.DB,index::String;ifexists::Bool=false)`
81-
82-
83-
These are pretty self-explanatory. They're really just a convenience methods to execute DROP TABLE/DROP INDEX commands, while also calling "VACUUM" to clean out freed memory from the database.
84-
85-
86-
* `SQLite.createindex!(db::DB,table::AbstractString,index::AbstractString,cols::AbstractString;unique::Bool=true,ifnotexists::Bool=false)`
87-
88-
Create a new index named `index` for `table` with the columns in `cols`, which should be a comma delimited list of column names. `unique` indicates whether the index will have unique values or not. `ifnotexists` will not throw an error if the index already exists.
89-
90-
91-
* `SQLite.removeduplicates!(db,table::AbstractString,cols::AbstractString)`
92-
93-
A convenience method for the common task of removing duplicate rows in a dataset according to some subset of columns that make up a "primary key".
94-
95-
96-
* `SQLite.tables(db::SQLite.DB)`
97-
98-
List the tables in an SQLite database `db`
99-
100-
101-
* `SQLite.columns(db::SQLite.DB,table::AbstractString)`
102-
103-
List the columns in an SQLite table
104-
105-
106-
* `SQLite.indices(db::SQLite.DB)`
107-
108-
List the indices that have been created in `db`
109-
110-
* `SQLite.enable_load_extensions(db::SQLite.DB, enable::Bool=true)`
111-
112-
Enables extension loading (off by default) on the sqlite database `db`. Pass `false` as the second argument to disable.
113-
114-
* `SQLite.register(db::SQLite.DB, func::Function; nargs::Int=-1, name::AbstractString=string(func), isdeterm::Bool=true)`
115-
116-
117-
* `SQLite.register(db::SQLite.DB, init, step::Function, final::Function=identity; nargs::Int=-1, name::AbstractString=string(final), isdeterm::Bool=true)`
118-
119-
Register a scalar (first method) or aggregate (second method) function with a `SQLite.DB`.
120-
121-
122-
* `@register db function`
123-
124-
Automatically define then register `function` with a `SQLite.DB`.
125-
126-
127-
* `sr"..."`
128-
129-
This string literal is used to escape all special characters in the string, useful for using regex in a query.
13013

14+
```@docs
15+
SQLite.DB
16+
SQLite.Stmt
17+
SQLite.bind!
18+
SQLite.execute!
19+
SQLite.drop!
20+
SQLite.dropindex!
21+
SQLite.createindex!
22+
SQLite.removeduplicates!
23+
SQLite.tables
24+
SQLite.columns
25+
SQLite.indices
26+
SQLite.enable_load_extension
27+
SQLite.register
28+
SQLite.@register
29+
SQLite.@sr_str
30+
SQLite.sqlreturn
31+
```
13132

13233
* `SQLite.sqlreturn(contex, val)`
13334

@@ -136,9 +37,16 @@ re-execute the query and position the iterator back at the begining of the resul
13637

13738
## User Defined Functions
13839

139-
### SQLite Regular Expressions
40+
### [SQLite Regular Expressions](@id regex)
14041

141-
SQLite provides syntax for calling the [`regexp` function](http://sqlite.org/lang_expr.html#regexp) from inside `WHERE` clauses. Unfortunately, however, SQLite does not provide a default implementation of the `regexp` function so SQLite.jl creates one automatically when you open a database. The function can be called in the following ways (examples using the [Chinook Database](http://chinookdatabase.codeplex.com/))
42+
SQLite provides syntax for calling
43+
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.
48+
The function can be called in the following ways
49+
(examples using the [Chinook Database](http://chinookdatabase.codeplex.com/))
14250

14351
```julia
14452
julia> using SQLite
@@ -189,7 +97,10 @@ julia> SQLite.Query(db, "SELECT * FROM Genre ORDER BY GenreId LIMIT 2") |> DataF
18997
| 2 | 1 | "Rock" |
19098
```
19199

192-
Due to the heavy use of escape characters you may run into problems where julia parses out some backslashes in your query, for example `"\y"` simply becomes `"y"`. For example the following two queries are identical
100+
Due to the heavy use of escape characters,
101+
you may run into problems where julia parses out some backslashes in your query,
102+
for example `"\y"` simply becomes `"y"`.
103+
For example, the following two queries are identical:
193104

194105
```julia
195106
julia> SQLite.Query(db, "SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
@@ -205,7 +116,10 @@ julia> SQLite.Query(db, "SELECT * FROM MediaType WHERE Name REGEXP '-d'") |> Dat
205116
| 1 | 0 |
206117
```
207118

208-
This can be avoided in two ways. You can either escape each backslash yourself or you can use the sr"..." string literal that SQLite.jl exports. The previous query can then successfully be run like so
119+
This can be avoided in two ways.
120+
You can either escape each backslash yourself
121+
or you can use the sr"..." string literal that SQLite.jl exports.
122+
The previous query can then successfully be run like so:
209123

210124
```julia
211125
julia> # manually escaping backslashes
@@ -225,36 +139,51 @@ julia> SQLite.Query(db, sr"SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |>
225139
| 1 | 3 | "Protected MPEG-4 video file" |
226140
```
227141

228-
The sr"..." currently escapes all special characters in a string but it may be changed in the future to escape only characters which are part of a regex.
142+
The `sr"..."` currently escapes all special characters in a string
143+
but it may be changed in the future to escape only characters which are part of a regex.
229144

230145

231146
### Custom Scalar Functions
232147

233-
SQLite.jl also provides a way that you can implement your own [Scalar Functions](https://www.sqlite.org/lang_corefunc.html). This is done using the `register` function and macro.
148+
SQLite.jl also provides a way
149+
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.
234151

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

237155
```julia
238156
julia> @register db function add3(x)
239157
x + 3
240158
end
241159
```
242160

243-
inline function syntax
161+
inline function syntax:
244162

245163
```julia
246164
julia> @register db mult3(x) = 3 * x
247165
```
248166

249-
and previously defined functions
167+
and previously defined functions:
250168

251169
```julia
252170
julia> @register db sin
253171
```
254172

255-
The `SQLite.register` function takes optional arguments; `nargs` which defaults to `-1`, `name` which defaults to the name of the function, `isdeterm` which defaults to `true`. In practice these rarely need to be used.
256-
257-
The `SQLite.register` function uses the `sqlreturn` function to return your function's return value to SQLite. By default, `sqlreturn` maps the returned value to a [native SQLite type](http://sqlite.org/c3ref/result_blob.html) or, failing that, serializes the julia value and stores it as a `BLOB`. To change this behaviour simply define a new method for `sqlreturn` which then calls a previously defined method for `sqlreturn`. Methods which map to native SQLite types are
173+
The [`SQLite.register`](@ref) function takes optional arguments;
174+
`nargs` which defaults to `-1`,
175+
`name` which defaults to the name of the function,
176+
`isdeterm` which defaults to `true`.
177+
In practice these rarely need to be used.
178+
179+
The [`SQLite.register`](@ref) function uses the [`SQLite.sqlreturn`](@ref) function
180+
to return your function's return value to SQLite.
181+
By default, `sqlreturn` maps the returned value
182+
to a [native SQLite type](http://sqlite.org/c3ref/result_blob.html)
183+
or, failing that, serializes the julia value and stores it as a `BLOB`.
184+
To change this behaviour simply define a new method for `sqlreturn`
185+
which then calls a previously defined method for `sqlreturn`.
186+
Methods which map to native SQLite types are
258187

259188
```julia
260189
sqlreturn(context, ::NullType)
@@ -266,25 +195,40 @@ sqlreturn(context, val::String)
266195
sqlreturn(context, val::Any)
267196
```
268197

269-
As an example, say you would like `BigInt`s to be stored as `TEXT` rather than a `BLOB`. You would simply need to define the following method
198+
As an example,
199+
say you would like `BigInt`s to be stored as `TEXT` rather than a `BLOB`.
200+
You would simply need to define the following method:
270201

271202
```julia
272203
sqlreturn(context, val::BigInt) = sqlreturn(context, string(val))
273204
```
274205

275-
Another example is the `sqlreturn` used by the `regexp` function. For `regexp` to work correctly it must return it must return an `Int` (more specifically a `0` or `1`) but `ismatch` (used by `regexp`) returns a `Bool`. For this reason the following method was defined
206+
Another example is the [`SQLite.sqlreturn`](@ref) used by the `regexp` function.
207+
For `regexp` to work correctly,
208+
it must return it must return an `Int` (more specifically a `0` or `1`)
209+
but `occursin` (used by `regexp`) returns a `Bool`.
210+
For this reason the following method was defined:
276211

277212
```julia
278213
sqlreturn(context, val::Bool) = sqlreturn(context, int(val))
279214
```
280215

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

283220
### Custom Aggregate Functions
284221

285-
Using the `SQLite.register` function, you can also define your own aggregate functions with largely the same semantics.
222+
Using the [`SQLite.register`](@ref) function,
223+
you can also define your own aggregate functions with largely the same semantics.
286224

287-
The `SQLite.register` function for aggregates must take a `SQLite.DB`, an initial value, a step function and a final function. The first argument to the step function will be the return value of the previous function (or the initial value if it is the first iteration). The final function must take a single argument which will be the return value of the last step function.
225+
The `SQLite.register` function for aggregates must take a `SQLite.DB`,
226+
an initial value, a step function and a final function.
227+
The first argument to the step function
228+
will be the return value of the previous function
229+
(or the initial value if it is the first iteration).
230+
The final function must take a single argument
231+
which will be the return value of the last step function.
288232

289233
```julia
290234
julia> dsum(prev, cur) = prev + cur
@@ -294,7 +238,9 @@ julia> dsum(prev) = 2 * prev
294238
julia> SQLite.register(db, 0, dsum, dsum)
295239
```
296240

297-
If no name is given the name of the first (step) function is used (in this case "dsum"). You can also use lambdas, the following does the same as the previous code snippet
241+
If no name is given,
242+
the name of the first (step) function is used (in this case "dsum").
243+
You can also use lambdas; the following does the same as the previous code snippet
298244

299245
```julia
300246
julia> SQLite.register(db, 0, (p,c) -> p+c, p -> 2p, name="dsum")

0 commit comments

Comments
 (0)