OLD | NEW |
1 /* | 1 /* |
2 ** 2006 June 10 | 2 ** 2006 June 10 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains code used to help implement virtual tables. | 12 ** This file contains code used to help implement virtual tables. |
13 */ | 13 */ |
14 #ifndef SQLITE_OMIT_VIRTUALTABLE | 14 #ifndef SQLITE_OMIT_VIRTUALTABLE |
15 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
16 | 16 |
17 /* | 17 /* |
18 ** Before a virtual table xCreate() or xConnect() method is invoked, the | 18 ** Before a virtual table xCreate() or xConnect() method is invoked, the |
19 ** sqlite3.pVtabCtx member variable is set to point to an instance of | 19 ** sqlite3.pVtabCtx member variable is set to point to an instance of |
20 ** this struct allocated on the stack. It is used by the implementation of | 20 ** this struct allocated on the stack. It is used by the implementation of |
21 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which | 21 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which |
22 ** are invoked only from within xCreate and xConnect methods. | 22 ** are invoked only from within xCreate and xConnect methods. |
23 */ | 23 */ |
24 struct VtabCtx { | 24 struct VtabCtx { |
25 VTable *pVTable; /* The virtual table being constructed */ | 25 VTable *pVTable; /* The virtual table being constructed */ |
26 Table *pTab; /* The Table object to which the virtual table belongs */ | 26 Table *pTab; /* The Table object to which the virtual table belongs */ |
| 27 VtabCtx *pPrior; /* Parent context (if any) */ |
| 28 int bDeclared; /* True after sqlite3_declare_vtab() is called */ |
27 }; | 29 }; |
28 | 30 |
29 /* | 31 /* |
30 ** The actual function that does the work of creating a new module. | 32 ** The actual function that does the work of creating a new module. |
31 ** This function implements the sqlite3_create_module() and | 33 ** This function implements the sqlite3_create_module() and |
32 ** sqlite3_create_module_v2() interfaces. | 34 ** sqlite3_create_module_v2() interfaces. |
33 */ | 35 */ |
34 static int createModule( | 36 static int createModule( |
35 sqlite3 *db, /* Database in which module is registered */ | 37 sqlite3 *db, /* Database in which module is registered */ |
36 const char *zName, /* Name assigned to this module */ | 38 const char *zName, /* Name assigned to this module */ |
(...skipping 12 matching lines...) Expand all Loading... |
49 Module *pMod; | 51 Module *pMod; |
50 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); | 52 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); |
51 if( pMod ){ | 53 if( pMod ){ |
52 Module *pDel; | 54 Module *pDel; |
53 char *zCopy = (char *)(&pMod[1]); | 55 char *zCopy = (char *)(&pMod[1]); |
54 memcpy(zCopy, zName, nName+1); | 56 memcpy(zCopy, zName, nName+1); |
55 pMod->zName = zCopy; | 57 pMod->zName = zCopy; |
56 pMod->pModule = pModule; | 58 pMod->pModule = pModule; |
57 pMod->pAux = pAux; | 59 pMod->pAux = pAux; |
58 pMod->xDestroy = xDestroy; | 60 pMod->xDestroy = xDestroy; |
| 61 pMod->pEpoTab = 0; |
59 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); | 62 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); |
60 assert( pDel==0 || pDel==pMod ); | 63 assert( pDel==0 || pDel==pMod ); |
61 if( pDel ){ | 64 if( pDel ){ |
62 db->mallocFailed = 1; | 65 db->mallocFailed = 1; |
63 sqlite3DbFree(db, pDel); | 66 sqlite3DbFree(db, pDel); |
64 } | 67 } |
65 } | 68 } |
66 } | 69 } |
67 rc = sqlite3ApiExit(db, rc); | 70 rc = sqlite3ApiExit(db, rc); |
68 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux); | 71 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux); |
69 | 72 |
70 sqlite3_mutex_leave(db->mutex); | 73 sqlite3_mutex_leave(db->mutex); |
71 return rc; | 74 return rc; |
72 } | 75 } |
73 | 76 |
74 | 77 |
75 /* | 78 /* |
76 ** External API function used to create a new virtual-table module. | 79 ** External API function used to create a new virtual-table module. |
77 */ | 80 */ |
78 int sqlite3_create_module( | 81 int sqlite3_create_module( |
79 sqlite3 *db, /* Database in which module is registered */ | 82 sqlite3 *db, /* Database in which module is registered */ |
80 const char *zName, /* Name assigned to this module */ | 83 const char *zName, /* Name assigned to this module */ |
81 const sqlite3_module *pModule, /* The definition of the module */ | 84 const sqlite3_module *pModule, /* The definition of the module */ |
82 void *pAux /* Context pointer for xCreate/xConnect */ | 85 void *pAux /* Context pointer for xCreate/xConnect */ |
83 ){ | 86 ){ |
| 87 #ifdef SQLITE_ENABLE_API_ARMOR |
| 88 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 89 #endif |
84 return createModule(db, zName, pModule, pAux, 0); | 90 return createModule(db, zName, pModule, pAux, 0); |
85 } | 91 } |
86 | 92 |
87 /* | 93 /* |
88 ** External API function used to create a new virtual-table module. | 94 ** External API function used to create a new virtual-table module. |
89 */ | 95 */ |
90 int sqlite3_create_module_v2( | 96 int sqlite3_create_module_v2( |
91 sqlite3 *db, /* Database in which module is registered */ | 97 sqlite3 *db, /* Database in which module is registered */ |
92 const char *zName, /* Name assigned to this module */ | 98 const char *zName, /* Name assigned to this module */ |
93 const sqlite3_module *pModule, /* The definition of the module */ | 99 const sqlite3_module *pModule, /* The definition of the module */ |
94 void *pAux, /* Context pointer for xCreate/xConnect */ | 100 void *pAux, /* Context pointer for xCreate/xConnect */ |
95 void (*xDestroy)(void *) /* Module destructor function */ | 101 void (*xDestroy)(void *) /* Module destructor function */ |
96 ){ | 102 ){ |
| 103 #ifdef SQLITE_ENABLE_API_ARMOR |
| 104 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 105 #endif |
97 return createModule(db, zName, pModule, pAux, xDestroy); | 106 return createModule(db, zName, pModule, pAux, xDestroy); |
98 } | 107 } |
99 | 108 |
100 /* | 109 /* |
101 ** Lock the virtual table so that it cannot be disconnected. | 110 ** Lock the virtual table so that it cannot be disconnected. |
102 ** Locks nest. Every lock should have a corresponding unlock. | 111 ** Locks nest. Every lock should have a corresponding unlock. |
103 ** If an unlock is omitted, resources leaks will occur. | 112 ** If an unlock is omitted, resources leaks will occur. |
104 ** | 113 ** |
105 ** If a disconnect is attempted while a virtual table is locked, | 114 ** If a disconnect is attempted while a virtual table is locked, |
106 ** the disconnect is deferred until all locks have been removed. | 115 ** the disconnect is deferred until all locks have been removed. |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 } | 279 } |
271 } | 280 } |
272 | 281 |
273 /* | 282 /* |
274 ** Add a new module argument to pTable->azModuleArg[]. | 283 ** Add a new module argument to pTable->azModuleArg[]. |
275 ** The string is not copied - the pointer is stored. The | 284 ** The string is not copied - the pointer is stored. The |
276 ** string will be freed automatically when the table is | 285 ** string will be freed automatically when the table is |
277 ** deleted. | 286 ** deleted. |
278 */ | 287 */ |
279 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ | 288 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ |
280 int i = pTable->nModuleArg++; | 289 int nBytes = sizeof(char *)*(2+pTable->nModuleArg); |
281 int nBytes = sizeof(char *)*(1+pTable->nModuleArg); | |
282 char **azModuleArg; | 290 char **azModuleArg; |
283 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); | 291 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); |
284 if( azModuleArg==0 ){ | 292 if( azModuleArg==0 ){ |
285 int j; | |
286 for(j=0; j<i; j++){ | |
287 sqlite3DbFree(db, pTable->azModuleArg[j]); | |
288 } | |
289 sqlite3DbFree(db, zArg); | 293 sqlite3DbFree(db, zArg); |
290 sqlite3DbFree(db, pTable->azModuleArg); | |
291 pTable->nModuleArg = 0; | |
292 }else{ | 294 }else{ |
| 295 int i = pTable->nModuleArg++; |
293 azModuleArg[i] = zArg; | 296 azModuleArg[i] = zArg; |
294 azModuleArg[i+1] = 0; | 297 azModuleArg[i+1] = 0; |
| 298 pTable->azModuleArg = azModuleArg; |
295 } | 299 } |
296 pTable->azModuleArg = azModuleArg; | |
297 } | 300 } |
298 | 301 |
299 /* | 302 /* |
300 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE | 303 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE |
301 ** statement. The module name has been parsed, but the optional list | 304 ** statement. The module name has been parsed, but the optional list |
302 ** of parameters that follow the module name are still pending. | 305 ** of parameters that follow the module name are still pending. |
303 */ | 306 */ |
304 void sqlite3VtabBeginParse( | 307 void sqlite3VtabBeginParse( |
305 Parse *pParse, /* Parsing context */ | 308 Parse *pParse, /* Parsing context */ |
306 Token *pName1, /* Name of new table, or database name */ | 309 Token *pName1, /* Name of new table, or database name */ |
(...skipping 12 matching lines...) Expand all Loading... |
319 | 322 |
320 db = pParse->db; | 323 db = pParse->db; |
321 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); | 324 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); |
322 assert( iDb>=0 ); | 325 assert( iDb>=0 ); |
323 | 326 |
324 pTable->tabFlags |= TF_Virtual; | 327 pTable->tabFlags |= TF_Virtual; |
325 pTable->nModuleArg = 0; | 328 pTable->nModuleArg = 0; |
326 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); | 329 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
327 addModuleArgument(db, pTable, 0); | 330 addModuleArgument(db, pTable, 0); |
328 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); | 331 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
329 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); | 332 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) |
| 333 || (pParse->sNameToken.z==pName1->z && pName2->z==0) |
| 334 ); |
| 335 pParse->sNameToken.n = (int)( |
| 336 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z |
| 337 ); |
330 | 338 |
331 #ifndef SQLITE_OMIT_AUTHORIZATION | 339 #ifndef SQLITE_OMIT_AUTHORIZATION |
332 /* Creating a virtual table invokes the authorization callback twice. | 340 /* Creating a virtual table invokes the authorization callback twice. |
333 ** The first invocation, to obtain permission to INSERT a row into the | 341 ** The first invocation, to obtain permission to INSERT a row into the |
334 ** sqlite_master table, has already been made by sqlite3StartTable(). | 342 ** sqlite_master table, has already been made by sqlite3StartTable(). |
335 ** The second call, to obtain permission to create the table, is made now. | 343 ** The second call, to obtain permission to create the table, is made now. |
336 */ | 344 */ |
337 if( pTable->azModuleArg ){ | 345 if( pTable->azModuleArg ){ |
338 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, | 346 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, |
339 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); | 347 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 /* If the CREATE VIRTUAL TABLE statement is being entered for the | 379 /* If the CREATE VIRTUAL TABLE statement is being entered for the |
372 ** first time (in other words if the virtual table is actually being | 380 ** first time (in other words if the virtual table is actually being |
373 ** created now instead of just being read out of sqlite_master) then | 381 ** created now instead of just being read out of sqlite_master) then |
374 ** do additional initialization work and store the statement text | 382 ** do additional initialization work and store the statement text |
375 ** in the sqlite_master table. | 383 ** in the sqlite_master table. |
376 */ | 384 */ |
377 if( !db->init.busy ){ | 385 if( !db->init.busy ){ |
378 char *zStmt; | 386 char *zStmt; |
379 char *zWhere; | 387 char *zWhere; |
380 int iDb; | 388 int iDb; |
| 389 int iReg; |
381 Vdbe *v; | 390 Vdbe *v; |
382 | 391 |
383 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ | 392 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ |
384 if( pEnd ){ | 393 if( pEnd ){ |
385 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; | 394 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; |
386 } | 395 } |
387 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); | 396 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); |
388 | 397 |
389 /* A slot for the record has already been allocated in the | 398 /* A slot for the record has already been allocated in the |
390 ** SQLITE_MASTER table. We just need to update that slot with all | 399 ** SQLITE_MASTER table. We just need to update that slot with all |
(...skipping 14 matching lines...) Expand all Loading... |
405 zStmt, | 414 zStmt, |
406 pParse->regRowid | 415 pParse->regRowid |
407 ); | 416 ); |
408 sqlite3DbFree(db, zStmt); | 417 sqlite3DbFree(db, zStmt); |
409 v = sqlite3GetVdbe(pParse); | 418 v = sqlite3GetVdbe(pParse); |
410 sqlite3ChangeCookie(pParse, iDb); | 419 sqlite3ChangeCookie(pParse, iDb); |
411 | 420 |
412 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); | 421 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
413 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); | 422 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); |
414 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); | 423 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
415 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, | 424 |
416 pTab->zName, sqlite3Strlen30(pTab->zName) + 1); | 425 iReg = ++pParse->nMem; |
| 426 sqlite3VdbeLoadString(v, iReg, pTab->zName); |
| 427 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg); |
417 } | 428 } |
418 | 429 |
419 /* If we are rereading the sqlite_master table create the in-memory | 430 /* If we are rereading the sqlite_master table create the in-memory |
420 ** record of the table. The xConnect() method is not called until | 431 ** record of the table. The xConnect() method is not called until |
421 ** the first time the virtual table is used in an SQL statement. This | 432 ** the first time the virtual table is used in an SQL statement. This |
422 ** allows a schema that contains virtual tables to be loaded before | 433 ** allows a schema that contains virtual tables to be loaded before |
423 ** the required virtual table implementations are registered. */ | 434 ** the required virtual table implementations are registered. */ |
424 else { | 435 else { |
425 Table *pOld; | 436 Table *pOld; |
426 Schema *pSchema = pTab->pSchema; | 437 Schema *pSchema = pTab->pSchema; |
(...skipping 22 matching lines...) Expand all Loading... |
449 /* | 460 /* |
450 ** The parser calls this routine for each token after the first token | 461 ** The parser calls this routine for each token after the first token |
451 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. | 462 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. |
452 */ | 463 */ |
453 void sqlite3VtabArgExtend(Parse *pParse, Token *p){ | 464 void sqlite3VtabArgExtend(Parse *pParse, Token *p){ |
454 Token *pArg = &pParse->sArg; | 465 Token *pArg = &pParse->sArg; |
455 if( pArg->z==0 ){ | 466 if( pArg->z==0 ){ |
456 pArg->z = p->z; | 467 pArg->z = p->z; |
457 pArg->n = p->n; | 468 pArg->n = p->n; |
458 }else{ | 469 }else{ |
459 assert(pArg->z < p->z); | 470 assert(pArg->z <= p->z); |
460 pArg->n = (int)(&p->z[p->n] - pArg->z); | 471 pArg->n = (int)(&p->z[p->n] - pArg->z); |
461 } | 472 } |
462 } | 473 } |
463 | 474 |
464 /* | 475 /* |
465 ** Invoke a virtual table constructor (either xCreate or xConnect). The | 476 ** Invoke a virtual table constructor (either xCreate or xConnect). The |
466 ** pointer to the function to invoke is passed as the fourth parameter | 477 ** pointer to the function to invoke is passed as the fourth parameter |
467 ** to this procedure. | 478 ** to this procedure. |
468 */ | 479 */ |
469 static int vtabCallConstructor( | 480 static int vtabCallConstructor( |
470 sqlite3 *db, | 481 sqlite3 *db, |
471 Table *pTab, | 482 Table *pTab, |
472 Module *pMod, | 483 Module *pMod, |
473 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), | 484 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), |
474 char **pzErr | 485 char **pzErr |
475 ){ | 486 ){ |
476 VtabCtx sCtx, *pPriorCtx; | 487 VtabCtx sCtx; |
477 VTable *pVTable; | 488 VTable *pVTable; |
478 int rc; | 489 int rc; |
479 const char *const*azArg = (const char *const*)pTab->azModuleArg; | 490 const char *const*azArg = (const char *const*)pTab->azModuleArg; |
480 int nArg = pTab->nModuleArg; | 491 int nArg = pTab->nModuleArg; |
481 char *zErr = 0; | 492 char *zErr = 0; |
482 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); | 493 char *zModuleName; |
483 int iDb; | 494 int iDb; |
| 495 VtabCtx *pCtx; |
484 | 496 |
| 497 /* Check that the virtual-table is not already being initialized */ |
| 498 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){ |
| 499 if( pCtx->pTab==pTab ){ |
| 500 *pzErr = sqlite3MPrintf(db, |
| 501 "vtable constructor called recursively: %s", pTab->zName |
| 502 ); |
| 503 return SQLITE_LOCKED; |
| 504 } |
| 505 } |
| 506 |
| 507 zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
485 if( !zModuleName ){ | 508 if( !zModuleName ){ |
486 return SQLITE_NOMEM; | 509 return SQLITE_NOMEM; |
487 } | 510 } |
488 | 511 |
489 pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); | 512 pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); |
490 if( !pVTable ){ | 513 if( !pVTable ){ |
491 sqlite3DbFree(db, zModuleName); | 514 sqlite3DbFree(db, zModuleName); |
492 return SQLITE_NOMEM; | 515 return SQLITE_NOMEM; |
493 } | 516 } |
494 pVTable->db = db; | 517 pVTable->db = db; |
495 pVTable->pMod = pMod; | 518 pVTable->pMod = pMod; |
496 | 519 |
497 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 520 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
498 pTab->azModuleArg[1] = db->aDb[iDb].zName; | 521 pTab->azModuleArg[1] = db->aDb[iDb].zName; |
499 | 522 |
500 /* Invoke the virtual table constructor */ | 523 /* Invoke the virtual table constructor */ |
501 assert( &db->pVtabCtx ); | 524 assert( &db->pVtabCtx ); |
502 assert( xConstruct ); | 525 assert( xConstruct ); |
503 sCtx.pTab = pTab; | 526 sCtx.pTab = pTab; |
504 sCtx.pVTable = pVTable; | 527 sCtx.pVTable = pVTable; |
505 pPriorCtx = db->pVtabCtx; | 528 sCtx.pPrior = db->pVtabCtx; |
| 529 sCtx.bDeclared = 0; |
506 db->pVtabCtx = &sCtx; | 530 db->pVtabCtx = &sCtx; |
507 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); | 531 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); |
508 db->pVtabCtx = pPriorCtx; | 532 db->pVtabCtx = sCtx.pPrior; |
509 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; | 533 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| 534 assert( sCtx.pTab==pTab ); |
510 | 535 |
511 if( SQLITE_OK!=rc ){ | 536 if( SQLITE_OK!=rc ){ |
512 if( zErr==0 ){ | 537 if( zErr==0 ){ |
513 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); | 538 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); |
514 }else { | 539 }else { |
515 *pzErr = sqlite3MPrintf(db, "%s", zErr); | 540 *pzErr = sqlite3MPrintf(db, "%s", zErr); |
516 sqlite3_free(zErr); | 541 sqlite3_free(zErr); |
517 } | 542 } |
518 sqlite3DbFree(db, pVTable); | 543 sqlite3DbFree(db, pVTable); |
519 }else if( ALWAYS(pVTable->pVtab) ){ | 544 }else if( ALWAYS(pVTable->pVtab) ){ |
520 /* Justification of ALWAYS(): A correct vtab constructor must allocate | 545 /* Justification of ALWAYS(): A correct vtab constructor must allocate |
521 ** the sqlite3_vtab object if successful. */ | 546 ** the sqlite3_vtab object if successful. */ |
522 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); | 547 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); |
523 pVTable->pVtab->pModule = pMod->pModule; | 548 pVTable->pVtab->pModule = pMod->pModule; |
524 pVTable->nRef = 1; | 549 pVTable->nRef = 1; |
525 if( sCtx.pTab ){ | 550 if( sCtx.bDeclared==0 ){ |
526 const char *zFormat = "vtable constructor did not declare schema: %s"; | 551 const char *zFormat = "vtable constructor did not declare schema: %s"; |
527 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); | 552 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); |
528 sqlite3VtabUnlock(pVTable); | 553 sqlite3VtabUnlock(pVTable); |
529 rc = SQLITE_ERROR; | 554 rc = SQLITE_ERROR; |
530 }else{ | 555 }else{ |
531 int iCol; | 556 int iCol; |
| 557 u8 oooHidden = 0; |
532 /* If everything went according to plan, link the new VTable structure | 558 /* If everything went according to plan, link the new VTable structure |
533 ** into the linked list headed by pTab->pVTable. Then loop through the | 559 ** into the linked list headed by pTab->pVTable. Then loop through the |
534 ** columns of the table to see if any of them contain the token "hidden". | 560 ** columns of the table to see if any of them contain the token "hidden". |
535 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from | 561 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from |
536 ** the type string. */ | 562 ** the type string. */ |
537 pVTable->pNext = pTab->pVTable; | 563 pVTable->pNext = pTab->pVTable; |
538 pTab->pVTable = pVTable; | 564 pTab->pVTable = pVTable; |
539 | 565 |
540 for(iCol=0; iCol<pTab->nCol; iCol++){ | 566 for(iCol=0; iCol<pTab->nCol; iCol++){ |
541 char *zType = pTab->aCol[iCol].zType; | 567 char *zType = pTab->aCol[iCol].zType; |
542 int nType; | 568 int nType; |
543 int i = 0; | 569 int i = 0; |
544 if( !zType ) continue; | 570 if( !zType ){ |
| 571 pTab->tabFlags |= oooHidden; |
| 572 continue; |
| 573 } |
545 nType = sqlite3Strlen30(zType); | 574 nType = sqlite3Strlen30(zType); |
546 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){ | 575 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){ |
547 for(i=0; i<nType; i++){ | 576 for(i=0; i<nType; i++){ |
548 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) | 577 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) |
549 && (zType[i+7]=='\0' || zType[i+7]==' ') | 578 && (zType[i+7]=='\0' || zType[i+7]==' ') |
550 ){ | 579 ){ |
551 i++; | 580 i++; |
552 break; | 581 break; |
553 } | 582 } |
554 } | 583 } |
555 } | 584 } |
556 if( i<nType ){ | 585 if( i<nType ){ |
557 int j; | 586 int j; |
558 int nDel = 6 + (zType[i+6] ? 1 : 0); | 587 int nDel = 6 + (zType[i+6] ? 1 : 0); |
559 for(j=i; (j+nDel)<=nType; j++){ | 588 for(j=i; (j+nDel)<=nType; j++){ |
560 zType[j] = zType[j+nDel]; | 589 zType[j] = zType[j+nDel]; |
561 } | 590 } |
562 if( zType[i]=='\0' && i>0 ){ | 591 if( zType[i]=='\0' && i>0 ){ |
563 assert(zType[i-1]==' '); | 592 assert(zType[i-1]==' '); |
564 zType[i-1] = '\0'; | 593 zType[i-1] = '\0'; |
565 } | 594 } |
566 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; | 595 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; |
| 596 oooHidden = TF_OOOHidden; |
| 597 }else{ |
| 598 pTab->tabFlags |= oooHidden; |
567 } | 599 } |
568 } | 600 } |
569 } | 601 } |
570 } | 602 } |
571 | 603 |
572 sqlite3DbFree(db, zModuleName); | 604 sqlite3DbFree(db, zModuleName); |
573 return rc; | 605 return rc; |
574 } | 606 } |
575 | 607 |
576 /* | 608 /* |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); | 692 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); |
661 | 693 |
662 /* Locate the required virtual table module */ | 694 /* Locate the required virtual table module */ |
663 zMod = pTab->azModuleArg[0]; | 695 zMod = pTab->azModuleArg[0]; |
664 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); | 696 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); |
665 | 697 |
666 /* If the module has been registered and includes a Create method, | 698 /* If the module has been registered and includes a Create method, |
667 ** invoke it now. If the module has not been registered, return an | 699 ** invoke it now. If the module has not been registered, return an |
668 ** error. Otherwise, do nothing. | 700 ** error. Otherwise, do nothing. |
669 */ | 701 */ |
670 if( !pMod ){ | 702 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){ |
671 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); | 703 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); |
672 rc = SQLITE_ERROR; | 704 rc = SQLITE_ERROR; |
673 }else{ | 705 }else{ |
674 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); | 706 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); |
675 } | 707 } |
676 | 708 |
677 /* Justification of ALWAYS(): The xConstructor method is required to | 709 /* Justification of ALWAYS(): The xConstructor method is required to |
678 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ | 710 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ |
679 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ | 711 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ |
680 rc = growVTrans(db); | 712 rc = growVTrans(db); |
681 if( rc==SQLITE_OK ){ | 713 if( rc==SQLITE_OK ){ |
682 addToVTrans(db, sqlite3GetVTable(db, pTab)); | 714 addToVTrans(db, sqlite3GetVTable(db, pTab)); |
683 } | 715 } |
684 } | 716 } |
685 | 717 |
686 return rc; | 718 return rc; |
687 } | 719 } |
688 | 720 |
689 /* | 721 /* |
690 ** This function is used to set the schema of a virtual table. It is only | 722 ** This function is used to set the schema of a virtual table. It is only |
691 ** valid to call this function from within the xCreate() or xConnect() of a | 723 ** valid to call this function from within the xCreate() or xConnect() of a |
692 ** virtual table module. | 724 ** virtual table module. |
693 */ | 725 */ |
694 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ | 726 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
| 727 VtabCtx *pCtx; |
695 Parse *pParse; | 728 Parse *pParse; |
696 | |
697 int rc = SQLITE_OK; | 729 int rc = SQLITE_OK; |
698 Table *pTab; | 730 Table *pTab; |
699 char *zErr = 0; | 731 char *zErr = 0; |
700 | 732 |
| 733 #ifdef SQLITE_ENABLE_API_ARMOR |
| 734 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ |
| 735 return SQLITE_MISUSE_BKPT; |
| 736 } |
| 737 #endif |
701 sqlite3_mutex_enter(db->mutex); | 738 sqlite3_mutex_enter(db->mutex); |
702 if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ | 739 pCtx = db->pVtabCtx; |
| 740 if( !pCtx || pCtx->bDeclared ){ |
703 sqlite3Error(db, SQLITE_MISUSE); | 741 sqlite3Error(db, SQLITE_MISUSE); |
704 sqlite3_mutex_leave(db->mutex); | 742 sqlite3_mutex_leave(db->mutex); |
705 return SQLITE_MISUSE_BKPT; | 743 return SQLITE_MISUSE_BKPT; |
706 } | 744 } |
| 745 pTab = pCtx->pTab; |
707 assert( (pTab->tabFlags & TF_Virtual)!=0 ); | 746 assert( (pTab->tabFlags & TF_Virtual)!=0 ); |
708 | 747 |
709 pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); | 748 pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); |
710 if( pParse==0 ){ | 749 if( pParse==0 ){ |
711 rc = SQLITE_NOMEM; | 750 rc = SQLITE_NOMEM; |
712 }else{ | 751 }else{ |
713 pParse->declareVtab = 1; | 752 pParse->declareVtab = 1; |
714 pParse->db = db; | 753 pParse->db = db; |
715 pParse->nQueryLoop = 1; | 754 pParse->nQueryLoop = 1; |
716 | 755 |
717 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) | 756 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) |
718 && pParse->pNewTable | 757 && pParse->pNewTable |
719 && !db->mallocFailed | 758 && !db->mallocFailed |
720 && !pParse->pNewTable->pSelect | 759 && !pParse->pNewTable->pSelect |
721 && (pParse->pNewTable->tabFlags & TF_Virtual)==0 | 760 && (pParse->pNewTable->tabFlags & TF_Virtual)==0 |
722 ){ | 761 ){ |
723 if( !pTab->aCol ){ | 762 if( !pTab->aCol ){ |
724 pTab->aCol = pParse->pNewTable->aCol; | 763 pTab->aCol = pParse->pNewTable->aCol; |
725 pTab->nCol = pParse->pNewTable->nCol; | 764 pTab->nCol = pParse->pNewTable->nCol; |
726 pParse->pNewTable->nCol = 0; | 765 pParse->pNewTable->nCol = 0; |
727 pParse->pNewTable->aCol = 0; | 766 pParse->pNewTable->aCol = 0; |
728 } | 767 } |
729 db->pVtabCtx->pTab = 0; | 768 pCtx->bDeclared = 1; |
730 }else{ | 769 }else{ |
731 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); | 770 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); |
732 sqlite3DbFree(db, zErr); | 771 sqlite3DbFree(db, zErr); |
733 rc = SQLITE_ERROR; | 772 rc = SQLITE_ERROR; |
734 } | 773 } |
735 pParse->declareVtab = 0; | 774 pParse->declareVtab = 0; |
736 | 775 |
737 if( pParse->pVdbe ){ | 776 if( pParse->pVdbe ){ |
738 sqlite3VdbeFinalize(pParse->pVdbe); | 777 sqlite3VdbeFinalize(pParse->pVdbe); |
739 } | 778 } |
(...skipping 14 matching lines...) Expand all Loading... |
754 ** when a DROP TABLE is mentioned. | 793 ** when a DROP TABLE is mentioned. |
755 ** | 794 ** |
756 ** This call is a no-op if zTab is not a virtual table. | 795 ** This call is a no-op if zTab is not a virtual table. |
757 */ | 796 */ |
758 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ | 797 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ |
759 int rc = SQLITE_OK; | 798 int rc = SQLITE_OK; |
760 Table *pTab; | 799 Table *pTab; |
761 | 800 |
762 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); | 801 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
763 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ | 802 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ |
764 VTable *p = vtabDisconnectAll(db, pTab); | 803 VTable *p; |
765 | 804 int (*xDestroy)(sqlite3_vtab *); |
766 assert( rc==SQLITE_OK ); | 805 for(p=pTab->pVTable; p; p=p->pNext){ |
767 rc = p->pMod->pModule->xDestroy(p->pVtab); | 806 assert( p->pVtab ); |
768 | 807 if( p->pVtab->nRef>0 ){ |
| 808 return SQLITE_LOCKED; |
| 809 } |
| 810 } |
| 811 p = vtabDisconnectAll(db, pTab); |
| 812 xDestroy = p->pMod->pModule->xDestroy; |
| 813 assert( xDestroy!=0 ); /* Checked before the virtual table is created */ |
| 814 rc = xDestroy(p->pVtab); |
769 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ | 815 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ |
770 if( rc==SQLITE_OK ){ | 816 if( rc==SQLITE_OK ){ |
771 assert( pTab->pVTable==p && p->pNext==0 ); | 817 assert( pTab->pVTable==p && p->pNext==0 ); |
772 p->pVtab = 0; | 818 p->pVtab = 0; |
773 pTab->pVTable = 0; | 819 pTab->pVTable = 0; |
774 sqlite3VtabUnlock(p); | 820 sqlite3VtabUnlock(p); |
775 } | 821 } |
776 } | 822 } |
777 | 823 |
778 return rc; | 824 return rc; |
779 } | 825 } |
780 | 826 |
781 /* | 827 /* |
782 ** This function invokes either the xRollback or xCommit method | 828 ** This function invokes either the xRollback or xCommit method |
783 ** of each of the virtual tables in the sqlite3.aVTrans array. The method | 829 ** of each of the virtual tables in the sqlite3.aVTrans array. The method |
784 ** called is identified by the second argument, "offset", which is | 830 ** called is identified by the second argument, "offset", which is |
785 ** the offset of the method to call in the sqlite3_module structure. | 831 ** the offset of the method to call in the sqlite3_module structure. |
786 ** | 832 ** |
787 ** The array is cleared after invoking the callbacks. | 833 ** The array is cleared after invoking the callbacks. |
788 */ | 834 */ |
789 static void callFinaliser(sqlite3 *db, int offset){ | 835 static void callFinaliser(sqlite3 *db, int offset){ |
790 int i; | 836 int i; |
791 if( db->aVTrans ){ | 837 if( db->aVTrans ){ |
| 838 VTable **aVTrans = db->aVTrans; |
| 839 db->aVTrans = 0; |
792 for(i=0; i<db->nVTrans; i++){ | 840 for(i=0; i<db->nVTrans; i++){ |
793 VTable *pVTab = db->aVTrans[i]; | 841 VTable *pVTab = aVTrans[i]; |
794 sqlite3_vtab *p = pVTab->pVtab; | 842 sqlite3_vtab *p = pVTab->pVtab; |
795 if( p ){ | 843 if( p ){ |
796 int (*x)(sqlite3_vtab *); | 844 int (*x)(sqlite3_vtab *); |
797 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); | 845 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); |
798 if( x ) x(p); | 846 if( x ) x(p); |
799 } | 847 } |
800 pVTab->iSavepoint = 0; | 848 pVTab->iSavepoint = 0; |
801 sqlite3VtabUnlock(pVTab); | 849 sqlite3VtabUnlock(pVTab); |
802 } | 850 } |
803 sqlite3DbFree(db, db->aVTrans); | 851 sqlite3DbFree(db, aVTrans); |
804 db->nVTrans = 0; | 852 db->nVTrans = 0; |
805 db->aVTrans = 0; | |
806 } | 853 } |
807 } | 854 } |
808 | 855 |
809 /* | 856 /* |
810 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans | 857 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans |
811 ** array. Return the error code for the first error that occurs, or | 858 ** array. Return the error code for the first error that occurs, or |
812 ** SQLITE_OK if all xSync operations are successful. | 859 ** SQLITE_OK if all xSync operations are successful. |
813 ** | 860 ** |
814 ** If an error message is available, leave it in p->zErrMsg. | 861 ** If an error message is available, leave it in p->zErrMsg. |
815 */ | 862 */ |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
883 return SQLITE_OK; | 930 return SQLITE_OK; |
884 } | 931 } |
885 } | 932 } |
886 | 933 |
887 /* Invoke the xBegin method. If successful, add the vtab to the | 934 /* Invoke the xBegin method. If successful, add the vtab to the |
888 ** sqlite3.aVTrans[] array. */ | 935 ** sqlite3.aVTrans[] array. */ |
889 rc = growVTrans(db); | 936 rc = growVTrans(db); |
890 if( rc==SQLITE_OK ){ | 937 if( rc==SQLITE_OK ){ |
891 rc = pModule->xBegin(pVTab->pVtab); | 938 rc = pModule->xBegin(pVTab->pVtab); |
892 if( rc==SQLITE_OK ){ | 939 if( rc==SQLITE_OK ){ |
| 940 int iSvpt = db->nStatement + db->nSavepoint; |
893 addToVTrans(db, pVTab); | 941 addToVTrans(db, pVTab); |
| 942 if( iSvpt ) rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, iSvpt-1); |
894 } | 943 } |
895 } | 944 } |
896 } | 945 } |
897 return rc; | 946 return rc; |
898 } | 947 } |
899 | 948 |
900 /* | 949 /* |
901 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all | 950 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all |
902 ** virtual tables that currently have an open transaction. Pass iSavepoint | 951 ** virtual tables that currently have an open transaction. Pass iSavepoint |
903 ** as the second argument to the virtual table method invoked. | 952 ** as the second argument to the virtual table method invoked. |
904 ** | 953 ** |
905 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is | 954 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is |
906 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is | 955 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is |
907 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with | 956 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with |
908 ** an open transaction is invoked. | 957 ** an open transaction is invoked. |
909 ** | 958 ** |
910 ** If any virtual table method returns an error code other than SQLITE_OK, | 959 ** If any virtual table method returns an error code other than SQLITE_OK, |
911 ** processing is abandoned and the error returned to the caller of this | 960 ** processing is abandoned and the error returned to the caller of this |
912 ** function immediately. If all calls to virtual table methods are successful, | 961 ** function immediately. If all calls to virtual table methods are successful, |
913 ** SQLITE_OK is returned. | 962 ** SQLITE_OK is returned. |
914 */ | 963 */ |
915 int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ | 964 int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ |
916 int rc = SQLITE_OK; | 965 int rc = SQLITE_OK; |
917 | 966 |
918 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); | 967 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); |
919 assert( iSavepoint>=0 ); | 968 assert( iSavepoint>=-1 ); |
920 if( db->aVTrans ){ | 969 if( db->aVTrans ){ |
921 int i; | 970 int i; |
922 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ | 971 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
923 VTable *pVTab = db->aVTrans[i]; | 972 VTable *pVTab = db->aVTrans[i]; |
924 const sqlite3_module *pMod = pVTab->pMod->pModule; | 973 const sqlite3_module *pMod = pVTab->pMod->pModule; |
925 if( pVTab->pVtab && pMod->iVersion>=2 ){ | 974 if( pVTab->pVtab && pMod->iVersion>=2 ){ |
926 int (*xMethod)(sqlite3_vtab *, int); | 975 int (*xMethod)(sqlite3_vtab *, int); |
927 switch( op ){ | 976 switch( op ){ |
928 case SAVEPOINT_BEGIN: | 977 case SAVEPOINT_BEGIN: |
929 xMethod = pMod->xSavepoint; | 978 xMethod = pMod->xSavepoint; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1027 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ | 1076 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ |
1028 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 1077 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
1029 int i, n; | 1078 int i, n; |
1030 Table **apVtabLock; | 1079 Table **apVtabLock; |
1031 | 1080 |
1032 assert( IsVirtual(pTab) ); | 1081 assert( IsVirtual(pTab) ); |
1033 for(i=0; i<pToplevel->nVtabLock; i++){ | 1082 for(i=0; i<pToplevel->nVtabLock; i++){ |
1034 if( pTab==pToplevel->apVtabLock[i] ) return; | 1083 if( pTab==pToplevel->apVtabLock[i] ) return; |
1035 } | 1084 } |
1036 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); | 1085 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); |
1037 apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n); | 1086 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n); |
1038 if( apVtabLock ){ | 1087 if( apVtabLock ){ |
1039 pToplevel->apVtabLock = apVtabLock; | 1088 pToplevel->apVtabLock = apVtabLock; |
1040 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; | 1089 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; |
1041 }else{ | 1090 }else{ |
1042 pToplevel->db->mallocFailed = 1; | 1091 pToplevel->db->mallocFailed = 1; |
1043 } | 1092 } |
1044 } | 1093 } |
1045 | 1094 |
1046 /* | 1095 /* |
| 1096 ** Check to see if virtual tale module pMod can be have an eponymous |
| 1097 ** virtual table instance. If it can, create one if one does not already |
| 1098 ** exist. Return non-zero if the eponymous virtual table instance exists |
| 1099 ** when this routine returns, and return zero if it does not exist. |
| 1100 ** |
| 1101 ** An eponymous virtual table instance is one that is named after its |
| 1102 ** module, and more importantly, does not require a CREATE VIRTUAL TABLE |
| 1103 ** statement in order to come into existance. Eponymous virtual table |
| 1104 ** instances always exist. They cannot be DROP-ed. |
| 1105 ** |
| 1106 ** Any virtual table module for which xConnect and xCreate are the same |
| 1107 ** method can have an eponymous virtual table instance. |
| 1108 */ |
| 1109 int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){ |
| 1110 const sqlite3_module *pModule = pMod->pModule; |
| 1111 Table *pTab; |
| 1112 char *zErr = 0; |
| 1113 int nName; |
| 1114 int rc; |
| 1115 sqlite3 *db = pParse->db; |
| 1116 if( pMod->pEpoTab ) return 1; |
| 1117 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0; |
| 1118 nName = sqlite3Strlen30(pMod->zName) + 1; |
| 1119 pTab = sqlite3DbMallocZero(db, sizeof(Table) + nName); |
| 1120 if( pTab==0 ) return 0; |
| 1121 pMod->pEpoTab = pTab; |
| 1122 pTab->zName = (char*)&pTab[1]; |
| 1123 memcpy(pTab->zName, pMod->zName, nName); |
| 1124 pTab->nRef = 1; |
| 1125 pTab->pSchema = db->aDb[0].pSchema; |
| 1126 pTab->tabFlags |= TF_Virtual; |
| 1127 pTab->nModuleArg = 0; |
| 1128 pTab->iPKey = -1; |
| 1129 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName)); |
| 1130 addModuleArgument(db, pTab, 0); |
| 1131 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName)); |
| 1132 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr); |
| 1133 if( rc ){ |
| 1134 sqlite3ErrorMsg(pParse, "%s", zErr); |
| 1135 sqlite3DbFree(db, zErr); |
| 1136 sqlite3VtabEponymousTableClear(db, pMod); |
| 1137 return 0; |
| 1138 } |
| 1139 return 1; |
| 1140 } |
| 1141 |
| 1142 /* |
| 1143 ** Erase the eponymous virtual table instance associated with |
| 1144 ** virtual table module pMod, if it exists. |
| 1145 */ |
| 1146 void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){ |
| 1147 Table *pTab = pMod->pEpoTab; |
| 1148 if( pTab!=0 ){ |
| 1149 sqlite3DeleteColumnNames(db, pTab); |
| 1150 sqlite3VtabClear(db, pTab); |
| 1151 sqlite3DbFree(db, pTab); |
| 1152 pMod->pEpoTab = 0; |
| 1153 } |
| 1154 } |
| 1155 |
| 1156 /* |
1047 ** Return the ON CONFLICT resolution mode in effect for the virtual | 1157 ** Return the ON CONFLICT resolution mode in effect for the virtual |
1048 ** table update operation currently in progress. | 1158 ** table update operation currently in progress. |
1049 ** | 1159 ** |
1050 ** The results of this routine are undefined unless it is called from | 1160 ** The results of this routine are undefined unless it is called from |
1051 ** within an xUpdate method. | 1161 ** within an xUpdate method. |
1052 */ | 1162 */ |
1053 int sqlite3_vtab_on_conflict(sqlite3 *db){ | 1163 int sqlite3_vtab_on_conflict(sqlite3 *db){ |
1054 static const unsigned char aMap[] = { | 1164 static const unsigned char aMap[] = { |
1055 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE | 1165 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
1056 }; | 1166 }; |
| 1167 #ifdef SQLITE_ENABLE_API_ARMOR |
| 1168 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 1169 #endif |
1057 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); | 1170 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
1058 assert( OE_Ignore==4 && OE_Replace==5 ); | 1171 assert( OE_Ignore==4 && OE_Replace==5 ); |
1059 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); | 1172 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
1060 return (int)aMap[db->vtabOnConflict-1]; | 1173 return (int)aMap[db->vtabOnConflict-1]; |
1061 } | 1174 } |
1062 | 1175 |
1063 /* | 1176 /* |
1064 ** Call from within the xCreate() or xConnect() methods to provide | 1177 ** Call from within the xCreate() or xConnect() methods to provide |
1065 ** the SQLite core with additional information about the behavior | 1178 ** the SQLite core with additional information about the behavior |
1066 ** of the virtual table being implemented. | 1179 ** of the virtual table being implemented. |
1067 */ | 1180 */ |
1068 int sqlite3_vtab_config(sqlite3 *db, int op, ...){ | 1181 int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
1069 va_list ap; | 1182 va_list ap; |
1070 int rc = SQLITE_OK; | 1183 int rc = SQLITE_OK; |
1071 | 1184 |
| 1185 #ifdef SQLITE_ENABLE_API_ARMOR |
| 1186 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 1187 #endif |
1072 sqlite3_mutex_enter(db->mutex); | 1188 sqlite3_mutex_enter(db->mutex); |
1073 | |
1074 va_start(ap, op); | 1189 va_start(ap, op); |
1075 switch( op ){ | 1190 switch( op ){ |
1076 case SQLITE_VTAB_CONSTRAINT_SUPPORT: { | 1191 case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
1077 VtabCtx *p = db->pVtabCtx; | 1192 VtabCtx *p = db->pVtabCtx; |
1078 if( !p ){ | 1193 if( !p ){ |
1079 rc = SQLITE_MISUSE_BKPT; | 1194 rc = SQLITE_MISUSE_BKPT; |
1080 }else{ | 1195 }else{ |
1081 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 ); | 1196 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 ); |
1082 p->pVTable->bConstraint = (u8)va_arg(ap, int); | 1197 p->pVTable->bConstraint = (u8)va_arg(ap, int); |
1083 } | 1198 } |
1084 break; | 1199 break; |
1085 } | 1200 } |
1086 default: | 1201 default: |
1087 rc = SQLITE_MISUSE_BKPT; | 1202 rc = SQLITE_MISUSE_BKPT; |
1088 break; | 1203 break; |
1089 } | 1204 } |
1090 va_end(ap); | 1205 va_end(ap); |
1091 | 1206 |
1092 if( rc!=SQLITE_OK ) sqlite3Error(db, rc); | 1207 if( rc!=SQLITE_OK ) sqlite3Error(db, rc); |
1093 sqlite3_mutex_leave(db->mutex); | 1208 sqlite3_mutex_leave(db->mutex); |
1094 return rc; | 1209 return rc; |
1095 } | 1210 } |
1096 | 1211 |
1097 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 1212 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
OLD | NEW |