drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2006 June 10 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used to help implement virtual tables. |
| 13 | ** |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame^] | 14 | ** $Id: vtab.c,v 1.23 2006/06/24 08:51:05 danielk1977 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 ){ |
| 142 | char *z = pParse->sArg.z; |
| 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; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 181 | if( !pMod ){ |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 182 | sqlite3ErrorMsg(pParse, "no such module: %s", zModule); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ |
| 186 | if( pEnd ){ |
| 187 | pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n; |
| 188 | } |
| 189 | zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken); |
| 190 | |
| 191 | /* A slot for the record has already been allocated in the |
| 192 | ** SQLITE_MASTER table. We just need to update that slot with all |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 193 | ** the information we've collected. |
| 194 | ** |
| 195 | ** The top of the stack is the rootpage allocated by sqlite3StartTable(). |
| 196 | ** This value is always 0 and is ignored, a virtual table does not have a |
| 197 | ** rootpage. The next entry on the stack is the rowid of the record |
| 198 | ** in the sqlite_master table. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 199 | */ |
| 200 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 201 | sqlite3NestedParse(pParse, |
| 202 | "UPDATE %Q.%s " |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 203 | "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " |
| 204 | "WHERE rowid=#1", |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 205 | db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
| 206 | pTab->zName, |
| 207 | pTab->zName, |
| 208 | zStmt |
| 209 | ); |
| 210 | sqliteFree(zStmt); |
| 211 | v = sqlite3GetVdbe(pParse); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 212 | sqlite3ChangeCookie(db, v, iDb); |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 213 | |
| 214 | sqlite3VdbeAddOp(v, OP_Expire, 0, 0); |
| 215 | zWhere = sqlite3MPrintf("name='%q'", pTab->zName); |
| 216 | sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC); |
| 217 | sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 218 | } |
| 219 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 220 | /* If we are rereading the sqlite_master table create the in-memory |
danielk1977 | c7d5410 | 2006-06-15 07:29:00 +0000 | [diff] [blame] | 221 | ** record of the table. If the module has already been registered, |
| 222 | ** also call the xConnect method here. |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 223 | */ |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 224 | else { |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 225 | Table *pOld; |
| 226 | Schema *pSchema = pTab->pSchema; |
| 227 | const char *zName = pTab->zName; |
| 228 | int nName = strlen(zName) + 1; |
| 229 | pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); |
| 230 | if( pOld ){ |
| 231 | assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 232 | return; |
| 233 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 234 | pParse->pNewTable = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | ** The parser calls this routine when it sees the first token |
| 240 | ** of an argument to the module name in a CREATE VIRTUAL TABLE statement. |
| 241 | */ |
| 242 | void sqlite3VtabArgInit(Parse *pParse){ |
| 243 | addArgumentToVtab(pParse); |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame^] | 244 | pParse->sArg.z = 0; |
| 245 | pParse->sArg.n = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* |
| 249 | ** The parser calls this routine for each token after the first token |
| 250 | ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. |
| 251 | */ |
| 252 | void sqlite3VtabArgExtend(Parse *pParse, Token *p){ |
danielk1977 | 33b3933 | 2006-06-24 08:51:05 +0000 | [diff] [blame^] | 253 | Token *pArg = &pParse->sArg; |
| 254 | if( pArg->z==0 ){ |
| 255 | pArg->z = p->z; |
| 256 | pArg->n = p->n; |
| 257 | }else{ |
| 258 | assert(pArg->z < p->z); |
| 259 | pArg->n = (p->z + p->n - pArg->z); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 260 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 261 | } |
| 262 | |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 263 | /* |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 264 | ** Invoke a virtual table constructor (either xCreate or xConnect). The |
| 265 | ** pointer to the function to invoke is passed as the fourth parameter |
| 266 | ** to this procedure. |
| 267 | */ |
| 268 | static int vtabCallConstructor( |
| 269 | sqlite3 *db, |
| 270 | Table *pTab, |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 271 | Module *pMod, |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 272 | int (*xConstruct)(sqlite3*, void *, int, char **, sqlite3_vtab **), |
| 273 | char **pzErr |
| 274 | ){ |
| 275 | int rc; |
| 276 | int rc2; |
| 277 | char **azArg = pTab->azModuleArg; |
| 278 | int nArg = pTab->nModuleArg; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 279 | char *zErr = sqlite3MPrintf("vtable constructor failed: %s", pTab->zName); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 280 | |
| 281 | assert( !db->pVTab ); |
| 282 | assert( xConstruct ); |
| 283 | |
| 284 | db->pVTab = pTab; |
| 285 | rc = sqlite3SafetyOff(db); |
| 286 | assert( rc==SQLITE_OK ); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 287 | rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 288 | rc2 = sqlite3SafetyOn(db); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 289 | if( rc==SQLITE_OK && pTab->pVtab ){ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 290 | pTab->pVtab->pModule = pMod->pModule; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 291 | pTab->pVtab->nRef = 1; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if( SQLITE_OK!=rc ){ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 295 | *pzErr = zErr; |
| 296 | zErr = 0; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 297 | } else if( db->pVTab ){ |
| 298 | const char *zFormat = "vtable constructor did not declare schema: %s"; |
| 299 | *pzErr = sqlite3MPrintf(zFormat, pTab->zName); |
| 300 | rc = SQLITE_ERROR; |
| 301 | } |
| 302 | if( rc==SQLITE_OK ){ |
| 303 | rc = rc2; |
| 304 | } |
| 305 | db->pVTab = 0; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 306 | sqliteFree(zErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 307 | return rc; |
| 308 | } |
| 309 | |
| 310 | /* |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 311 | ** This function is invoked by the parser to call the xConnect() method |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 312 | ** of the virtual table pTab. If an error occurs, an error code is returned |
| 313 | ** and an error left in pParse. |
| 314 | ** |
| 315 | ** 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] | 316 | */ |
| 317 | int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 318 | Module *pMod; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 319 | const char *zModule; |
| 320 | int rc = SQLITE_OK; |
| 321 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 322 | if( !pTab || !pTab->isVirtual || pTab->pVtab ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 323 | return SQLITE_OK; |
| 324 | } |
| 325 | |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 326 | pMod = pTab->pMod; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 327 | zModule = pTab->azModuleArg[0]; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 328 | if( !pMod ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 329 | const char *zModule = pTab->azModuleArg[0]; |
danielk1977 | a4e7636 | 2006-06-14 06:31:28 +0000 | [diff] [blame] | 330 | sqlite3ErrorMsg(pParse, "no such module: %s", zModule); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 331 | rc = SQLITE_ERROR; |
| 332 | } else { |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 333 | char *zErr = 0; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 334 | sqlite3 *db = pParse->db; |
| 335 | rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 336 | if( rc!=SQLITE_OK ){ |
| 337 | sqlite3ErrorMsg(pParse, "%s", zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 338 | } |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 339 | sqliteFree(zErr); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return rc; |
| 343 | } |
| 344 | |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 345 | /* |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 346 | ** Add the virtual table pVtab to the array sqlite3.aVTrans[]. |
| 347 | */ |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 348 | static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 349 | const int ARRAY_INCR = 5; |
| 350 | |
| 351 | /* Grow the sqlite3.aVTrans array if required */ |
| 352 | if( (db->nVTrans%ARRAY_INCR)==0 ){ |
| 353 | sqlite3_vtab **aVTrans; |
| 354 | int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); |
| 355 | aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes); |
| 356 | if( !aVTrans ){ |
| 357 | return SQLITE_NOMEM; |
| 358 | } |
| 359 | memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); |
| 360 | db->aVTrans = aVTrans; |
| 361 | } |
| 362 | |
| 363 | /* Add pVtab to the end of sqlite3.aVTrans */ |
| 364 | db->aVTrans[db->nVTrans++] = pVtab; |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 365 | pVtab->nRef++; |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 366 | return SQLITE_OK; |
| 367 | } |
| 368 | |
| 369 | /* |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 370 | ** This function is invoked by the vdbe to call the xCreate method |
| 371 | ** of the virtual table named zTab in database iDb. |
| 372 | ** |
| 373 | ** If an error occurs, *pzErr is set to point an an English language |
| 374 | ** description of the error and an SQLITE_XXX error code is returned. |
| 375 | ** In this case the caller must call sqliteFree() on *pzErr. |
| 376 | */ |
| 377 | int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ |
| 378 | int rc = SQLITE_OK; |
| 379 | Table *pTab; |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 380 | Module *pMod; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 381 | const char *zModule; |
| 382 | |
| 383 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
| 384 | assert(pTab && pTab->isVirtual && !pTab->pVtab); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 385 | pMod = pTab->pMod; |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 386 | zModule = pTab->azModuleArg[0]; |
| 387 | |
| 388 | /* If the module has been registered and includes a Create method, |
| 389 | ** invoke it now. If the module has not been registered, return an |
| 390 | ** error. Otherwise, do nothing. |
| 391 | */ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 392 | if( !pMod ){ |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 393 | *pzErr = sqlite3MPrintf("no such module: %s", zModule); |
| 394 | rc = SQLITE_ERROR; |
| 395 | }else{ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 396 | rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 397 | } |
| 398 | |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 399 | if( rc==SQLITE_OK && pTab->pVtab ){ |
| 400 | rc = addToVTrans(db, pTab->pVtab); |
| 401 | } |
| 402 | |
danielk1977 | 9da9d47 | 2006-06-14 06:58:15 +0000 | [diff] [blame] | 403 | return rc; |
| 404 | } |
danielk1977 | be8a783 | 2006-06-13 15:00:54 +0000 | [diff] [blame] | 405 | |
| 406 | /* |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 407 | ** This function is used to set the schema of a virtual table. It is only |
| 408 | ** valid to call this function from within the xCreate() or xConnect() of a |
| 409 | ** virtual table module. |
| 410 | */ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 411 | int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
| 412 | Parse sParse; |
| 413 | |
| 414 | int rc = SQLITE_OK; |
| 415 | Table *pTab = db->pVTab; |
| 416 | char *zErr = 0; |
| 417 | |
| 418 | if( !pTab ){ |
| 419 | sqlite3Error(db, SQLITE_MISUSE, 0); |
| 420 | return SQLITE_MISUSE; |
| 421 | } |
| 422 | assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0); |
| 423 | |
| 424 | memset(&sParse, 0, sizeof(Parse)); |
| 425 | sParse.declareVtab = 1; |
| 426 | sParse.db = db; |
| 427 | |
| 428 | if( |
| 429 | SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) && |
| 430 | sParse.pNewTable && |
| 431 | !sParse.pNewTable->pSelect && |
| 432 | !sParse.pNewTable->isVirtual |
| 433 | ){ |
| 434 | pTab->aCol = sParse.pNewTable->aCol; |
| 435 | pTab->nCol = sParse.pNewTable->nCol; |
| 436 | sParse.pNewTable->nCol = 0; |
| 437 | sParse.pNewTable->aCol = 0; |
| 438 | } else { |
| 439 | sqlite3Error(db, SQLITE_ERROR, zErr); |
| 440 | sqliteFree(zErr); |
| 441 | rc = SQLITE_ERROR; |
| 442 | } |
| 443 | sParse.declareVtab = 0; |
| 444 | |
| 445 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
| 446 | sqlite3DeleteTable(0, sParse.pNewTable); |
| 447 | sParse.pNewTable = 0; |
| 448 | db->pVTab = 0; |
| 449 | |
| 450 | return rc; |
| 451 | } |
| 452 | |
| 453 | /* |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 454 | ** This function is invoked by the vdbe to call the xDestroy method |
| 455 | ** of the virtual table named zTab in database iDb. This occurs |
| 456 | ** when a DROP TABLE is mentioned. |
| 457 | ** |
| 458 | ** This call is a no-op if zTab is not a virtual table. |
| 459 | */ |
| 460 | int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab) |
| 461 | { |
| 462 | int rc = SQLITE_OK; |
| 463 | Table *pTab; |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 464 | |
| 465 | pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 466 | assert(pTab); |
| 467 | if( pTab->pVtab ){ |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 468 | int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy; |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 469 | rc = sqlite3SafetyOff(db); |
| 470 | assert( rc==SQLITE_OK ); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 471 | if( xDestroy ){ |
| 472 | rc = xDestroy(pTab->pVtab); |
| 473 | } |
danielk1977 | 9e39ce8 | 2006-06-12 16:01:21 +0000 | [diff] [blame] | 474 | sqlite3SafetyOn(db); |
| 475 | if( rc==SQLITE_OK ){ |
| 476 | pTab->pVtab = 0; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 483 | /* |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 484 | ** This function invokes either the xRollback or xCommit method |
| 485 | ** of each of the virtual tables in the sqlite3.aVTrans array. The method |
| 486 | ** called is identified by the second argument, "offset", which is |
| 487 | ** the offset of the method to call in the sqlite3_module structure. |
| 488 | ** |
| 489 | ** The array is cleared after invoking the callbacks. |
| 490 | */ |
| 491 | static void callFinaliser(sqlite3 *db, int offset){ |
| 492 | int i; |
| 493 | for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){ |
| 494 | sqlite3_vtab *pVtab = db->aVTrans[i]; |
| 495 | int (*x)(sqlite3_vtab *); |
| 496 | x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset); |
| 497 | if( x ) x(pVtab); |
danielk1977 | be71889 | 2006-06-23 08:05:19 +0000 | [diff] [blame] | 498 | pVtab->nRef--; |
| 499 | if( pVtab->nRef==0 ){ |
| 500 | pVtab->pModule->xDisconnect(pVtab); |
| 501 | } |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 502 | } |
| 503 | sqliteFree(db->aVTrans); |
| 504 | db->nVTrans = 0; |
| 505 | db->aVTrans = 0; |
| 506 | } |
| 507 | |
| 508 | /* |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 509 | ** If argument rc is not SQLITE_OK, then return it and do nothing. |
| 510 | ** Otherwise, invoke the xSync method of all virtual tables in the |
| 511 | ** sqlite3.aVTrans array. Return the error code for the first error |
| 512 | ** that occurs, or SQLITE_OK if all xSync operations are successful. |
| 513 | */ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 514 | int sqlite3VtabSync(sqlite3 *db, int rc2){ |
| 515 | int i; |
| 516 | int rc = SQLITE_OK; |
| 517 | if( rc2!=SQLITE_OK ) return rc2; |
| 518 | for(i=0; rc==SQLITE_OK && i<db->nVTrans && db->aVTrans[i]; i++){ |
| 519 | sqlite3_vtab *pVtab = db->aVTrans[i]; |
| 520 | int (*x)(sqlite3_vtab *); |
| 521 | x = pVtab->pModule->xSync; |
| 522 | if( x ){ |
| 523 | rc = x(pVtab); |
| 524 | } |
| 525 | } |
| 526 | return rc; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* |
| 530 | ** Invoke the xRollback method of all virtual tables in the |
| 531 | ** sqlite3.aVTrans array. Then clear the array itself. |
| 532 | */ |
| 533 | int sqlite3VtabRollback(sqlite3 *db){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 534 | callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback)); |
| 535 | return SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
| 539 | ** Invoke the xCommit method of all virtual tables in the |
| 540 | ** sqlite3.aVTrans array. Then clear the array itself. |
| 541 | */ |
| 542 | int sqlite3VtabCommit(sqlite3 *db){ |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 543 | callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit)); |
| 544 | return SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | ** If the virtual table pVtab supports the transaction interface |
| 549 | ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is |
| 550 | ** not currently open, invoke the xBegin method now. |
| 551 | ** |
| 552 | ** If the xBegin call is successful, place the sqlite3_vtab pointer |
| 553 | ** in the sqlite3.aVTrans array. |
| 554 | */ |
| 555 | int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){ |
| 556 | int rc = SQLITE_OK; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 557 | const sqlite3_module *pModule = pVtab->pModule; |
| 558 | if( pModule->xBegin ){ |
| 559 | int i; |
| 560 | |
| 561 | /* If pVtab is already in the aVTrans array, return early */ |
| 562 | for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){ |
| 563 | if( db->aVTrans[i]==pVtab ){ |
| 564 | return SQLITE_OK; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* Invoke the xBegin method */ |
| 569 | rc = pModule->xBegin(pVtab); |
| 570 | if( rc!=SQLITE_OK ){ |
| 571 | return rc; |
| 572 | } |
| 573 | |
danielk1977 | e7ff403 | 2006-06-17 11:30:32 +0000 | [diff] [blame] | 574 | rc = addToVTrans(db, pVtab); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 575 | } |
| 576 | return rc; |
| 577 | } |
| 578 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 579 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |