You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
`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
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.
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.
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.
130
13
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
+
```
131
32
132
33
*`SQLite.sqlreturn(contex, val)`
133
34
@@ -136,9 +37,16 @@ re-execute the query and position the iterator back at the begining of the resul
136
37
137
38
## User Defined Functions
138
39
139
-
### SQLite Regular Expressions
40
+
### [SQLite Regular Expressions](@id regex)
140
41
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/))
142
50
143
51
```julia
144
52
julia>using SQLite
@@ -189,7 +97,10 @@ julia> SQLite.Query(db, "SELECT * FROM Genre ORDER BY GenreId LIMIT 2") |> DataF
189
97
|2|1|"Rock"|
190
98
```
191
99
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:
193
104
194
105
```julia
195
106
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
205
116
|1|0|
206
117
```
207
118
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:
209
123
210
124
```julia
211
125
julia># manually escaping backslashes
@@ -225,36 +139,51 @@ julia> SQLite.Query(db, sr"SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |>
225
139
|1|3|"Protected MPEG-4 video file"|
226
140
```
227
141
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.
229
144
230
145
231
146
### Custom Scalar Functions
232
147
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.
234
151
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:
236
154
237
155
```julia
238
156
julia>@register db functionadd3(x)
239
157
x +3
240
158
end
241
159
```
242
160
243
-
inline function syntax
161
+
inline function syntax:
244
162
245
163
```julia
246
164
julia>@register db mult3(x) =3* x
247
165
```
248
166
249
-
and previously defined functions
167
+
and previously defined functions:
250
168
251
169
```julia
252
170
julia>@register db sin
253
171
```
254
172
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`.
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`.
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.
282
219
283
220
### Custom Aggregate Functions
284
221
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.
286
224
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.
288
232
289
233
```julia
290
234
julia>dsum(prev, cur) = prev + cur
@@ -294,7 +238,9 @@ julia> dsum(prev) = 2 * prev
294
238
julia> SQLite.register(db, 0, dsum, dsum)
295
239
```
296
240
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
298
244
299
245
```julia
300
246
julia> SQLite.register(db, 0, (p,c) -> p+c, p ->2p, name="dsum")
0 commit comments