blob: 2b647b12ea0f3d6a95508c0c2b9b251b827e0b8d [file] [log] [blame]
drhb9bb7c12006-06-11 23:41:55 +00001/*
2** 2006 June 10
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code used to help implement virtual tables.
13**
danielk1977f1a381e2006-06-16 08:01:02 +000014** $Id: vtab.c,v 1.14 2006/06/16 08:01:04 danielk1977 Exp $
drhb9bb7c12006-06-11 23:41:55 +000015*/
16#ifndef SQLITE_OMIT_VIRTUALTABLE
17#include "sqliteInt.h"
18
19/*
20** External API function used to create a new virtual-table module.
21*/
22int sqlite3_create_module(
23 sqlite3 *db, /* Database in which module is registered */
24 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000025 const sqlite3_module *pModule, /* The definition of the module */
26 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000027){
danielk1977d1ab1ba2006-06-15 04:28:13 +000028 int nName = strlen(zName);
29 Module *pMod = (Module *)sqliteMallocRaw(sizeof(Module) + nName + 1);
30 if( pMod ){
31 char *zCopy = (char *)(&pMod[1]);
32 strcpy(zCopy, zName);
33 pMod->zName = zCopy;
34 pMod->pModule = pModule;
35 pMod->pAux = pAux;
36 pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
37 sqliteFree(pMod);
38 sqlite3ResetInternalSchema(db, 0);
39 }
40 return sqlite3ApiExit(db, SQLITE_OK);
drhb9bb7c12006-06-11 23:41:55 +000041}
42
drhb9bb7c12006-06-11 23:41:55 +000043/*
44** Clear any and all virtual-table information from the Table record.
45** This routine is called, for example, just before deleting the Table
46** record.
47*/
48void sqlite3VtabClear(Table *p){
49 if( p->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +000050 assert( p->pMod && p->pMod->pModule );
51 p->pMod->pModule->xDisconnect(p->pVtab);
drhb9bb7c12006-06-11 23:41:55 +000052 }
53 if( p->azModuleArg ){
54 int i;
55 for(i=0; i<p->nModuleArg; i++){
56 sqliteFree(p->azModuleArg[i]);
57 }
58 sqliteFree(p->azModuleArg);
59 }
60}
61
62/*
63** Add a new module argument to pTable->azModuleArg[].
64** The string is not copied - the pointer is stored. The
65** string will be freed automatically when the table is
66** deleted.
67*/
68static void addModuleArgument(Table *pTable, char *zArg){
69 int i = pTable->nModuleArg++;
70 pTable->azModuleArg = sqliteRealloc(pTable->azModuleArg,
71 sizeof(char*)*(pTable->nModuleArg+1));
72 if( pTable->azModuleArg==0 ){
73 pTable->nModuleArg = 0;
74 sqliteFree(zArg);
75 }else{
76 pTable->azModuleArg[i] = zArg;
77 pTable->azModuleArg[i+1] = 0;
78 }
79}
80
81/*
82** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
83** statement. The module name has been parsed, but the optional list
84** of parameters that follow the module name are still pending.
85*/
86void sqlite3VtabBeginParse(
87 Parse *pParse, /* Parsing context */
88 Token *pName1, /* Name of new table, or database name */
89 Token *pName2, /* Name of new table or NULL */
90 Token *pModuleName /* Name of the module for the virtual table */
91){
danielk1977f1a381e2006-06-16 08:01:02 +000092 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +000093 Table *pTable; /* The new virtual table */
danielk1977f1a381e2006-06-16 08:01:02 +000094 Token *pDummy; /* Dummy arg for sqlite3TwoPartName() */
drhb9bb7c12006-06-11 23:41:55 +000095
danielk1977f1a381e2006-06-16 08:01:02 +000096 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +000097 pTable = pParse->pNewTable;
danielk1977f1a381e2006-06-16 08:01:02 +000098 if( pTable==0 || pParse->nErr ) return;
99 assert( 0==pTable->pIndex );
100
drhb9bb7c12006-06-11 23:41:55 +0000101 pTable->isVirtual = 1;
102 pTable->nModuleArg = 0;
103 addModuleArgument(pTable, sqlite3NameFromToken(pModuleName));
104 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
danielk1977f1a381e2006-06-16 08:01:02 +0000105
106#ifndef SQLITE_OMIT_AUTHORIZATION
107 /* Creating a virtual table invokes the authorization callback twice.
108 ** The first invocation, to obtain permission to INSERT a row into the
109 ** sqlite_master table, has already been made by sqlite3StartTable().
110 ** The second call, to obtain permission to create the table, is made now.
111 */
112 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pDummy);
113 assert( iDb>=0 );
114 if( sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
115 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName)
116 ){
117 return;
118 }
119#endif
drhb9bb7c12006-06-11 23:41:55 +0000120}
121
122/*
123** This routine takes the module argument that has been accumulating
124** in pParse->zArg[] and appends it to the list of arguments on the
125** virtual table currently under construction in pParse->pTable.
126*/
127static void addArgumentToVtab(Parse *pParse){
128 if( pParse->nArgUsed && pParse->pNewTable ){
129 addModuleArgument(pParse->pNewTable, sqliteStrDup(pParse->zArg));
130 }
131 pParse->nArgUsed = 0;
132}
133
134/*
135** The parser calls this routine after the CREATE VIRTUAL TABLE statement
136** has been completely parsed.
137*/
138void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
139 Table *pTab; /* The table being constructed */
140 sqlite3 *db; /* The database connection */
141 char *zModule; /* The module name of the table: USING modulename */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000142 Module *pMod = 0;
drhb9bb7c12006-06-11 23:41:55 +0000143
144 addArgumentToVtab(pParse);
145 sqliteFree(pParse->zArg);
146 pParse->zArg = 0;
147 pParse->nArgAlloc = 0;
148
149 /* Lookup the module name. */
150 pTab = pParse->pNewTable;
151 if( pTab==0 ) return;
152 db = pParse->db;
153 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000154 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000155 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
156 pTab->pMod = pMod;
drhb9bb7c12006-06-11 23:41:55 +0000157
158 /* If the CREATE VIRTUAL TABLE statement is being entered for the
159 ** first time (in other words if the virtual table is actually being
160 ** created now instead of just being read out of sqlite_master) then
161 ** do additional initialization work and store the statement text
162 ** in the sqlite_master table.
163 */
164 if( !db->init.busy ){
165 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000166 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000167 int iDb;
168 Vdbe *v;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000169 if( !pMod ){
danielk1977a4e76362006-06-14 06:31:28 +0000170 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
drhb9bb7c12006-06-11 23:41:55 +0000171 }
172
173 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
174 if( pEnd ){
175 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
176 }
177 zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
178
179 /* A slot for the record has already been allocated in the
180 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000181 ** the information we've collected.
182 **
183 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
184 ** This value is always 0 and is ignored, a virtual table does not have a
185 ** rootpage. The next entry on the stack is the rowid of the record
186 ** in the sqlite_master table.
drhb9bb7c12006-06-11 23:41:55 +0000187 */
188 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
189 sqlite3NestedParse(pParse,
190 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000191 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
192 "WHERE rowid=#1",
drhb9bb7c12006-06-11 23:41:55 +0000193 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
194 pTab->zName,
195 pTab->zName,
196 zStmt
197 );
198 sqliteFree(zStmt);
199 v = sqlite3GetVdbe(pParse);
drhb9bb7c12006-06-11 23:41:55 +0000200 sqlite3ChangeCookie(db, v, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000201
202 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
203 zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
204 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
205 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000206 }
207
danielk197778efaba2006-06-12 06:09:17 +0000208 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000209 ** record of the table. If the module has already been registered,
210 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000211 */
danielk197778efaba2006-06-12 06:09:17 +0000212 else {
danielk197778efaba2006-06-12 06:09:17 +0000213 Table *pOld;
214 Schema *pSchema = pTab->pSchema;
215 const char *zName = pTab->zName;
216 int nName = strlen(zName) + 1;
217 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
218 if( pOld ){
219 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
220 return;
221 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000222 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000223 }
224}
225
226/*
227** The parser calls this routine when it sees the first token
228** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
229*/
230void sqlite3VtabArgInit(Parse *pParse){
231 addArgumentToVtab(pParse);
232 pParse->nArgUsed = 0;
233}
234
235/*
236** The parser calls this routine for each token after the first token
237** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
238*/
239void sqlite3VtabArgExtend(Parse *pParse, Token *p){
240 if( pParse->nArgUsed + p->n + 2 >= pParse->nArgAlloc ){
241 pParse->nArgAlloc = pParse->nArgAlloc*2 + p->n + 200;
242 pParse->zArg = sqliteRealloc(pParse->zArg, pParse->nArgAlloc);
243 if( pParse->zArg==0 ){
244 pParse->nArgAlloc = 0;
245 return;
246 }
247 }
248 if( pParse->nArgUsed ){
249 pParse->zArg[pParse->nArgUsed++] = ' ';
250 }
251 memcpy(&pParse->zArg[pParse->nArgUsed], p->z, p->n);
252 pParse->nArgUsed += p->n;
253 pParse->zArg[pParse->nArgUsed] = 0;
254}
255
danielk197778efaba2006-06-12 06:09:17 +0000256/*
danielk19779da9d472006-06-14 06:58:15 +0000257** Invoke a virtual table constructor (either xCreate or xConnect). The
258** pointer to the function to invoke is passed as the fourth parameter
259** to this procedure.
260*/
261static int vtabCallConstructor(
262 sqlite3 *db,
263 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000264 Module *pMod,
danielk19779da9d472006-06-14 06:58:15 +0000265 int (*xConstruct)(sqlite3*, void *, int, char **, sqlite3_vtab **),
266 char **pzErr
267){
268 int rc;
269 int rc2;
270 char **azArg = pTab->azModuleArg;
271 int nArg = pTab->nModuleArg;
272
273 assert( !db->pVTab );
274 assert( xConstruct );
275
276 db->pVTab = pTab;
277 rc = sqlite3SafetyOff(db);
278 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000279 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab);
danielk19779da9d472006-06-14 06:58:15 +0000280 rc2 = sqlite3SafetyOn(db);
281 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000282 pTab->pVtab->pModule = pMod->pModule;
danielk19779da9d472006-06-14 06:58:15 +0000283 }
284
285 if( SQLITE_OK!=rc ){
286 *pzErr = sqlite3MPrintf("vtable constructor failed: %s", pTab->zName);
287 } else if( db->pVTab ){
288 const char *zFormat = "vtable constructor did not declare schema: %s";
289 *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
290 rc = SQLITE_ERROR;
291 }
292 if( rc==SQLITE_OK ){
293 rc = rc2;
294 }
295 db->pVTab = 0;
296 return rc;
297}
298
299/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000300** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000301** of the virtual table pTab. If an error occurs, an error code is returned
302** and an error left in pParse.
303**
304** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000305*/
306int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000307 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000308 const char *zModule;
309 int rc = SQLITE_OK;
310
danielk1977fe3fcbe22006-06-12 12:08:45 +0000311 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000312 return SQLITE_OK;
313 }
314
danielk1977d1ab1ba2006-06-15 04:28:13 +0000315 pMod = pTab->pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000316 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000317 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000318 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000319 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000320 rc = SQLITE_ERROR;
321 } else {
danielk19779da9d472006-06-14 06:58:15 +0000322 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000323 sqlite3 *db = pParse->db;
324 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000325 if( rc!=SQLITE_OK ){
326 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000327 }
danielk19779da9d472006-06-14 06:58:15 +0000328 sqliteFree(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000329 }
330
331 return rc;
332}
333
danielk19779da9d472006-06-14 06:58:15 +0000334/*
335** This function is invoked by the vdbe to call the xCreate method
336** of the virtual table named zTab in database iDb.
337**
338** If an error occurs, *pzErr is set to point an an English language
339** description of the error and an SQLITE_XXX error code is returned.
340** In this case the caller must call sqliteFree() on *pzErr.
341*/
342int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
343 int rc = SQLITE_OK;
344 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000345 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000346 const char *zModule;
347
348 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
349 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000350 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000351 zModule = pTab->azModuleArg[0];
352
353 /* If the module has been registered and includes a Create method,
354 ** invoke it now. If the module has not been registered, return an
355 ** error. Otherwise, do nothing.
356 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000357 if( !pMod ){
danielk19779da9d472006-06-14 06:58:15 +0000358 *pzErr = sqlite3MPrintf("no such module: %s", zModule);
359 rc = SQLITE_ERROR;
360 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000361 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000362 }
363
364 return rc;
365}
danielk1977be8a7832006-06-13 15:00:54 +0000366
367/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000368** This function is used to set the schema of a virtual table. It is only
369** valid to call this function from within the xCreate() or xConnect() of a
370** virtual table module.
371*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000372int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
373 Parse sParse;
374
375 int rc = SQLITE_OK;
376 Table *pTab = db->pVTab;
377 char *zErr = 0;
378
379 if( !pTab ){
380 sqlite3Error(db, SQLITE_MISUSE, 0);
381 return SQLITE_MISUSE;
382 }
383 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
384
385 memset(&sParse, 0, sizeof(Parse));
386 sParse.declareVtab = 1;
387 sParse.db = db;
388
389 if(
390 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
391 sParse.pNewTable &&
392 !sParse.pNewTable->pSelect &&
393 !sParse.pNewTable->isVirtual
394 ){
395 pTab->aCol = sParse.pNewTable->aCol;
396 pTab->nCol = sParse.pNewTable->nCol;
397 sParse.pNewTable->nCol = 0;
398 sParse.pNewTable->aCol = 0;
399 } else {
400 sqlite3Error(db, SQLITE_ERROR, zErr);
401 sqliteFree(zErr);
402 rc = SQLITE_ERROR;
403 }
404 sParse.declareVtab = 0;
405
406 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
407 sqlite3DeleteTable(0, sParse.pNewTable);
408 sParse.pNewTable = 0;
409 db->pVTab = 0;
410
411 return rc;
412}
413
414/*
danielk19779e39ce82006-06-12 16:01:21 +0000415** This function is invoked by the vdbe to call the xDestroy method
416** of the virtual table named zTab in database iDb. This occurs
417** when a DROP TABLE is mentioned.
418**
419** This call is a no-op if zTab is not a virtual table.
420*/
421int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
422{
423 int rc = SQLITE_OK;
424 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000425
426 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000427 assert(pTab);
428 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000429 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000430 rc = sqlite3SafetyOff(db);
431 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000432 if( xDestroy ){
433 rc = xDestroy(pTab->pVtab);
434 }
danielk19779e39ce82006-06-12 16:01:21 +0000435 sqlite3SafetyOn(db);
436 if( rc==SQLITE_OK ){
437 pTab->pVtab = 0;
438 }
439 }
440
441 return rc;
442}
443
drhb9bb7c12006-06-11 23:41:55 +0000444#endif /* SQLITE_OMIT_VIRTUALTABLE */