1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
3
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
4
<title>The Virtual Table Mechanism Of SQLite</title>
5
<style type="text/css">
8
font-family: Verdana, sans-serif;
13
a:visited { color: #734559 }
15
.logo { position:absolute; margin:3px; }
31
.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
32
.toolbar a:visited { color: white; }
33
.toolbar a:hover { color: #044a64; background: white; }
35
.content { margin: 5%; }
36
.content dt { font-weight:bold; }
37
.content dd { margin-bottom: 25px; margin-left:20%; }
38
.content ul { padding:0px; padding-left: 15px; margin:0px; }
41
.se { background: url(images/se.gif) 100% 100% no-repeat #044a64}
42
.sw { background: url(images/sw.gif) 0% 100% no-repeat }
43
.ne { background: url(images/ne.gif) 100% 0% no-repeat }
44
.nw { background: url(images/nw.gif) 0% 0% no-repeat }
46
/* Things for "fancyformat" documents start here. */
47
.fancy img+p {font-style:italic}
48
.fancy .codeblock i { color: darkblue; }
49
.fancy h1,.fancy h2,.fancy h3,.fancy h4 {font-weight:normal;color:#044a64}
50
.fancy h2 { margin-left: 10px }
51
.fancy h3 { margin-left: 20px }
52
.fancy h4 { margin-left: 30px }
53
.fancy th {white-space:nowrap;text-align:left;border-bottom:solid 1px #444}
54
.fancy th, .fancy td {padding: 0.2em 1ex; vertical-align:top}
55
.fancy #toc a { color: darkblue ; text-decoration: none }
56
.fancy .todo { color: #AA3333 ; font-style : italic }
57
.fancy .todo:before { content: 'TODO:' }
58
.fancy p.todo { border: solid #AA3333 1px; padding: 1ex }
59
.fancy img { display:block; }
60
.fancy :link:hover, .fancy :visited:hover { background: wheat }
61
.fancy p,.fancy ul,.fancy ol { margin: 1em 5ex }
62
.fancy li p { margin: 1em 0 }
63
/* End of "fancyformat" specific rules. */
69
<div><!-- container div to satisfy validator -->
72
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite Logo"
74
<div><!-- IE hack to prevent disappearing logo--></div>
75
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>
77
<table width=100% style="clear:both"><tr><td>
78
<div class="se"><div class="sw"><div class="ne"><div class="nw">
79
<table width=100% style="padding:0;margin:0;cell-spacing:0"><tr>
82
<a href="about.html">About</a>
83
<a href="sitemap.html">Sitemap</a>
84
<a href="docs.html">Documentation</a>
85
<a href="download.html">Download</a>
86
<a href="copyright.html">License</a>
87
<a href="news.html">News</a>
88
<a href="support.html">Support</a>
91
gMsg = "Search SQLite Docs..."
92
function entersearch() {
93
var q = document.getElementById("q");
94
if( q.value == gMsg ) { q.value = "" }
95
q.style.color = "black"
96
q.style.fontStyle = "normal"
98
function leavesearch() {
99
var q = document.getElementById("q");
100
if( q.value == "" ) {
102
q.style.color = "#044a64"
103
q.style.fontStyle = "italic"
108
<div style="padding:0 1em 0px 0;white-space:nowrap">
109
<form name=f method="GET" action="http://www.sqlite.org/search">
110
<input id=q name=q type=text
111
onfocus="entersearch()" onblur="leavesearch()" style="width:24ex;padding:1px 1ex; border:solid white 1px; font-size:0.9em ; font-style:italic;color:#044a64;" value="Search SQLite Docs...">
112
<input type=submit value="Go" style="border:solid white 1px;background-color:#044a64;color:white;font-size:0.9em;padding:0 1ex">
116
</div></div></div></div>
118
<div class=startsearch></div>
122
<h1 align="center">The Virtual Table Mechanism Of SQLite</h1>
125
<h2>1.0 Introduction</h2>
127
<p>A virtual table is an object that is registered with an open SQLite
128
<a href="c3ref/sqlite3.html">database connection</a>. From the perspective of an SQL statement,
129
the virtual table object looks like any other table or view.
130
But behind the scenes, queries from and updates to a virtual table
131
invoke callback methods on the virtual table object instead of
132
reading and writing to the database file.
134
<p>The virtual table mechanism allows an application to publish
135
interfaces that are accessible from SQL statements as if they were
136
tables. SQL statements can in general do anything to a
137
virtual table that they can do to a real table, with the following
142
<li> One cannot create a trigger on a virtual table.
143
<li> One cannot create additional indices on a virtual table.
144
(Virtual tables can have indices but that must be built into
145
the virtual table implementation. Indices cannot be added
146
separately using <a href="lang_createindex.html">CREATE INDEX</a> statements.)
147
<li> One cannot run <a href="lang_altertable.html">ALTER TABLE ... ADD COLUMN</a>
148
commands against a virtual table.
151
<p>Particular virtual table implementations might impose additional
152
constraints. For example, some virtual implementations might provide
153
read-only tables. Or some virtual table implementations might allow
154
<a href="lang_insert.html">INSERT</a> or <a href="lang_delete.html">DELETE</a> but not <a href="lang_update.html">UPDATE</a>. Or some virtual table implementations
155
might limit the kinds of UPDATEs that can be made.
157
<p>A virtual table might represent an in-memory data structures.
158
Or it might represent a view of data on disk that is not in the
159
SQLite format. Or the application might compute the content of the
160
virtual table on demand.
162
<p>Here are some postulated uses for virtual tables:
165
<li> A <a href="fts3.html">full-text search</a> interface
166
<li> Spatial indices using <a href="rtree.html">R-Trees</a>
167
<li> Read and/or write the content of a comma-separated value (CSV)
169
<li> Access to the filesystem of the host computer
170
<li> Enabling SQL manipulation of data in statistics packages like R
175
<p>A virtual table is created using using a <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement.
176
This statement creates a table with a particular name and associates the
177
table with a "module".
180
CREATE VIRTUAL TABLE tablename USING modulename;
183
<p>One can also provide comma-separated arguments to the module following
187
CREATE VIRTUAL TABLE tablename USING modulename(arg1, arg2, ...);
190
<p>The format of the arguments to the module is very general. Each argument
191
can consist of keywords, string literals, identifiers, numbers, and
192
punctuation. The arguments are passed as written (as text) into the
193
<a href="vtab.html#xcreate">constructor method</a> of the virtual table implementation
195
table is created and that constructor is responsible for parsing and
196
interpreting the arguments. The argument syntax is sufficiently general
197
that a virtual table implementation can, if it wants to, interpret its
198
arguments as <a href="lang_createtable.html#tablecoldef">column definitions</a> in an ordinary <a href="lang_createtable.html">CREATE TABLE</a> statement.
199
The implementation could also impose some other interpretation on the
202
<p>Once a virtual table has been created, it can be used like any other
203
table with the exceptions noted above and imposed by specific virtual
204
table implementations. A virtual table is destroyed using the ordinary
205
<a href="lang_droptable.html">DROP TABLE</a> syntax.
207
<h2>2.2 Implementation</h2>
209
<p>Several new C-level objects are used by the virtual table implementation:
212
typedef struct sqlite3_vtab sqlite3_vtab;
213
typedef struct sqlite3_index_info sqlite3_index_info;
214
typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
215
typedef struct sqlite3_module sqlite3_module;
218
<p>The <a href="c3ref/module.html">sqlite3_module</a> structure defines a module object used to implement
219
a virtual table. Think of a module as a class from which one can
220
construct multiple virtual tables having similar properties. For example,
221
one might have a module that provides read-only access to
222
comma-separated-value (CSV) files on disk. That one module can then be
223
used to create several virtual tables where each virtual table refers
224
to a different CSV file.
226
<p>The module structure contains methods that are invoked by SQLite to
227
perform various actions on the virtual table such as creating new
228
instances of a virtual table or destroying old ones, reading and
229
writing data, searching for and deleting, updating, or inserting rows.
230
The module structure is explained in more detail below.
232
<p>Each virtual table instance is represented by an <a href="c3ref/vtab.html">sqlite3_vtab</a> structure.
233
The sqlite3_vtab structure looks like this:
236
struct sqlite3_vtab {
237
const sqlite3_module *pModule;
243
<p>Virtual table implementations will normally subclass this structure
244
to add additional private and implementation-specific fields.
245
The nRef field is used internally by the SQLite core and should not
246
be altered by the virtual table implementation. The virtual table
247
implementation may pass error message text to the core by putting
248
an error message string in zErrMsg.
249
Space to hold this error message string must be obtained from an
250
SQLite memory allocation function such as <a href="c3ref/mprintf.html">sqlite3_mprintf()</a> or
251
<a href="c3ref/free.html">sqlite3_malloc()</a>.
252
Prior to assigning a new value to zErrMsg, the virtual table
253
implementation must free any preexisting content of zErrMsg using
254
<a href="c3ref/free.html">sqlite3_free()</a>. Failure to do this will result in a memory leak.
255
The SQLite core will free and zero the content of zErrMsg when it
256
delivers the error message text to the client application or when
257
it destroys the virtual table. The virtual table implementation only
258
needs to worry about freeing the zErrMsg content when it overwrites
259
the content with a new, different error message.
261
<p>The <a href="c3ref/vtab_cursor.html">sqlite3_vtab_cursor</a> structure represents a pointer to a specific
262
row of a virtual table. This is what an sqlite3_vtab_cursor looks like:
265
struct sqlite3_vtab_cursor {
270
<p>Once again, practical implementations will likely subclass this
271
structure to add additional private fields.
273
<p>The <a href="c3ref/index_info.html">sqlite3_index_info</a> structure is used to pass information into
274
and out of the xBestIndex method of the module that implements a
277
<p>Before a <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement can be run, the module
278
specified in that statement must be registered with the database
279
connection. This is accomplished using either of the <a href="c3ref/create_module.html">sqlite3_create_module()</a>
280
or <a href="c3ref/create_module.html">sqlite3_create_module_v2()</a> interfaces:
283
int sqlite3_create_module(
284
sqlite3 *db, /* SQLite connection to register module with */
285
const char *zName, /* Name of the module */
286
const sqlite3_module *, /* Methods for the module */
287
void * /* Client data for xCreate/xConnect */
289
int sqlite3_create_module_v2(
290
sqlite3 *db, /* SQLite connection to register module with */
291
const char *zName, /* Name of the module */
292
const sqlite3_module *, /* Methods for the module */
293
void *, /* Client data for xCreate/xConnect */
294
void(*xDestroy)(void*) /* Client data destructor function */
298
<p>The <a href="c3ref/create_module.html">sqlite3_create_module()</a> and <a href="c3ref/create_module.html">sqlite3_create_module_v2()</a>
299
routines associates a module name with
300
an <a href="c3ref/module.html">sqlite3_module</a> structure and a separate client data that is specific
301
to each module. The only difference between the two create_module methods
302
is that the _v2 method includes an extra parameter that specifies a
303
destructor for client data pointer. The module structure is what defines
304
the behavior of a virtual table. The module structure looks like this:
307
struct sqlite3_module {
309
int (*xCreate)(sqlite3*, void *pAux,
310
int argc, char **argv,
311
sqlite3_vtab **ppVTab,
313
int (*xConnect)(sqlite3*, void *pAux,
314
int argc, char **argv,
315
sqlite3_vtab **ppVTab,
317
int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
318
int (*xDisconnect)(sqlite3_vtab *pVTab);
319
int (*xDestroy)(sqlite3_vtab *pVTab);
320
int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
321
int (*xClose)(sqlite3_vtab_cursor*);
322
int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
323
int argc, sqlite3_value **argv);
324
int (*xNext)(sqlite3_vtab_cursor*);
325
int (*xEof)(sqlite3_vtab_cursor*);
326
int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
327
int (*xRowid)(sqlite3_vtab_cursor*, sqlite_int64 *pRowid);
328
int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite_int64 *);
329
int (*xBegin)(sqlite3_vtab *pVTab);
330
int (*xSync)(sqlite3_vtab *pVTab);
331
int (*xCommit)(sqlite3_vtab *pVTab);
332
int (*xRollback)(sqlite3_vtab *pVTab);
333
int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
334
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
336
int (*Rename)(sqlite3_vtab *pVtab, const char *zNew);
337
/* The methods above are in version 1 of the sqlite_module object. Those
338
** below are for version 2 and greater. */
339
int (*xSavepoint)(sqlite3_vtab *pVTab, int);
340
int (*xRelease)(sqlite3_vtab *pVTab, int);
341
int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
345
<p>The module structure defines all of the methods for each virtual
346
table object. The module structure also contains the iVersion field which
347
defines the particular edition of the module table structure. Currently,
348
iVersion is always 1, but in future releases of SQLite the module structure
349
definition might be extended with additional methods and in that case
350
the iVersion value will be increased.
352
<p>The rest of the module structure consists of methods used to implement
353
various features of the virtual table. Details on what each of these
354
methods do are provided in the sequel.
356
<h3>1.3 Virtual Tables And Shared Cache</h3>
358
<p>Prior to SQLite <a href="releaselog/3_6_17.html">version 3.6.17</a>, the virtual table mechanism assumes
359
that each <a href="c3ref/sqlite3.html">database connection</a> kept
360
its own copy of the database schema. Hence, the virtual table mechanism
361
could not be used in a database that has <a href="sharedcache.html">shared cache mode</a> enabled.
362
The <a href="c3ref/create_module.html">sqlite3_create_module()</a> interface would return an error if
363
<a href="sharedcache.html">shared cache mode</a> is enabled. That restriction was relaxed
364
beginning with SQLite <a href="releaselog/3_6_17.html">version 3.6.17</a>.
366
<h3>1.4 Creating New Virtual Table Implementations</h3>
368
<p>Follow these steps to create your own virtual table:
372
<li> Write all necessary methods.
373
<li> Create an instance of the <a href="c3ref/module.html">sqlite3_module</a> structure containing pointers
374
to all the methods from step 1.
375
<li> Register your <a href="c3ref/module.html">sqlite3_module</a> structure using one of the
376
<a href="c3ref/create_module.html">sqlite3_create_module()</a> or <a href="c3ref/create_module.html">sqlite3_create_module_v2()</a> interfaces.
377
<li> Run a <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> command that specifies the new module in
381
<p>The only really hard part is step 1. You might want to start with an
382
existing virtual table implementation and modify it to suit your needs.
383
There are several virtual table implementations in the SQLite source tree
384
(for testing purposes). You might use one of those as a guide. Locate
385
these test virtual table implementations by searching
386
for "sqlite3_create_module".
388
<p>You might also want to implement your new virtual table as a
389
<a href="c3ref/load_extension.html">loadable extension</a>.
391
<h2>2.0 Virtual Table Methods</h2>
393
<a name="xcreate"></a>
395
<h3>2.1 The xCreate Method</h3>
398
int (*xCreate)(sqlite3 *db, void *pAux,
399
int argc, char **argv,
400
sqlite3_vtab **ppVTab,
404
<p>This method is called to create a new instance of a virtual table
405
in response to a <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement.
406
The db parameter is a pointer to the SQLite <a href="c3ref/sqlite3.html">database connection</a> that
407
is executing the <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement.
408
The pAux argument is the copy of the client data pointer that was the
409
fourth argument to the <a href="c3ref/create_module.html">sqlite3_create_module()</a> or
410
<a href="c3ref/create_module.html">sqlite3_create_module_v2()</a> call that registered the
411
<a href="c3ref/module.html">virtual table module</a>.
412
The argv parameter is an array of argc pointers to null terminated strings.
413
The first string, argv[0], is the name of the module being invoked. The
414
module name is the name provided as the second argument to
415
<a href="c3ref/create_module.html">sqlite3_create_module()</a> and as the argument to the USING clause of the
416
<a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement that is running.
417
The second, argv[1], is the name of the database in which the new virtual table is being created. The database name is "main" for the primary database, or
418
"temp" for TEMP database, or the name given at the end of the <a href="lang_attach.html">ATTACH</a>
419
statement for attached databases. The third element of the array, argv[2],
420
is the name of the new virtual table, as specified following the TABLE
421
keyword in the <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement.
422
If present, the fourth and subsequent strings in the argv[] array report
423
the arguments to the module name in the <a href="lang_createvtab.html">CREATE VIRTUAL TABLE</a> statement.
425
<p>The job of this method is to construct the new virtual table object
426
(an <a href="c3ref/vtab.html">sqlite3_vtab</a> object) and return a pointer to it in *ppVTab.
428
<p>As part of the task of creating a new <a href="c3ref/vtab.html">sqlite3_vtab</a> structure, this
429
method <u>must</u> invoke <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> to tell the SQLite
430
core about the columns and datatypes in the virtual table.
431
The <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> API has the following prototype:
434
int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable)
437
<p>The first argument to <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> must be the same
438
<a href="c3ref/sqlite3.html">database connection</a> pointer as the first parameter to this method.
439
The second argument to <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> must a zero-terminated
440
UTF-8 string that contains a well-formed <a href="lang_createtable.html">CREATE TABLE</a> statement that
441
defines the columns in the virtual table and their data types.
442
The name of the table in this CREATE TABLE statement is ignored,
443
as are all constraints. Only the column names and datatypes matter.
444
The CREATE TABLE statement string need not to be
445
held in persistent memory. The string can be
446
deallocated and/or reused as soon as the <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a>
449
<p>The xCreate method need not initialize the pModule, nRef, and zErrMsg
450
fields of the <a href="c3ref/vtab.html">sqlite3_vtab</a> object. The SQLite core will take care of
453
<p>The xCreate must should return <a href="c3ref/c_abort.html">SQLITE_OK</a> if it is successful in
454
creating the new virtual table, or <a href="c3ref/c_abort.html">SQLITE_ERROR</a> if it is not successful.
455
If not successful, the <a href="c3ref/vtab.html">sqlite3_vtab</a> structure must not be allocated.
456
An error message may optionally be returned in *pzErr if unsuccessful.
457
Space to hold the error message string must be allocated using
458
an SQLite memory allocation function like
459
<a href="c3ref/free.html">sqlite3_malloc()</a> or <a href="c3ref/mprintf.html">sqlite3_mprintf()</a> as the SQLite core will
460
attempt to free the space using <a href="c3ref/free.html">sqlite3_free()</a> after the error has
461
been reported up to the application.
463
<p>The xCreate method is required for every virtual table implementation,
464
though the xCreate and <a href="vtab.html#xconnect">xConnect</a> pointers of the <a href="c3ref/module.html">sqlite3_module</a> object
465
may point to the same function the virtual table does not need to initialize
468
<a name="hiddencol"></a>
470
<h4>2.1.1 Hidden columns in virtual tables</h4>
471
<p>If a column datatype contains the special keyword "HIDDEN"
472
(in any combination of upper and lower case letters) then that keyword
473
it is omitted from the column datatype name and the column is marked
474
as a hidden column internally.
475
A hidden column differs from a normal column in three respects:
479
<li> Hidden columns are not listed in the dataset returned by
480
"<a href="pragma.html#pragma_table_info">PRAGMA table_info</a>",
481
<li> Hidden columns are not included in the expansion of a "*"
482
expression in the result set of a <a href="lang_select.html">SELECT</a>, and
483
<li> Hidden columns are not included in the implicit column-list
484
used by an <a href="lang_insert.html">INSERT</a> statement that lacks an explicit column-list.
487
<p>For example, if the following SQL is passed to <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a>:
490
CREATE TABLE x(a HIDDEN VARCHAR(12), b INTEGER, c INTEGER Hidden);
493
<p>Then the virtual table would be created with two hidden columns,
494
and with datatypes of "VARCHAR(12)" and "INTEGER".
496
<p>An example use of hidden columns can be seen in the <a href="fts3.html">FTS3</a> virtual
497
table implementation, where every FTS virtual table
498
contains an <a href="fts3.html#hiddencol">FTS hidden column</a> that is used to pass information from the
499
virtual table into <a href="fts3.html#snippet">FTS auxiliary functions</a> and to the <a href="fts3.html#section_3">FTS MATCH</a> operator.
502
<a name="xconnect"></a>
504
<h3>2.2 The xConnect Method</h3>
507
int (*xConnect)(sqlite3*, void *pAux,
508
int argc, char **argv,
509
sqlite3_vtab **ppVTab,
513
<p>The xConnect method is very similar to <a href="vtab.html#xcreate">xCreate</a>.
514
It has the same parameters and constructs a new <a href="c3ref/vtab.html">sqlite3_vtab</a> structure
516
And it must also call <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> like xCreate.
518
<p>The difference is that xConnect is called to establish a new
519
connection to an existing virtual table whereas xCreate is called
520
to create a new virtual table from scratch.
522
<p>The xCreate and xConnect methods are only different when the
523
virtual table has some kind of backing store that must be initialized
524
the first time the virtual table is created. The xCreate method creates
525
and initializes the backing store. The xConnect method just connects
526
to an existing backing store.
528
<p>As an example, consider a virtual table implementation that
529
provides read-only access to existing comma-separated-value (CSV)
530
files on disk. There is no backing store that needs to be created
531
or initialized for such a virtual table (since the CSV files already
532
exist on disk) so the xCreate and xConnect methods will be identical
535
<p>Another example is a virtual table that implements a full-text index.
536
The xCreate method must create and initialize data structures to hold
537
the dictionary and posting lists for that index. The xConnect method,
538
on the other hand, only has to locate and use an existing dictionary
539
and posting lists that were created by a prior xCreate call.
541
<p>The xConnect method must return <a href="c3ref/c_abort.html">SQLITE_OK</a> if it is successful
542
in creating the new virtual table, or <a href="c3ref/c_abort.html">SQLITE_ERROR</a> if it is not
543
successful. If not successful, the <a href="c3ref/vtab.html">sqlite3_vtab</a> structure must not be
544
allocated. An error message may optionally be returned in *pzErr if
546
Space to hold the error message string must be allocated using
547
an SQLite memory allocation function like
548
<a href="c3ref/free.html">sqlite3_malloc()</a> or <a href="c3ref/mprintf.html">sqlite3_mprintf()</a> as the SQLite core will
549
attempt to free the space using <a href="c3ref/free.html">sqlite3_free()</a> after the error has
550
been reported up to the application.
552
<p>The xConnect method is required for every virtual table implementation,
553
though the <a href="vtab.html#xcreate">xCreate</a> and xConnect pointers of the <a href="c3ref/module.html">sqlite3_module</a> object
554
may point to the same function the virtual table does not need to initialize
557
<a name="xbestindex"></a>
559
<h3>2.3 The xBestIndex Method</h3>
561
<p>SQLite uses the xBestIndex method of a virtual table module to determine
562
the best way to access the virtual table.
563
The xBestIndex method has a prototype like this:
566
int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
569
<p>The SQLite core communicates with the xBestIndex method by filling
570
in certain fields of the <a href="c3ref/index_info.html">sqlite3_index_info</a> structure and passing a
571
pointer to that structure into xBestIndex as the second parameter.
572
The xBestIndex method fills out other fields of this structure which
573
forms the reply. The <a href="c3ref/index_info.html">sqlite3_index_info</a> structure looks like this:
576
struct sqlite3_index_info {
578
const int nConstraint; /* Number of entries in aConstraint */
579
const struct sqlite3_index_constraint {
580
int iColumn; /* Column on left-hand side of constraint */
581
unsigned char op; /* Constraint operator */
582
unsigned char usable; /* True if this constraint is usable */
583
int iTermOffset; /* Used internally - xBestIndex should ignore */
584
} *const aConstraint; /* Table of WHERE clause constraints */
585
const int nOrderBy; /* Number of terms in the ORDER BY clause */
586
const struct sqlite3_index_orderby {
587
int iColumn; /* Column number */
588
unsigned char desc; /* True for DESC. False for ASC. */
589
} *const aOrderBy; /* The ORDER BY clause */
592
struct sqlite3_index_constraint_usage {
593
int argvIndex; /* if >0, constraint is part of argv to xFilter */
594
unsigned char omit; /* Do not code a test for this constraint */
595
} *const aConstraintUsage;
596
int idxNum; /* Number used to identify the index */
597
char *idxStr; /* String, possibly obtained from sqlite3_malloc */
598
int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
599
int orderByConsumed; /* True if output is already ordered */
600
double estimatedCost; /* Estimated cost of using this index */
604
<p>In addition, there are some defined constants:
607
#define SQLITE_INDEX_CONSTRAINT_EQ 2
608
#define SQLITE_INDEX_CONSTRAINT_GT 4
609
#define SQLITE_INDEX_CONSTRAINT_LE 8
610
#define SQLITE_INDEX_CONSTRAINT_LT 16
611
#define SQLITE_INDEX_CONSTRAINT_GE 32
612
#define SQLITE_INDEX_CONSTRAINT_MATCH 64
615
<p>The SQLite core calls the xBestIndex method when it is compiling a query
616
that involves a virtual table. In other words, SQLite calls this method
617
when it is running <a href="c3ref/prepare.html">sqlite3_prepare()</a> or the equivalent.
618
By calling this method, the
619
SQLite core is saying to the virtual table that it needs to access
620
some subset of the rows in the virtual table and it wants to know the
621
most efficient way to do that access. The xBestIndex method replies
622
with information that the SQLite core can then use to conduct an
623
efficient search of the virtual table.
625
<p>While compiling a single SQL query, the SQLite core might call
626
xBestIndex multiple times with different settings in <a href="c3ref/index_info.html">sqlite3_index_info</a>.
627
The SQLite core will then select the combination that appears to
628
give the best performance.
630
<p>Before calling this method, the SQLite core initializes an instance
631
of the <a href="c3ref/index_info.html">sqlite3_index_info</a> structure with information about the
632
query that it is currently trying to process. This information
633
derives mainly from the WHERE clause and ORDER BY or GROUP BY clauses
634
of the query, but also from any ON or USING clauses if the query is a
635
join. The information that the SQLite core provides to the xBestIndex
636
method is held in the part of the structure that is marked as "Inputs".
637
The "Outputs" section is initialized to zero.
639
<p>The information in the <a href="c3ref/index_info.html">sqlite3_index_info</a> structure is ephemeral
640
and may be overwritten or deallocated as soon as the xBestIndex method
641
returns. If the xBestIndex method needs to remember any part of the
642
<a href="c3ref/index_info.html">sqlite3_index_info</a> structure, it should make a copy. Care must be
643
take to store the copy in a place where it will be deallocated, such
644
as in the idxStr field with needToFreeIdxStr set to 1.
646
<p>Note that xBestIndex will always be called before <a href="vtab.html#xfilter">xFilter</a>, since
647
the idxNum and idxStr outputs from xBestIndex are required inputs to
648
xFilter. However, there is no guarantee that xFilter will be called
649
following a successful xBestIndex.
651
<p>The xBestIndex method is required for every virtual table implementation.
653
<h4>2.3.1 Inputs</h4>
655
<p>The main thing that the SQLite core is trying to communicate to
656
the virtual table is the constraints that are available to limit
657
the number of rows that need to be searched. The aConstraint[] array
658
contains one entry for each constraint. There will be exactly
659
nConstraint entries in that array.
661
<p>Each constraint will correspond to a term in the WHERE clause
662
or in a USING or ON clause that is of the form
668
<p>Where "column" is a column in the virtual table, OP is an operator
669
like "=" or "<", and EXPR is an arbitrary expression. So, for example,
670
if the WHERE clause contained a term like this:
676
<p>Then one of the constraints would be on the "a" column with
677
operator "=" and an expression of "5". Constraints need not have a
678
literal representation of the WHERE clause. The query optimizer might
679
make transformations to the
680
WHERE clause in order to extract as many constraints
681
as it can. So, for example, if the WHERE clause contained something
685
x BETWEEN 10 AND 100 AND 999>y
688
<p>The query optimizer might translate this into three separate constraints:
696
<p>For each constraint, the aConstraint[].iColumn field indicates which
697
column appears on the left-hand side of the constraint.
698
The first column of the virtual table is column 0.
699
The rowid of the virtual table is column -1.
700
The aConstraint[].op field indicates which operator is used.
701
The SQLITE_INDEX_CONSTRAINT_* constants map integer constants
702
into operator values.
703
Columns occur in the order they were defined by the call to
704
<a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> in the <a href="vtab.html#xcreate">xCreate</a> or <a href="vtab.html#xconnect">xConnect</a> method.
705
Hidden columns are counted when determining the column index.
707
<p>The aConstraint[] array contains information about all constraints
708
that apply to the virtual table. But some of the constraints might
709
not be usable because of the way tables are ordered in a join.
710
The xBestIndex method must therefore only consider constraints
711
that have an aConstraint[].usable flag which is true.
713
<p>In addition to WHERE clause constraints, the SQLite core also
714
tells the xBestIndex method about the ORDER BY clause.
715
(In an aggregate query, the SQLite core might put in GROUP BY clause
716
information in place of the ORDER BY clause information, but this fact
717
should not make any difference to the xBestIndex method.)
718
If all terms of the ORDER BY clause are columns in the virtual table,
719
then nOrderBy will be the number of terms in the ORDER BY clause
720
and the aOrderBy[] array will identify the column for each term
721
in the order by clause and whether or not that column is ASC or DESC.
723
<h4>2.3.2 Outputs</h4>
725
<p>Given all of the information above, the job of the xBestIndex
726
method it to figure out the best way to search the virtual table.
728
<p>The xBestIndex method fills the idxNum and idxStr fields with
729
information that communicates an indexing strategy to the <a href="vtab.html#xfilter">xFilter</a>
730
method. The information in idxNum and idxStr is arbitrary as far
731
as the SQLite core is concerned. The SQLite core just copies the
732
information through to the <a href="vtab.html#xfilter">xFilter</a> method. Any desired meaning can
733
be assigned to idxNum and idxStr as long as xBestIndex and xFilter
734
agree on what that meaning is.
736
<p>The idxStr value may be a string obtained from an SQLite
737
memory allocation function such as <a href="c3ref/mprintf.html">sqlite3_mprintf()</a>.
738
If this is the case, then the needToFreeIdxStr flag must be set to
739
true so that the SQLite core will know to call <a href="c3ref/free.html">sqlite3_free()</a> on
740
that string when it has finished with it, and thus avoid a memory leak.
742
<p>If the virtual table will output rows in the order specified by
743
the ORDER BY clause, then the orderByConsumed flag may be set to
744
true. If the output is not automatically in the correct order
745
then orderByConsumed must be left in its default false setting.
746
This will indicate to the SQLite core that it will need to do a
747
separate sorting pass over the data after it comes out of the virtual table.
749
<p>The estimatedCost field should be set to the estimated number
750
of disk access operations required to execute this query against
751
the virtual table. The SQLite core will often call xBestIndex
752
multiple times with different constraints, obtain multiple cost
753
estimates, then choose the query plan that gives the lowest estimate.
755
<p>The aConstraintUsage[] array contains one element for each of
756
the nConstraint constraints in the inputs section of the
757
<a href="c3ref/index_info.html">sqlite3_index_info</a> structure.
758
The aConstraintUsage[] array is used by xBestIndex to tell the
759
core how it is using the constraints.
761
<p>The xBestIndex method may set aConstraintUsage[].argvIndex
762
entries to values greater than one.
763
Exactly one entry should be set to 1, another to 2, another to 3,
764
and so forth up to as many or as few as the xBestIndex method wants.
765
The EXPR of the corresponding constraints will then be passed
766
in as the argv[] parameters to xFilter.
768
<p>For example, if the aConstraint[3].argvIndex is set to 1, then
769
when xFilter is called, the argv[0] passed to xFilter will have
770
the EXPR value of the aConstraint[3] constraint.
772
<p>By default, the SQLite core double checks all constraints on
773
each row of the virtual table that it receives. If such a check
774
is redundant, the xBestFilter method can suppress that double-check by
775
setting aConstraintUsage[].omit.
777
<a name="xdisconnect"></a>
779
<h3>2.4 The xDisconnect Method</h3>
782
int (*xDisconnect)(sqlite3_vtab *pVTab);
785
<p>This method releases a connection to a virtual table.
786
Only the <a href="c3ref/vtab.html">sqlite3_vtab</a> object is destroyed.
787
The virtual table is not destroyed and any backing store
788
associated with the virtual table persists.
790
This method undoes the work of <a href="vtab.html#xconnect">xConnect</a>.
792
<p>This method is a destructor for a connection to the virtual table.
793
Contrast this method with <a href="vtab.html#sqlite3_module.xDestroy">xDestroy</a>. The xDestroy is a destructor
794
for the entire virtual table.
796
<p>The xDestroy method is required for every virtual table implementation,
797
though it is acceptable for the <a href="vtab.html#xdisconnect">xDisconnect</a> and xDestroy methods to be
798
the same function if that makes sense for the particular virtual table.
800
<a name="sqlite3_module.xDestroy"></a>
802
<h3>2.5 The xDestroy Method</h3>
805
int (*xDestroy)(sqlite3_vtab *pVTab);
808
<p>This method releases a connection to a virtual table, just like
809
the <a href="vtab.html#xdisconnect">xDisconnect</a> method, and it also destroys the underlying
810
table implementation. This method undoes the work of <a href="vtab.html#xcreate">xCreate</a>.
812
<p>The <a href="vtab.html#xdisconnect">xDisconnect</a> method is called whenever a database connection
813
that uses a virtual table is closed. The xDestroy method is only
814
called when a <a href="lang_droptable.html">DROP TABLE</a> statement is executed against the virtual table.
816
<p>The xDisconnect method is required for every virtual table implementation,
817
though it is acceptable for the xDisconnect and <a href="vtab.html#sqlite3_module.xDestroy">xDestroy</a> methods to be
818
the same function if that makes sense for the particular virtual table.
822
<h3>2.6 The xOpen Method</h3>
825
int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
828
<p>The xOpen method creates a new cursor used for accessing (read and/or
829
writing) a virtual table. A successful invocation of this method
830
will allocate the memory for the <a href="c3ref/vtab_cursor.html">sqlite3_vtab_cursor</a> (or a subclass),
831
initialize the new object, and make *ppCursor point to the new object.
832
The successful call then returns <a href="c3ref/c_abort.html">SQLITE_OK</a>.
834
<p>For every successful call to this method, the SQLite core will
835
later invoke the <a href="vtab.html#xclose">xClose</a> method to destroy
836
the allocated cursor.
838
<p>The xOpen method need not initialize the pVtab field of the
839
<a href="c3ref/vtab_cursor.html">sqlite3_vtab_cursor</a> structure. The SQLite core will take care
840
of that chore automatically.
842
<p>A virtual table implementation must be able to support an arbitrary
843
number of simultaneously open cursors.
845
<p>When initially opened, the cursor is in an undefined state.
846
The SQLite core will invoke the <a href="vtab.html#xfilter">xFilter</a> method
847
on the cursor prior to any attempt to position or read from the cursor.
849
<p>The xOpen method is required for every virtual table implementation.
851
<a name="xclose"></a>
853
<h3>2.7 The xClose Method</h3>
856
int (*xClose)(sqlite3_vtab_cursor*);
859
<p>The xClose method closes a cursor previously opened by
860
<a href="vtab.html#xopen">xOpen</a>.
861
The SQLite core will always call xClose once for each cursor opened
864
<p>This method must release all resources allocated by the
865
corresponding xOpen call. The routine will not be called again even if it
866
returns an error. The SQLite core will not use the
867
<a href="c3ref/vtab_cursor.html">sqlite3_vtab_cursor</a> again after it has been closed.
869
<p>The xClose method is required for every virtual table implementation.
873
<h3>2.8 The xEof Method</h3>
876
int (*xEof)(sqlite3_vtab_cursor*);
879
<p>The xEof method must return false (zero) if the specified cursor
880
currently points to a valid row of data, or true (non-zero) otherwise.
881
This method is called by the SQL engine immediately after each
882
<a href="vtab.html#xfilter">xFilter</a> and <a href="vtab.html#xnext">xNext</a> invocation.
884
<p>The xEof method is required for every virtual table implementation.
886
<a name="xfilter"></a>
888
<h3>2.9 The xFilter Method</h3>
891
int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
892
int argc, sqlite3_value **argv);
895
<p>This method begins a search of a virtual table.
896
The first argument is a cursor opened by <a href="vtab.html#xopen">xOpen</a>.
897
The next two argument define a particular search index previously
898
chosen by <a href="vtab.html#xbestindex">xBestIndex</a>. The specific meanings of idxNum and idxStr
899
are unimportant as long as xFilter and xBestIndex agree on what
902
<p>The xBestIndex function may have requested the values of
903
certain expressions using the aConstraintUsage[].argvIndex values
904
of the <a href="c3ref/index_info.html">sqlite3_index_info</a> structure.
905
Those values are passed to xFilter using the argc and argv parameters.
907
<p>If the virtual table contains one or more rows that match the
908
search criteria, then the cursor must be left point at the first row.
909
Subsequent calls to <a href="vtab.html#xeof">xEof</a> must return false (zero).
910
If there are no rows match, then the cursor must be left in a state
911
that will cause the <a href="vtab.html#xeof">xEof</a> to return true (non-zero).
912
The SQLite engine will use
913
the <a href="vtab.html#xcolumn">xColumn</a> and <a href="vtab.html#xrowid">xRowid</a> methods to access that row content.
914
The <a href="vtab.html#xnext">xNext</a> method will be used to advance to the next row.
916
<p>This method must return <a href="c3ref/c_abort.html">SQLITE_OK</a> if successful, or an sqlite
917
<a href="c3ref/c_abort.html">error code</a> if an error occurs.
919
<p>The xFilter method is required for every virtual table implementation.
923
<h3>2.10 The xNext Method</h3>
926
int (*xNext)(sqlite3_vtab_cursor*);
929
<p>The xNext method advances a <a href="c3ref/vtab_cursor.html">virtual table cursor</a>
930
to the next row of a result set initiated by <a href="vtab.html#xfilter">xFilter</a>.
931
If the cursor is already pointing at the last row when this
932
routine is called, then the cursor no longer points to valid
933
data and a subsequent call to the <a href="vtab.html#xeof">xEof</a> method must return true (non-zero).
934
If the cursor is successfully advanced to another row of content, then
935
subsequent calls to <a href="vtab.html#xeof">xEof</a> must return false (zero).
937
<p>This method must return <a href="c3ref/c_abort.html">SQLITE_OK</a> if successful, or an sqlite
938
<a href="c3ref/c_abort.html">error code</a> if an error occurs.
940
<p>The xNext method is required for every virtual table implementation.
942
<a name="xcolumn"></a>
944
<h3>2.11 The xColumn Method</h3>
947
int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int N);
950
<p>The SQLite core invokes this method in order to find the value for
951
the N-th column of the current row. N is zero-based so the first column
953
The xColumn method may return its result back to SQLite using one of the
958
<li> <a href="c3ref/result_blob.html">sqlite3_result_blob()</a>
959
<li> <a href="c3ref/result_blob.html">sqlite3_result_double()</a>
960
<li> <a href="c3ref/result_blob.html">sqlite3_result_int()</a>
961
<li> <a href="c3ref/result_blob.html">sqlite3_result_int64()</a>
962
<li> <a href="c3ref/result_blob.html">sqlite3_result_null()</a>
963
<li> <a href="c3ref/result_blob.html">sqlite3_result_text()</a>
964
<li> <a href="c3ref/result_blob.html">sqlite3_result_text16()</a>
965
<li> <a href="c3ref/result_blob.html">sqlite3_result_text16le()</a>
966
<li> <a href="c3ref/result_blob.html">sqlite3_result_text16be()</a>
967
<li> <a href="c3ref/result_blob.html">sqlite3_result_zeroblob()</a>
971
<p>If the xColumn method implementation calls none of the functions above,
972
then the value of the column defaults to an SQL NULL.
974
<p>To raise an error, the xColumn method should use one of the result_text()
975
methods to set the error message text, then return an appropriate
976
<a href="c3ref/c_abort.html">error code</a>. The xColumn method must return <a href="c3ref/c_abort.html">SQLITE_OK</a> on success.
978
<p>The xColumn method is required for every virtual table implementation.
980
<a name="xrowid"></a>
982
<h3>2.12 The xRowid Method</h3>
985
int (*xRowid)(sqlite3_vtab_cursor *pCur, sqlite_int64 *pRowid);
988
<p>A successful invocation of this method will cause *pRowid to be
989
filled with the <a href="lang_createtable.html#rowid">rowid</a> of row that the
990
<a href="c3ref/vtab_cursor.html">virtual table cursor</a> pCur is currently pointing at.
991
This method returns <a href="c3ref/c_abort.html">SQLITE_OK</a> on success.
992
It returns an appropriate <a href="c3ref/c_abort.html">error code</a> on failure.</p>
994
<p>The xRowid method is required for every virtual table implementation.
996
<a name="xupdate"></a>
998
<h3>2.13 The xUpdate Method</h3>
1002
sqlite3_vtab *pVTab,
1004
sqlite3_value **argv,
1005
sqlite_int64 *pRowid
1009
<p>All changes to a virtual table are made using the xUpdate method.
1010
This one method can be used to insert, delete, or update.
1012
<p>The argc parameter specifies the number of entries in the argv array.
1013
The value of argc will be 1 for a pure delete operation or N+2 for an insert
1014
or replace or update where N is the number of columns in the table.
1015
In the previous sentence, N includes any hidden columns.
1017
<p>Every argv entry will have a non-NULL value in C but may contain the
1018
SQL value NULL. In other words, it is always true that
1019
<tt>argv[i]!=0</tt> for <b>i</b> between 0 and <tt>argc-1</tt>.
1020
However, it might be the case that
1021
<tt>sqlite3_value_type(argv[i])==SQLITE_NULL</tt>.
1023
<p>The argv[0] parameter is the <a href="lang_createtable.html#rowid">rowid</a> of a row in the virtual table
1024
to be deleted. If argv[0] is an SQL NULL, then no deletion occurs.
1026
<p>The argv[1] parameter is the rowid of a new row to be inserted
1027
into the virtual table. If argv[1] is an SQL NULL, then the implementation
1028
must choose a rowid for the newly inserted row. Subsequent argv[]
1029
entries contain values of the columns of the virtual table, in the
1030
order that the columns were declared. The number of columns will
1031
match the table declaration that the <a href="vtab.html#xconnect">xConnect</a> or <a href="vtab.html#xcreate">xCreate</a> method made
1032
using the <a href="c3ref/declare_vtab.html">sqlite3_declare_vtab()</a> call. All hidden columns are included.
1034
<p>When doing an insert without a rowid (argc>1, argv[1] is an SQL NULL), the
1035
implementation must set *pRowid to the rowid of the newly inserted row;
1036
this will become the value returned by the <a href="c3ref/last_insert_rowid.html">sqlite3_last_insert_rowid()</a>
1037
function. Setting this value in all the other cases is a harmless no-op;
1038
the SQLite engine ignores the *pRowid return value if argc==1 or
1039
argv[1] is not an SQL NULL.
1041
<p>Each call to xUpdate will fall into one of cases shown below.
1042
Not that references to <b>argv[i]</b> mean the SQL value
1043
held within the argv[i] object, not the argv[i]
1049
<dd><p>The single row with rowid equal to argv[0] is deleted. No insert occurs.
1051
<dt><b>argc > 1 <br> argv[0] = NULL</b>
1052
<dd><p>A new row is inserted with a rowid argv[1] and column values in
1053
argv[2] and following. If argv[1] is an SQL NULL,
1054
the a new unique rowid is generated automatically.
1056
<dt><b>argc > 1 <br> argv[0] ≠ NULL <br> argv[0] = argv[1]</b>
1057
<dd><p>The row with rowid argv[0] is updated with new values
1058
in argv[2] and following parameters.
1060
<dt><b>argc > 1 <br> argv[0] ≠ NULL <br> argv[0] ≠ argv[1]</b>
1061
<dd><p> The row with rowid argv[0] is updated with rowid argv[1]
1062
and new values in argv[2] and following parameters. This will occur
1063
when an SQL statement updates a rowid, as in the statement:
1065
<a href="lang_update.html">UPDATE</a> table SET rowid=rowid+1 WHERE ...;
1070
<p>The xUpdate method must return <a href="c3ref/c_abort.html">SQLITE_OK</a> if and only if it is
1071
successful. If a failure occurs, the xUpdate must return an appropriate
1072
<a href="c3ref/c_abort.html">error code</a>. On a failure, the pVTab->zErrMsg element may optionally
1073
be replaced with error message text stored in memory allocated from SQLite
1074
using functions such as <a href="c3ref/mprintf.html">sqlite3_mprintf()</a> or <a href="c3ref/free.html">sqlite3_malloc()</a>.
1076
<p>If the xUpdate method violates some constraint of the virtual table
1077
(including, but not limited to, attempting to store a value of the wrong
1078
datatype, attempting to store a value that is too
1079
large or too small, or attempting to change a read-only value) then the
1080
xUpdate must fail with an appropriate <a href="c3ref/c_abort.html">error code</a>.
1082
<p>There might be one or more <a href="c3ref/vtab_cursor.html">sqlite3_vtab_cursor</a> objects open and in use
1083
on the virtual table instance and perhaps even on the row of the virtual
1084
table when the xUpdate method is invoked. The implementation of
1085
xUpdate must be prepared for attempts to delete or modify rows of the table
1086
out from other existing cursors. If the virtual table cannot accommodate
1087
such changes, the xUpdate method must return an <a href="c3ref/c_abort.html">error code</a>.
1089
<p>The xUpdate method is optional.
1090
If the xUpdate pointer in the <a href="c3ref/module.html">sqlite3_module</a> for a virtual table
1091
is a NULL pointer, then the virtual table is read-only.
1094
<a name="xfindfunction"></a>
1096
<h3>2.14 The xFindFunction Method</h3>
1099
int (*xFindFunction)(
1100
sqlite3_vtab *pVtab,
1103
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1108
<p>This method is called during <a href="c3ref/prepare.html">sqlite3_prepare()</a> to give the virtual
1109
table implementation an opportunity to overload functions.
1110
This method may be set to NULL in which case no overloading occurs.
1112
<p>When a function uses a column from a virtual table as its first
1113
argument, this method is called to see if the virtual table would
1114
like to overload the function. The first three parameters are inputs:
1115
the virtual table, the number of arguments to the function, and the
1116
name of the function. If no overloading is desired, this method
1117
returns 0. To overload the function, this method writes the new
1118
function implementation into *pxFunc and writes user data into *ppArg
1121
<p>Note that infix functions (<a href="lang_expr.html#like">LIKE</a>, <a href="lang_expr.html#glob">GLOB</a>, <a href="lang_expr.html#regexp">REGEXP</a>, and <a href="lang_expr.html#match">MATCH</a>) reverse
1122
the order of their arguments. So "like(A,B)" is equivalent to "B like A".
1123
For the form "B like A" the B term is considered the first argument
1124
to the function. But for "like(A,B)" the A term is considered the
1127
<p>The function pointer returned by this routine must be valid for
1128
the lifetime of the <a href="c3ref/vtab.html">sqlite3_vtab</a> object given in the first parameter.
1130
<a name="xBegin"></a>
1132
<h3>2.15 The xBegin Method</h3>
1135
int (*xBegin)(sqlite3_vtab *pVTab);
1138
<p>This method begins a transaction on a virtual table.
1139
This is method is optional. The xBegin pointer of <a href="c3ref/module.html">sqlite3_module</a>
1142
<p>This method is always followed by one call to either the
1143
<a href="vtab.html#xcommit">xCommit</a> or <a href="vtab.html#xrollback">xRollback</a> method. Virtual table transactions do
1144
not nest, so the xBegin method will not be invoked more than once
1145
on a single virtual table
1146
without an intervening call to either <a href="vtab.html#xcommit">xCommit</a> or <a href="vtab.html#xrollback">xRollback</a>.
1147
Multiple calls to other methods can and likely will occur in between
1148
the xBegin and the corresponding <a href="vtab.html#xcommit">xCommit</a> or <a href="vtab.html#xrollback">xRollback</a>.
1150
<a name="xsync"></a>
1152
<h3>2.16 The xSync Method</h3>
1155
int (*xSync)(sqlite3_vtab *pVTab);
1159
<p>This method signals the start of a two-phase commit on a virtual
1161
This is method is optional. The xSync pointer of <a href="c3ref/module.html">sqlite3_module</a>
1164
<p>This method is only invoked after call to the <a href="vtab.html#xBegin">xBegin</a> method and
1165
prior to an <a href="vtab.html#xcommit">xCommit</a> or <a href="vtab.html#xrollback">xRollback</a>. In order to implement two-phase
1166
commit, the xSync method on all virtual tables is invoked prior to
1167
invoking the <a href="vtab.html#xcommit">xCommit</a> method on any virtual table. If any of the
1168
xSync methods fail, the entire transaction is rolled back.
1170
<a name="xcommit"></a>
1172
<h3>2.17 The xCommit Method</h3>
1175
int (*xCommit)(sqlite3_vtab *pVTab);
1178
<p>This method causes a virtual table transaction to commit.
1179
This is method is optional. The xCommit pointer of <a href="c3ref/module.html">sqlite3_module</a>
1182
<p>A call to this method always follows a prior call to <a href="vtab.html#xBegin">xBegin</a> and
1183
<a href="vtab.html#xsync">xSync</a>.
1186
<a name="xrollback"></a>
1188
<h3>2.18 The xRollback Method</h3>
1191
int (*xRollback)(sqlite3_vtab *pVTab);
1194
<p>This method causes a virtual table transaction to rollback.
1195
This is method is optional. The xRollback pointer of <a href="c3ref/module.html">sqlite3_module</a>
1198
<p>A call to this method always follows a prior call to <a href="vtab.html#xBegin">xBegin</a>.
1201
<a name="xrename"></a>
1203
<h3>2.19 The xRename Method</h3>
1206
int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
1209
<p>This method provides notification that the virtual table implementation
1210
that the virtual table will be given a new name.
1211
If this method returns <a href="c3ref/c_abort.html">SQLITE_OK</a> then SQLite renames the table.
1212
If this method returns an <a href="c3ref/c_abort.html">error code</a> then the renaming is prevented.
1214
<p>The xRename method is required for every virtual table implementation.
1216
<a name="xsavepoint"></a>
1218
<h3>2.20 The xSavepoint, xRelease, and xRollbackTo Methods</h3>
1221
int (*xSavepoint)(sqlite3_vtab *pVtab, int);
1222
int (*xRelease)(sqlite3_vtab *pVtab, int);
1223
int (*xRollbackTo)(sqlite3_vtab *pVtab, int);
1227
These methods provide the virtual table implementation an opportunity to
1228
implement nested transactions. They are always optional and will only be
1229
called in SQLite <a href="releaselog/3_7_7.html">version 3.7.7</a> and later.
1233
When xSavepoint(X,N) is invoked, that is a signal to the virtual table X
1234
that it should save its current state as savepoint N.
1236
to xRollbackTo(X,R) means that the state of the virtual table should return
1237
to what it was when xSavepoint(X,R) was last called.
1239
to xRollbackTo(X,R) will invalidate all savepoints with N>R; none of the
1240
invalided savepoints will be rolled back or released without first
1241
being reinitialized by a call to xSavepoint().
1242
A call to xRelease(X,M) invalidates all savepoints where N>=M.
1246
None of the xSavepoint(), xRelease(), or xRollbackTo() methods will ever
1247
be called except in between calls to xBegin() and
1248
either xCommit() or xRollback().