drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1 | /* |
| 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. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 13 | */ |
| 14 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 15 | #include "sqliteInt.h" |
| 16 | |
drh | 6384241 | 2009-04-11 16:27:19 +0000 | [diff] [blame] | 17 | /* |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 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 |
| 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 |
| 22 | ** are invoked only from within xCreate and xConnect methods. |
| 23 | */ |
| 24 | struct VtabCtx { |
| 25 | Table *pTab; |
| 26 | VTable *pVTable; |
| 27 | }; |
| 28 | |
| 29 | /* |
drh | 6384241 | 2009-04-11 16:27:19 +0000 | [diff] [blame] | 30 | ** The actual function that does the work of creating a new module. |
| 31 | ** This function implements the sqlite3_create_module() and |
| 32 | ** sqlite3_create_module_v2() interfaces. |
| 33 | */ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 34 | static int createModule( |
| 35 | sqlite3 *db, /* Database in which module is registered */ |
| 36 | const char *zName, /* Name assigned to this module */ |
| 37 | const sqlite3_module *pModule, /* The definition of the module */ |
| 38 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 39 | void (*xDestroy)(void *) /* Module destructor function */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 40 | ){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 41 | int rc, nName; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 42 | Module *pMod; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 43 | |
| 44 | sqlite3_mutex_enter(db->mutex); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 45 | nName = sqlite3Strlen30(zName); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 46 | pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 47 | if( pMod ){ |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 48 | Module *pDel; |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 49 | char *zCopy = (char *)(&pMod[1]); |
| 50 | memcpy(zCopy, zName, nName+1); |
| 51 | pMod->zName = zCopy; |
| 52 | pMod->pModule = pModule; |
| 53 | pMod->pAux = pAux; |
| 54 | pMod->xDestroy = xDestroy; |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 55 | pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod); |
| 56 | if( pDel && pDel->xDestroy ){ |
drh | 7af72cf | 2011-05-05 13:54:28 +0000 | [diff] [blame] | 57 | sqlite3ResetInternalSchema(db, -1); |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 58 | pDel->xDestroy(pDel->pAux); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 59 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 60 | sqlite3DbFree(db, pDel); |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 61 | if( pDel==pMod ){ |
| 62 | db->mallocFailed = 1; |
| 63 | } |
danielk1977 | 777da08 | 2008-11-13 19:12:34 +0000 | [diff] [blame] | 64 | }else if( xDestroy ){ |
| 65 | xDestroy(pAux); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 66 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 67 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 68 | sqlite3_mutex_leave(db->mutex); |
| 69 | return rc; |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 73 | /* |
| 74 | ** External API function used to create a new virtual-table module. |
| 75 | */ |
| 76 | int sqlite3_create_module( |
| 77 | sqlite3 *db, /* Database in which module is registered */ |
| 78 | const char *zName, /* Name assigned to this module */ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 79 | const sqlite3_module *pModule, /* The definition of the module */ |
| 80 | void *pAux /* Context pointer for xCreate/xConnect */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 81 | ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 82 | return createModule(db, zName, pModule, pAux, 0); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | ** External API function used to create a new virtual-table module. |
| 87 | */ |
| 88 | int sqlite3_create_module_v2( |
| 89 | sqlite3 *db, /* Database in which module is registered */ |
| 90 | const char *zName, /* Name assigned to this module */ |
| 91 | const sqlite3_module *pModule, /* The definition of the module */ |
| 92 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 93 | void (*xDestroy)(void *) /* Module destructor function */ |
| 94 | ){ |
| 95 | return createModule(db, zName, pModule, pAux, xDestroy); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 96 | } |
| 97 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 98 | /* |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 99 | ** Lock the virtual table so that it cannot be disconnected. |
| 100 | ** Locks nest. Every lock should have a corresponding unlock. |
| 101 | ** If an unlock is omitted, resources leaks will occur. |
| 102 | ** |
| 103 | ** If a disconnect is attempted while a virtual table is locked, |
| 104 | ** the disconnect is deferred until all locks have been removed. |
| 105 | */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 106 | void sqlite3VtabLock(VTable *pVTab){ |
| 107 | pVTab->nRef++; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* |
| 112 | ** pTab is a pointer to a Table structure representing a virtual-table. |
| 113 | ** Return a pointer to the VTable object used by connection db to access |
| 114 | ** this virtual-table, if one has been created, or NULL otherwise. |
| 115 | */ |
| 116 | VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){ |
| 117 | VTable *pVtab; |
| 118 | assert( IsVirtual(pTab) ); |
| 119 | for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext); |
| 120 | return pVtab; |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 124 | ** Decrement the ref-count on a virtual table object. When the ref-count |
| 125 | ** reaches zero, call the xDisconnect() method to delete the object. |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 126 | */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 127 | void sqlite3VtabUnlock(VTable *pVTab){ |
| 128 | sqlite3 *db = pVTab->db; |
| 129 | |
| 130 | assert( db ); |
| 131 | assert( pVTab->nRef>0 ); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 132 | assert( sqlite3SafetyCheckOk(db) ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 133 | |
| 134 | pVTab->nRef--; |
| 135 | if( pVTab->nRef==0 ){ |
| 136 | sqlite3_vtab *p = pVTab->pVtab; |
| 137 | if( p ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 138 | p->pModule->xDisconnect(p); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 139 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 140 | sqlite3DbFree(db, pVTab); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | ** Table p is a virtual table. This function moves all elements in the |
| 146 | ** p->pVTable list to the sqlite3.pDisconnect lists of their associated |
| 147 | ** database connections to be disconnected at the next opportunity. |
| 148 | ** Except, if argument db is not NULL, then the entry associated with |
| 149 | ** connection db is left in the p->pVTable list. |
| 150 | */ |
| 151 | static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){ |
| 152 | VTable *pRet = 0; |
| 153 | VTable *pVTable = p->pVTable; |
| 154 | p->pVTable = 0; |
| 155 | |
| 156 | /* Assert that the mutex (if any) associated with the BtShared database |
| 157 | ** that contains table p is held by the caller. See header comments |
| 158 | ** above function sqlite3VtabUnlockList() for an explanation of why |
| 159 | ** this makes it safe to access the sqlite3.pDisconnect list of any |
drh | 9bbc2e2 | 2011-04-04 20:40:22 +0000 | [diff] [blame] | 160 | ** database connection that may have an entry in the p->pVTable list. |
| 161 | */ |
| 162 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 163 | |
| 164 | while( pVTable ){ |
| 165 | sqlite3 *db2 = pVTable->db; |
| 166 | VTable *pNext = pVTable->pNext; |
| 167 | assert( db2 ); |
| 168 | if( db2==db ){ |
| 169 | pRet = pVTable; |
| 170 | p->pVTable = pRet; |
| 171 | pRet->pNext = 0; |
| 172 | }else{ |
| 173 | pVTable->pNext = db2->pDisconnect; |
| 174 | db2->pDisconnect = pVTable; |
| 175 | } |
| 176 | pVTable = pNext; |
| 177 | } |
| 178 | |
| 179 | assert( !db || pRet ); |
| 180 | return pRet; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* |
| 185 | ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. |
| 186 | ** |
| 187 | ** This function may only be called when the mutexes associated with all |
| 188 | ** shared b-tree databases opened using connection db are held by the |
| 189 | ** caller. This is done to protect the sqlite3.pDisconnect list. The |
| 190 | ** sqlite3.pDisconnect list is accessed only as follows: |
| 191 | ** |
| 192 | ** 1) By this function. In this case, all BtShared mutexes and the mutex |
| 193 | ** associated with the database handle itself must be held. |
| 194 | ** |
| 195 | ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to |
| 196 | ** the sqlite3.pDisconnect list. In this case either the BtShared mutex |
| 197 | ** associated with the database the virtual table is stored in is held |
| 198 | ** or, if the virtual table is stored in a non-sharable database, then |
| 199 | ** the database handle mutex is held. |
| 200 | ** |
| 201 | ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously |
| 202 | ** by multiple threads. It is thread-safe. |
| 203 | */ |
| 204 | void sqlite3VtabUnlockList(sqlite3 *db){ |
| 205 | VTable *p = db->pDisconnect; |
| 206 | db->pDisconnect = 0; |
| 207 | |
| 208 | assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 209 | assert( sqlite3_mutex_held(db->mutex) ); |
| 210 | |
| 211 | if( p ){ |
| 212 | sqlite3ExpirePreparedStatements(db); |
| 213 | do { |
| 214 | VTable *pNext = p->pNext; |
| 215 | sqlite3VtabUnlock(p); |
| 216 | p = pNext; |
| 217 | }while( p ); |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 222 | ** Clear any and all virtual-table information from the Table record. |
| 223 | ** This routine is called, for example, just before deleting the Table |
| 224 | ** record. |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 225 | ** |
| 226 | ** Since it is a virtual-table, the Table structure contains a pointer |
| 227 | ** to the head of a linked list of VTable structures. Each VTable |
| 228 | ** structure is associated with a single sqlite3* user of the schema. |
| 229 | ** The reference count of the VTable structure associated with database |
| 230 | ** connection db is decremented immediately (which may lead to the |
| 231 | ** structure being xDisconnected and free). Any other VTable structures |
| 232 | ** in the list are moved to the sqlite3.pDisconnect list of the associated |
| 233 | ** database connection. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 234 | */ |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 235 | void sqlite3VtabClear(sqlite3 *db, Table *p){ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 236 | if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 237 | if( p->azModuleArg ){ |
| 238 | int i; |
| 239 | for(i=0; i<p->nModuleArg; i++){ |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 240 | sqlite3DbFree(db, p->azModuleArg[i]); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 241 | } |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 242 | sqlite3DbFree(db, p->azModuleArg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | ** Add a new module argument to pTable->azModuleArg[]. |
| 248 | ** The string is not copied - the pointer is stored. The |
| 249 | ** string will be freed automatically when the table is |
| 250 | ** deleted. |
| 251 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 252 | static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 253 | int i = pTable->nModuleArg++; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 254 | int nBytes = sizeof(char *)*(1+pTable->nModuleArg); |
| 255 | char **azModuleArg; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 256 | azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 257 | if( azModuleArg==0 ){ |
| 258 | int j; |
| 259 | for(j=0; j<i; j++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 260 | sqlite3DbFree(db, pTable->azModuleArg[j]); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 261 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 262 | sqlite3DbFree(db, zArg); |
| 263 | sqlite3DbFree(db, pTable->azModuleArg); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 264 | pTable->nModuleArg = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 265 | }else{ |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 266 | azModuleArg[i] = zArg; |
| 267 | azModuleArg[i+1] = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 268 | } |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 269 | pTable->azModuleArg = azModuleArg; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* |
| 273 | ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE |
| 274 | ** statement. The module name has been parsed, but the optional list |
| 275 | ** of parameters that follow the module name are still pending. |
| 276 | */ |
| 277 | void sqlite3VtabBeginParse( |
| 278 | Parse *pParse, /* Parsing context */ |
| 279 | Token *pName1, /* Name of new table, or database name */ |
| 280 | Token *pName2, /* Name of new table or NULL */ |
drh | b421b89 | 2012-01-28 19:41:53 +0000 | [diff] [blame] | 281 | Token *pModuleName, /* Name of the module for the virtual table */ |
| 282 | int ifNotExists /* No error if the table already exists */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 283 | ){ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 284 | int iDb; /* The database the table is being created in */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 285 | Table *pTable; /* The new virtual table */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 286 | sqlite3 *db; /* Database connection */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 287 | |
drh | b421b89 | 2012-01-28 19:41:53 +0000 | [diff] [blame] | 288 | sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 289 | pTable = pParse->pNewTable; |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 290 | if( pTable==0 ) return; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 291 | assert( 0==pTable->pIndex ); |
| 292 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 293 | db = pParse->db; |
| 294 | iDb = sqlite3SchemaToIndex(db, pTable->pSchema); |
danielk1977 | 70ba164 | 2006-06-21 16:02:42 +0000 | [diff] [blame] | 295 | assert( iDb>=0 ); |
| 296 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 297 | pTable->tabFlags |= TF_Virtual; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 298 | pTable->nModuleArg = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 299 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 300 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); |
| 301 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 302 | pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 303 | |
| 304 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 305 | /* Creating a virtual table invokes the authorization callback twice. |
| 306 | ** The first invocation, to obtain permission to INSERT a row into the |
| 307 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| 308 | ** The second call, to obtain permission to create the table, is made now. |
| 309 | */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 310 | if( pTable->azModuleArg ){ |
| 311 | sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, |
| 312 | pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 313 | } |
| 314 | #endif |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
| 318 | ** This routine takes the module argument that has been accumulating |
| 319 | ** in pParse->zArg[] and appends it to the list of arguments on the |
| 320 | ** virtual table currently under construction in pParse->pTable. |
| 321 | */ |
| 322 | static void addArgumentToVtab(Parse *pParse){ |
drh | b421b89 | 2012-01-28 19:41:53 +0000 | [diff] [blame] | 323 | if( pParse->sArg.z && pParse->pNewTable ){ |
drh | 7c2d87c | 2006-09-02 14:16:59 +0000 | [diff] [blame] | 324 | const char *z = (const char*)pParse->sArg.z; |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 325 | int n = pParse->sArg.n; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 326 | sqlite3 *db = pParse->db; |
| 327 | addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 328 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | ** The parser calls this routine after the CREATE VIRTUAL TABLE statement |
| 333 | ** has been completely parsed. |
| 334 | */ |
| 335 | void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 336 | Table *pTab = pParse->pNewTable; /* The table being constructed */ |
| 337 | sqlite3 *db = pParse->db; /* The database connection */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 338 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 339 | if( pTab==0 ) return; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 340 | addArgumentToVtab(pParse); |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 341 | pParse->sArg.z = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 342 | if( pTab->nModuleArg<1 ) return; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 343 | |
| 344 | /* If the CREATE VIRTUAL TABLE statement is being entered for the |
| 345 | ** first time (in other words if the virtual table is actually being |
| 346 | ** created now instead of just being read out of sqlite_master) then |
| 347 | ** do additional initialization work and store the statement text |
| 348 | ** in the sqlite_master table. |
| 349 | */ |
| 350 | if( !db->init.busy ){ |
| 351 | char *zStmt; |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 352 | char *zWhere; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 353 | int iDb; |
| 354 | Vdbe *v; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 355 | |
| 356 | /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ |
| 357 | if( pEnd ){ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 358 | pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 359 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 360 | zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 361 | |
| 362 | /* A slot for the record has already been allocated in the |
| 363 | ** SQLITE_MASTER table. We just need to update that slot with all |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 364 | ** the information we've collected. |
| 365 | ** |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 366 | ** The VM register number pParse->regRowid holds the rowid of an |
| 367 | ** entry in the sqlite_master table tht was created for this vtab |
| 368 | ** by sqlite3StartTable(). |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 369 | */ |
| 370 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 371 | sqlite3NestedParse(pParse, |
| 372 | "UPDATE %Q.%s " |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 373 | "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 374 | "WHERE rowid=#%d", |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 375 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 376 | pTab->zName, |
| 377 | pTab->zName, |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 378 | zStmt, |
| 379 | pParse->regRowid |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 380 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 381 | sqlite3DbFree(db, zStmt); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 382 | v = sqlite3GetVdbe(pParse); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 383 | sqlite3ChangeCookie(pParse, iDb); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 384 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 385 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
dan | 39f1bcb | 2010-09-29 07:16:46 +0000 | [diff] [blame] | 386 | zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 387 | sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 388 | sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 389 | pTab->zName, sqlite3Strlen30(pTab->zName) + 1); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 390 | } |
| 391 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 392 | /* If we are rereading the sqlite_master table create the in-memory |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 393 | ** record of the table. The xConnect() method is not called until |
| 394 | ** the first time the virtual table is used in an SQL statement. This |
| 395 | ** allows a schema that contains virtual tables to be loaded before |
| 396 | ** the required virtual table implementations are registered. */ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 397 | else { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 398 | Table *pOld; |
| 399 | Schema *pSchema = pTab->pSchema; |
| 400 | const char *zName = pTab->zName; |
drh | a83ccca | 2009-04-28 13:01:09 +0000 | [diff] [blame] | 401 | int nName = sqlite3Strlen30(zName); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 402 | assert( sqlite3SchemaMutexHeld(db, 0, pSchema) ); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 403 | pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); |
| 404 | if( pOld ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 405 | db->mallocFailed = 1; |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 406 | assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 407 | return; |
| 408 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 409 | pParse->pNewTable = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | ** The parser calls this routine when it sees the first token |
| 415 | ** of an argument to the module name in a CREATE VIRTUAL TABLE statement. |
| 416 | */ |
| 417 | void sqlite3VtabArgInit(Parse *pParse){ |
| 418 | addArgumentToVtab(pParse); |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 419 | pParse->sArg.z = 0; |
| 420 | pParse->sArg.n = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* |
| 424 | ** The parser calls this routine for each token after the first token |
| 425 | ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. |
| 426 | */ |
| 427 | void sqlite3VtabArgExtend(Parse *pParse, Token *p){ |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 428 | Token *pArg = &pParse->sArg; |
| 429 | if( pArg->z==0 ){ |
| 430 | pArg->z = p->z; |
| 431 | pArg->n = p->n; |
| 432 | }else{ |
| 433 | assert(pArg->z < p->z); |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 434 | pArg->n = (int)(&p->z[p->n] - pArg->z); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 435 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 436 | } |
| 437 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 438 | /* |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 439 | ** Invoke a virtual table constructor (either xCreate or xConnect). The |
| 440 | ** pointer to the function to invoke is passed as the fourth parameter |
| 441 | ** to this procedure. |
| 442 | */ |
| 443 | static int vtabCallConstructor( |
| 444 | sqlite3 *db, |
| 445 | Table *pTab, |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 446 | Module *pMod, |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 447 | int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 448 | char **pzErr |
| 449 | ){ |
drh | 9250581 | 2012-04-26 22:47:20 +0000 | [diff] [blame] | 450 | VtabCtx sCtx, *pPriorCtx; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 451 | VTable *pVTable; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 452 | int rc; |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 453 | const char *const*azArg = (const char *const*)pTab->azModuleArg; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 454 | int nArg = pTab->nModuleArg; |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 455 | char *zErr = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 456 | char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 457 | |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 458 | if( !zModuleName ){ |
| 459 | return SQLITE_NOMEM; |
| 460 | } |
| 461 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 462 | pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); |
| 463 | if( !pVTable ){ |
| 464 | sqlite3DbFree(db, zModuleName); |
| 465 | return SQLITE_NOMEM; |
| 466 | } |
| 467 | pVTable->db = db; |
| 468 | pVTable->pMod = pMod; |
| 469 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 470 | /* Invoke the virtual table constructor */ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 471 | assert( &db->pVtabCtx ); |
| 472 | assert( xConstruct ); |
| 473 | sCtx.pTab = pTab; |
| 474 | sCtx.pVTable = pVTable; |
drh | 9250581 | 2012-04-26 22:47:20 +0000 | [diff] [blame] | 475 | pPriorCtx = db->pVtabCtx; |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 476 | db->pVtabCtx = &sCtx; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 477 | rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); |
drh | 9250581 | 2012-04-26 22:47:20 +0000 | [diff] [blame] | 478 | db->pVtabCtx = pPriorCtx; |
drh | 55f04f2 | 2009-05-11 23:37:59 +0000 | [diff] [blame] | 479 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 480 | |
| 481 | if( SQLITE_OK!=rc ){ |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 482 | if( zErr==0 ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 483 | *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 484 | }else { |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 485 | *pzErr = sqlite3MPrintf(db, "%s", zErr); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 486 | sqlite3_free(zErr); |
drh | fe1368e | 2006-09-10 17:08:29 +0000 | [diff] [blame] | 487 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 488 | sqlite3DbFree(db, pVTable); |
| 489 | }else if( ALWAYS(pVTable->pVtab) ){ |
| 490 | /* Justification of ALWAYS(): A correct vtab constructor must allocate |
| 491 | ** the sqlite3_vtab object if successful. */ |
| 492 | pVTable->pVtab->pModule = pMod->pModule; |
| 493 | pVTable->nRef = 1; |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 494 | if( sCtx.pTab ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 495 | const char *zFormat = "vtable constructor did not declare schema: %s"; |
| 496 | *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); |
| 497 | sqlite3VtabUnlock(pVTable); |
| 498 | rc = SQLITE_ERROR; |
| 499 | }else{ |
| 500 | int iCol; |
| 501 | /* If everything went according to plan, link the new VTable structure |
| 502 | ** into the linked list headed by pTab->pVTable. Then loop through the |
| 503 | ** columns of the table to see if any of them contain the token "hidden". |
| 504 | ** If so, set the Column.isHidden flag and remove the token from |
| 505 | ** the type string. */ |
| 506 | pVTable->pNext = pTab->pVTable; |
| 507 | pTab->pVTable = pVTable; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 508 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 509 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 510 | char *zType = pTab->aCol[iCol].zType; |
| 511 | int nType; |
| 512 | int i = 0; |
| 513 | if( !zType ) continue; |
| 514 | nType = sqlite3Strlen30(zType); |
| 515 | if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){ |
| 516 | for(i=0; i<nType; i++){ |
| 517 | if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) |
| 518 | && (zType[i+7]=='\0' || zType[i+7]==' ') |
| 519 | ){ |
| 520 | i++; |
| 521 | break; |
| 522 | } |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 525 | if( i<nType ){ |
| 526 | int j; |
| 527 | int nDel = 6 + (zType[i+6] ? 1 : 0); |
| 528 | for(j=i; (j+nDel)<=nType; j++){ |
| 529 | zType[j] = zType[j+nDel]; |
| 530 | } |
| 531 | if( zType[i]=='\0' && i>0 ){ |
| 532 | assert(zType[i-1]==' '); |
| 533 | zType[i-1] = '\0'; |
| 534 | } |
| 535 | pTab->aCol[iCol].isHidden = 1; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 536 | } |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 540 | |
| 541 | sqlite3DbFree(db, zModuleName); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 542 | return rc; |
| 543 | } |
| 544 | |
| 545 | /* |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 546 | ** This function is invoked by the parser to call the xConnect() method |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 547 | ** of the virtual table pTab. If an error occurs, an error code is returned |
| 548 | ** and an error left in pParse. |
| 549 | ** |
| 550 | ** This call is a no-op if table pTab is not a virtual table. |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 551 | */ |
| 552 | int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 553 | sqlite3 *db = pParse->db; |
| 554 | const char *zMod; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 555 | Module *pMod; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 556 | int rc; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 557 | |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 558 | assert( pTab ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 559 | if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 560 | return SQLITE_OK; |
| 561 | } |
| 562 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 563 | /* Locate the required virtual table module */ |
| 564 | zMod = pTab->azModuleArg[0]; |
| 565 | pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod)); |
| 566 | |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 567 | if( !pMod ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 568 | const char *zModule = pTab->azModuleArg[0]; |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 569 | sqlite3ErrorMsg(pParse, "no such module: %s", zModule); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 570 | rc = SQLITE_ERROR; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 571 | }else{ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 572 | char *zErr = 0; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 573 | rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 574 | if( rc!=SQLITE_OK ){ |
| 575 | sqlite3ErrorMsg(pParse, "%s", zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 576 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 577 | sqlite3DbFree(db, zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | return rc; |
| 581 | } |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 582 | /* |
dan | 2cac207 | 2011-05-25 18:46:22 +0000 | [diff] [blame] | 583 | ** Grow the db->aVTrans[] array so that there is room for at least one |
| 584 | ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise. |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 585 | */ |
dan | 2cac207 | 2011-05-25 18:46:22 +0000 | [diff] [blame] | 586 | static int growVTrans(sqlite3 *db){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 587 | const int ARRAY_INCR = 5; |
| 588 | |
| 589 | /* Grow the sqlite3.aVTrans array if required */ |
| 590 | if( (db->nVTrans%ARRAY_INCR)==0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 591 | VTable **aVTrans; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 592 | int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 593 | aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 594 | if( !aVTrans ){ |
| 595 | return SQLITE_NOMEM; |
| 596 | } |
| 597 | memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); |
| 598 | db->aVTrans = aVTrans; |
| 599 | } |
| 600 | |
dan | 2cac207 | 2011-05-25 18:46:22 +0000 | [diff] [blame] | 601 | return SQLITE_OK; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should |
| 606 | ** have already been reserved using growVTrans(). |
| 607 | */ |
| 608 | static void addToVTrans(sqlite3 *db, VTable *pVTab){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 609 | /* Add pVtab to the end of sqlite3.aVTrans */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 610 | db->aVTrans[db->nVTrans++] = pVTab; |
| 611 | sqlite3VtabLock(pVTab); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 615 | ** This function is invoked by the vdbe to call the xCreate method |
| 616 | ** of the virtual table named zTab in database iDb. |
| 617 | ** |
| 618 | ** If an error occurs, *pzErr is set to point an an English language |
| 619 | ** description of the error and an SQLITE_XXX error code is returned. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 620 | ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 621 | */ |
| 622 | int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ |
| 623 | int rc = SQLITE_OK; |
| 624 | Table *pTab; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 625 | Module *pMod; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 626 | const char *zMod; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 627 | |
| 628 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 629 | assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); |
| 630 | |
| 631 | /* Locate the required virtual table module */ |
| 632 | zMod = pTab->azModuleArg[0]; |
| 633 | pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod)); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 634 | |
| 635 | /* If the module has been registered and includes a Create method, |
| 636 | ** invoke it now. If the module has not been registered, return an |
| 637 | ** error. Otherwise, do nothing. |
| 638 | */ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 639 | if( !pMod ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 640 | *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 641 | rc = SQLITE_ERROR; |
| 642 | }else{ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 643 | rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 644 | } |
| 645 | |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 646 | /* Justification of ALWAYS(): The xConstructor method is required to |
| 647 | ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 648 | if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ |
dan | 2cac207 | 2011-05-25 18:46:22 +0000 | [diff] [blame] | 649 | rc = growVTrans(db); |
| 650 | if( rc==SQLITE_OK ){ |
| 651 | addToVTrans(db, sqlite3GetVTable(db, pTab)); |
| 652 | } |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 653 | } |
| 654 | |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 655 | return rc; |
| 656 | } |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 657 | |
| 658 | /* |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 659 | ** This function is used to set the schema of a virtual table. It is only |
| 660 | ** valid to call this function from within the xCreate() or xConnect() of a |
| 661 | ** virtual table module. |
| 662 | */ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 663 | int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
drh | e98c904 | 2009-06-02 21:31:38 +0000 | [diff] [blame] | 664 | Parse *pParse; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 665 | |
| 666 | int rc = SQLITE_OK; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 667 | Table *pTab; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 668 | char *zErr = 0; |
| 669 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 670 | sqlite3_mutex_enter(db->mutex); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 671 | if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 672 | sqlite3Error(db, SQLITE_MISUSE, 0); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 673 | sqlite3_mutex_leave(db->mutex); |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 674 | return SQLITE_MISUSE_BKPT; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 675 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 676 | assert( (pTab->tabFlags & TF_Virtual)!=0 ); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 677 | |
drh | e98c904 | 2009-06-02 21:31:38 +0000 | [diff] [blame] | 678 | pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); |
| 679 | if( pParse==0 ){ |
| 680 | rc = SQLITE_NOMEM; |
| 681 | }else{ |
| 682 | pParse->declareVtab = 1; |
| 683 | pParse->db = db; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 684 | pParse->nQueryLoop = 1; |
drh | e98c904 | 2009-06-02 21:31:38 +0000 | [diff] [blame] | 685 | |
dan | 84db21e | 2009-12-08 19:05:53 +0000 | [diff] [blame] | 686 | if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) |
dan | 84db21e | 2009-12-08 19:05:53 +0000 | [diff] [blame] | 687 | && pParse->pNewTable |
drh | 0e3c512 | 2009-12-08 22:16:15 +0000 | [diff] [blame] | 688 | && !db->mallocFailed |
dan | 84db21e | 2009-12-08 19:05:53 +0000 | [diff] [blame] | 689 | && !pParse->pNewTable->pSelect |
| 690 | && (pParse->pNewTable->tabFlags & TF_Virtual)==0 |
drh | e98c904 | 2009-06-02 21:31:38 +0000 | [diff] [blame] | 691 | ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 692 | if( !pTab->aCol ){ |
| 693 | pTab->aCol = pParse->pNewTable->aCol; |
| 694 | pTab->nCol = pParse->pNewTable->nCol; |
| 695 | pParse->pNewTable->nCol = 0; |
| 696 | pParse->pNewTable->aCol = 0; |
| 697 | } |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 698 | db->pVtabCtx->pTab = 0; |
dan | 84db21e | 2009-12-08 19:05:53 +0000 | [diff] [blame] | 699 | }else{ |
dan | 06b5db0 | 2010-10-21 15:12:44 +0000 | [diff] [blame] | 700 | sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); |
dan | 78f9b73 | 2010-08-11 11:59:37 +0000 | [diff] [blame] | 701 | sqlite3DbFree(db, zErr); |
drh | e98c904 | 2009-06-02 21:31:38 +0000 | [diff] [blame] | 702 | rc = SQLITE_ERROR; |
| 703 | } |
| 704 | pParse->declareVtab = 0; |
| 705 | |
| 706 | if( pParse->pVdbe ){ |
| 707 | sqlite3VdbeFinalize(pParse->pVdbe); |
| 708 | } |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 709 | sqlite3DeleteTable(db, pParse->pNewTable); |
drh | e98c904 | 2009-06-02 21:31:38 +0000 | [diff] [blame] | 710 | sqlite3StackFree(db, pParse); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 711 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 712 | |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 713 | assert( (rc&0xff)==rc ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 714 | rc = sqlite3ApiExit(db, rc); |
| 715 | sqlite3_mutex_leave(db->mutex); |
| 716 | return rc; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 720 | ** This function is invoked by the vdbe to call the xDestroy method |
| 721 | ** of the virtual table named zTab in database iDb. This occurs |
| 722 | ** when a DROP TABLE is mentioned. |
| 723 | ** |
| 724 | ** This call is a no-op if zTab is not a virtual table. |
| 725 | */ |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 726 | int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 727 | int rc = SQLITE_OK; |
| 728 | Table *pTab; |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 729 | |
| 730 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 731 | if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ |
| 732 | VTable *p = vtabDisconnectAll(db, pTab); |
| 733 | |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 734 | assert( rc==SQLITE_OK ); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 735 | rc = p->pMod->pModule->xDestroy(p->pVtab); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 736 | |
| 737 | /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 738 | if( rc==SQLITE_OK ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 739 | assert( pTab->pVTable==p && p->pNext==0 ); |
| 740 | p->pVtab = 0; |
| 741 | pTab->pVTable = 0; |
| 742 | sqlite3VtabUnlock(p); |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | return rc; |
| 747 | } |
| 748 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 749 | /* |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 750 | ** This function invokes either the xRollback or xCommit method |
| 751 | ** of each of the virtual tables in the sqlite3.aVTrans array. The method |
| 752 | ** called is identified by the second argument, "offset", which is |
| 753 | ** the offset of the method to call in the sqlite3_module structure. |
| 754 | ** |
| 755 | ** The array is cleared after invoking the callbacks. |
| 756 | */ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 757 | static void callFinaliser(sqlite3 *db, int offset){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 758 | int i; |
danielk1977 | 0b83fa8 | 2007-04-19 14:48:37 +0000 | [diff] [blame] | 759 | if( db->aVTrans ){ |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 760 | for(i=0; i<db->nVTrans; i++){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 761 | VTable *pVTab = db->aVTrans[i]; |
| 762 | sqlite3_vtab *p = pVTab->pVtab; |
| 763 | if( p ){ |
| 764 | int (*x)(sqlite3_vtab *); |
| 765 | x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); |
| 766 | if( x ) x(p); |
| 767 | } |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 768 | pVTab->iSavepoint = 0; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 769 | sqlite3VtabUnlock(pVTab); |
danielk1977 | 0b83fa8 | 2007-04-19 14:48:37 +0000 | [diff] [blame] | 770 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 771 | sqlite3DbFree(db, db->aVTrans); |
danielk1977 | 0b83fa8 | 2007-04-19 14:48:37 +0000 | [diff] [blame] | 772 | db->nVTrans = 0; |
| 773 | db->aVTrans = 0; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 774 | } |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 778 | ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans |
| 779 | ** array. Return the error code for the first error that occurs, or |
| 780 | ** SQLITE_OK if all xSync operations are successful. |
| 781 | ** |
| 782 | ** Set *pzErrmsg to point to a buffer that should be released using |
| 783 | ** sqlite3DbFree() containing an error message, if one is available. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 784 | */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 785 | int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 786 | int i; |
| 787 | int rc = SQLITE_OK; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 788 | VTable **aVTrans = db->aVTrans; |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 789 | |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 790 | db->aVTrans = 0; |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 791 | for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 792 | int (*x)(sqlite3_vtab *); |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 793 | sqlite3_vtab *pVtab = aVTrans[i]->pVtab; |
| 794 | if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 795 | rc = x(pVtab); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 796 | sqlite3DbFree(db, *pzErrmsg); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 797 | *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 798 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 801 | db->aVTrans = aVTrans; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 802 | return rc; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
| 806 | ** Invoke the xRollback method of all virtual tables in the |
| 807 | ** sqlite3.aVTrans array. Then clear the array itself. |
| 808 | */ |
| 809 | int sqlite3VtabRollback(sqlite3 *db){ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 810 | callFinaliser(db, offsetof(sqlite3_module,xRollback)); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 811 | return SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* |
| 815 | ** Invoke the xCommit method of all virtual tables in the |
| 816 | ** sqlite3.aVTrans array. Then clear the array itself. |
| 817 | */ |
| 818 | int sqlite3VtabCommit(sqlite3 *db){ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 819 | callFinaliser(db, offsetof(sqlite3_module,xCommit)); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 820 | return SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* |
| 824 | ** If the virtual table pVtab supports the transaction interface |
| 825 | ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is |
| 826 | ** not currently open, invoke the xBegin method now. |
| 827 | ** |
| 828 | ** If the xBegin call is successful, place the sqlite3_vtab pointer |
| 829 | ** in the sqlite3.aVTrans array. |
| 830 | */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 831 | int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 832 | int rc = SQLITE_OK; |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 833 | const sqlite3_module *pModule; |
| 834 | |
| 835 | /* Special case: If db->aVTrans is NULL and db->nVTrans is greater |
| 836 | ** than zero, then this function is being called from within a |
| 837 | ** virtual module xSync() callback. It is illegal to write to |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 838 | ** virtual module tables in this case, so return SQLITE_LOCKED. |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 839 | */ |
danielk1977 | 093e0f6 | 2008-11-13 18:00:14 +0000 | [diff] [blame] | 840 | if( sqlite3VtabInSync(db) ){ |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 841 | return SQLITE_LOCKED; |
| 842 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 843 | if( !pVTab ){ |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 844 | return SQLITE_OK; |
| 845 | } |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 846 | pModule = pVTab->pVtab->pModule; |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 847 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 848 | if( pModule->xBegin ){ |
| 849 | int i; |
| 850 | |
| 851 | /* If pVtab is already in the aVTrans array, return early */ |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 852 | for(i=0; i<db->nVTrans; i++){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 853 | if( db->aVTrans[i]==pVTab ){ |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 854 | return SQLITE_OK; |
| 855 | } |
| 856 | } |
| 857 | |
dan | 2cac207 | 2011-05-25 18:46:22 +0000 | [diff] [blame] | 858 | /* Invoke the xBegin method. If successful, add the vtab to the |
| 859 | ** sqlite3.aVTrans[] array. */ |
| 860 | rc = growVTrans(db); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 861 | if( rc==SQLITE_OK ){ |
dan | 2cac207 | 2011-05-25 18:46:22 +0000 | [diff] [blame] | 862 | rc = pModule->xBegin(pVTab->pVtab); |
| 863 | if( rc==SQLITE_OK ){ |
| 864 | addToVTrans(db, pVTab); |
| 865 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 866 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 867 | } |
| 868 | return rc; |
| 869 | } |
| 870 | |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 871 | /* |
| 872 | ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all |
| 873 | ** virtual tables that currently have an open transaction. Pass iSavepoint |
| 874 | ** as the second argument to the virtual table method invoked. |
| 875 | ** |
| 876 | ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is |
| 877 | ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is |
| 878 | ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with |
| 879 | ** an open transaction is invoked. |
| 880 | ** |
| 881 | ** If any virtual table method returns an error code other than SQLITE_OK, |
| 882 | ** processing is abandoned and the error returned to the caller of this |
| 883 | ** function immediately. If all calls to virtual table methods are successful, |
| 884 | ** SQLITE_OK is returned. |
| 885 | */ |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 886 | int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 887 | int rc = SQLITE_OK; |
| 888 | |
| 889 | assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 890 | assert( iSavepoint>=0 ); |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 891 | if( db->aVTrans ){ |
| 892 | int i; |
| 893 | for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
drh | e485522 | 2011-05-24 15:36:01 +0000 | [diff] [blame] | 894 | VTable *pVTab = db->aVTrans[i]; |
| 895 | const sqlite3_module *pMod = pVTab->pMod->pModule; |
dan | 341cade | 2011-10-29 11:43:04 +0000 | [diff] [blame] | 896 | if( pVTab->pVtab && pMod->iVersion>=2 ){ |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 897 | int (*xMethod)(sqlite3_vtab *, int); |
| 898 | switch( op ){ |
| 899 | case SAVEPOINT_BEGIN: |
| 900 | xMethod = pMod->xSavepoint; |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 901 | pVTab->iSavepoint = iSavepoint+1; |
dan | d9495cd | 2011-04-27 12:08:04 +0000 | [diff] [blame] | 902 | break; |
| 903 | case SAVEPOINT_ROLLBACK: |
| 904 | xMethod = pMod->xRollbackTo; |
| 905 | break; |
| 906 | default: |
| 907 | xMethod = pMod->xRelease; |
| 908 | break; |
| 909 | } |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 910 | if( xMethod && pVTab->iSavepoint>iSavepoint ){ |
dan | 341cade | 2011-10-29 11:43:04 +0000 | [diff] [blame] | 911 | rc = xMethod(pVTab->pVtab, iSavepoint); |
drh | 346506f | 2011-05-25 01:16:42 +0000 | [diff] [blame] | 912 | } |
dan | a311b80 | 2011-04-26 19:21:34 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | } |
| 916 | return rc; |
| 917 | } |
| 918 | |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 919 | /* |
| 920 | ** The first parameter (pDef) is a function implementation. The |
| 921 | ** second parameter (pExpr) is the first argument to this function. |
| 922 | ** If pExpr is a column in a virtual table, then let the virtual |
| 923 | ** table implementation have an opportunity to overload the function. |
| 924 | ** |
| 925 | ** This routine is used to allow virtual table implementations to |
| 926 | ** overload MATCH, LIKE, GLOB, and REGEXP operators. |
| 927 | ** |
| 928 | ** Return either the pDef argument (indicating no change) or a |
| 929 | ** new FuncDef structure that is marked as ephemeral using the |
| 930 | ** SQLITE_FUNC_EPHEM flag. |
| 931 | */ |
| 932 | FuncDef *sqlite3VtabOverloadFunction( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 933 | sqlite3 *db, /* Database connection for reporting malloc problems */ |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 934 | FuncDef *pDef, /* Function to possibly overload */ |
| 935 | int nArg, /* Number of arguments to the function */ |
| 936 | Expr *pExpr /* First argument to the function */ |
| 937 | ){ |
| 938 | Table *pTab; |
| 939 | sqlite3_vtab *pVtab; |
| 940 | sqlite3_module *pMod; |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 941 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0; |
| 942 | void *pArg = 0; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 943 | FuncDef *pNew; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 944 | int rc = 0; |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 945 | char *zLowerName; |
| 946 | unsigned char *z; |
| 947 | |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 948 | |
| 949 | /* Check to see the left operand is a column in a virtual table */ |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 950 | if( NEVER(pExpr==0) ) return pDef; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 951 | if( pExpr->op!=TK_COLUMN ) return pDef; |
| 952 | pTab = pExpr->pTab; |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 953 | if( NEVER(pTab==0) ) return pDef; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 954 | if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 955 | pVtab = sqlite3GetVTable(db, pTab)->pVtab; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 956 | assert( pVtab!=0 ); |
| 957 | assert( pVtab->pModule!=0 ); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 958 | pMod = (sqlite3_module *)pVtab->pModule; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 959 | if( pMod->xFindFunction==0 ) return pDef; |
| 960 | |
rse | b72e88d | 2007-09-20 11:32:18 +0000 | [diff] [blame] | 961 | /* Call the xFindFunction method on the virtual table implementation |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 962 | ** to see if the implementation wants to overload this function |
| 963 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 964 | zLowerName = sqlite3DbStrDup(db, pDef->zName); |
| 965 | if( zLowerName ){ |
| 966 | for(z=(unsigned char*)zLowerName; *z; z++){ |
| 967 | *z = sqlite3UpperToLower[*z]; |
| 968 | } |
| 969 | rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 970 | sqlite3DbFree(db, zLowerName); |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 971 | } |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 972 | if( rc==0 ){ |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 973 | return pDef; |
| 974 | } |
| 975 | |
| 976 | /* Create a new ephemeral function definition for the overloaded |
| 977 | ** function */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 978 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) |
danielk1977 | e25a50b | 2009-07-01 18:04:20 +0000 | [diff] [blame] | 979 | + sqlite3Strlen30(pDef->zName) + 1); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 980 | if( pNew==0 ){ |
| 981 | return pDef; |
| 982 | } |
| 983 | *pNew = *pDef; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 984 | pNew->zName = (char *)&pNew[1]; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 985 | memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 986 | pNew->xFunc = xFunc; |
| 987 | pNew->pUserData = pArg; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 988 | pNew->flags |= SQLITE_FUNC_EPHEM; |
| 989 | return pNew; |
| 990 | } |
| 991 | |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 992 | /* |
| 993 | ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] |
| 994 | ** array so that an OP_VBegin will get generated for it. Add pTab to the |
| 995 | ** array if it is missing. If pTab is already in the array, this routine |
| 996 | ** is a no-op. |
| 997 | */ |
| 998 | void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 999 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 1000 | int i, n; |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 1001 | Table **apVtabLock; |
| 1002 | |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 1003 | assert( IsVirtual(pTab) ); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 1004 | for(i=0; i<pToplevel->nVtabLock; i++){ |
| 1005 | if( pTab==pToplevel->apVtabLock[i] ) return; |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 1006 | } |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 1007 | n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); |
| 1008 | apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n); |
drh | 748ce21 | 2009-05-20 20:10:47 +0000 | [diff] [blame] | 1009 | if( apVtabLock ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 1010 | pToplevel->apVtabLock = apVtabLock; |
| 1011 | pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; |
drh | 344c38e | 2008-05-05 13:23:04 +0000 | [diff] [blame] | 1012 | }else{ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 1013 | pToplevel->db->mallocFailed = 1; |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
drh | ef45bb7 | 2011-05-05 15:39:50 +0000 | [diff] [blame] | 1017 | /* |
| 1018 | ** Return the ON CONFLICT resolution mode in effect for the virtual |
| 1019 | ** table update operation currently in progress. |
| 1020 | ** |
| 1021 | ** The results of this routine are undefined unless it is called from |
| 1022 | ** within an xUpdate method. |
| 1023 | */ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1024 | int sqlite3_vtab_on_conflict(sqlite3 *db){ |
drh | ef45bb7 | 2011-05-05 15:39:50 +0000 | [diff] [blame] | 1025 | static const unsigned char aMap[] = { |
drh | 87f67bf | 2011-05-05 17:41:58 +0000 | [diff] [blame] | 1026 | SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1027 | }; |
| 1028 | assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 1029 | assert( OE_Ignore==4 && OE_Replace==5 ); |
| 1030 | assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
drh | ef45bb7 | 2011-05-05 15:39:50 +0000 | [diff] [blame] | 1031 | return (int)aMap[db->vtabOnConflict-1]; |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
drh | ef45bb7 | 2011-05-05 15:39:50 +0000 | [diff] [blame] | 1034 | /* |
| 1035 | ** Call from within the xCreate() or xConnect() methods to provide |
| 1036 | ** the SQLite core with additional information about the behavior |
| 1037 | ** of the virtual table being implemented. |
| 1038 | */ |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1039 | int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 1040 | va_list ap; |
| 1041 | int rc = SQLITE_OK; |
| 1042 | |
| 1043 | sqlite3_mutex_enter(db->mutex); |
| 1044 | |
| 1045 | va_start(ap, op); |
| 1046 | switch( op ){ |
| 1047 | case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 1048 | VtabCtx *p = db->pVtabCtx; |
| 1049 | if( !p ){ |
| 1050 | rc = SQLITE_MISUSE_BKPT; |
| 1051 | }else{ |
drh | 367e84d | 2011-05-05 23:07:43 +0000 | [diff] [blame] | 1052 | assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 ); |
dan | b061d05 | 2011-04-25 18:49:57 +0000 | [diff] [blame] | 1053 | p->pVTable->bConstraint = (u8)va_arg(ap, int); |
| 1054 | } |
| 1055 | break; |
| 1056 | } |
| 1057 | default: |
| 1058 | rc = SQLITE_MISUSE_BKPT; |
| 1059 | break; |
| 1060 | } |
| 1061 | va_end(ap); |
| 1062 | |
| 1063 | if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0); |
| 1064 | sqlite3_mutex_leave(db->mutex); |
| 1065 | return rc; |
| 1066 | } |
| 1067 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1068 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |