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