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