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