Documentation Source Text

Check-in [9cc758b326]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add documentation for the "swarmvtab" extension.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9cc758b32637b3e8c7504d1ec9c89551e5b6e889788f136ff90b84c9336b77d4
User & Date: dan 2017-12-16 17:20:11.954
Context
2017-12-21
18:50
Add docs for experimental ".expert" command. (check-in: 7f57c342de user: dan tags: trunk)
2017-12-19
17:34
Add docs for a proposed addition to the SQLite shell tool. (Closed-Leaf check-in: cd6f445782 user: dan tags: expert-in-shell)
2017-12-16
17:20
Add documentation for the "swarmvtab" extension. (check-in: 9cc758b326 user: dan tags: trunk)
2017-12-12
01:16
Changes to walformat.html document. (check-in: c0a1fede37 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Added pages/swarmvtab.in.




















































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

<title>Swarmvtab Virtual Table</title>

<table_of_contents>

<h1 tags="swarmvtab">Overview</h1>

<p>The "swarmvtab" virtual table allows the user to query a large number 
of tables (hereafter "component" tables) with similar schemas but distinct
ranges of rowid values as if they were a single database table. The tables may
be (and usually are) located in different databases. Swarmvtab tables are
read-only.

<p>Component tables must not be declared WITHOUT ROWID, and must all have
the same schema, but may have different names within their databases. In
this context, "the same schema" means that:

<ul>
  <li>All component tables must have the same set of columns, in the same 
      order.
  <li>The types and default collation sequences attached to each column
      must be the same for all component tables.
  <li>All component tables must have the same PRIMARY KEY declaration (if any).
</ul>

<p>A swarmvtab table has the same schema as each of its component tables.

<p>A swarmvtab virtual table is created as follows:

<codeblock>
  CREATE VIRTUAL TABLE temp.&lt;name&gt; USING swarmvtab(&lt;sql-statement&gt;);
</codeblock>

<p>Swarmvtab virtual tables must be created in the temp schema. Attempting
to create a swarmvtab in the main or an attached database is an error.

<p>The SQL statement supplied as the argument to the CREATE VIRTUAL TABLE
statement is executed when the table is created. It must return either four
or five columns. Each row returned describes one of the component tables. The
first four columns are interpreted, from first to last, as:

<ul>
  <li> <b>Database URI</b>. A filename or URI that can be used to open the
  database containing the component table.

  <li> <b>Table name</b>. The name of the component table within its database.

  <li> <b>Minimum rowid</b>. The smallest rowid value that the component
  table may contain.

  <li> <b>Maximum rowid</b>. The smallest rowid value that the component
  table may contain.
</ul>

<p>The interpretation of the the final column, if it is present, is 
[swarmvtab context|described here].

<p>For example, say the SQL statement returns the following data when 
executed:

<table striped=1>
<tr><th>Database URI<th>Table name<th>Mimimum rowid<th>Maximum rowid
<tr><td>test.db1 <td>t1 <td>0 <td>10
<tr><td>test.db2 <td>t2 <td>11 <td>20
<tr><td>test.db3 <td>t1 <td>21 <td>30
<tr><td>test.db4 <td>t1 <td>31 <td>40
</table>

<p>and the user queries the swarmvtab table for the row with rowid value
25. The swarmvtab table will open database file "test.db3" and read the
data to return from table "t1" (as 25 falls within the range of rowids
assigned to table "t1" in "test.db3").

<p>Swarmvtab efficiently handles range and equality constraints on the
rowid (or other INTEGER PRIMARY KEY) field only. If a query does not 
contain such a constraint, then swarmvtab finds the results by opening
each database in turn and linearly scanning the component table. Which 
generates a correct result, but is often slow.

<p>There must be no overlapping rowid ranges in the rows returned by
the SQL statement. It is an error if there are.

<p>The swarmvtab implementation may open or close databases at any 
point. By default, it attempts to limit the maximum number of 
simultaneously open database files to nine. This is not a hard limit -
it is possible to construct a scenario that will cause swarmvtab to 
exceed it.

<h1 tags="compilation">Compiling and Using Swarmvtab</h1>

<p>The code for the swarmvtab virtual table is found in the
ext/misc/unionvtab.c file of the main SQLite source tree. It may be compiled
into an SQLite [loadable extension] using a command like:

<codeblock>
    gcc -g -fPIC -shared unionvtab.c -o unionvtab.so
</codeblock>

<p>Alternatively, the unionvtab.c file may be compiled into the application. 
In this case, the following function should be invoked to register the
extension with each new database connection:

<codeblock>
  int sqlite3_unionvtab_init(sqlite3 *db, void*, void*);
</codeblock>

<p> The first argument passed should be the database handle to register the
extension with. The second and third arguments should both be passed 0.

<p> The source file and entry point are named for "unionvtab" instead of
"swarmvtab". Unionvtab is a [unionvtab|separately documented] virtual table 
that is bundled with swarmvtab.

<h1 tags="advanced">Advanced Usage</h1>

<p>Most users of swarmvtab will only use the features described above. 
This section describes features designed for more esoteric use cases. These
features all involve specifying extra optional parameters following the SQL
statement as part of the CREATE VIRTUAL TABLE command. An optional parameter 
is specified using its name, followed by an "=" character, followed by an
optionally quoted value. Whitespace may separate the name, "=" character 
and value. For example:

<codeblock>
  CREATE VIRTUAL TABLE temp.sv USING swarmvtab (
    'SELECT ...',                <i>-- the SELECT statement</i>
    maxopen = 20,                <i>-- An optional parameter</i>
    missing='missing_udf'        <i>-- Another optional parameter</i>
  );
</codeblock>

<p>The following sections describe the supported parameters. Specifying
an unrecognized parameter name is an error.

<h2 tags="sql parameters">SQL Parameters</h2>

<p>If a parameter name begins with a ":", then it is assumed to be a
value to bind to the SQL statement before executing it. The value is always
bound as text. It is an error if the specified SQL parameter does not
exist. For example:

<codeblock>
  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
    "SELECT :dir || local_filename, tbl, min, max FROM components",
    :dir = '/home/user/app/databases/'
  );
</codeblock>

<p>When the above CREATE VIRTUAL TABLE statement is executed, swarmvtab binds
the text value "/home/user/app/databases/" to the :dir parameter of the
SQL statement before executing it.

<p>A single CREATE VIRTUAL TABLE statement may contain any number of SQL
parameters.

<h2 tags="maxopen parameter">The "maxopen" Parameter</h2>

<p>By default, swarmvtab attempts to limit the number of simultaneously
open databases to nine. This parameter allows that limit to be changed.
For example, to create a swarmvtab table that may hold up to 30 databases
open simultaneously:

<codeblock>
  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
    "SELECT ...",
    maxopen=30
  );
</codeblock>

<p>Raising the number of open databases may improve performance in some
scenarios.

<h2 tags="openclose callback">The "openclose" Callback</h2>

<p>The "openclose" parameter allows the user to specify the name of a
[application-defined SQL function] that will be invoked just before
swarmvtab opens a database, and again just after it closes one. The first
argument passed to the open close function is the filename or URI
identifying the database to be opened or just recently closed (the same
value returned in the leftmost column of the SQL statement provided to
the CREATE VIRTUAL TABLE command). The second argument is integer value
0 when the function is invoked before opening a database, and 1 when it
is invoked after one is closed. For example, if:

<codeblock>
  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
    "SELECT ...",
    openclose = 'openclose_udf'
  );
</codeblock>

<p>then before each database containing a component table is opened, 
swarmvtab effectively executes:

<codeblock>
  SELECT openclose_udf(&lt;database-name&gt;, 0);
</codeblock>

<p>After a database is closed, swarmvtab runs the equivalent of:

<codeblock>
  SELECT openclose_udf(&lt;database-name&gt;, 1);
</codeblock>

<p>Any value returned by the openclose function is ignored. If an invocation
made before opening a database returns an error, then the database file is
not opened and the error returned to the user. This is the only scenario
in which swarmvtab will issue an "open" invocation without also eventually
issuing a corresponding "close" call. If there are still databases open,
"close" calls may be issued from within the eventual sqlite3_close() call
on the applications database that deletes the temp schema in which the
swarmvtab table resides.

<p>Errors returned by "close" invocations are always ignored.

<h2 tags="missing callback">The "missing" Callback</h2>

<p>The "missing" parameter allows the user to specify the name of a
[application-defined SQL function] that will be invoked just before
swarmvtab opens a database if it finds that the required database file
is not present on disk. This provides the application with an opportunity
to retrieve the required database from a remote source before swarmvtab
attempts to open it. The only argument passed to the "missing" function
is the name or URI that identifies the database being opened. Assuming:

<codeblock>
  CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
    "SELECT ...",
    openclose = 'openclose_udf',
    missing='missing_udf'
  );
</codeblock>

<p>then the missing function is invoked as follows:

<codeblock>
  SELECT missing_udf(&lt;database-name&gt;);
</codeblock>

<p>If the missing function returns an error, then the database is not 
opened and the error returned to the user. If an openclose function is
configured, then a "close" invocation is issued at this point to match
the earlier "open". The following pseudo-code illustrates the procedure used
by a swarmvtab instance with both missing and openclose functions configured
when a component database is opened.

<codeblock>
  SELECT openclose_udf(&lt;database-name&gt;, 0);
  if( error ) return error;
  if( db does not exist ){
    SELECT missing_udf(&lt;database-name&gt;);
    if( error ){
      SELECT openclose_udf(&lt;database-name&gt;, 1);
      return error;
    }
  }
  sqlite3_open_v2(&lt;database-name&gt;);
  if( error ){
    SELECT openclose_udf(&lt;database-name&gt;, 1);
    return error;
  }
  // db successfully opened!
</codeblock>

<h2 tags="swarmvtab context">Component table "context" values</h2>

<p> If the SELECT statement specified as part of the CREATE VIRTUAL 
TABLE command returns five columns, then the final column is used
for application context only. Swarmvtab does not use this value at
all, except that it is passed after &lt;database-name&gt; to both
the openclose and missing functions, if specified. In other words,
instead of invoking the functions as described above, if the "context"
column is present swarmvtab instead invokes:

<codeblock>
  SELECT missing_udf(&lt;database-name&gt;, &lt;context&gt;);
  SELECT openclose_udf(&lt;database-name&gt;, &lt;context&gt;, 0);
  SELECT openclose_udf(&lt;database-name&gt;, &lt;context&gt;, 1);
</codeblock>

<p>as required.