sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2003 April 6 |
| 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 implement the ATTACH and DETACH commands. |
| 13 | */ |
| 14 | #include "sqliteInt.h" |
| 15 | |
| 16 | #ifndef SQLITE_OMIT_ATTACH |
| 17 | /* |
| 18 | ** Resolve an expression that was part of an ATTACH or DETACH statement. This |
| 19 | ** is slightly different from resolving a normal SQL expression, because simple |
| 20 | ** identifiers are treated as strings, not possible column names or aliases. |
| 21 | ** |
| 22 | ** i.e. if the parser sees: |
| 23 | ** |
| 24 | ** ATTACH DATABASE abc AS def |
| 25 | ** |
| 26 | ** it treats the two expressions as literal strings 'abc' and 'def' instead of |
| 27 | ** looking for columns of the same name. |
| 28 | ** |
| 29 | ** This only applies to the root node of pExpr, so the statement: |
| 30 | ** |
| 31 | ** ATTACH DATABASE abc||def AS 'db2' |
| 32 | ** |
| 33 | ** will fail because neither abc or def can be resolved. |
| 34 | */ |
| 35 | static int resolveAttachExpr(NameContext *pName, Expr *pExpr) |
| 36 | { |
| 37 | int rc = SQLITE_OK; |
| 38 | if( pExpr ){ |
| 39 | if( pExpr->op!=TK_ID ){ |
| 40 | rc = sqlite3ResolveExprNames(pName, pExpr); |
| 41 | }else{ |
| 42 | pExpr->op = TK_STRING; |
| 43 | } |
| 44 | } |
| 45 | return rc; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | ** Return true if zName points to a name that may be used to refer to |
| 50 | ** database iDb attached to handle db. |
| 51 | */ |
| 52 | int sqlite3DbIsNamed(sqlite3 *db, int iDb, const char *zName){ |
| 53 | return ( |
| 54 | sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0 |
| 55 | || (iDb==0 && sqlite3StrICmp("main", zName)==0) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | ** If any TEMP triggers reference schema pSchema, move those triggers to |
| 61 | ** reference the TEMP schema itself. |
| 62 | */ |
| 63 | static void detachTempTriggers(sqlite3 *db, Schema *pSchema){ |
| 64 | HashElem *pEntry; |
| 65 | assert( db->aDb[1].pSchema ); |
| 66 | pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash); |
| 67 | while( pEntry ){ |
| 68 | Trigger *pTrig = (Trigger*)sqliteHashData(pEntry); |
| 69 | if( pTrig->pTabSchema==pSchema ){ |
| 70 | pTrig->pTabSchema = pTrig->pSchema; |
| 71 | } |
| 72 | pEntry = sqliteHashNext(pEntry); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | ** An SQL user-function registered to do the work of an ATTACH statement. The |
| 78 | ** three arguments to the function come directly from an attach statement: |
| 79 | ** |
| 80 | ** ATTACH DATABASE x AS y KEY z |
| 81 | ** |
| 82 | ** SELECT sqlite_attach(x, y, z) |
| 83 | ** |
| 84 | ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the |
| 85 | ** third argument. |
| 86 | ** |
| 87 | ** If the db->init.reopenMemdb flags is set, then instead of attaching a |
| 88 | ** new database, close the database on db->init.iDb and reopen it as an |
| 89 | ** empty MemDB. |
| 90 | */ |
| 91 | static void attachFunc( |
| 92 | sqlite3_context *context, |
| 93 | int NotUsed, |
| 94 | sqlite3_value **argv |
| 95 | ){ |
| 96 | int i; |
| 97 | int rc = 0; |
| 98 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 99 | const char *zName; |
| 100 | const char *zFile; |
| 101 | char *zPath = 0; |
| 102 | char *zErr = 0; |
| 103 | unsigned int flags; |
| 104 | Db *aNew; /* New array of Db pointers */ |
| 105 | Db *pNew = 0; /* Db object for the newly attached database */ |
| 106 | char *zErrDyn = 0; |
| 107 | sqlite3_vfs *pVfs; |
| 108 | |
| 109 | UNUSED_PARAMETER(NotUsed); |
| 110 | zFile = (const char *)sqlite3_value_text(argv[0]); |
| 111 | zName = (const char *)sqlite3_value_text(argv[1]); |
| 112 | if( zFile==0 ) zFile = ""; |
| 113 | if( zName==0 ) zName = ""; |
| 114 | |
| 115 | #ifndef SQLITE_OMIT_DESERIALIZE |
| 116 | # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb) |
| 117 | #else |
| 118 | # define REOPEN_AS_MEMDB(db) (0) |
| 119 | #endif |
| 120 | |
| 121 | if( REOPEN_AS_MEMDB(db) ){ |
| 122 | /* This is not a real ATTACH. Instead, this routine is being called |
| 123 | ** from sqlite3_deserialize() to close database db->init.iDb and |
| 124 | ** reopen it as a MemDB */ |
| 125 | Btree *pNewBt = 0; |
| 126 | |
| 127 | pNew = &db->aDb[db->init.iDb]; |
| 128 | assert( pNew->pBt!=0 ); |
| 129 | if( sqlite3BtreeTxnState(pNew->pBt)!=SQLITE_TXN_NONE |
| 130 | || sqlite3BtreeIsInBackup(pNew->pBt) |
| 131 | ){ |
| 132 | rc = SQLITE_BUSY; |
| 133 | goto attach_error; |
| 134 | } |
| 135 | |
| 136 | pVfs = sqlite3_vfs_find("memdb"); |
| 137 | if( pVfs==0 ) return; |
| 138 | rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNewBt, 0, SQLITE_OPEN_MAIN_DB); |
| 139 | if( rc==SQLITE_OK ){ |
| 140 | Schema *pNewSchema = sqlite3SchemaGet(db, pNewBt); |
| 141 | if( pNewSchema ){ |
| 142 | /* Both the Btree and the new Schema were allocated successfully. |
| 143 | ** Close the old db and update the aDb[] slot with the new memdb |
| 144 | ** values. */ |
| 145 | detachTempTriggers(db, pNew->pSchema); |
| 146 | sqlite3BtreeClose(pNew->pBt); |
| 147 | pNew->pBt = pNewBt; |
| 148 | pNew->pSchema = pNewSchema; |
| 149 | if( db->init.iDb==0 ){ |
| 150 | /* Clear all eponymous virtual table instances, as they are holding |
| 151 | ** pointers to the schema object just freed by sqlite3BtreeClose() */ |
| 152 | sqlite3VtabEponymousTableClearAll(db); |
| 153 | } |
| 154 | sqlite3ExpirePreparedStatements(db, 1); |
| 155 | }else{ |
| 156 | sqlite3BtreeClose(pNewBt); |
| 157 | rc = SQLITE_NOMEM; |
| 158 | } |
| 159 | } |
| 160 | if( rc ) goto attach_error; |
| 161 | }else{ |
| 162 | /* This is a real ATTACH |
| 163 | ** |
| 164 | ** Check for the following errors: |
| 165 | ** |
| 166 | ** * Too many attached databases, |
| 167 | ** * Transaction currently open |
| 168 | ** * Specified database name already being used. |
| 169 | */ |
| 170 | if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ |
| 171 | zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", |
| 172 | db->aLimit[SQLITE_LIMIT_ATTACHED] |
| 173 | ); |
| 174 | goto attach_error; |
| 175 | } |
| 176 | for(i=0; i<db->nDb; i++){ |
| 177 | assert( zName ); |
| 178 | if( sqlite3DbIsNamed(db, i, zName) ){ |
| 179 | zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName); |
| 180 | goto attach_error; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* Allocate the new entry in the db->aDb[] array and initialize the schema |
| 185 | ** hash tables. |
| 186 | */ |
| 187 | if( db->aDb==db->aDbStatic ){ |
| 188 | aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 ); |
| 189 | if( aNew==0 ) return; |
| 190 | memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); |
| 191 | }else{ |
| 192 | aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(1+(i64)db->nDb)); |
| 193 | if( aNew==0 ) return; |
| 194 | } |
| 195 | db->aDb = aNew; |
| 196 | pNew = &db->aDb[db->nDb]; |
| 197 | memset(pNew, 0, sizeof(*pNew)); |
| 198 | |
| 199 | /* Open the database file. If the btree is successfully opened, use |
| 200 | ** it to obtain the database schema. At this point the schema may |
| 201 | ** or may not be initialized. |
| 202 | */ |
| 203 | flags = db->openFlags; |
| 204 | rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr); |
| 205 | if( rc!=SQLITE_OK ){ |
| 206 | if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); |
| 207 | sqlite3_result_error(context, zErr, -1); |
| 208 | sqlite3_free(zErr); |
| 209 | return; |
| 210 | } |
| 211 | if( (db->flags & SQLITE_AttachWrite)==0 ){ |
| 212 | flags &= ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE); |
| 213 | flags |= SQLITE_OPEN_READONLY; |
| 214 | }else if( (db->flags & SQLITE_AttachCreate)==0 ){ |
| 215 | flags &= ~SQLITE_OPEN_CREATE; |
| 216 | } |
| 217 | assert( pVfs ); |
| 218 | flags |= SQLITE_OPEN_MAIN_DB; |
| 219 | rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags); |
| 220 | db->nDb++; |
| 221 | pNew->zDbSName = sqlite3DbStrDup(db, zName); |
| 222 | } |
| 223 | db->noSharedCache = 0; |
| 224 | if( rc==SQLITE_CONSTRAINT ){ |
| 225 | rc = SQLITE_ERROR; |
| 226 | zErrDyn = sqlite3MPrintf(db, "database is already attached"); |
| 227 | }else if( rc==SQLITE_OK ){ |
| 228 | Pager *pPager; |
| 229 | pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt); |
| 230 | if( !pNew->pSchema ){ |
| 231 | rc = SQLITE_NOMEM_BKPT; |
| 232 | }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){ |
| 233 | zErrDyn = sqlite3MPrintf(db, |
| 234 | "attached databases must use the same text encoding as main database"); |
| 235 | rc = SQLITE_ERROR; |
| 236 | } |
| 237 | sqlite3BtreeEnter(pNew->pBt); |
| 238 | pPager = sqlite3BtreePager(pNew->pBt); |
| 239 | sqlite3PagerLockingMode(pPager, db->dfltLockMode); |
| 240 | sqlite3BtreeSecureDelete(pNew->pBt, |
| 241 | sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) ); |
| 242 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 243 | sqlite3BtreeSetPagerFlags(pNew->pBt, |
| 244 | PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK)); |
| 245 | #endif |
| 246 | sqlite3BtreeLeave(pNew->pBt); |
| 247 | } |
| 248 | pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; |
| 249 | if( rc==SQLITE_OK && pNew->zDbSName==0 ){ |
| 250 | rc = SQLITE_NOMEM_BKPT; |
| 251 | } |
| 252 | sqlite3_free_filename( zPath ); |
| 253 | |
| 254 | /* If the file was opened successfully, read the schema for the new database. |
| 255 | ** If this fails, or if opening the file failed, then close the file and |
| 256 | ** remove the entry from the db->aDb[] array. i.e. put everything back the |
| 257 | ** way we found it. |
| 258 | */ |
| 259 | if( rc==SQLITE_OK ){ |
| 260 | sqlite3BtreeEnterAll(db); |
| 261 | db->init.iDb = 0; |
| 262 | db->mDbFlags &= ~(DBFLAG_SchemaKnownOk); |
| 263 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 264 | if( db->setlkFlags & SQLITE_SETLK_BLOCK_ON_CONNECT ){ |
| 265 | int val = 1; |
| 266 | sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pNew->pBt)); |
| 267 | sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, &val); |
| 268 | } |
| 269 | #endif |
| 270 | if( !REOPEN_AS_MEMDB(db) ){ |
| 271 | rc = sqlite3Init(db, &zErrDyn); |
| 272 | } |
| 273 | sqlite3BtreeLeaveAll(db); |
| 274 | assert( zErrDyn==0 || rc!=SQLITE_OK ); |
| 275 | } |
| 276 | if( rc ){ |
| 277 | if( ALWAYS(!REOPEN_AS_MEMDB(db)) ){ |
| 278 | int iDb = db->nDb - 1; |
| 279 | assert( iDb>=2 ); |
| 280 | if( db->aDb[iDb].pBt ){ |
| 281 | sqlite3BtreeClose(db->aDb[iDb].pBt); |
| 282 | db->aDb[iDb].pBt = 0; |
| 283 | db->aDb[iDb].pSchema = 0; |
| 284 | } |
| 285 | sqlite3ResetAllSchemasOfConnection(db); |
| 286 | db->nDb = iDb; |
| 287 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
| 288 | sqlite3OomFault(db); |
| 289 | sqlite3DbFree(db, zErrDyn); |
| 290 | zErrDyn = sqlite3MPrintf(db, "out of memory"); |
| 291 | }else if( zErrDyn==0 ){ |
| 292 | zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile); |
| 293 | } |
| 294 | } |
| 295 | goto attach_error; |
| 296 | } |
| 297 | |
| 298 | return; |
| 299 | |
| 300 | attach_error: |
| 301 | /* Return an error if we get here */ |
| 302 | if( zErrDyn ){ |
| 303 | sqlite3_result_error(context, zErrDyn, -1); |
| 304 | sqlite3DbFree(db, zErrDyn); |
| 305 | } |
| 306 | if( rc ) sqlite3_result_error_code(context, rc); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | ** An SQL user-function registered to do the work of an DETACH statement. The |
| 311 | ** three arguments to the function come directly from a detach statement: |
| 312 | ** |
| 313 | ** DETACH DATABASE x |
| 314 | ** |
| 315 | ** SELECT sqlite_detach(x) |
| 316 | */ |
| 317 | static void detachFunc( |
| 318 | sqlite3_context *context, |
| 319 | int NotUsed, |
| 320 | sqlite3_value **argv |
| 321 | ){ |
| 322 | const char *zName = (const char *)sqlite3_value_text(argv[0]); |
| 323 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 324 | int i; |
| 325 | Db *pDb = 0; |
| 326 | char zErr[128]; |
| 327 | |
| 328 | UNUSED_PARAMETER(NotUsed); |
| 329 | |
| 330 | if( zName==0 ) zName = ""; |
| 331 | for(i=0; i<db->nDb; i++){ |
| 332 | pDb = &db->aDb[i]; |
| 333 | if( pDb->pBt==0 ) continue; |
| 334 | if( sqlite3DbIsNamed(db, i, zName) ) break; |
| 335 | } |
| 336 | |
| 337 | if( i>=db->nDb ){ |
| 338 | sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); |
| 339 | goto detach_error; |
| 340 | } |
| 341 | if( i<2 ){ |
| 342 | sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); |
| 343 | goto detach_error; |
| 344 | } |
| 345 | if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE |
| 346 | || sqlite3BtreeIsInBackup(pDb->pBt) |
| 347 | ){ |
| 348 | sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); |
| 349 | goto detach_error; |
| 350 | } |
| 351 | |
| 352 | detachTempTriggers(db, pDb->pSchema); |
| 353 | sqlite3BtreeClose(pDb->pBt); |
| 354 | pDb->pBt = 0; |
| 355 | pDb->pSchema = 0; |
| 356 | sqlite3CollapseDatabaseArray(db); |
| 357 | return; |
| 358 | |
| 359 | detach_error: |
| 360 | sqlite3_result_error(context, zErr, -1); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | ** This procedure generates VDBE code for a single invocation of either the |
| 365 | ** sqlite_detach() or sqlite_attach() SQL user functions. |
| 366 | */ |
| 367 | static void codeAttach( |
| 368 | Parse *pParse, /* The parser context */ |
| 369 | int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ |
| 370 | FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */ |
| 371 | Expr *pAuthArg, /* Expression to pass to authorization callback */ |
| 372 | Expr *pFilename, /* Name of database file */ |
| 373 | Expr *pDbname, /* Name of the database to use internally */ |
| 374 | Expr *pKey /* Database key for encryption extension */ |
| 375 | ){ |
| 376 | int rc; |
| 377 | NameContext sName; |
| 378 | Vdbe *v; |
| 379 | sqlite3* db = pParse->db; |
| 380 | int regArgs; |
| 381 | |
| 382 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto attach_end; |
| 383 | |
| 384 | if( pParse->nErr ) goto attach_end; |
| 385 | memset(&sName, 0, sizeof(NameContext)); |
| 386 | sName.pParse = pParse; |
| 387 | |
| 388 | if( |
| 389 | SQLITE_OK!=resolveAttachExpr(&sName, pFilename) || |
| 390 | SQLITE_OK!=resolveAttachExpr(&sName, pDbname) || |
| 391 | SQLITE_OK!=resolveAttachExpr(&sName, pKey) |
| 392 | ){ |
| 393 | goto attach_end; |
| 394 | } |
| 395 | |
| 396 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 397 | if( ALWAYS(pAuthArg) ){ |
| 398 | char *zAuthArg; |
| 399 | if( pAuthArg->op==TK_STRING ){ |
| 400 | assert( !ExprHasProperty(pAuthArg, EP_IntValue) ); |
| 401 | zAuthArg = pAuthArg->u.zToken; |
| 402 | }else{ |
| 403 | zAuthArg = 0; |
| 404 | } |
| 405 | rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); |
| 406 | if(rc!=SQLITE_OK ){ |
| 407 | goto attach_end; |
| 408 | } |
| 409 | } |
| 410 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |
| 411 | |
| 412 | |
| 413 | v = sqlite3GetVdbe(pParse); |
| 414 | regArgs = sqlite3GetTempRange(pParse, 4); |
| 415 | sqlite3ExprCode(pParse, pFilename, regArgs); |
| 416 | sqlite3ExprCode(pParse, pDbname, regArgs+1); |
| 417 | sqlite3ExprCode(pParse, pKey, regArgs+2); |
| 418 | |
| 419 | assert( v || db->mallocFailed ); |
| 420 | if( v ){ |
| 421 | sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3, |
| 422 | pFunc->nArg, pFunc, 0); |
| 423 | /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this |
| 424 | ** statement only). For DETACH, set it to false (expire all existing |
| 425 | ** statements). |
| 426 | */ |
| 427 | sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH)); |
| 428 | } |
| 429 | |
| 430 | attach_end: |
| 431 | sqlite3ExprDelete(db, pFilename); |
| 432 | sqlite3ExprDelete(db, pDbname); |
| 433 | sqlite3ExprDelete(db, pKey); |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | ** Called by the parser to compile a DETACH statement. |
| 438 | ** |
| 439 | ** DETACH pDbname |
| 440 | */ |
| 441 | void sqlite3Detach(Parse *pParse, Expr *pDbname){ |
| 442 | static const FuncDef detach_func = { |
| 443 | 1, /* nArg */ |
| 444 | SQLITE_UTF8, /* funcFlags */ |
| 445 | 0, /* pUserData */ |
| 446 | 0, /* pNext */ |
| 447 | detachFunc, /* xSFunc */ |
| 448 | 0, /* xFinalize */ |
| 449 | 0, 0, /* xValue, xInverse */ |
| 450 | "sqlite_detach", /* zName */ |
| 451 | {0} |
| 452 | }; |
| 453 | codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | ** Called by the parser to compile an ATTACH statement. |
| 458 | ** |
| 459 | ** ATTACH p AS pDbname KEY pKey |
| 460 | */ |
| 461 | void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ |
| 462 | static const FuncDef attach_func = { |
| 463 | 3, /* nArg */ |
| 464 | SQLITE_UTF8, /* funcFlags */ |
| 465 | 0, /* pUserData */ |
| 466 | 0, /* pNext */ |
| 467 | attachFunc, /* xSFunc */ |
| 468 | 0, /* xFinalize */ |
| 469 | 0, 0, /* xValue, xInverse */ |
| 470 | "sqlite_attach", /* zName */ |
| 471 | {0} |
| 472 | }; |
| 473 | codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); |
| 474 | } |
| 475 | #endif /* SQLITE_OMIT_ATTACH */ |
| 476 | |
| 477 | /* |
| 478 | ** Expression callback used by sqlite3FixAAAA() routines. |
| 479 | */ |
| 480 | static int fixExprCb(Walker *p, Expr *pExpr){ |
| 481 | DbFixer *pFix = p->u.pFix; |
| 482 | if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL); |
| 483 | if( pExpr->op==TK_VARIABLE ){ |
| 484 | if( pFix->pParse->db->init.busy ){ |
| 485 | pExpr->op = TK_NULL; |
| 486 | }else{ |
| 487 | sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType); |
| 488 | return WRC_Abort; |
| 489 | } |
| 490 | } |
| 491 | return WRC_Continue; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | ** Select callback used by sqlite3FixAAAA() routines. |
| 496 | */ |
| 497 | static int fixSelectCb(Walker *p, Select *pSelect){ |
| 498 | DbFixer *pFix = p->u.pFix; |
| 499 | int i; |
| 500 | SrcItem *pItem; |
| 501 | sqlite3 *db = pFix->pParse->db; |
| 502 | int iDb = sqlite3FindDbName(db, pFix->zDb); |
| 503 | SrcList *pList = pSelect->pSrc; |
| 504 | |
| 505 | if( NEVER(pList==0) ) return WRC_Continue; |
| 506 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
| 507 | if( pFix->bTemp==0 && pItem->fg.isSubquery==0 ){ |
| 508 | if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){ |
| 509 | if( iDb!=sqlite3FindDbName(db, pItem->u4.zDatabase) ){ |
| 510 | sqlite3ErrorMsg(pFix->pParse, |
| 511 | "%s %T cannot reference objects in database %s", |
| 512 | pFix->zType, pFix->pName, pItem->u4.zDatabase); |
| 513 | return WRC_Abort; |
| 514 | } |
| 515 | sqlite3DbFree(db, pItem->u4.zDatabase); |
| 516 | pItem->fg.notCte = 1; |
| 517 | pItem->fg.hadSchema = 1; |
| 518 | } |
| 519 | pItem->u4.pSchema = pFix->pSchema; |
| 520 | pItem->fg.fromDDL = 1; |
| 521 | pItem->fg.fixedSchema = 1; |
| 522 | } |
| 523 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) |
| 524 | if( pList->a[i].fg.isUsing==0 |
| 525 | && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn) |
| 526 | ){ |
| 527 | return WRC_Abort; |
| 528 | } |
| 529 | #endif |
| 530 | } |
| 531 | if( pSelect->pWith ){ |
| 532 | for(i=0; i<pSelect->pWith->nCte; i++){ |
| 533 | if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){ |
| 534 | return WRC_Abort; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | return WRC_Continue; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | ** Initialize a DbFixer structure. This routine must be called prior |
| 543 | ** to passing the structure to one of the sqliteFixAAAA() routines below. |
| 544 | */ |
| 545 | void sqlite3FixInit( |
| 546 | DbFixer *pFix, /* The fixer to be initialized */ |
| 547 | Parse *pParse, /* Error messages will be written here */ |
| 548 | int iDb, /* This is the database that must be used */ |
| 549 | const char *zType, /* "view", "trigger", or "index" */ |
| 550 | const Token *pName /* Name of the view, trigger, or index */ |
| 551 | ){ |
| 552 | sqlite3 *db = pParse->db; |
| 553 | assert( db->nDb>iDb ); |
| 554 | pFix->pParse = pParse; |
| 555 | pFix->zDb = db->aDb[iDb].zDbSName; |
| 556 | pFix->pSchema = db->aDb[iDb].pSchema; |
| 557 | pFix->zType = zType; |
| 558 | pFix->pName = pName; |
| 559 | pFix->bTemp = (iDb==1); |
| 560 | pFix->w.pParse = pParse; |
| 561 | pFix->w.xExprCallback = fixExprCb; |
| 562 | pFix->w.xSelectCallback = fixSelectCb; |
| 563 | pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback; |
| 564 | pFix->w.walkerDepth = 0; |
| 565 | pFix->w.eCode = 0; |
| 566 | pFix->w.u.pFix = pFix; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | ** The following set of routines walk through the parse tree and assign |
| 571 | ** a specific database to all table references where the database name |
| 572 | ** was left unspecified in the original SQL statement. The pFix structure |
| 573 | ** must have been initialized by a prior call to sqlite3FixInit(). |
| 574 | ** |
| 575 | ** These routines are used to make sure that an index, trigger, or |
| 576 | ** view in one database does not refer to objects in a different database. |
| 577 | ** (Exception: indices, triggers, and views in the TEMP database are |
| 578 | ** allowed to refer to anything.) If a reference is explicitly made |
| 579 | ** to an object in a different database, an error message is added to |
| 580 | ** pParse->zErrMsg and these routines return non-zero. If everything |
| 581 | ** checks out, these routines return 0. |
| 582 | */ |
| 583 | int sqlite3FixSrcList( |
| 584 | DbFixer *pFix, /* Context of the fixation */ |
| 585 | SrcList *pList /* The Source list to check and modify */ |
| 586 | ){ |
| 587 | int res = 0; |
| 588 | if( pList ){ |
| 589 | Select s; |
| 590 | memset(&s, 0, sizeof(s)); |
| 591 | s.pSrc = pList; |
| 592 | res = sqlite3WalkSelect(&pFix->w, &s); |
| 593 | } |
| 594 | return res; |
| 595 | } |
| 596 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) |
| 597 | int sqlite3FixSelect( |
| 598 | DbFixer *pFix, /* Context of the fixation */ |
| 599 | Select *pSelect /* The SELECT statement to be fixed to one database */ |
| 600 | ){ |
| 601 | return sqlite3WalkSelect(&pFix->w, pSelect); |
| 602 | } |
| 603 | int sqlite3FixExpr( |
| 604 | DbFixer *pFix, /* Context of the fixation */ |
| 605 | Expr *pExpr /* The expression to be fixed to one database */ |
| 606 | ){ |
| 607 | return sqlite3WalkExpr(&pFix->w, pExpr); |
| 608 | } |
| 609 | #endif |
| 610 | |
| 611 | #ifndef SQLITE_OMIT_TRIGGER |
| 612 | int sqlite3FixTriggerStep( |
| 613 | DbFixer *pFix, /* Context of the fixation */ |
| 614 | TriggerStep *pStep /* The trigger step be fixed to one database */ |
| 615 | ){ |
| 616 | while( pStep ){ |
| 617 | if( sqlite3WalkSelect(&pFix->w, pStep->pSelect) |
| 618 | || sqlite3WalkExpr(&pFix->w, pStep->pWhere) |
| 619 | || sqlite3WalkExprList(&pFix->w, pStep->pExprList) |
| 620 | || sqlite3FixSrcList(pFix, pStep->pSrc) |
| 621 | ){ |
| 622 | return 1; |
| 623 | } |
| 624 | #ifndef SQLITE_OMIT_UPSERT |
| 625 | { |
| 626 | Upsert *pUp; |
| 627 | for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){ |
| 628 | if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget) |
| 629 | || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere) |
| 630 | || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet) |
| 631 | || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere) |
| 632 | ){ |
| 633 | return 1; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | #endif |
| 638 | pStep = pStep->pNext; |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | #endif |