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. |
| 13 | ** |
danielk1977 | 777da08 | 2008-11-13 19:12:34 +0000 | [diff] [blame] | 14 | ** $Id: vtab.c,v 1.78 2008/11/13 19:12:36 danielk1977 Exp $ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 15 | */ |
| 16 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 17 | #include "sqliteInt.h" |
| 18 | |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 19 | static int createModule( |
| 20 | sqlite3 *db, /* Database in which module is registered */ |
| 21 | const char *zName, /* Name assigned to this module */ |
| 22 | const sqlite3_module *pModule, /* The definition of the module */ |
| 23 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 24 | void (*xDestroy)(void *) /* Module destructor function */ |
| 25 | ) { |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 26 | int rc, nName; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 27 | Module *pMod; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 28 | |
| 29 | sqlite3_mutex_enter(db->mutex); |
| 30 | nName = strlen(zName); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 31 | pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 32 | if( pMod ){ |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 33 | Module *pDel; |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 34 | char *zCopy = (char *)(&pMod[1]); |
| 35 | memcpy(zCopy, zName, nName+1); |
| 36 | pMod->zName = zCopy; |
| 37 | pMod->pModule = pModule; |
| 38 | pMod->pAux = pAux; |
| 39 | pMod->xDestroy = xDestroy; |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 40 | pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod); |
| 41 | if( pDel && pDel->xDestroy ){ |
| 42 | pDel->xDestroy(pDel->pAux); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 43 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 44 | sqlite3DbFree(db, pDel); |
danielk1977 | 27a430c | 2008-06-23 17:44:18 +0000 | [diff] [blame] | 45 | if( pDel==pMod ){ |
| 46 | db->mallocFailed = 1; |
| 47 | } |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 48 | sqlite3ResetInternalSchema(db, 0); |
danielk1977 | 777da08 | 2008-11-13 19:12:34 +0000 | [diff] [blame] | 49 | }else if( xDestroy ){ |
| 50 | xDestroy(pAux); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 51 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 52 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 53 | sqlite3_mutex_leave(db->mutex); |
| 54 | return rc; |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 58 | /* |
| 59 | ** External API function used to create a new virtual-table module. |
| 60 | */ |
| 61 | int sqlite3_create_module( |
| 62 | sqlite3 *db, /* Database in which module is registered */ |
| 63 | const char *zName, /* Name assigned to this module */ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 64 | const sqlite3_module *pModule, /* The definition of the module */ |
| 65 | void *pAux /* Context pointer for xCreate/xConnect */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 66 | ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 67 | return createModule(db, zName, pModule, pAux, 0); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | ** External API function used to create a new virtual-table module. |
| 72 | */ |
| 73 | int sqlite3_create_module_v2( |
| 74 | sqlite3 *db, /* Database in which module is registered */ |
| 75 | const char *zName, /* Name assigned to this module */ |
| 76 | const sqlite3_module *pModule, /* The definition of the module */ |
| 77 | void *pAux, /* Context pointer for xCreate/xConnect */ |
| 78 | void (*xDestroy)(void *) /* Module destructor function */ |
| 79 | ){ |
| 80 | return createModule(db, zName, pModule, pAux, xDestroy); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 81 | } |
| 82 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 83 | /* |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 84 | ** Lock the virtual table so that it cannot be disconnected. |
| 85 | ** Locks nest. Every lock should have a corresponding unlock. |
| 86 | ** If an unlock is omitted, resources leaks will occur. |
| 87 | ** |
| 88 | ** If a disconnect is attempted while a virtual table is locked, |
| 89 | ** the disconnect is deferred until all locks have been removed. |
| 90 | */ |
| 91 | void sqlite3VtabLock(sqlite3_vtab *pVtab){ |
| 92 | pVtab->nRef++; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | ** Unlock a virtual table. When the last lock is removed, |
| 97 | ** disconnect the virtual table. |
| 98 | */ |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 99 | void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){ |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 100 | pVtab->nRef--; |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 101 | assert(db); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 102 | assert( sqlite3SafetyCheckOk(db) ); |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 103 | if( pVtab->nRef==0 ){ |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 104 | if( db->magic==SQLITE_MAGIC_BUSY ){ |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 105 | (void)sqlite3SafetyOff(db); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 106 | pVtab->pModule->xDisconnect(pVtab); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 107 | (void)sqlite3SafetyOn(db); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 108 | } else { |
| 109 | pVtab->pModule->xDisconnect(pVtab); |
| 110 | } |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 115 | ** Clear any and all virtual-table information from the Table record. |
| 116 | ** This routine is called, for example, just before deleting the Table |
| 117 | ** record. |
| 118 | */ |
| 119 | void sqlite3VtabClear(Table *p){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 120 | sqlite3_vtab *pVtab = p->pVtab; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 121 | sqlite3 *db = p->db; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 122 | if( pVtab ){ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 123 | assert( p->pMod && p->pMod->pModule ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 124 | sqlite3VtabUnlock(db, pVtab); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 125 | p->pVtab = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 126 | } |
| 127 | if( p->azModuleArg ){ |
| 128 | int i; |
| 129 | for(i=0; i<p->nModuleArg; i++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 130 | sqlite3DbFree(db, p->azModuleArg[i]); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 131 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 132 | sqlite3DbFree(db, p->azModuleArg); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | ** Add a new module argument to pTable->azModuleArg[]. |
| 138 | ** The string is not copied - the pointer is stored. The |
| 139 | ** string will be freed automatically when the table is |
| 140 | ** deleted. |
| 141 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 142 | static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 143 | int i = pTable->nModuleArg++; |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 144 | int nBytes = sizeof(char *)*(1+pTable->nModuleArg); |
| 145 | char **azModuleArg; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 146 | azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 147 | if( azModuleArg==0 ){ |
| 148 | int j; |
| 149 | for(j=0; j<i; j++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 150 | sqlite3DbFree(db, pTable->azModuleArg[j]); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 151 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 152 | sqlite3DbFree(db, zArg); |
| 153 | sqlite3DbFree(db, pTable->azModuleArg); |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 154 | pTable->nModuleArg = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 155 | }else{ |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 156 | azModuleArg[i] = zArg; |
| 157 | azModuleArg[i+1] = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 158 | } |
danielk1977 | b7a2f2e | 2006-06-23 11:34:54 +0000 | [diff] [blame] | 159 | pTable->azModuleArg = azModuleArg; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE |
| 164 | ** statement. The module name has been parsed, but the optional list |
| 165 | ** of parameters that follow the module name are still pending. |
| 166 | */ |
| 167 | void sqlite3VtabBeginParse( |
| 168 | Parse *pParse, /* Parsing context */ |
| 169 | Token *pName1, /* Name of new table, or database name */ |
| 170 | Token *pName2, /* Name of new table or NULL */ |
| 171 | Token *pModuleName /* Name of the module for the virtual table */ |
| 172 | ){ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 173 | int iDb; /* The database the table is being created in */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 174 | Table *pTable; /* The new virtual table */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 175 | sqlite3 *db; /* Database connection */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 176 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 177 | if( pParse->db->flags & SQLITE_SharedCache ){ |
danielk1977 | 113e545 | 2007-04-16 15:49:41 +0000 | [diff] [blame] | 178 | sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode"); |
| 179 | return; |
| 180 | } |
| 181 | |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 182 | sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 183 | pTable = pParse->pNewTable; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 184 | if( pTable==0 || pParse->nErr ) return; |
| 185 | assert( 0==pTable->pIndex ); |
| 186 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 187 | db = pParse->db; |
| 188 | iDb = sqlite3SchemaToIndex(db, pTable->pSchema); |
danielk1977 | 70ba164 | 2006-06-21 16:02:42 +0000 | [diff] [blame] | 189 | assert( iDb>=0 ); |
| 190 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 191 | pTable->tabFlags |= TF_Virtual; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 192 | pTable->nModuleArg = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 193 | addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 194 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); |
| 195 | addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 196 | pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 197 | |
| 198 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 199 | /* Creating a virtual table invokes the authorization callback twice. |
| 200 | ** The first invocation, to obtain permission to INSERT a row into the |
| 201 | ** sqlite_master table, has already been made by sqlite3StartTable(). |
| 202 | ** The second call, to obtain permission to create the table, is made now. |
| 203 | */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 204 | if( pTable->azModuleArg ){ |
| 205 | sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, |
| 206 | pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 207 | } |
| 208 | #endif |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | ** This routine takes the module argument that has been accumulating |
| 213 | ** in pParse->zArg[] and appends it to the list of arguments on the |
| 214 | ** virtual table currently under construction in pParse->pTable. |
| 215 | */ |
| 216 | static void addArgumentToVtab(Parse *pParse){ |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 217 | if( pParse->sArg.z && pParse->pNewTable ){ |
drh | 7c2d87c | 2006-09-02 14:16:59 +0000 | [diff] [blame] | 218 | const char *z = (const char*)pParse->sArg.z; |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 219 | int n = pParse->sArg.n; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 220 | sqlite3 *db = pParse->db; |
| 221 | addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 222 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
| 226 | ** The parser calls this routine after the CREATE VIRTUAL TABLE statement |
| 227 | ** has been completely parsed. |
| 228 | */ |
| 229 | void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ |
| 230 | Table *pTab; /* The table being constructed */ |
| 231 | sqlite3 *db; /* The database connection */ |
| 232 | char *zModule; /* The module name of the table: USING modulename */ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 233 | Module *pMod = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 234 | |
| 235 | addArgumentToVtab(pParse); |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 236 | pParse->sArg.z = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 237 | |
| 238 | /* Lookup the module name. */ |
| 239 | pTab = pParse->pNewTable; |
| 240 | if( pTab==0 ) return; |
| 241 | db = pParse->db; |
| 242 | if( pTab->nModuleArg<1 ) return; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 243 | zModule = pTab->azModuleArg[0]; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 244 | pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule)); |
| 245 | pTab->pMod = pMod; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 246 | |
| 247 | /* If the CREATE VIRTUAL TABLE statement is being entered for the |
| 248 | ** first time (in other words if the virtual table is actually being |
| 249 | ** created now instead of just being read out of sqlite_master) then |
| 250 | ** do additional initialization work and store the statement text |
| 251 | ** in the sqlite_master table. |
| 252 | */ |
| 253 | if( !db->init.busy ){ |
| 254 | char *zStmt; |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 255 | char *zWhere; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 256 | int iDb; |
| 257 | Vdbe *v; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 258 | |
| 259 | /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ |
| 260 | if( pEnd ){ |
| 261 | pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n; |
| 262 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 263 | zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 264 | |
| 265 | /* A slot for the record has already been allocated in the |
| 266 | ** SQLITE_MASTER table. We just need to update that slot with all |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 267 | ** the information we've collected. |
| 268 | ** |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 269 | ** The VM register number pParse->regRowid holds the rowid of an |
| 270 | ** entry in the sqlite_master table tht was created for this vtab |
| 271 | ** by sqlite3StartTable(). |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 272 | */ |
| 273 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 274 | sqlite3NestedParse(pParse, |
| 275 | "UPDATE %Q.%s " |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 276 | "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 277 | "WHERE rowid=#%d", |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 278 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 279 | pTab->zName, |
| 280 | pTab->zName, |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 281 | zStmt, |
| 282 | pParse->regRowid |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 283 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 284 | sqlite3DbFree(db, zStmt); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 285 | v = sqlite3GetVdbe(pParse); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 286 | sqlite3ChangeCookie(pParse, iDb); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 287 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 288 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 289 | zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 290 | sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); |
| 291 | sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 292 | pTab->zName, strlen(pTab->zName) + 1); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 293 | } |
| 294 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 295 | /* If we are rereading the sqlite_master table create the in-memory |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 296 | ** record of the table. If the module has already been registered, |
| 297 | ** also call the xConnect method here. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 298 | */ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 299 | else { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 300 | Table *pOld; |
| 301 | Schema *pSchema = pTab->pSchema; |
| 302 | const char *zName = pTab->zName; |
| 303 | int nName = strlen(zName) + 1; |
| 304 | pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); |
| 305 | if( pOld ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 306 | db->mallocFailed = 1; |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 307 | assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 308 | return; |
| 309 | } |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 310 | pSchema->db = pParse->db; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 311 | pParse->pNewTable = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** The parser calls this routine when it sees the first token |
| 317 | ** of an argument to the module name in a CREATE VIRTUAL TABLE statement. |
| 318 | */ |
| 319 | void sqlite3VtabArgInit(Parse *pParse){ |
| 320 | addArgumentToVtab(pParse); |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 321 | pParse->sArg.z = 0; |
| 322 | pParse->sArg.n = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | ** The parser calls this routine for each token after the first token |
| 327 | ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. |
| 328 | */ |
| 329 | void sqlite3VtabArgExtend(Parse *pParse, Token *p){ |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame] | 330 | Token *pArg = &pParse->sArg; |
| 331 | if( pArg->z==0 ){ |
| 332 | pArg->z = p->z; |
| 333 | pArg->n = p->n; |
| 334 | }else{ |
| 335 | assert(pArg->z < p->z); |
| 336 | pArg->n = (p->z + p->n - pArg->z); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 337 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 338 | } |
| 339 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 340 | /* |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 341 | ** Invoke a virtual table constructor (either xCreate or xConnect). The |
| 342 | ** pointer to the function to invoke is passed as the fourth parameter |
| 343 | ** to this procedure. |
| 344 | */ |
| 345 | static int vtabCallConstructor( |
| 346 | sqlite3 *db, |
| 347 | Table *pTab, |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 348 | Module *pMod, |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 349 | int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 350 | char **pzErr |
| 351 | ){ |
| 352 | int rc; |
| 353 | int rc2; |
danielk1977 | 5bccfc9 | 2007-09-04 15:38:57 +0000 | [diff] [blame] | 354 | sqlite3_vtab *pVtab = 0; |
drh | e410296 | 2006-09-11 00:34:22 +0000 | [diff] [blame] | 355 | const char *const*azArg = (const char *const*)pTab->azModuleArg; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 356 | int nArg = pTab->nModuleArg; |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 357 | char *zErr = 0; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 358 | char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 359 | |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 360 | if( !zModuleName ){ |
| 361 | return SQLITE_NOMEM; |
| 362 | } |
| 363 | |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 364 | assert( !db->pVTab ); |
| 365 | assert( xConstruct ); |
| 366 | |
| 367 | db->pVTab = pTab; |
| 368 | rc = sqlite3SafetyOff(db); |
| 369 | assert( rc==SQLITE_OK ); |
danielk1977 | 5bccfc9 | 2007-09-04 15:38:57 +0000 | [diff] [blame] | 370 | rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 371 | rc2 = sqlite3SafetyOn(db); |
drh | fe1368e | 2006-09-10 17:08:29 +0000 | [diff] [blame] | 372 | if( rc==SQLITE_OK && pVtab ){ |
| 373 | pVtab->pModule = pMod->pModule; |
| 374 | pVtab->nRef = 1; |
danielk1977 | 5bccfc9 | 2007-09-04 15:38:57 +0000 | [diff] [blame] | 375 | pTab->pVtab = pVtab; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | if( SQLITE_OK!=rc ){ |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 379 | if( zErr==0 ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 380 | *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); |
drh | 4ca8aac | 2006-09-10 17:31:58 +0000 | [diff] [blame] | 381 | }else { |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 382 | *pzErr = sqlite3MPrintf(db, "%s", zErr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 383 | sqlite3DbFree(db, zErr); |
drh | fe1368e | 2006-09-10 17:08:29 +0000 | [diff] [blame] | 384 | } |
| 385 | }else if( db->pVTab ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 386 | const char *zFormat = "vtable constructor did not declare schema: %s"; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 387 | *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 388 | rc = SQLITE_ERROR; |
| 389 | } |
| 390 | if( rc==SQLITE_OK ){ |
| 391 | rc = rc2; |
| 392 | } |
| 393 | db->pVTab = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 394 | sqlite3DbFree(db, zModuleName); |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 395 | |
| 396 | /* If everything went according to plan, loop through the columns |
| 397 | ** of the table to see if any of them contain the token "hidden". |
| 398 | ** If so, set the Column.isHidden flag and remove the token from |
| 399 | ** the type string. |
| 400 | */ |
| 401 | if( rc==SQLITE_OK ){ |
| 402 | int iCol; |
| 403 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 404 | char *zType = pTab->aCol[iCol].zType; |
| 405 | int nType; |
| 406 | int i = 0; |
| 407 | if( !zType ) continue; |
| 408 | nType = strlen(zType); |
| 409 | if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){ |
| 410 | for(i=0; i<nType; i++){ |
| 411 | if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) |
| 412 | && (zType[i+7]=='\0' || zType[i+7]==' ') |
| 413 | ){ |
| 414 | i++; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | if( i<nType ){ |
| 420 | int j; |
| 421 | int nDel = 6 + (zType[i+6] ? 1 : 0); |
| 422 | for(j=i; (j+nDel)<=nType; j++){ |
| 423 | zType[j] = zType[j+nDel]; |
| 424 | } |
| 425 | if( zType[i]=='\0' && i>0 ){ |
| 426 | assert(zType[i-1]==' '); |
| 427 | zType[i-1] = '\0'; |
| 428 | } |
| 429 | pTab->aCol[iCol].isHidden = 1; |
| 430 | } |
| 431 | } |
| 432 | } |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 433 | return rc; |
| 434 | } |
| 435 | |
| 436 | /* |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 437 | ** This function is invoked by the parser to call the xConnect() method |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 438 | ** of the virtual table pTab. If an error occurs, an error code is returned |
| 439 | ** and an error left in pParse. |
| 440 | ** |
| 441 | ** 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] | 442 | */ |
| 443 | int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 444 | Module *pMod; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 445 | int rc = SQLITE_OK; |
| 446 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 447 | if( !pTab || (pTab->tabFlags & TF_Virtual)==0 || pTab->pVtab ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 448 | return SQLITE_OK; |
| 449 | } |
| 450 | |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 451 | pMod = pTab->pMod; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 452 | if( !pMod ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 453 | const char *zModule = pTab->azModuleArg[0]; |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 454 | sqlite3ErrorMsg(pParse, "no such module: %s", zModule); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 455 | rc = SQLITE_ERROR; |
| 456 | } else { |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 457 | char *zErr = 0; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 458 | sqlite3 *db = pParse->db; |
| 459 | rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 460 | if( rc!=SQLITE_OK ){ |
| 461 | sqlite3ErrorMsg(pParse, "%s", zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 462 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 463 | sqlite3DbFree(db, zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return rc; |
| 467 | } |
| 468 | |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 469 | /* |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 470 | ** Add the virtual table pVtab to the array sqlite3.aVTrans[]. |
| 471 | */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 472 | static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 473 | const int ARRAY_INCR = 5; |
| 474 | |
| 475 | /* Grow the sqlite3.aVTrans array if required */ |
| 476 | if( (db->nVTrans%ARRAY_INCR)==0 ){ |
| 477 | sqlite3_vtab **aVTrans; |
| 478 | int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 479 | aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 480 | if( !aVTrans ){ |
| 481 | return SQLITE_NOMEM; |
| 482 | } |
| 483 | memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); |
| 484 | db->aVTrans = aVTrans; |
| 485 | } |
| 486 | |
| 487 | /* Add pVtab to the end of sqlite3.aVTrans */ |
| 488 | db->aVTrans[db->nVTrans++] = pVtab; |
drh | 189d4af | 2006-09-02 20:57:52 +0000 | [diff] [blame] | 489 | sqlite3VtabLock(pVtab); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 490 | return SQLITE_OK; |
| 491 | } |
| 492 | |
| 493 | /* |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 494 | ** This function is invoked by the vdbe to call the xCreate method |
| 495 | ** of the virtual table named zTab in database iDb. |
| 496 | ** |
| 497 | ** If an error occurs, *pzErr is set to point an an English language |
| 498 | ** description of the error and an SQLITE_XXX error code is returned. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 499 | ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 500 | */ |
| 501 | int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ |
| 502 | int rc = SQLITE_OK; |
| 503 | Table *pTab; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 504 | Module *pMod; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 505 | const char *zModule; |
| 506 | |
| 507 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 508 | assert(pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVtab); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 509 | pMod = pTab->pMod; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 510 | zModule = pTab->azModuleArg[0]; |
| 511 | |
| 512 | /* If the module has been registered and includes a Create method, |
| 513 | ** invoke it now. If the module has not been registered, return an |
| 514 | ** error. Otherwise, do nothing. |
| 515 | */ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 516 | if( !pMod ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 517 | *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 518 | rc = SQLITE_ERROR; |
| 519 | }else{ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 520 | rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 521 | } |
| 522 | |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 523 | if( rc==SQLITE_OK && pTab->pVtab ){ |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 524 | rc = addToVTrans(db, pTab->pVtab); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 525 | } |
| 526 | |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 527 | return rc; |
| 528 | } |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 529 | |
| 530 | /* |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 531 | ** This function is used to set the schema of a virtual table. It is only |
| 532 | ** valid to call this function from within the xCreate() or xConnect() of a |
| 533 | ** virtual table module. |
| 534 | */ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 535 | int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
| 536 | Parse sParse; |
| 537 | |
| 538 | int rc = SQLITE_OK; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 539 | Table *pTab; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 540 | char *zErr = 0; |
| 541 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 542 | sqlite3_mutex_enter(db->mutex); |
| 543 | pTab = db->pVTab; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 544 | if( !pTab ){ |
| 545 | sqlite3Error(db, SQLITE_MISUSE, 0); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 546 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 547 | return SQLITE_MISUSE; |
| 548 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 549 | assert((pTab->tabFlags & TF_Virtual)!=0 && pTab->nCol==0 && pTab->aCol==0); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 550 | |
| 551 | memset(&sParse, 0, sizeof(Parse)); |
| 552 | sParse.declareVtab = 1; |
| 553 | sParse.db = db; |
| 554 | |
| 555 | if( |
| 556 | SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) && |
| 557 | sParse.pNewTable && |
| 558 | !sParse.pNewTable->pSelect && |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 559 | (sParse.pNewTable->tabFlags & TF_Virtual)==0 |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 560 | ){ |
| 561 | pTab->aCol = sParse.pNewTable->aCol; |
| 562 | pTab->nCol = sParse.pNewTable->nCol; |
| 563 | sParse.pNewTable->nCol = 0; |
| 564 | sParse.pNewTable->aCol = 0; |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 565 | db->pVTab = 0; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 566 | } else { |
| 567 | sqlite3Error(db, SQLITE_ERROR, zErr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 568 | sqlite3DbFree(db, zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 569 | rc = SQLITE_ERROR; |
| 570 | } |
| 571 | sParse.declareVtab = 0; |
| 572 | |
| 573 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 574 | sqlite3DeleteTable(sParse.pNewTable); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 575 | sParse.pNewTable = 0; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 576 | |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 577 | assert( (rc&0xff)==rc ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 578 | rc = sqlite3ApiExit(db, rc); |
| 579 | sqlite3_mutex_leave(db->mutex); |
| 580 | return rc; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 584 | ** This function is invoked by the vdbe to call the xDestroy method |
| 585 | ** of the virtual table named zTab in database iDb. This occurs |
| 586 | ** when a DROP TABLE is mentioned. |
| 587 | ** |
| 588 | ** This call is a no-op if zTab is not a virtual table. |
| 589 | */ |
| 590 | int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab) |
| 591 | { |
| 592 | int rc = SQLITE_OK; |
| 593 | Table *pTab; |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 594 | |
| 595 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 596 | assert(pTab); |
| 597 | if( pTab->pVtab ){ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 598 | int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy; |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 599 | rc = sqlite3SafetyOff(db); |
| 600 | assert( rc==SQLITE_OK ); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 601 | if( xDestroy ){ |
| 602 | rc = xDestroy(pTab->pVtab); |
| 603 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 604 | (void)sqlite3SafetyOn(db); |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 605 | if( rc==SQLITE_OK ){ |
drh | 665850f | 2008-04-10 18:35:21 +0000 | [diff] [blame] | 606 | int i; |
| 607 | for(i=0; i<db->nVTrans; i++){ |
| 608 | if( db->aVTrans[i]==pTab->pVtab ){ |
| 609 | db->aVTrans[i] = db->aVTrans[--db->nVTrans]; |
| 610 | break; |
| 611 | } |
| 612 | } |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 613 | pTab->pVtab = 0; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return rc; |
| 618 | } |
| 619 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 620 | /* |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 621 | ** This function invokes either the xRollback or xCommit method |
| 622 | ** of each of the virtual tables in the sqlite3.aVTrans array. The method |
| 623 | ** called is identified by the second argument, "offset", which is |
| 624 | ** the offset of the method to call in the sqlite3_module structure. |
| 625 | ** |
| 626 | ** The array is cleared after invoking the callbacks. |
| 627 | */ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 628 | static void callFinaliser(sqlite3 *db, int offset){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 629 | int i; |
danielk1977 | 0b83fa8 | 2007-04-19 14:48:37 +0000 | [diff] [blame] | 630 | if( db->aVTrans ){ |
| 631 | for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){ |
| 632 | sqlite3_vtab *pVtab = db->aVTrans[i]; |
| 633 | int (*x)(sqlite3_vtab *); |
| 634 | x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset); |
| 635 | if( x ) x(pVtab); |
| 636 | sqlite3VtabUnlock(db, pVtab); |
| 637 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 638 | sqlite3DbFree(db, db->aVTrans); |
danielk1977 | 0b83fa8 | 2007-04-19 14:48:37 +0000 | [diff] [blame] | 639 | db->nVTrans = 0; |
| 640 | db->aVTrans = 0; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 641 | } |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 645 | ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans |
| 646 | ** array. Return the error code for the first error that occurs, or |
| 647 | ** SQLITE_OK if all xSync operations are successful. |
| 648 | ** |
| 649 | ** Set *pzErrmsg to point to a buffer that should be released using |
| 650 | ** sqlite3DbFree() containing an error message, if one is available. |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 651 | */ |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 652 | int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 653 | int i; |
| 654 | int rc = SQLITE_OK; |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 655 | int rcsafety; |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 656 | sqlite3_vtab **aVTrans = db->aVTrans; |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 657 | |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 658 | rc = sqlite3SafetyOff(db); |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 659 | db->aVTrans = 0; |
| 660 | for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){ |
| 661 | sqlite3_vtab *pVtab = aVTrans[i]; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 662 | int (*x)(sqlite3_vtab *); |
| 663 | x = pVtab->pModule->xSync; |
| 664 | if( x ){ |
| 665 | rc = x(pVtab); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 666 | sqlite3DbFree(db, *pzErrmsg); |
| 667 | *pzErrmsg = pVtab->zErrMsg; |
| 668 | pVtab->zErrMsg = 0; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 671 | db->aVTrans = aVTrans; |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 672 | rcsafety = sqlite3SafetyOn(db); |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 673 | |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 674 | if( rc==SQLITE_OK ){ |
| 675 | rc = rcsafety; |
| 676 | } |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 677 | return rc; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* |
| 681 | ** Invoke the xRollback method of all virtual tables in the |
| 682 | ** sqlite3.aVTrans array. Then clear the array itself. |
| 683 | */ |
| 684 | int sqlite3VtabRollback(sqlite3 *db){ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 685 | callFinaliser(db, offsetof(sqlite3_module,xRollback)); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 686 | return SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* |
| 690 | ** Invoke the xCommit method of all virtual tables in the |
| 691 | ** sqlite3.aVTrans array. Then clear the array itself. |
| 692 | */ |
| 693 | int sqlite3VtabCommit(sqlite3 *db){ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 694 | callFinaliser(db, offsetof(sqlite3_module,xCommit)); |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 695 | return SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
| 699 | ** If the virtual table pVtab supports the transaction interface |
| 700 | ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is |
| 701 | ** not currently open, invoke the xBegin method now. |
| 702 | ** |
| 703 | ** If the xBegin call is successful, place the sqlite3_vtab pointer |
| 704 | ** in the sqlite3.aVTrans array. |
| 705 | */ |
| 706 | int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){ |
| 707 | int rc = SQLITE_OK; |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 708 | const sqlite3_module *pModule; |
| 709 | |
| 710 | /* Special case: If db->aVTrans is NULL and db->nVTrans is greater |
| 711 | ** than zero, then this function is being called from within a |
| 712 | ** virtual module xSync() callback. It is illegal to write to |
| 713 | ** virtual module tables in this case, so return SQLITE_LOCKED. |
| 714 | */ |
danielk1977 | 093e0f6 | 2008-11-13 18:00:14 +0000 | [diff] [blame] | 715 | if( sqlite3VtabInSync(db) ){ |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 716 | return SQLITE_LOCKED; |
| 717 | } |
| 718 | if( !pVtab ){ |
| 719 | return SQLITE_OK; |
| 720 | } |
| 721 | pModule = pVtab->pModule; |
| 722 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 723 | if( pModule->xBegin ){ |
| 724 | int i; |
| 725 | |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 726 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 727 | /* If pVtab is already in the aVTrans array, return early */ |
| 728 | for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){ |
| 729 | if( db->aVTrans[i]==pVtab ){ |
| 730 | return SQLITE_OK; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | /* Invoke the xBegin method */ |
| 735 | rc = pModule->xBegin(pVtab); |
danielk1977 | 3e3a84d | 2008-08-01 17:37:40 +0000 | [diff] [blame] | 736 | if( rc==SQLITE_OK ){ |
| 737 | rc = addToVTrans(db, pVtab); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 738 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 739 | } |
| 740 | return rc; |
| 741 | } |
| 742 | |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 743 | /* |
| 744 | ** The first parameter (pDef) is a function implementation. The |
| 745 | ** second parameter (pExpr) is the first argument to this function. |
| 746 | ** If pExpr is a column in a virtual table, then let the virtual |
| 747 | ** table implementation have an opportunity to overload the function. |
| 748 | ** |
| 749 | ** This routine is used to allow virtual table implementations to |
| 750 | ** overload MATCH, LIKE, GLOB, and REGEXP operators. |
| 751 | ** |
| 752 | ** Return either the pDef argument (indicating no change) or a |
| 753 | ** new FuncDef structure that is marked as ephemeral using the |
| 754 | ** SQLITE_FUNC_EPHEM flag. |
| 755 | */ |
| 756 | FuncDef *sqlite3VtabOverloadFunction( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 757 | sqlite3 *db, /* Database connection for reporting malloc problems */ |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 758 | FuncDef *pDef, /* Function to possibly overload */ |
| 759 | int nArg, /* Number of arguments to the function */ |
| 760 | Expr *pExpr /* First argument to the function */ |
| 761 | ){ |
| 762 | Table *pTab; |
| 763 | sqlite3_vtab *pVtab; |
| 764 | sqlite3_module *pMod; |
drh | e94b0c3 | 2006-07-08 18:09:15 +0000 | [diff] [blame] | 765 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 766 | void *pArg; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 767 | FuncDef *pNew; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 768 | int rc = 0; |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 769 | char *zLowerName; |
| 770 | unsigned char *z; |
| 771 | |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 772 | |
| 773 | /* Check to see the left operand is a column in a virtual table */ |
| 774 | if( pExpr==0 ) return pDef; |
| 775 | if( pExpr->op!=TK_COLUMN ) return pDef; |
| 776 | pTab = pExpr->pTab; |
| 777 | if( pTab==0 ) return pDef; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 778 | if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 779 | pVtab = pTab->pVtab; |
| 780 | assert( pVtab!=0 ); |
| 781 | assert( pVtab->pModule!=0 ); |
danielk1977 | 5bd270b | 2006-07-25 15:14:52 +0000 | [diff] [blame] | 782 | pMod = (sqlite3_module *)pVtab->pModule; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 783 | if( pMod->xFindFunction==0 ) return pDef; |
| 784 | |
rse | b72e88d | 2007-09-20 11:32:18 +0000 | [diff] [blame] | 785 | /* Call the xFindFunction method on the virtual table implementation |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 786 | ** to see if the implementation wants to overload this function |
| 787 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 788 | zLowerName = sqlite3DbStrDup(db, pDef->zName); |
| 789 | if( zLowerName ){ |
| 790 | for(z=(unsigned char*)zLowerName; *z; z++){ |
| 791 | *z = sqlite3UpperToLower[*z]; |
| 792 | } |
| 793 | rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 794 | sqlite3DbFree(db, zLowerName); |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 795 | if( pVtab->zErrMsg ){ |
| 796 | sqlite3Error(db, rc, "%s", pVtab->zErrMsg); |
| 797 | sqlite3DbFree(db, pVtab->zErrMsg); |
| 798 | pVtab->zErrMsg = 0; |
| 799 | } |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 800 | } |
drh | a70034d | 2006-09-18 20:24:02 +0000 | [diff] [blame] | 801 | if( rc==0 ){ |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 802 | return pDef; |
| 803 | } |
| 804 | |
| 805 | /* Create a new ephemeral function definition for the overloaded |
| 806 | ** function */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 807 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) ); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 808 | if( pNew==0 ){ |
| 809 | return pDef; |
| 810 | } |
| 811 | *pNew = *pDef; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 812 | pNew->zName = (char *)&pNew[1]; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 813 | memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 814 | pNew->xFunc = xFunc; |
| 815 | pNew->pUserData = pArg; |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 816 | pNew->flags |= SQLITE_FUNC_EPHEM; |
| 817 | return pNew; |
| 818 | } |
| 819 | |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 820 | /* |
| 821 | ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] |
| 822 | ** array so that an OP_VBegin will get generated for it. Add pTab to the |
| 823 | ** array if it is missing. If pTab is already in the array, this routine |
| 824 | ** is a no-op. |
| 825 | */ |
| 826 | void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ |
| 827 | int i, n; |
| 828 | assert( IsVirtual(pTab) ); |
| 829 | for(i=0; i<pParse->nVtabLock; i++){ |
| 830 | if( pTab==pParse->apVtabLock[i] ) return; |
| 831 | } |
| 832 | n = (pParse->nVtabLock+1)*sizeof(pParse->apVtabLock[0]); |
| 833 | pParse->apVtabLock = sqlite3_realloc(pParse->apVtabLock, n); |
| 834 | if( pParse->apVtabLock ){ |
| 835 | pParse->apVtabLock[pParse->nVtabLock++] = pTab; |
drh | 344c38e | 2008-05-05 13:23:04 +0000 | [diff] [blame] | 836 | }else{ |
| 837 | pParse->db->mallocFailed = 1; |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 841 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |