Debian Codegraph @

www/swarmvtab.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Swarmvtab Virtual Table</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Swarmvtab Virtual Table
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#compiling_and_using_swarmvtab">2. Compiling and Using Swarmvtab</a></div>
130 <div class="fancy-toc1"><a href="#advanced_usage">3. Advanced Usage</a></div>
131 <div class="fancy-toc2"><a href="#sql_parameters">3.1. SQL Parameters</a></div>
132 <div class="fancy-toc2"><a href="#the_maxopen_parameter">3.2. The "maxopen" Parameter</a></div>
133 <div class="fancy-toc2"><a href="#the_openclose_callback">3.3. The "openclose" Callback</a></div>
134 <div class="fancy-toc2"><a href="#the_missing_callback">3.4. The "missing" Callback</a></div>
135 <div class="fancy-toc2"><a href="#component_table_context_values">3.5. Component table "context" values</a></div>
136 </div>
137 </div>
138 <script>
139 function toggle_toc(){
140 var sub = document.getElementById("toc_sub")
141 var mk = document.getElementById("toc_mk")
142 if( sub.style.display!="block" ){
143 sub.style.display = "block";
144 mk.innerHTML = "&#x25bc;";
145 } else {
146 sub.style.display = "none";
147 mk.innerHTML = "&#x25ba;";
148 }
149 }
150 </script>
151 </div>
152
153
154
155
156
157 <a name="overview"></a>
158 <h1 tags="swarmvtab" id="overview"><span>1. </span>Overview</h1>
159
160 <p>The "swarmvtab" virtual table allows the user to query a large number
161 of tables (hereafter "component" tables) with similar schemas but distinct
162 ranges of rowid values as if they were a single database table. The tables may
163 be (and usually are) located in different databases. Swarmvtab tables are
164 read-only.
165
166 </p><p>Component tables must not be declared WITHOUT ROWID, and must all have
167 the same schema, but may have different names within their databases. In
168 this context, "the same schema" means that:
169
170 </p><ul>
171 <li>All component tables must have the same set of columns, in the same
172 order.
173 </li><li>The types and default collation sequences attached to each column
174 must be the same for all component tables.
175 </li><li>All component tables must have the same PRIMARY KEY declaration (if any).
176 </li></ul>
177
178 <p>A swarmvtab table has the same schema as each of its component tables.
179
180 </p><p>A swarmvtab virtual table is created as follows:
181
182 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.&lt;name&gt; USING swarmvtab(&lt;sql-statement&gt;);
183 </pre></div>
184
185 <p>Swarmvtab virtual tables must be created in the temp schema. Attempting
186 to create a swarmvtab in the main or an attached database is an error.
187
188 </p><p>The SQL statement supplied as the argument to the CREATE VIRTUAL TABLE
189 statement is executed when the table is created. It must return either four
190 or five columns. Each row returned describes one of the component tables. The
191 first four columns are interpreted, from first to last, as:
192
193 </p><ul>
194 <li> <b>Database URI</b>. A filename or URI that can be used to open the
195 database containing the component table.
196
197 </li><li> <b>Table name</b>. The name of the component table within its database.
198
199 </li><li> <b>Minimum rowid</b>. The smallest rowid value that the component
200 table may contain.
201
202 </li><li> <b>Maximum rowid</b>. The smallest rowid value that the component
203 table may contain.
204 </li></ul>
205
206 <p>The interpretation of the final column, if it is present, is
207 <a href="swarmvtab.html#component_table_context_values">described here</a>.
208
209 </p><p>For example, say the SQL statement returns the following data when
210 executed:
211
212 </p><table striped="1" style="margin:1em auto; width:80%; border-spacing:0">
213 <tr style="text-align:left"><th>Database URI</th><th>Table name</th><th>Minimum rowid</th><th>Maximum rowid
214 </th></tr><tr style="text-align:left;background-color:#DDDDDD"><td>test.db1 </td><td>t1 </td><td>0 </td><td>10
215 </td></tr><tr style="text-align:left"><td>test.db2 </td><td>t2 </td><td>11 </td><td>20
216 </td></tr><tr style="text-align:left;background-color:#DDDDDD"><td>test.db3 </td><td>t1 </td><td>21 </td><td>30
217 </td></tr><tr style="text-align:left"><td>test.db4 </td><td>t1 </td><td>31 </td><td>40
218 </td></tr></table>
219
220 <p>and the user queries the swarmvtab table for the row with rowid value
221 25. The swarmvtab table will open database file "test.db3" and read the
222 data to return from table "t1" (as 25 falls within the range of rowids
223 assigned to table "t1" in "test.db3").
224
225 </p><p>Swarmvtab efficiently handles range and equality constraints on the
226 rowid (or other INTEGER PRIMARY KEY) field only. If a query does not
227 contain such a constraint, then swarmvtab finds the results by opening
228 each database in turn and linearly scanning the component table. Which
229 generates a correct result, but is often slow.
230
231 </p><p>There must be no overlapping rowid ranges in the rows returned by
232 the SQL statement. It is an error if there are.
233
234 </p><p>The swarmvtab implementation may open or close databases at any
235 point. By default, it attempts to limit the maximum number of
236 simultaneously open database files to nine. This is not a hard limit -
237 it is possible to construct a scenario that will cause swarmvtab to
238 exceed it.
239
240 </p><a name="compiling_and_using_swarmvtab"></a>
241 <h1 tags="compilation" id="compiling_and_using_swarmvtab"><span>2. </span>Compiling and Using Swarmvtab</h1>
242
243 <p>The code for the swarmvtab virtual table is found in the
244 ext/misc/unionvtab.c file of the main SQLite source tree. It may be compiled
245 into an SQLite <a href="loadext.html">loadable extension</a> using a command like:
246
247 </p><div class="codeblock"><pre>gcc -g -fPIC -shared unionvtab.c -o unionvtab.so
248 </pre></div>
249
250 <p>Alternatively, the unionvtab.c file may be compiled into the application.
251 In this case, the following function should be invoked to register the
252 extension with each new database connection:
253
254 </p><div class="codeblock"><pre>int sqlite3_unionvtab_init(sqlite3 *db, void*, void*);
255 </pre></div>
256
257 <p> The first argument passed should be the database handle to register the
258 extension with. The second and third arguments should both be passed 0.
259
260 </p><p> The source file and entry point are named for "unionvtab" instead of
261 "swarmvtab". Unionvtab is a <a href="unionvtab.html">separately documented</a> virtual table
262 that is bundled with swarmvtab.
263
264 </p><a name="advanced_usage"></a>
265 <h1 tags="advanced" id="advanced_usage"><span>3. </span>Advanced Usage</h1>
266
267 <p>Most users of swarmvtab will only use the features described above.
268 This section describes features designed for more esoteric use cases. These
269 features all involve specifying extra optional parameters following the SQL
270 statement as part of the CREATE VIRTUAL TABLE command. An optional parameter
271 is specified using its name, followed by an "=" character, followed by an
272 optionally quoted value. Whitespace may separate the name, "=" character
273 and value. For example:
274
275 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.sv USING swarmvtab (
276 'SELECT ...', <i>-- the SELECT statement</i>
277 maxopen = 20, <i>-- An optional parameter</i>
278 missing='missing_udf' <i>-- Another optional parameter</i>
279 );
280 </pre></div>
281
282 <p>The following sections describe the supported parameters. Specifying
283 an unrecognized parameter name is an error.
284
285 </p><a name="sql_parameters"></a>
286 <h2 tags="sql parameters" id="sql_parameters"><span>3.1. </span>SQL Parameters</h2>
287
288 <p>If a parameter name begins with a ":", then it is assumed to be a
289 value to bind to the SQL statement before executing it. The value is always
290 bound as text. It is an error if the specified SQL parameter does not
291 exist. For example:
292
293 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
294 "SELECT :dir || local_filename, tbl, min, max FROM components",
295 :dir = '/home/user/app/databases/'
296 );
297 </pre></div>
298
299 <p>When the above CREATE VIRTUAL TABLE statement is executed, swarmvtab binds
300 the text value "/home/user/app/databases/" to the :dir parameter of the
301 SQL statement before executing it.
302
303 </p><p>A single CREATE VIRTUAL TABLE statement may contain any number of SQL
304 parameters.
305
306 </p><a name="the_maxopen_parameter"></a>
307 <h2 tags="maxopen parameter" id="the_maxopen_parameter"><span>3.2. </span>The "maxopen" Parameter</h2>
308
309 <p>By default, swarmvtab attempts to limit the number of simultaneously
310 open databases to nine. This parameter allows that limit to be changed.
311 For example, to create a swarmvtab table that may hold up to 30 databases
312 open simultaneously:
313
314 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
315 "SELECT ...",
316 maxopen=30
317 );
318 </pre></div>
319
320 <p>Raising the number of open databases may improve performance in some
321 scenarios.
322
323 </p><a name="the_openclose_callback"></a>
324 <h2 tags="openclose callback" id="the_openclose_callback"><span>3.3. </span>The "openclose" Callback</h2>
325
326 <p>The "openclose" parameter allows the user to specify the name of a
327 <a href="appfunc.html">application-defined SQL function</a> that will be invoked just before
328 swarmvtab opens a database, and again just after it closes one. The first
329 argument passed to the open close function is the filename or URI
330 identifying the database to be opened or just recently closed (the same
331 value returned in the leftmost column of the SQL statement provided to
332 the CREATE VIRTUAL TABLE command). The second argument is integer value
333 0 when the function is invoked before opening a database, and 1 when it
334 is invoked after one is closed. For example, if:
335
336 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
337 "SELECT ...",
338 openclose = 'openclose_udf'
339 );
340 </pre></div>
341
342 <p>then before each database containing a component table is opened,
343 swarmvtab effectively executes:
344
345 </p><div class="codeblock"><pre>SELECT openclose_udf(&lt;database-name&gt;, 0);
346 </pre></div>
347
348 <p>After a database is closed, swarmvtab runs the equivalent of:
349
350 </p><div class="codeblock"><pre>SELECT openclose_udf(&lt;database-name&gt;, 1);
351 </pre></div>
352
353 <p>Any value returned by the openclose function is ignored. If an invocation
354 made before opening a database returns an error, then the database file is
355 not opened and the error returned to the user. This is the only scenario
356 in which swarmvtab will issue an "open" invocation without also eventually
357 issuing a corresponding "close" call. If there are still databases open,
358 "close" calls may be issued from within the eventual sqlite3_close() call
359 on the applications database that deletes the temp schema in which the
360 swarmvtab table resides.
361
362 </p><p>Errors returned by "close" invocations are always ignored.
363
364 </p><a name="the_missing_callback"></a>
365 <h2 tags="missing callback" id="the_missing_callback"><span>3.4. </span>The "missing" Callback</h2>
366
367 <p>The "missing" parameter allows the user to specify the name of a
368 <a href="appfunc.html">application-defined SQL function</a> that will be invoked just before
369 swarmvtab opens a database if it finds that the required database file
370 is not present on disk. This provides the application with an opportunity
371 to retrieve the required database from a remote source before swarmvtab
372 attempts to open it. The only argument passed to the "missing" function
373 is the name or URI that identifies the database being opened. Assuming:
374
375 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE temp.x1 USING swarmvtab (
376 "SELECT ...",
377 openclose = 'openclose_udf',
378 missing='missing_udf'
379 );
380 </pre></div>
381
382 <p>then the missing function is invoked as follows:
383
384 </p><div class="codeblock"><pre>SELECT missing_udf(&lt;database-name&gt;);
385 </pre></div>
386
387 <p>If the missing function returns an error, then the database is not
388 opened and the error returned to the user. If an openclose function is
389 configured, then a "close" invocation is issued at this point to match
390 the earlier "open". The following pseudo-code illustrates the procedure used
391 by a swarmvtab instance with both missing and openclose functions configured
392 when a component database is opened.
393
394 </p><div class="codeblock"><pre>SELECT openclose_udf(&lt;database-name&gt;, 0);
395 if( error ) return error;
396 if( db does not exist ){
397 SELECT missing_udf(&lt;database-name&gt;);
398 if( error ){
399 SELECT openclose_udf(&lt;database-name&gt;, 1);
400 return error;
401 }
402 }
403 sqlite3_open_v2(&lt;database-name&gt;);
404 if( error ){
405 SELECT openclose_udf(&lt;database-name&gt;, 1);
406 return error;
407 }
408 // db successfully opened!
409 </pre></div>
410
411 <a name="component_table_context_values"></a>
412 <h2 tags="swarmvtab context" id="component_table_context_values"><span>3.5. </span>Component table "context" values</h2>
413
414 <p> If the SELECT statement specified as part of the CREATE VIRTUAL
415 TABLE command returns five columns, then the final column is used
416 for application context only. Swarmvtab does not use this value at
417 all, except that it is passed after &lt;database-name&gt; to both
418 the openclose and missing functions, if specified. In other words,
419 instead of invoking the functions as described above, if the "context"
420 column is present swarmvtab instead invokes:
421
422 </p><div class="codeblock"><pre>SELECT missing_udf(&lt;database-name&gt;, &lt;context&gt;);
423 SELECT openclose_udf(&lt;database-name&gt;, &lt;context&gt;, 0);
424 SELECT openclose_udf(&lt;database-name&gt;, &lt;context&gt;, 1);
425 </pre></div>
426
427 <p>as required.
428 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/swarmvtab.in?m=e40b220bf8">2022-01-08 05:02:57</a> UTC </small></i></p>
429