| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 15 | 2 ** 2001 September 15 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains C code routines that are called by the SQLite parser | 12 ** This file contains C code routines that are called by the SQLite parser |
| 13 ** when syntax rules are reduced. The routines in this file handle the | 13 ** when syntax rules are reduced. The routines in this file handle the |
| 14 ** following kinds of SQL syntax: | 14 ** following kinds of SQL syntax: |
| 15 ** | 15 ** |
| 16 ** CREATE TABLE | 16 ** CREATE TABLE |
| 17 ** DROP TABLE | 17 ** DROP TABLE |
| 18 ** CREATE INDEX | 18 ** CREATE INDEX |
| 19 ** DROP INDEX | 19 ** DROP INDEX |
| 20 ** creating ID lists | 20 ** creating ID lists |
| 21 ** BEGIN TRANSACTION | 21 ** BEGIN TRANSACTION |
| 22 ** COMMIT | 22 ** COMMIT |
| 23 ** ROLLBACK | 23 ** ROLLBACK |
| 24 */ | 24 */ |
| 25 #include "sqliteInt.h" | 25 #include "sqliteInt.h" |
| 26 | 26 |
| 27 /* | |
| 28 ** This routine is called when a new SQL statement is beginning to | |
| 29 ** be parsed. Initialize the pParse structure as needed. | |
| 30 */ | |
| 31 void sqlite3BeginParse(Parse *pParse, int explainFlag){ | |
| 32 pParse->explain = (u8)explainFlag; | |
| 33 pParse->nVar = 0; | |
| 34 } | |
| 35 | |
| 36 #ifndef SQLITE_OMIT_SHARED_CACHE | 27 #ifndef SQLITE_OMIT_SHARED_CACHE |
| 37 /* | 28 /* |
| 38 ** The TableLock structure is only used by the sqlite3TableLock() and | 29 ** The TableLock structure is only used by the sqlite3TableLock() and |
| 39 ** codeTableLocks() functions. | 30 ** codeTableLocks() functions. |
| 40 */ | 31 */ |
| 41 struct TableLock { | 32 struct TableLock { |
| 42 int iDb; /* The database containing the table to be locked */ | 33 int iDb; /* The database containing the table to be locked */ |
| 43 int iTab; /* The root page of the table to be locked */ | 34 int iTab; /* The root page of the table to be locked */ |
| 44 u8 isWriteLock; /* True for write lock. False for a read lock */ | 35 u8 isWriteLock; /* True for write lock. False for a read lock */ |
| 45 const char *zName; /* Name of the table */ | 36 const char *zLockName; /* Name of the table */ |
| 46 }; | 37 }; |
| 47 | 38 |
| 48 /* | 39 /* |
| 49 ** Record the fact that we want to lock a table at run-time. | 40 ** Record the fact that we want to lock a table at run-time. |
| 50 ** | 41 ** |
| 51 ** The table to be locked has root page iTab and is found in database iDb. | 42 ** The table to be locked has root page iTab and is found in database iDb. |
| 52 ** A read or a write lock can be taken depending on isWritelock. | 43 ** A read or a write lock can be taken depending on isWritelock. |
| 53 ** | 44 ** |
| 54 ** This routine just records the fact that the lock is desired. The | 45 ** This routine just records the fact that the lock is desired. The |
| 55 ** code to make the lock occur is generated by a later call to | 46 ** code to make the lock occur is generated by a later call to |
| 56 ** codeTableLocks() which occurs during sqlite3FinishCoding(). | 47 ** codeTableLocks() which occurs during sqlite3FinishCoding(). |
| 57 */ | 48 */ |
| 58 void sqlite3TableLock( | 49 void sqlite3TableLock( |
| 59 Parse *pParse, /* Parsing context */ | 50 Parse *pParse, /* Parsing context */ |
| 60 int iDb, /* Index of the database containing the table to lock */ | 51 int iDb, /* Index of the database containing the table to lock */ |
| 61 int iTab, /* Root page number of the table to be locked */ | 52 int iTab, /* Root page number of the table to be locked */ |
| 62 u8 isWriteLock, /* True for a write lock */ | 53 u8 isWriteLock, /* True for a write lock */ |
| 63 const char *zName /* Name of the table to be locked */ | 54 const char *zName /* Name of the table to be locked */ |
| 64 ){ | 55 ){ |
| 65 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 56 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 66 int i; | 57 int i; |
| 67 int nBytes; | 58 int nBytes; |
| 68 TableLock *p; | 59 TableLock *p; |
| 69 assert( iDb>=0 ); | 60 assert( iDb>=0 ); |
| 70 | 61 |
| 62 if( iDb==1 ) return; |
| 63 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; |
| 71 for(i=0; i<pToplevel->nTableLock; i++){ | 64 for(i=0; i<pToplevel->nTableLock; i++){ |
| 72 p = &pToplevel->aTableLock[i]; | 65 p = &pToplevel->aTableLock[i]; |
| 73 if( p->iDb==iDb && p->iTab==iTab ){ | 66 if( p->iDb==iDb && p->iTab==iTab ){ |
| 74 p->isWriteLock = (p->isWriteLock || isWriteLock); | 67 p->isWriteLock = (p->isWriteLock || isWriteLock); |
| 75 return; | 68 return; |
| 76 } | 69 } |
| 77 } | 70 } |
| 78 | 71 |
| 79 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); | 72 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); |
| 80 pToplevel->aTableLock = | 73 pToplevel->aTableLock = |
| 81 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); | 74 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); |
| 82 if( pToplevel->aTableLock ){ | 75 if( pToplevel->aTableLock ){ |
| 83 p = &pToplevel->aTableLock[pToplevel->nTableLock++]; | 76 p = &pToplevel->aTableLock[pToplevel->nTableLock++]; |
| 84 p->iDb = iDb; | 77 p->iDb = iDb; |
| 85 p->iTab = iTab; | 78 p->iTab = iTab; |
| 86 p->isWriteLock = isWriteLock; | 79 p->isWriteLock = isWriteLock; |
| 87 p->zName = zName; | 80 p->zLockName = zName; |
| 88 }else{ | 81 }else{ |
| 89 pToplevel->nTableLock = 0; | 82 pToplevel->nTableLock = 0; |
| 90 pToplevel->db->mallocFailed = 1; | 83 sqlite3OomFault(pToplevel->db); |
| 91 } | 84 } |
| 92 } | 85 } |
| 93 | 86 |
| 94 /* | 87 /* |
| 95 ** Code an OP_TableLock instruction for each table locked by the | 88 ** Code an OP_TableLock instruction for each table locked by the |
| 96 ** statement (configured by calls to sqlite3TableLock()). | 89 ** statement (configured by calls to sqlite3TableLock()). |
| 97 */ | 90 */ |
| 98 static void codeTableLocks(Parse *pParse){ | 91 static void codeTableLocks(Parse *pParse){ |
| 99 int i; | 92 int i; |
| 100 Vdbe *pVdbe; | 93 Vdbe *pVdbe; |
| 101 | 94 |
| 102 pVdbe = sqlite3GetVdbe(pParse); | 95 pVdbe = sqlite3GetVdbe(pParse); |
| 103 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ | 96 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ |
| 104 | 97 |
| 105 for(i=0; i<pParse->nTableLock; i++){ | 98 for(i=0; i<pParse->nTableLock; i++){ |
| 106 TableLock *p = &pParse->aTableLock[i]; | 99 TableLock *p = &pParse->aTableLock[i]; |
| 107 int p1 = p->iDb; | 100 int p1 = p->iDb; |
| 108 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, | 101 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, |
| 109 p->zName, P4_STATIC); | 102 p->zLockName, P4_STATIC); |
| 110 } | 103 } |
| 111 } | 104 } |
| 112 #else | 105 #else |
| 113 #define codeTableLocks(x) | 106 #define codeTableLocks(x) |
| 114 #endif | 107 #endif |
| 115 | 108 |
| 116 /* | 109 /* |
| 117 ** Return TRUE if the given yDbMask object is empty - if it contains no | 110 ** Return TRUE if the given yDbMask object is empty - if it contains no |
| 118 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() | 111 ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() |
| 119 ** macros when SQLITE_MAX_ATTACHED is greater than 30. | 112 ** macros when SQLITE_MAX_ATTACHED is greater than 30. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 148 return; | 141 return; |
| 149 } | 142 } |
| 150 | 143 |
| 151 /* Begin by generating some termination code at the end of the | 144 /* Begin by generating some termination code at the end of the |
| 152 ** vdbe program | 145 ** vdbe program |
| 153 */ | 146 */ |
| 154 v = sqlite3GetVdbe(pParse); | 147 v = sqlite3GetVdbe(pParse); |
| 155 assert( !pParse->isMultiWrite | 148 assert( !pParse->isMultiWrite |
| 156 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); | 149 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
| 157 if( v ){ | 150 if( v ){ |
| 158 while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){} | |
| 159 sqlite3VdbeAddOp0(v, OP_Halt); | 151 sqlite3VdbeAddOp0(v, OP_Halt); |
| 160 | 152 |
| 161 #if SQLITE_USER_AUTHENTICATION | 153 #if SQLITE_USER_AUTHENTICATION |
| 162 if( pParse->nTableLock>0 && db->init.busy==0 ){ | 154 if( pParse->nTableLock>0 && db->init.busy==0 ){ |
| 163 sqlite3UserAuthInit(db); | 155 sqlite3UserAuthInit(db); |
| 164 if( db->auth.authLevel<UAUTH_User ){ | 156 if( db->auth.authLevel<UAUTH_User ){ |
| 157 sqlite3ErrorMsg(pParse, "user not authenticated"); |
| 165 pParse->rc = SQLITE_AUTH_USER; | 158 pParse->rc = SQLITE_AUTH_USER; |
| 166 sqlite3ErrorMsg(pParse, "user not authenticated"); | |
| 167 return; | 159 return; |
| 168 } | 160 } |
| 169 } | 161 } |
| 170 #endif | 162 #endif |
| 171 | 163 |
| 172 /* The cookie mask contains one bit for each database file open. | 164 /* The cookie mask contains one bit for each database file open. |
| 173 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are | 165 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 174 ** set for each database that is used. Generate code to start a | 166 ** set for each database that is used. Generate code to start a |
| 175 ** transaction on each used database and to verify the schema cookie | 167 ** transaction on each used database and to verify the schema cookie |
| 176 ** on each used database. | 168 ** on each used database. |
| 177 */ | 169 */ |
| 178 if( db->mallocFailed==0 | 170 if( db->mallocFailed==0 |
| 179 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) | 171 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) |
| 180 ){ | 172 ){ |
| 181 int iDb, i; | 173 int iDb, i; |
| 182 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); | 174 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 183 sqlite3VdbeJumpHere(v, 0); | 175 sqlite3VdbeJumpHere(v, 0); |
| 184 for(iDb=0; iDb<db->nDb; iDb++){ | 176 for(iDb=0; iDb<db->nDb; iDb++){ |
| 177 Schema *pSchema; |
| 185 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; | 178 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
| 186 sqlite3VdbeUsesBtree(v, iDb); | 179 sqlite3VdbeUsesBtree(v, iDb); |
| 180 pSchema = db->aDb[iDb].pSchema; |
| 187 sqlite3VdbeAddOp4Int(v, | 181 sqlite3VdbeAddOp4Int(v, |
| 188 OP_Transaction, /* Opcode */ | 182 OP_Transaction, /* Opcode */ |
| 189 iDb, /* P1 */ | 183 iDb, /* P1 */ |
| 190 DbMaskTest(pParse->writeMask,iDb), /* P2 */ | 184 DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
| 191 pParse->cookieValue[iDb], /* P3 */ | 185 pSchema->schema_cookie, /* P3 */ |
| 192 db->aDb[iDb].pSchema->iGeneration /* P4 */ | 186 pSchema->iGeneration /* P4 */ |
| 193 ); | 187 ); |
| 194 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); | 188 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 195 VdbeComment((v, | 189 VdbeComment((v, |
| 196 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); | 190 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
| 197 } | 191 } |
| 198 #ifndef SQLITE_OMIT_VIRTUALTABLE | 192 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 199 for(i=0; i<pParse->nVtabLock; i++){ | 193 for(i=0; i<pParse->nVtabLock; i++){ |
| 200 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); | 194 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 201 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); | 195 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 202 } | 196 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 230 | 224 |
| 231 /* Get the VDBE program ready for execution | 225 /* Get the VDBE program ready for execution |
| 232 */ | 226 */ |
| 233 if( v && pParse->nErr==0 && !db->mallocFailed ){ | 227 if( v && pParse->nErr==0 && !db->mallocFailed ){ |
| 234 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ | 228 assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ |
| 235 /* A minimum of one cursor is required if autoincrement is used | 229 /* A minimum of one cursor is required if autoincrement is used |
| 236 * See ticket [a696379c1f08866] */ | 230 * See ticket [a696379c1f08866] */ |
| 237 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; | 231 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; |
| 238 sqlite3VdbeMakeReady(v, pParse); | 232 sqlite3VdbeMakeReady(v, pParse); |
| 239 pParse->rc = SQLITE_DONE; | 233 pParse->rc = SQLITE_DONE; |
| 240 pParse->colNamesSet = 0; | |
| 241 }else{ | 234 }else{ |
| 242 pParse->rc = SQLITE_ERROR; | 235 pParse->rc = SQLITE_ERROR; |
| 243 } | 236 } |
| 244 pParse->nTab = 0; | |
| 245 pParse->nMem = 0; | |
| 246 pParse->nSet = 0; | |
| 247 pParse->nVar = 0; | |
| 248 DbMaskZero(pParse->cookieMask); | |
| 249 } | 237 } |
| 250 | 238 |
| 251 /* | 239 /* |
| 252 ** Run the parser and code generator recursively in order to generate | 240 ** Run the parser and code generator recursively in order to generate |
| 253 ** code for the SQL statement given onto the end of the pParse context | 241 ** code for the SQL statement given onto the end of the pParse context |
| 254 ** currently under construction. When the parser is run recursively | 242 ** currently under construction. When the parser is run recursively |
| 255 ** this way, the final OP_Halt is not appended and other initialization | 243 ** this way, the final OP_Halt is not appended and other initialization |
| 256 ** and finalization steps are omitted because those are handling by the | 244 ** and finalization steps are omitted because those are handling by the |
| 257 ** outermost parser. | 245 ** outermost parser. |
| 258 ** | 246 ** |
| 259 ** Not everything is nestable. This facility is designed to permit | 247 ** Not everything is nestable. This facility is designed to permit |
| 260 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use | 248 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use |
| 261 ** care if you decide to try to use this routine for some other purposes. | 249 ** care if you decide to try to use this routine for some other purposes. |
| 262 */ | 250 */ |
| 263 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ | 251 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 264 va_list ap; | 252 va_list ap; |
| 265 char *zSql; | 253 char *zSql; |
| 266 char *zErrMsg = 0; | 254 char *zErrMsg = 0; |
| 267 sqlite3 *db = pParse->db; | 255 sqlite3 *db = pParse->db; |
| 268 # define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar)) | 256 char saveBuf[PARSE_TAIL_SZ]; |
| 269 char saveBuf[SAVE_SZ]; | |
| 270 | 257 |
| 271 if( pParse->nErr ) return; | 258 if( pParse->nErr ) return; |
| 272 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ | 259 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
| 273 va_start(ap, zFormat); | 260 va_start(ap, zFormat); |
| 274 zSql = sqlite3VMPrintf(db, zFormat, ap); | 261 zSql = sqlite3VMPrintf(db, zFormat, ap); |
| 275 va_end(ap); | 262 va_end(ap); |
| 276 if( zSql==0 ){ | 263 if( zSql==0 ){ |
| 277 return; /* A malloc must have failed */ | 264 return; /* A malloc must have failed */ |
| 278 } | 265 } |
| 279 pParse->nested++; | 266 pParse->nested++; |
| 280 memcpy(saveBuf, &pParse->nVar, SAVE_SZ); | 267 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 281 memset(&pParse->nVar, 0, SAVE_SZ); | 268 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 282 sqlite3RunParser(pParse, zSql, &zErrMsg); | 269 sqlite3RunParser(pParse, zSql, &zErrMsg); |
| 283 sqlite3DbFree(db, zErrMsg); | 270 sqlite3DbFree(db, zErrMsg); |
| 284 sqlite3DbFree(db, zSql); | 271 sqlite3DbFree(db, zSql); |
| 285 memcpy(&pParse->nVar, saveBuf, SAVE_SZ); | 272 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
| 286 pParse->nested--; | 273 pParse->nested--; |
| 287 } | 274 } |
| 288 | 275 |
| 289 #if SQLITE_USER_AUTHENTICATION | 276 #if SQLITE_USER_AUTHENTICATION |
| 290 /* | 277 /* |
| 291 ** Return TRUE if zTable is the name of the system table that stores the | 278 ** Return TRUE if zTable is the name of the system table that stores the |
| 292 ** list of users and their access credentials. | 279 ** list of users and their access credentials. |
| 293 */ | 280 */ |
| 294 int sqlite3UserAuthTable(const char *zTable){ | 281 int sqlite3UserAuthTable(const char *zTable){ |
| 295 return sqlite3_stricmp(zTable, "sqlite_user")==0; | 282 return sqlite3_stricmp(zTable, "sqlite_user")==0; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 314 | 301 |
| 315 /* All mutexes are required for schema access. Make sure we hold them. */ | 302 /* All mutexes are required for schema access. Make sure we hold them. */ |
| 316 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); | 303 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 317 #if SQLITE_USER_AUTHENTICATION | 304 #if SQLITE_USER_AUTHENTICATION |
| 318 /* Only the admin user is allowed to know that the sqlite_user table | 305 /* Only the admin user is allowed to know that the sqlite_user table |
| 319 ** exists */ | 306 ** exists */ |
| 320 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ | 307 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 321 return 0; | 308 return 0; |
| 322 } | 309 } |
| 323 #endif | 310 #endif |
| 324 for(i=OMIT_TEMPDB; i<db->nDb; i++){ | 311 while(1){ |
| 325 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ | 312 for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
| 326 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue; | 313 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 327 assert( sqlite3SchemaMutexHeld(db, j, 0) ); | 314 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){ |
| 328 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); | 315 assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
| 329 if( p ) break; | 316 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName); |
| 317 if( p ) return p; |
| 318 } |
| 319 } |
| 320 /* Not found. If the name we were looking for was temp.sqlite_master |
| 321 ** then change the name to sqlite_temp_master and try again. */ |
| 322 if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break; |
| 323 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break; |
| 324 zName = TEMP_MASTER_NAME; |
| 330 } | 325 } |
| 331 return p; | 326 return 0; |
| 332 } | 327 } |
| 333 | 328 |
| 334 /* | 329 /* |
| 335 ** Locate the in-memory structure that describes a particular database | 330 ** Locate the in-memory structure that describes a particular database |
| 336 ** table given the name of that table and (optionally) the name of the | 331 ** table given the name of that table and (optionally) the name of the |
| 337 ** database containing the table. Return NULL if not found. Also leave an | 332 ** database containing the table. Return NULL if not found. Also leave an |
| 338 ** error message in pParse->zErrMsg. | 333 ** error message in pParse->zErrMsg. |
| 339 ** | 334 ** |
| 340 ** The difference between this routine and sqlite3FindTable() is that this | 335 ** The difference between this routine and sqlite3FindTable() is that this |
| 341 ** routine leaves an error message in pParse->zErrMsg where | 336 ** routine leaves an error message in pParse->zErrMsg where |
| 342 ** sqlite3FindTable() does not. | 337 ** sqlite3FindTable() does not. |
| 343 */ | 338 */ |
| 344 Table *sqlite3LocateTable( | 339 Table *sqlite3LocateTable( |
| 345 Parse *pParse, /* context in which to report errors */ | 340 Parse *pParse, /* context in which to report errors */ |
| 346 int isView, /* True if looking for a VIEW rather than a TABLE */ | 341 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ |
| 347 const char *zName, /* Name of the table we are looking for */ | 342 const char *zName, /* Name of the table we are looking for */ |
| 348 const char *zDbase /* Name of the database. Might be NULL */ | 343 const char *zDbase /* Name of the database. Might be NULL */ |
| 349 ){ | 344 ){ |
| 350 Table *p; | 345 Table *p; |
| 351 | 346 |
| 352 /* Read the database schema. If an error occurs, leave an error message | 347 /* Read the database schema. If an error occurs, leave an error message |
| 353 ** and code in pParse and return NULL. */ | 348 ** and code in pParse and return NULL. */ |
| 354 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 349 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 355 return 0; | 350 return 0; |
| 356 } | 351 } |
| 357 | 352 |
| 358 p = sqlite3FindTable(pParse->db, zName, zDbase); | 353 p = sqlite3FindTable(pParse->db, zName, zDbase); |
| 359 if( p==0 ){ | 354 if( p==0 ){ |
| 360 const char *zMsg = isView ? "no such view" : "no such table"; | 355 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| 361 #ifndef SQLITE_OMIT_VIRTUALTABLE | 356 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 362 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){ | 357 if( sqlite3FindDbName(pParse->db, zDbase)<1 ){ |
| 363 /* If zName is the not the name of a table in the schema created using | 358 /* If zName is the not the name of a table in the schema created using |
| 364 ** CREATE, then check to see if it is the name of an virtual table that | 359 ** CREATE, then check to see if it is the name of an virtual table that |
| 365 ** can be an eponymous virtual table. */ | 360 ** can be an eponymous virtual table. */ |
| 366 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName); | 361 Module *pMod = (Module*)sqlite3HashFind(&pParse->db->aModule, zName); |
| 362 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 363 pMod = sqlite3PragmaVtabRegister(pParse->db, zName); |
| 364 } |
| 367 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ | 365 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
| 368 return pMod->pEpoTab; | 366 return pMod->pEpoTab; |
| 369 } | 367 } |
| 370 } | 368 } |
| 371 #endif | 369 #endif |
| 372 if( zDbase ){ | 370 if( (flags & LOCATE_NOERR)==0 ){ |
| 373 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); | 371 if( zDbase ){ |
| 374 }else{ | 372 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
| 375 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); | 373 }else{ |
| 374 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
| 375 } |
| 376 pParse->checkSchema = 1; |
| 376 } | 377 } |
| 377 pParse->checkSchema = 1; | |
| 378 } | 378 } |
| 379 | 379 |
| 380 return p; | 380 return p; |
| 381 } | 381 } |
| 382 | 382 |
| 383 /* | 383 /* |
| 384 ** Locate the table identified by *p. | 384 ** Locate the table identified by *p. |
| 385 ** | 385 ** |
| 386 ** This is a wrapper around sqlite3LocateTable(). The difference between | 386 ** This is a wrapper around sqlite3LocateTable(). The difference between |
| 387 ** sqlite3LocateTable() and this function is that this function restricts | 387 ** sqlite3LocateTable() and this function is that this function restricts |
| 388 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be | 388 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
| 389 ** non-NULL if it is part of a view or trigger program definition. See | 389 ** non-NULL if it is part of a view or trigger program definition. See |
| 390 ** sqlite3FixSrcList() for details. | 390 ** sqlite3FixSrcList() for details. |
| 391 */ | 391 */ |
| 392 Table *sqlite3LocateTableItem( | 392 Table *sqlite3LocateTableItem( |
| 393 Parse *pParse, | 393 Parse *pParse, |
| 394 int isView, | 394 u32 flags, |
| 395 struct SrcList_item *p | 395 struct SrcList_item *p |
| 396 ){ | 396 ){ |
| 397 const char *zDb; | 397 const char *zDb; |
| 398 assert( p->pSchema==0 || p->zDatabase==0 ); | 398 assert( p->pSchema==0 || p->zDatabase==0 ); |
| 399 if( p->pSchema ){ | 399 if( p->pSchema ){ |
| 400 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); | 400 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
| 401 zDb = pParse->db->aDb[iDb].zName; | 401 zDb = pParse->db->aDb[iDb].zDbSName; |
| 402 }else{ | 402 }else{ |
| 403 zDb = p->zDatabase; | 403 zDb = p->zDatabase; |
| 404 } | 404 } |
| 405 return sqlite3LocateTable(pParse, isView, p->zName, zDb); | 405 return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
| 406 } | 406 } |
| 407 | 407 |
| 408 /* | 408 /* |
| 409 ** Locate the in-memory structure that describes | 409 ** Locate the in-memory structure that describes |
| 410 ** a particular index given the name of that index | 410 ** a particular index given the name of that index |
| 411 ** and the name of the database that contains the index. | 411 ** and the name of the database that contains the index. |
| 412 ** Return NULL if not found. | 412 ** Return NULL if not found. |
| 413 ** | 413 ** |
| 414 ** If zDatabase is 0, all databases are searched for the | 414 ** If zDatabase is 0, all databases are searched for the |
| 415 ** table and the first matching index is returned. (No checking | 415 ** table and the first matching index is returned. (No checking |
| 416 ** for duplicate index names is done.) The search order is | 416 ** for duplicate index names is done.) The search order is |
| 417 ** TEMP first, then MAIN, then any auxiliary databases added | 417 ** TEMP first, then MAIN, then any auxiliary databases added |
| 418 ** using the ATTACH command. | 418 ** using the ATTACH command. |
| 419 */ | 419 */ |
| 420 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ | 420 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
| 421 Index *p = 0; | 421 Index *p = 0; |
| 422 int i; | 422 int i; |
| 423 /* All mutexes are required for schema access. Make sure we hold them. */ | 423 /* All mutexes are required for schema access. Make sure we hold them. */ |
| 424 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); | 424 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
| 425 for(i=OMIT_TEMPDB; i<db->nDb; i++){ | 425 for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
| 426 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ | 426 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
| 427 Schema *pSchema = db->aDb[j].pSchema; | 427 Schema *pSchema = db->aDb[j].pSchema; |
| 428 assert( pSchema ); | 428 assert( pSchema ); |
| 429 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; | 429 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue; |
| 430 assert( sqlite3SchemaMutexHeld(db, j, 0) ); | 430 assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
| 431 p = sqlite3HashFind(&pSchema->idxHash, zName); | 431 p = sqlite3HashFind(&pSchema->idxHash, zName); |
| 432 if( p ) break; | 432 if( p ) break; |
| 433 } | 433 } |
| 434 return p; | 434 return p; |
| 435 } | 435 } |
| 436 | 436 |
| 437 /* | 437 /* |
| 438 ** Reclaim the memory used by an index | 438 ** Reclaim the memory used by an index |
| 439 */ | 439 */ |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 ** db->aDb[] structure to a smaller size, if possible. | 488 ** db->aDb[] structure to a smaller size, if possible. |
| 489 ** | 489 ** |
| 490 ** Entry 0 (the "main" database) and entry 1 (the "temp" database) | 490 ** Entry 0 (the "main" database) and entry 1 (the "temp" database) |
| 491 ** are never candidates for being collapsed. | 491 ** are never candidates for being collapsed. |
| 492 */ | 492 */ |
| 493 void sqlite3CollapseDatabaseArray(sqlite3 *db){ | 493 void sqlite3CollapseDatabaseArray(sqlite3 *db){ |
| 494 int i, j; | 494 int i, j; |
| 495 for(i=j=2; i<db->nDb; i++){ | 495 for(i=j=2; i<db->nDb; i++){ |
| 496 struct Db *pDb = &db->aDb[i]; | 496 struct Db *pDb = &db->aDb[i]; |
| 497 if( pDb->pBt==0 ){ | 497 if( pDb->pBt==0 ){ |
| 498 sqlite3DbFree(db, pDb->zName); | 498 sqlite3DbFree(db, pDb->zDbSName); |
| 499 pDb->zName = 0; | 499 pDb->zDbSName = 0; |
| 500 continue; | 500 continue; |
| 501 } | 501 } |
| 502 if( j<i ){ | 502 if( j<i ){ |
| 503 db->aDb[j] = db->aDb[i]; | 503 db->aDb[j] = db->aDb[i]; |
| 504 } | 504 } |
| 505 j++; | 505 j++; |
| 506 } | 506 } |
| 507 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j])); | |
| 508 db->nDb = j; | 507 db->nDb = j; |
| 509 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ | 508 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 510 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); | 509 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
| 511 sqlite3DbFree(db, db->aDb); | 510 sqlite3DbFree(db, db->aDb); |
| 512 db->aDb = db->aDbStatic; | 511 db->aDb = db->aDbStatic; |
| 513 } | 512 } |
| 514 } | 513 } |
| 515 | 514 |
| 516 /* | 515 /* |
| 517 ** Reset the schema for the database at index iDb. Also reset the | 516 ** Reset the schema for the database at index iDb. Also reset the |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 ** Table.aCol[] array). | 569 ** Table.aCol[] array). |
| 571 */ | 570 */ |
| 572 void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ | 571 void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
| 573 int i; | 572 int i; |
| 574 Column *pCol; | 573 Column *pCol; |
| 575 assert( pTable!=0 ); | 574 assert( pTable!=0 ); |
| 576 if( (pCol = pTable->aCol)!=0 ){ | 575 if( (pCol = pTable->aCol)!=0 ){ |
| 577 for(i=0; i<pTable->nCol; i++, pCol++){ | 576 for(i=0; i<pTable->nCol; i++, pCol++){ |
| 578 sqlite3DbFree(db, pCol->zName); | 577 sqlite3DbFree(db, pCol->zName); |
| 579 sqlite3ExprDelete(db, pCol->pDflt); | 578 sqlite3ExprDelete(db, pCol->pDflt); |
| 580 sqlite3DbFree(db, pCol->zDflt); | |
| 581 sqlite3DbFree(db, pCol->zType); | |
| 582 sqlite3DbFree(db, pCol->zColl); | 579 sqlite3DbFree(db, pCol->zColl); |
| 583 } | 580 } |
| 584 sqlite3DbFree(db, pTable->aCol); | 581 sqlite3DbFree(db, pTable->aCol); |
| 585 } | 582 } |
| 586 } | 583 } |
| 587 | 584 |
| 588 /* | 585 /* |
| 589 ** Remove the memory data structures associated with the given | 586 ** Remove the memory data structures associated with the given |
| 590 ** Table. No changes are made to disk by this routine. | 587 ** Table. No changes are made to disk by this routine. |
| 591 ** | 588 ** |
| 592 ** This routine just deletes the data structure. It does not unlink | 589 ** This routine just deletes the data structure. It does not unlink |
| 593 ** the table data structure from the hash table. But it does destroy | 590 ** the table data structure from the hash table. But it does destroy |
| 594 ** memory structures of the indices and foreign keys associated with | 591 ** memory structures of the indices and foreign keys associated with |
| 595 ** the table. | 592 ** the table. |
| 596 ** | 593 ** |
| 597 ** The db parameter is optional. It is needed if the Table object | 594 ** The db parameter is optional. It is needed if the Table object |
| 598 ** contains lookaside memory. (Table objects in the schema do not use | 595 ** contains lookaside memory. (Table objects in the schema do not use |
| 599 ** lookaside memory, but some ephemeral Table objects do.) Or the | 596 ** lookaside memory, but some ephemeral Table objects do.) Or the |
| 600 ** db parameter can be used with db->pnBytesFreed to measure the memory | 597 ** db parameter can be used with db->pnBytesFreed to measure the memory |
| 601 ** used by the Table object. | 598 ** used by the Table object. |
| 602 */ | 599 */ |
| 603 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ | 600 static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ |
| 604 Index *pIndex, *pNext; | 601 Index *pIndex, *pNext; |
| 605 TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */ | 602 TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */ |
| 606 | 603 |
| 607 assert( !pTable || pTable->nRef>0 ); | |
| 608 | |
| 609 /* Do not delete the table until the reference count reaches zero. */ | |
| 610 if( !pTable ) return; | |
| 611 if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return; | |
| 612 | |
| 613 /* Record the number of outstanding lookaside allocations in schema Tables | 604 /* Record the number of outstanding lookaside allocations in schema Tables |
| 614 ** prior to doing any free() operations. Since schema Tables do not use | 605 ** prior to doing any free() operations. Since schema Tables do not use |
| 615 ** lookaside, this number should not change. */ | 606 ** lookaside, this number should not change. */ |
| 616 TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ? | 607 TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ? |
| 617 db->lookaside.nOut : 0 ); | 608 db->lookaside.nOut : 0 ); |
| 618 | 609 |
| 619 /* Delete all indices associated with this table. */ | 610 /* Delete all indices associated with this table. */ |
| 620 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ | 611 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 621 pNext = pIndex->pNext; | 612 pNext = pIndex->pNext; |
| 622 assert( pIndex->pSchema==pTable->pSchema ); | 613 assert( pIndex->pSchema==pTable->pSchema |
| 623 if( !db || db->pnBytesFreed==0 ){ | 614 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
| 615 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ |
| 624 char *zName = pIndex->zName; | 616 char *zName = pIndex->zName; |
| 625 TESTONLY ( Index *pOld = ) sqlite3HashInsert( | 617 TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
| 626 &pIndex->pSchema->idxHash, zName, 0 | 618 &pIndex->pSchema->idxHash, zName, 0 |
| 627 ); | 619 ); |
| 628 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); | 620 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
| 629 assert( pOld==pIndex || pOld==0 ); | 621 assert( pOld==pIndex || pOld==0 ); |
| 630 } | 622 } |
| 631 freeIndex(db, pIndex); | 623 freeIndex(db, pIndex); |
| 632 } | 624 } |
| 633 | 625 |
| 634 /* Delete any foreign keys attached to this table. */ | 626 /* Delete any foreign keys attached to this table. */ |
| 635 sqlite3FkDelete(db, pTable); | 627 sqlite3FkDelete(db, pTable); |
| 636 | 628 |
| 637 /* Delete the Table structure itself. | 629 /* Delete the Table structure itself. |
| 638 */ | 630 */ |
| 639 sqlite3DeleteColumnNames(db, pTable); | 631 sqlite3DeleteColumnNames(db, pTable); |
| 640 sqlite3DbFree(db, pTable->zName); | 632 sqlite3DbFree(db, pTable->zName); |
| 641 sqlite3DbFree(db, pTable->zColAff); | 633 sqlite3DbFree(db, pTable->zColAff); |
| 642 sqlite3SelectDelete(db, pTable->pSelect); | 634 sqlite3SelectDelete(db, pTable->pSelect); |
| 643 sqlite3ExprListDelete(db, pTable->pCheck); | 635 sqlite3ExprListDelete(db, pTable->pCheck); |
| 644 #ifndef SQLITE_OMIT_VIRTUALTABLE | 636 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 645 sqlite3VtabClear(db, pTable); | 637 sqlite3VtabClear(db, pTable); |
| 646 #endif | 638 #endif |
| 647 sqlite3DbFree(db, pTable); | 639 sqlite3DbFree(db, pTable); |
| 648 | 640 |
| 649 /* Verify that no lookaside memory was used by schema tables */ | 641 /* Verify that no lookaside memory was used by schema tables */ |
| 650 assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); | 642 assert( nLookaside==0 || nLookaside==db->lookaside.nOut ); |
| 651 } | 643 } |
| 644 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 645 /* Do not delete the table until the reference count reaches zero. */ |
| 646 if( !pTable ) return; |
| 647 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; |
| 648 deleteTable(db, pTable); |
| 649 } |
| 650 |
| 652 | 651 |
| 653 /* | 652 /* |
| 654 ** Unlink the given table from the hash tables and the delete the | 653 ** Unlink the given table from the hash tables and the delete the |
| 655 ** table structure with all its indices and foreign keys. | 654 ** table structure with all its indices and foreign keys. |
| 656 */ | 655 */ |
| 657 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ | 656 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
| 658 Table *p; | 657 Table *p; |
| 659 Db *pDb; | 658 Db *pDb; |
| 660 | 659 |
| 661 assert( db!=0 ); | 660 assert( db!=0 ); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 692 } | 691 } |
| 693 return zName; | 692 return zName; |
| 694 } | 693 } |
| 695 | 694 |
| 696 /* | 695 /* |
| 697 ** Open the sqlite_master table stored in database number iDb for | 696 ** Open the sqlite_master table stored in database number iDb for |
| 698 ** writing. The table is opened using cursor 0. | 697 ** writing. The table is opened using cursor 0. |
| 699 */ | 698 */ |
| 700 void sqlite3OpenMasterTable(Parse *p, int iDb){ | 699 void sqlite3OpenMasterTable(Parse *p, int iDb){ |
| 701 Vdbe *v = sqlite3GetVdbe(p); | 700 Vdbe *v = sqlite3GetVdbe(p); |
| 702 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); | 701 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME); |
| 703 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); | 702 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5); |
| 704 if( p->nTab==0 ){ | 703 if( p->nTab==0 ){ |
| 705 p->nTab = 1; | 704 p->nTab = 1; |
| 706 } | 705 } |
| 707 } | 706 } |
| 708 | 707 |
| 709 /* | 708 /* |
| 710 ** Parameter zName points to a nul-terminated buffer containing the name | 709 ** Parameter zName points to a nul-terminated buffer containing the name |
| 711 ** of a database ("main", "temp" or the name of an attached db). This | 710 ** of a database ("main", "temp" or the name of an attached db). This |
| 712 ** function returns the index of the named database in db->aDb[], or | 711 ** function returns the index of the named database in db->aDb[], or |
| 713 ** -1 if the named db cannot be found. | 712 ** -1 if the named db cannot be found. |
| 714 */ | 713 */ |
| 715 int sqlite3FindDbName(sqlite3 *db, const char *zName){ | 714 int sqlite3FindDbName(sqlite3 *db, const char *zName){ |
| 716 int i = -1; /* Database number */ | 715 int i = -1; /* Database number */ |
| 717 if( zName ){ | 716 if( zName ){ |
| 718 Db *pDb; | 717 Db *pDb; |
| 719 int n = sqlite3Strlen30(zName); | |
| 720 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ | 718 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
| 721 if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && | 719 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; |
| 722 0==sqlite3StrICmp(pDb->zName, zName) ){ | 720 /* "main" is always an acceptable alias for the primary database |
| 723 break; | 721 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ |
| 724 } | 722 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; |
| 725 } | 723 } |
| 726 } | 724 } |
| 727 return i; | 725 return i; |
| 728 } | 726 } |
| 729 | 727 |
| 730 /* | 728 /* |
| 731 ** The token *pName contains the name of a database (either "main" or | 729 ** The token *pName contains the name of a database (either "main" or |
| 732 ** "temp" or the name of an attached db). This routine returns the | 730 ** "temp" or the name of an attached db). This routine returns the |
| 733 ** index of the named database in db->aDb[], or -1 if the named db | 731 ** index of the named database in db->aDb[], or -1 if the named db |
| 734 ** does not exist. | 732 ** does not exist. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 760 */ | 758 */ |
| 761 int sqlite3TwoPartName( | 759 int sqlite3TwoPartName( |
| 762 Parse *pParse, /* Parsing and code generating context */ | 760 Parse *pParse, /* Parsing and code generating context */ |
| 763 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ | 761 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
| 764 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ | 762 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
| 765 Token **pUnqual /* Write the unqualified object name here */ | 763 Token **pUnqual /* Write the unqualified object name here */ |
| 766 ){ | 764 ){ |
| 767 int iDb; /* Database holding the object */ | 765 int iDb; /* Database holding the object */ |
| 768 sqlite3 *db = pParse->db; | 766 sqlite3 *db = pParse->db; |
| 769 | 767 |
| 770 if( ALWAYS(pName2!=0) && pName2->n>0 ){ | 768 assert( pName2!=0 ); |
| 769 if( pName2->n>0 ){ |
| 771 if( db->init.busy ) { | 770 if( db->init.busy ) { |
| 772 sqlite3ErrorMsg(pParse, "corrupt database"); | 771 sqlite3ErrorMsg(pParse, "corrupt database"); |
| 773 return -1; | 772 return -1; |
| 774 } | 773 } |
| 775 *pUnqual = pName2; | 774 *pUnqual = pName2; |
| 776 iDb = sqlite3FindDb(db, pName1); | 775 iDb = sqlite3FindDb(db, pName1); |
| 777 if( iDb<0 ){ | 776 if( iDb<0 ){ |
| 778 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); | 777 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
| 779 return -1; | 778 return -1; |
| 780 } | 779 } |
| 781 }else{ | 780 }else{ |
| 782 assert( db->init.iDb==0 || db->init.busy ); | 781 assert( db->init.iDb==0 || db->init.busy || (db->flags & SQLITE_Vacuum)!=0); |
| 783 iDb = db->init.iDb; | 782 iDb = db->init.iDb; |
| 784 *pUnqual = pName1; | 783 *pUnqual = pName1; |
| 785 } | 784 } |
| 786 return iDb; | 785 return iDb; |
| 787 } | 786 } |
| 788 | 787 |
| 789 /* | 788 /* |
| 790 ** This routine is used to check if the UTF-8 string zName is a legal | 789 ** This routine is used to check if the UTF-8 string zName is a legal |
| 791 ** unqualified name for a new schema object (table, index, view or | 790 ** unqualified name for a new schema object (table, index, view or |
| 792 ** trigger). All names are legal except those that begin with the string | 791 ** trigger). All names are legal except those that begin with the string |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 int isVirtual, /* True if this is a VIRTUAL table */ | 848 int isVirtual, /* True if this is a VIRTUAL table */ |
| 850 int noErr /* Do nothing if table already exists */ | 849 int noErr /* Do nothing if table already exists */ |
| 851 ){ | 850 ){ |
| 852 Table *pTable; | 851 Table *pTable; |
| 853 char *zName = 0; /* The name of the new table */ | 852 char *zName = 0; /* The name of the new table */ |
| 854 sqlite3 *db = pParse->db; | 853 sqlite3 *db = pParse->db; |
| 855 Vdbe *v; | 854 Vdbe *v; |
| 856 int iDb; /* Database number to create the table in */ | 855 int iDb; /* Database number to create the table in */ |
| 857 Token *pName; /* Unqualified name of the table to create */ | 856 Token *pName; /* Unqualified name of the table to create */ |
| 858 | 857 |
| 859 /* The table or view name to create is passed to this routine via tokens | 858 if( db->init.busy && db->init.newTnum==1 ){ |
| 860 ** pName1 and pName2. If the table name was fully qualified, for example: | 859 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */ |
| 861 ** | 860 iDb = db->init.iDb; |
| 862 ** CREATE TABLE xxx.yyy (...); | 861 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); |
| 863 ** | 862 pName = pName1; |
| 864 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if | 863 }else{ |
| 865 ** the table name is not fully qualified, i.e.: | 864 /* The common case */ |
| 866 ** | 865 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 867 ** CREATE TABLE yyy(...); | 866 if( iDb<0 ) return; |
| 868 ** | 867 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
| 869 ** Then pName1 is set to "yyy" and pName2 is "". | 868 /* If creating a temp table, the name may not be qualified. Unless |
| 870 ** | 869 ** the database name is "temp" anyway. */ |
| 871 ** The call below sets the pName pointer to point at the token (pName1 or | 870 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 872 ** pName2) that stores the unqualified table name. The variable iDb is | 871 return; |
| 873 ** set to the index of the database that the table or view is to be | 872 } |
| 874 ** created in. | 873 if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 875 */ | 874 zName = sqlite3NameFromToken(db, pName); |
| 876 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); | |
| 877 if( iDb<0 ) return; | |
| 878 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ | |
| 879 /* If creating a temp table, the name may not be qualified. Unless | |
| 880 ** the database name is "temp" anyway. */ | |
| 881 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); | |
| 882 return; | |
| 883 } | 875 } |
| 884 if( !OMIT_TEMPDB && isTemp ) iDb = 1; | |
| 885 | |
| 886 pParse->sNameToken = *pName; | 876 pParse->sNameToken = *pName; |
| 887 zName = sqlite3NameFromToken(db, pName); | |
| 888 if( zName==0 ) return; | 877 if( zName==0 ) return; |
| 889 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ | 878 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 890 goto begin_table_error; | 879 goto begin_table_error; |
| 891 } | 880 } |
| 892 if( db->init.iDb==1 ) isTemp = 1; | 881 if( db->init.iDb==1 ) isTemp = 1; |
| 893 #ifndef SQLITE_OMIT_AUTHORIZATION | 882 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 894 assert( (isTemp & 1)==isTemp ); | 883 assert( isTemp==0 || isTemp==1 ); |
| 884 assert( isView==0 || isView==1 ); |
| 895 { | 885 { |
| 896 int code; | 886 static const u8 aCode[] = { |
| 897 char *zDb = db->aDb[iDb].zName; | 887 SQLITE_CREATE_TABLE, |
| 888 SQLITE_CREATE_TEMP_TABLE, |
| 889 SQLITE_CREATE_VIEW, |
| 890 SQLITE_CREATE_TEMP_VIEW |
| 891 }; |
| 892 char *zDb = db->aDb[iDb].zDbSName; |
| 898 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ | 893 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
| 899 goto begin_table_error; | 894 goto begin_table_error; |
| 900 } | 895 } |
| 901 if( isView ){ | 896 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], |
| 902 if( !OMIT_TEMPDB && isTemp ){ | 897 zName, 0, zDb) ){ |
| 903 code = SQLITE_CREATE_TEMP_VIEW; | |
| 904 }else{ | |
| 905 code = SQLITE_CREATE_VIEW; | |
| 906 } | |
| 907 }else{ | |
| 908 if( !OMIT_TEMPDB && isTemp ){ | |
| 909 code = SQLITE_CREATE_TEMP_TABLE; | |
| 910 }else{ | |
| 911 code = SQLITE_CREATE_TABLE; | |
| 912 } | |
| 913 } | |
| 914 if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){ | |
| 915 goto begin_table_error; | 898 goto begin_table_error; |
| 916 } | 899 } |
| 917 } | 900 } |
| 918 #endif | 901 #endif |
| 919 | 902 |
| 920 /* Make sure the new table name does not collide with an existing | 903 /* Make sure the new table name does not collide with an existing |
| 921 ** index or table name in the same database. Issue an error message if | 904 ** index or table name in the same database. Issue an error message if |
| 922 ** it does. The exception is if the statement being parsed was passed | 905 ** it does. The exception is if the statement being parsed was passed |
| 923 ** to an sqlite3_declare_vtab() call. In that case only the column names | 906 ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 924 ** and types will be used, so there is no need to test for namespace | 907 ** and types will be used, so there is no need to test for namespace |
| 925 ** collisions. | 908 ** collisions. |
| 926 */ | 909 */ |
| 927 if( !IN_DECLARE_VTAB ){ | 910 if( !IN_DECLARE_VTAB ){ |
| 928 char *zDb = db->aDb[iDb].zName; | 911 char *zDb = db->aDb[iDb].zDbSName; |
| 929 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 912 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 930 goto begin_table_error; | 913 goto begin_table_error; |
| 931 } | 914 } |
| 932 pTable = sqlite3FindTable(db, zName, zDb); | 915 pTable = sqlite3FindTable(db, zName, zDb); |
| 933 if( pTable ){ | 916 if( pTable ){ |
| 934 if( !noErr ){ | 917 if( !noErr ){ |
| 935 sqlite3ErrorMsg(pParse, "table %T already exists", pName); | 918 sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 936 }else{ | 919 }else{ |
| 937 assert( !db->init.busy || CORRUPT_DB ); | 920 assert( !db->init.busy || CORRUPT_DB ); |
| 938 sqlite3CodeVerifySchema(pParse, iDb); | 921 sqlite3CodeVerifySchema(pParse, iDb); |
| 939 } | 922 } |
| 940 goto begin_table_error; | 923 goto begin_table_error; |
| 941 } | 924 } |
| 942 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ | 925 if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
| 943 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); | 926 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 944 goto begin_table_error; | 927 goto begin_table_error; |
| 945 } | 928 } |
| 946 } | 929 } |
| 947 | 930 |
| 948 pTable = sqlite3DbMallocZero(db, sizeof(Table)); | 931 pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
| 949 if( pTable==0 ){ | 932 if( pTable==0 ){ |
| 950 db->mallocFailed = 1; | 933 assert( db->mallocFailed ); |
| 951 pParse->rc = SQLITE_NOMEM; | 934 pParse->rc = SQLITE_NOMEM_BKPT; |
| 952 pParse->nErr++; | 935 pParse->nErr++; |
| 953 goto begin_table_error; | 936 goto begin_table_error; |
| 954 } | 937 } |
| 955 pTable->zName = zName; | 938 pTable->zName = zName; |
| 956 pTable->iPKey = -1; | 939 pTable->iPKey = -1; |
| 957 pTable->pSchema = db->aDb[iDb].pSchema; | 940 pTable->pSchema = db->aDb[iDb].pSchema; |
| 958 pTable->nRef = 1; | 941 pTable->nTabRef = 1; |
| 959 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); | 942 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 960 assert( pParse->pNewTable==0 ); | 943 assert( pParse->pNewTable==0 ); |
| 961 pParse->pNewTable = pTable; | 944 pParse->pNewTable = pTable; |
| 962 | 945 |
| 963 /* If this is the magic sqlite_sequence table used by autoincrement, | 946 /* If this is the magic sqlite_sequence table used by autoincrement, |
| 964 ** then record a pointer to this table in the main database structure | 947 ** then record a pointer to this table in the main database structure |
| 965 ** so that INSERT can find the table easily. | 948 ** so that INSERT can find the table easily. |
| 966 */ | 949 */ |
| 967 #ifndef SQLITE_OMIT_AUTOINCREMENT | 950 #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 968 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ | 951 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 997 ** set them now. | 980 ** set them now. |
| 998 */ | 981 */ |
| 999 reg1 = pParse->regRowid = ++pParse->nMem; | 982 reg1 = pParse->regRowid = ++pParse->nMem; |
| 1000 reg2 = pParse->regRoot = ++pParse->nMem; | 983 reg2 = pParse->regRoot = ++pParse->nMem; |
| 1001 reg3 = ++pParse->nMem; | 984 reg3 = ++pParse->nMem; |
| 1002 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); | 985 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
| 1003 sqlite3VdbeUsesBtree(v, iDb); | 986 sqlite3VdbeUsesBtree(v, iDb); |
| 1004 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); | 987 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
| 1005 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? | 988 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
| 1006 1 : SQLITE_MAX_FILE_FORMAT; | 989 1 : SQLITE_MAX_FILE_FORMAT; |
| 1007 sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3); | 990 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); |
| 1008 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3); | 991 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); |
| 1009 sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3); | |
| 1010 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3); | |
| 1011 sqlite3VdbeJumpHere(v, addr1); | 992 sqlite3VdbeJumpHere(v, addr1); |
| 1012 | 993 |
| 1013 /* This just creates a place-holder record in the sqlite_master table. | 994 /* This just creates a place-holder record in the sqlite_master table. |
| 1014 ** The record created does not contain anything yet. It will be replaced | 995 ** The record created does not contain anything yet. It will be replaced |
| 1015 ** by the real entry in code generated at sqlite3EndTable(). | 996 ** by the real entry in code generated at sqlite3EndTable(). |
| 1016 ** | 997 ** |
| 1017 ** The rowid for the new entry is left in register pParse->regRowid. | 998 ** The rowid for the new entry is left in register pParse->regRowid. |
| 1018 ** The root page number of the new table is left in reg pParse->regRoot. | 999 ** The root page number of the new table is left in reg pParse->regRoot. |
| 1019 ** The rowid and root page number values are needed by the code that | 1000 ** The rowid and root page number values are needed by the code that |
| 1020 ** sqlite3EndTable will generate. | 1001 ** sqlite3EndTable will generate. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 | 1040 |
| 1060 | 1041 |
| 1061 /* | 1042 /* |
| 1062 ** Add a new column to the table currently being constructed. | 1043 ** Add a new column to the table currently being constructed. |
| 1063 ** | 1044 ** |
| 1064 ** The parser calls this routine once for each column declaration | 1045 ** The parser calls this routine once for each column declaration |
| 1065 ** in a CREATE TABLE statement. sqlite3StartTable() gets called | 1046 ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
| 1066 ** first to get things going. Then this routine is called for each | 1047 ** first to get things going. Then this routine is called for each |
| 1067 ** column. | 1048 ** column. |
| 1068 */ | 1049 */ |
| 1069 void sqlite3AddColumn(Parse *pParse, Token *pName){ | 1050 void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){ |
| 1070 Table *p; | 1051 Table *p; |
| 1071 int i; | 1052 int i; |
| 1072 char *z; | 1053 char *z; |
| 1054 char *zType; |
| 1073 Column *pCol; | 1055 Column *pCol; |
| 1074 sqlite3 *db = pParse->db; | 1056 sqlite3 *db = pParse->db; |
| 1075 if( (p = pParse->pNewTable)==0 ) return; | 1057 if( (p = pParse->pNewTable)==0 ) return; |
| 1076 #if SQLITE_MAX_COLUMN | 1058 #if SQLITE_MAX_COLUMN |
| 1077 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ | 1059 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 1078 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); | 1060 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
| 1079 return; | 1061 return; |
| 1080 } | 1062 } |
| 1081 #endif | 1063 #endif |
| 1082 z = sqlite3NameFromToken(db, pName); | 1064 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); |
| 1083 if( z==0 ) return; | 1065 if( z==0 ) return; |
| 1066 memcpy(z, pName->z, pName->n); |
| 1067 z[pName->n] = 0; |
| 1068 sqlite3Dequote(z); |
| 1084 for(i=0; i<p->nCol; i++){ | 1069 for(i=0; i<p->nCol; i++){ |
| 1085 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ | 1070 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){ |
| 1086 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); | 1071 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
| 1087 sqlite3DbFree(db, z); | 1072 sqlite3DbFree(db, z); |
| 1088 return; | 1073 return; |
| 1089 } | 1074 } |
| 1090 } | 1075 } |
| 1091 if( (p->nCol & 0x7)==0 ){ | 1076 if( (p->nCol & 0x7)==0 ){ |
| 1092 Column *aNew; | 1077 Column *aNew; |
| 1093 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); | 1078 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); |
| 1094 if( aNew==0 ){ | 1079 if( aNew==0 ){ |
| 1095 sqlite3DbFree(db, z); | 1080 sqlite3DbFree(db, z); |
| 1096 return; | 1081 return; |
| 1097 } | 1082 } |
| 1098 p->aCol = aNew; | 1083 p->aCol = aNew; |
| 1099 } | 1084 } |
| 1100 pCol = &p->aCol[p->nCol]; | 1085 pCol = &p->aCol[p->nCol]; |
| 1101 memset(pCol, 0, sizeof(p->aCol[0])); | 1086 memset(pCol, 0, sizeof(p->aCol[0])); |
| 1102 pCol->zName = z; | 1087 pCol->zName = z; |
| 1103 sqlite3ColumnPropertiesFromName(p, pCol); | 1088 sqlite3ColumnPropertiesFromName(p, pCol); |
| 1104 | 1089 |
| 1105 /* If there is no type specified, columns have the default affinity | 1090 if( pType->n==0 ){ |
| 1106 ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will | 1091 /* If there is no type specified, columns have the default affinity |
| 1107 ** be called next to set pCol->affinity correctly. | 1092 ** 'BLOB'. */ |
| 1108 */ | 1093 pCol->affinity = SQLITE_AFF_BLOB; |
| 1109 pCol->affinity = SQLITE_AFF_BLOB; | 1094 pCol->szEst = 1; |
| 1110 pCol->szEst = 1; | 1095 }else{ |
| 1096 zType = z + sqlite3Strlen30(z) + 1; |
| 1097 memcpy(zType, pType->z, pType->n); |
| 1098 zType[pType->n] = 0; |
| 1099 sqlite3Dequote(zType); |
| 1100 pCol->affinity = sqlite3AffinityType(zType, &pCol->szEst); |
| 1101 pCol->colFlags |= COLFLAG_HASTYPE; |
| 1102 } |
| 1111 p->nCol++; | 1103 p->nCol++; |
| 1104 pParse->constraintName.n = 0; |
| 1112 } | 1105 } |
| 1113 | 1106 |
| 1114 /* | 1107 /* |
| 1115 ** This routine is called by the parser while in the middle of | 1108 ** This routine is called by the parser while in the middle of |
| 1116 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has | 1109 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 1117 ** been seen on a column. This routine sets the notNull flag on | 1110 ** been seen on a column. This routine sets the notNull flag on |
| 1118 ** the column currently under construction. | 1111 ** the column currently under construction. |
| 1119 */ | 1112 */ |
| 1120 void sqlite3AddNotNull(Parse *pParse, int onError){ | 1113 void sqlite3AddNotNull(Parse *pParse, int onError){ |
| 1121 Table *p; | 1114 Table *p; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1147 ** 'DOUB' | SQLITE_AFF_REAL | 1140 ** 'DOUB' | SQLITE_AFF_REAL |
| 1148 ** | 1141 ** |
| 1149 ** If none of the substrings in the above table are found, | 1142 ** If none of the substrings in the above table are found, |
| 1150 ** SQLITE_AFF_NUMERIC is returned. | 1143 ** SQLITE_AFF_NUMERIC is returned. |
| 1151 */ | 1144 */ |
| 1152 char sqlite3AffinityType(const char *zIn, u8 *pszEst){ | 1145 char sqlite3AffinityType(const char *zIn, u8 *pszEst){ |
| 1153 u32 h = 0; | 1146 u32 h = 0; |
| 1154 char aff = SQLITE_AFF_NUMERIC; | 1147 char aff = SQLITE_AFF_NUMERIC; |
| 1155 const char *zChar = 0; | 1148 const char *zChar = 0; |
| 1156 | 1149 |
| 1157 if( zIn==0 ) return aff; | 1150 assert( zIn!=0 ); |
| 1158 while( zIn[0] ){ | 1151 while( zIn[0] ){ |
| 1159 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; | 1152 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
| 1160 zIn++; | 1153 zIn++; |
| 1161 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ | 1154 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
| 1162 aff = SQLITE_AFF_TEXT; | 1155 aff = SQLITE_AFF_TEXT; |
| 1163 zChar = zIn; | 1156 zChar = zIn; |
| 1164 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ | 1157 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| 1165 aff = SQLITE_AFF_TEXT; | 1158 aff = SQLITE_AFF_TEXT; |
| 1166 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ | 1159 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 1167 aff = SQLITE_AFF_TEXT; | 1160 aff = SQLITE_AFF_TEXT; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1205 } | 1198 } |
| 1206 }else{ | 1199 }else{ |
| 1207 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ | 1200 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ |
| 1208 } | 1201 } |
| 1209 } | 1202 } |
| 1210 } | 1203 } |
| 1211 return aff; | 1204 return aff; |
| 1212 } | 1205 } |
| 1213 | 1206 |
| 1214 /* | 1207 /* |
| 1215 ** This routine is called by the parser while in the middle of | |
| 1216 ** parsing a CREATE TABLE statement. The pFirst token is the first | |
| 1217 ** token in the sequence of tokens that describe the type of the | |
| 1218 ** column currently under construction. pLast is the last token | |
| 1219 ** in the sequence. Use this information to construct a string | |
| 1220 ** that contains the typename of the column and store that string | |
| 1221 ** in zType. | |
| 1222 */ | |
| 1223 void sqlite3AddColumnType(Parse *pParse, Token *pType){ | |
| 1224 Table *p; | |
| 1225 Column *pCol; | |
| 1226 | |
| 1227 p = pParse->pNewTable; | |
| 1228 if( p==0 || NEVER(p->nCol<1) ) return; | |
| 1229 pCol = &p->aCol[p->nCol-1]; | |
| 1230 assert( pCol->zType==0 || CORRUPT_DB ); | |
| 1231 sqlite3DbFree(pParse->db, pCol->zType); | |
| 1232 pCol->zType = sqlite3NameFromToken(pParse->db, pType); | |
| 1233 pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst); | |
| 1234 } | |
| 1235 | |
| 1236 /* | |
| 1237 ** The expression is the default value for the most recently added column | 1208 ** The expression is the default value for the most recently added column |
| 1238 ** of the table currently under construction. | 1209 ** of the table currently under construction. |
| 1239 ** | 1210 ** |
| 1240 ** Default value expressions must be constant. Raise an exception if this | 1211 ** Default value expressions must be constant. Raise an exception if this |
| 1241 ** is not the case. | 1212 ** is not the case. |
| 1242 ** | 1213 ** |
| 1243 ** This routine is called by the parser while in the middle of | 1214 ** This routine is called by the parser while in the middle of |
| 1244 ** parsing a CREATE TABLE statement. | 1215 ** parsing a CREATE TABLE statement. |
| 1245 */ | 1216 */ |
| 1246 void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ | 1217 void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ |
| 1247 Table *p; | 1218 Table *p; |
| 1248 Column *pCol; | 1219 Column *pCol; |
| 1249 sqlite3 *db = pParse->db; | 1220 sqlite3 *db = pParse->db; |
| 1250 p = pParse->pNewTable; | 1221 p = pParse->pNewTable; |
| 1251 if( p!=0 ){ | 1222 if( p!=0 ){ |
| 1252 pCol = &(p->aCol[p->nCol-1]); | 1223 pCol = &(p->aCol[p->nCol-1]); |
| 1253 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ | 1224 if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){ |
| 1254 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", | 1225 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
| 1255 pCol->zName); | 1226 pCol->zName); |
| 1256 }else{ | 1227 }else{ |
| 1257 /* A copy of pExpr is used instead of the original, as pExpr contains | 1228 /* A copy of pExpr is used instead of the original, as pExpr contains |
| 1258 ** tokens that point to volatile memory. The 'span' of the expression | 1229 ** tokens that point to volatile memory. The 'span' of the expression |
| 1259 ** is required by pragma table_info. | 1230 ** is required by pragma table_info. |
| 1260 */ | 1231 */ |
| 1232 Expr x; |
| 1261 sqlite3ExprDelete(db, pCol->pDflt); | 1233 sqlite3ExprDelete(db, pCol->pDflt); |
| 1262 pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE); | 1234 memset(&x, 0, sizeof(x)); |
| 1263 sqlite3DbFree(db, pCol->zDflt); | 1235 x.op = TK_SPAN; |
| 1264 pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart, | 1236 x.u.zToken = sqlite3DbStrNDup(db, (char*)pSpan->zStart, |
| 1265 (int)(pSpan->zEnd - pSpan->zStart)); | 1237 (int)(pSpan->zEnd - pSpan->zStart)); |
| 1238 x.pLeft = pSpan->pExpr; |
| 1239 x.flags = EP_Skip; |
| 1240 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
| 1241 sqlite3DbFree(db, x.u.zToken); |
| 1266 } | 1242 } |
| 1267 } | 1243 } |
| 1268 sqlite3ExprDelete(db, pSpan->pExpr); | 1244 sqlite3ExprDelete(db, pSpan->pExpr); |
| 1269 } | 1245 } |
| 1270 | 1246 |
| 1271 /* | 1247 /* |
| 1272 ** Backwards Compatibility Hack: | 1248 ** Backwards Compatibility Hack: |
| 1273 ** | 1249 ** |
| 1274 ** Historical versions of SQLite accepted strings as column names in | 1250 ** Historical versions of SQLite accepted strings as column names in |
| 1275 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: | 1251 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1311 ** index for the key. No index is created for INTEGER PRIMARY KEYs. | 1287 ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 1312 */ | 1288 */ |
| 1313 void sqlite3AddPrimaryKey( | 1289 void sqlite3AddPrimaryKey( |
| 1314 Parse *pParse, /* Parsing context */ | 1290 Parse *pParse, /* Parsing context */ |
| 1315 ExprList *pList, /* List of field names to be indexed */ | 1291 ExprList *pList, /* List of field names to be indexed */ |
| 1316 int onError, /* What to do with a uniqueness conflict */ | 1292 int onError, /* What to do with a uniqueness conflict */ |
| 1317 int autoInc, /* True if the AUTOINCREMENT keyword is present */ | 1293 int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
| 1318 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ | 1294 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
| 1319 ){ | 1295 ){ |
| 1320 Table *pTab = pParse->pNewTable; | 1296 Table *pTab = pParse->pNewTable; |
| 1321 char *zType = 0; | 1297 Column *pCol = 0; |
| 1322 int iCol = -1, i; | 1298 int iCol = -1, i; |
| 1323 int nTerm; | 1299 int nTerm; |
| 1324 if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit; | 1300 if( pTab==0 ) goto primary_key_exit; |
| 1325 if( pTab->tabFlags & TF_HasPrimaryKey ){ | 1301 if( pTab->tabFlags & TF_HasPrimaryKey ){ |
| 1326 sqlite3ErrorMsg(pParse, | 1302 sqlite3ErrorMsg(pParse, |
| 1327 "table \"%s\" has more than one primary key", pTab->zName); | 1303 "table \"%s\" has more than one primary key", pTab->zName); |
| 1328 goto primary_key_exit; | 1304 goto primary_key_exit; |
| 1329 } | 1305 } |
| 1330 pTab->tabFlags |= TF_HasPrimaryKey; | 1306 pTab->tabFlags |= TF_HasPrimaryKey; |
| 1331 if( pList==0 ){ | 1307 if( pList==0 ){ |
| 1332 iCol = pTab->nCol - 1; | 1308 iCol = pTab->nCol - 1; |
| 1333 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; | 1309 pCol = &pTab->aCol[iCol]; |
| 1334 zType = pTab->aCol[iCol].zType; | 1310 pCol->colFlags |= COLFLAG_PRIMKEY; |
| 1335 nTerm = 1; | 1311 nTerm = 1; |
| 1336 }else{ | 1312 }else{ |
| 1337 nTerm = pList->nExpr; | 1313 nTerm = pList->nExpr; |
| 1338 for(i=0; i<nTerm; i++){ | 1314 for(i=0; i<nTerm; i++){ |
| 1339 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); | 1315 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
| 1340 assert( pCExpr!=0 ); | 1316 assert( pCExpr!=0 ); |
| 1341 sqlite3StringToId(pCExpr); | 1317 sqlite3StringToId(pCExpr); |
| 1342 if( pCExpr->op==TK_ID ){ | 1318 if( pCExpr->op==TK_ID ){ |
| 1343 const char *zCName = pCExpr->u.zToken; | 1319 const char *zCName = pCExpr->u.zToken; |
| 1344 for(iCol=0; iCol<pTab->nCol; iCol++){ | 1320 for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 1345 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ | 1321 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ |
| 1346 pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY; | 1322 pCol = &pTab->aCol[iCol]; |
| 1347 zType = pTab->aCol[iCol].zType; | 1323 pCol->colFlags |= COLFLAG_PRIMKEY; |
| 1348 break; | 1324 break; |
| 1349 } | 1325 } |
| 1350 } | 1326 } |
| 1351 } | 1327 } |
| 1352 } | 1328 } |
| 1353 } | 1329 } |
| 1354 if( nTerm==1 | 1330 if( nTerm==1 |
| 1355 && zType && sqlite3StrICmp(zType, "INTEGER")==0 | 1331 && pCol |
| 1332 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 |
| 1356 && sortOrder!=SQLITE_SO_DESC | 1333 && sortOrder!=SQLITE_SO_DESC |
| 1357 ){ | 1334 ){ |
| 1358 pTab->iPKey = iCol; | 1335 pTab->iPKey = iCol; |
| 1359 pTab->keyConf = (u8)onError; | 1336 pTab->keyConf = (u8)onError; |
| 1360 assert( autoInc==0 || autoInc==1 ); | 1337 assert( autoInc==0 || autoInc==1 ); |
| 1361 pTab->tabFlags |= autoInc*TF_Autoincrement; | 1338 pTab->tabFlags |= autoInc*TF_Autoincrement; |
| 1362 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; | 1339 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder; |
| 1363 }else if( autoInc ){ | 1340 }else if( autoInc ){ |
| 1364 #ifndef SQLITE_OMIT_AUTOINCREMENT | 1341 #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 1365 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " | 1342 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1366 "INTEGER PRIMARY KEY"); | 1343 "INTEGER PRIMARY KEY"); |
| 1367 #endif | 1344 #endif |
| 1368 }else{ | 1345 }else{ |
| 1369 Index *p; | 1346 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
| 1370 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, | 1347 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); |
| 1371 0, sortOrder, 0); | |
| 1372 if( p ){ | |
| 1373 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY; | |
| 1374 } | |
| 1375 pList = 0; | 1348 pList = 0; |
| 1376 } | 1349 } |
| 1377 | 1350 |
| 1378 primary_key_exit: | 1351 primary_key_exit: |
| 1379 sqlite3ExprListDelete(pParse->db, pList); | 1352 sqlite3ExprListDelete(pParse->db, pList); |
| 1380 return; | 1353 return; |
| 1381 } | 1354 } |
| 1382 | 1355 |
| 1383 /* | 1356 /* |
| 1384 ** Add a new CHECK constraint to the table currently under construction. | 1357 ** Add a new CHECK constraint to the table currently under construction. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1483 ** changes. When a process first reads the schema it records the | 1456 ** changes. When a process first reads the schema it records the |
| 1484 ** cookie. Thereafter, whenever it goes to access the database, | 1457 ** cookie. Thereafter, whenever it goes to access the database, |
| 1485 ** it checks the cookie to make sure the schema has not changed | 1458 ** it checks the cookie to make sure the schema has not changed |
| 1486 ** since it was last read. | 1459 ** since it was last read. |
| 1487 ** | 1460 ** |
| 1488 ** This plan is not completely bullet-proof. It is possible for | 1461 ** This plan is not completely bullet-proof. It is possible for |
| 1489 ** the schema to change multiple times and for the cookie to be | 1462 ** the schema to change multiple times and for the cookie to be |
| 1490 ** set back to prior value. But schema changes are infrequent | 1463 ** set back to prior value. But schema changes are infrequent |
| 1491 ** and the probability of hitting the same cookie value is only | 1464 ** and the probability of hitting the same cookie value is only |
| 1492 ** 1 chance in 2^32. So we're safe enough. | 1465 ** 1 chance in 2^32. So we're safe enough. |
| 1466 ** |
| 1467 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments |
| 1468 ** the schema-version whenever the schema changes. |
| 1493 */ | 1469 */ |
| 1494 void sqlite3ChangeCookie(Parse *pParse, int iDb){ | 1470 void sqlite3ChangeCookie(Parse *pParse, int iDb){ |
| 1495 int r1 = sqlite3GetTempReg(pParse); | |
| 1496 sqlite3 *db = pParse->db; | 1471 sqlite3 *db = pParse->db; |
| 1497 Vdbe *v = pParse->pVdbe; | 1472 Vdbe *v = pParse->pVdbe; |
| 1498 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 1473 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 1499 sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1); | 1474 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, |
| 1500 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1); | 1475 db->aDb[iDb].pSchema->schema_cookie+1); |
| 1501 sqlite3ReleaseTempReg(pParse, r1); | |
| 1502 } | 1476 } |
| 1503 | 1477 |
| 1504 /* | 1478 /* |
| 1505 ** Measure the number of characters needed to output the given | 1479 ** Measure the number of characters needed to output the given |
| 1506 ** identifier. The number returned includes any quotes used | 1480 ** identifier. The number returned includes any quotes used |
| 1507 ** but does not include the null terminator. | 1481 ** but does not include the null terminator. |
| 1508 ** | 1482 ** |
| 1509 ** The estimate is conservative. It might be larger that what is | 1483 ** The estimate is conservative. It might be larger that what is |
| 1510 ** really needed. | 1484 ** really needed. |
| 1511 */ | 1485 */ |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1573 zSep2 = ","; | 1547 zSep2 = ","; |
| 1574 zEnd = ")"; | 1548 zEnd = ")"; |
| 1575 }else{ | 1549 }else{ |
| 1576 zSep = "\n "; | 1550 zSep = "\n "; |
| 1577 zSep2 = ",\n "; | 1551 zSep2 = ",\n "; |
| 1578 zEnd = "\n)"; | 1552 zEnd = "\n)"; |
| 1579 } | 1553 } |
| 1580 n += 35 + 6*p->nCol; | 1554 n += 35 + 6*p->nCol; |
| 1581 zStmt = sqlite3DbMallocRaw(0, n); | 1555 zStmt = sqlite3DbMallocRaw(0, n); |
| 1582 if( zStmt==0 ){ | 1556 if( zStmt==0 ){ |
| 1583 db->mallocFailed = 1; | 1557 sqlite3OomFault(db); |
| 1584 return 0; | 1558 return 0; |
| 1585 } | 1559 } |
| 1586 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); | 1560 sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
| 1587 k = sqlite3Strlen30(zStmt); | 1561 k = sqlite3Strlen30(zStmt); |
| 1588 identPut(zStmt, &k, p->zName); | 1562 identPut(zStmt, &k, p->zName); |
| 1589 zStmt[k++] = '('; | 1563 zStmt[k++] = '('; |
| 1590 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ | 1564 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
| 1591 static const char * const azType[] = { | 1565 static const char * const azType[] = { |
| 1592 /* SQLITE_AFF_BLOB */ "", | 1566 /* SQLITE_AFF_BLOB */ "", |
| 1593 /* SQLITE_AFF_TEXT */ " TEXT", | 1567 /* SQLITE_AFF_TEXT */ " TEXT", |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1626 ** Resize an Index object to hold N columns total. Return SQLITE_OK | 1600 ** Resize an Index object to hold N columns total. Return SQLITE_OK |
| 1627 ** on success and SQLITE_NOMEM on an OOM error. | 1601 ** on success and SQLITE_NOMEM on an OOM error. |
| 1628 */ | 1602 */ |
| 1629 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ | 1603 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ |
| 1630 char *zExtra; | 1604 char *zExtra; |
| 1631 int nByte; | 1605 int nByte; |
| 1632 if( pIdx->nColumn>=N ) return SQLITE_OK; | 1606 if( pIdx->nColumn>=N ) return SQLITE_OK; |
| 1633 assert( pIdx->isResized==0 ); | 1607 assert( pIdx->isResized==0 ); |
| 1634 nByte = (sizeof(char*) + sizeof(i16) + 1)*N; | 1608 nByte = (sizeof(char*) + sizeof(i16) + 1)*N; |
| 1635 zExtra = sqlite3DbMallocZero(db, nByte); | 1609 zExtra = sqlite3DbMallocZero(db, nByte); |
| 1636 if( zExtra==0 ) return SQLITE_NOMEM; | 1610 if( zExtra==0 ) return SQLITE_NOMEM_BKPT; |
| 1637 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); | 1611 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
| 1638 pIdx->azColl = (const char**)zExtra; | 1612 pIdx->azColl = (const char**)zExtra; |
| 1639 zExtra += sizeof(char*)*N; | 1613 zExtra += sizeof(char*)*N; |
| 1640 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); | 1614 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
| 1641 pIdx->aiColumn = (i16*)zExtra; | 1615 pIdx->aiColumn = (i16*)zExtra; |
| 1642 zExtra += sizeof(i16)*N; | 1616 zExtra += sizeof(i16)*N; |
| 1643 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); | 1617 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
| 1644 pIdx->aSortOrder = (u8*)zExtra; | 1618 pIdx->aSortOrder = (u8*)zExtra; |
| 1645 pIdx->nColumn = N; | 1619 pIdx->nColumn = N; |
| 1646 pIdx->isResized = 1; | 1620 pIdx->isResized = 1; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1683 return 0; | 1657 return 0; |
| 1684 } | 1658 } |
| 1685 | 1659 |
| 1686 /* | 1660 /* |
| 1687 ** This routine runs at the end of parsing a CREATE TABLE statement that | 1661 ** This routine runs at the end of parsing a CREATE TABLE statement that |
| 1688 ** has a WITHOUT ROWID clause. The job of this routine is to convert both | 1662 ** has a WITHOUT ROWID clause. The job of this routine is to convert both |
| 1689 ** internal schema data structures and the generated VDBE code so that they | 1663 ** internal schema data structures and the generated VDBE code so that they |
| 1690 ** are appropriate for a WITHOUT ROWID table instead of a rowid table. | 1664 ** are appropriate for a WITHOUT ROWID table instead of a rowid table. |
| 1691 ** Changes include: | 1665 ** Changes include: |
| 1692 ** | 1666 ** |
| 1693 ** (1) Convert the OP_CreateTable into an OP_CreateIndex. There is | 1667 ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. |
| 1668 ** (2) Convert the OP_CreateTable into an OP_CreateIndex. There is |
| 1694 ** no rowid btree for a WITHOUT ROWID. Instead, the canonical | 1669 ** no rowid btree for a WITHOUT ROWID. Instead, the canonical |
| 1695 ** data storage is a covering index btree. | 1670 ** data storage is a covering index btree. |
| 1696 ** (2) Bypass the creation of the sqlite_master table entry | 1671 ** (3) Bypass the creation of the sqlite_master table entry |
| 1697 ** for the PRIMARY KEY as the primary key index is now | 1672 ** for the PRIMARY KEY as the primary key index is now |
| 1698 ** identified by the sqlite_master table entry of the table itself. | 1673 ** identified by the sqlite_master table entry of the table itself. |
| 1699 ** (3) Set the Index.tnum of the PRIMARY KEY Index object in the | 1674 ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the |
| 1700 ** schema to the rootpage from the main table. | 1675 ** schema to the rootpage from the main table. |
| 1701 ** (4) Set all columns of the PRIMARY KEY schema object to be NOT NULL. | |
| 1702 ** (5) Add all table columns to the PRIMARY KEY Index object | 1676 ** (5) Add all table columns to the PRIMARY KEY Index object |
| 1703 ** so that the PRIMARY KEY is a covering index. The surplus | 1677 ** so that the PRIMARY KEY is a covering index. The surplus |
| 1704 ** columns are part of KeyInfo.nXField and are not used for | 1678 ** columns are part of KeyInfo.nXField and are not used for |
| 1705 ** sorting or lookup or uniqueness checks. | 1679 ** sorting or lookup or uniqueness checks. |
| 1706 ** (6) Replace the rowid tail on all automatically generated UNIQUE | 1680 ** (6) Replace the rowid tail on all automatically generated UNIQUE |
| 1707 ** indices with the PRIMARY KEY columns. | 1681 ** indices with the PRIMARY KEY columns. |
| 1682 ** |
| 1683 ** For virtual tables, only (1) is performed. |
| 1708 */ | 1684 */ |
| 1709 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ | 1685 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ |
| 1710 Index *pIdx; | 1686 Index *pIdx; |
| 1711 Index *pPk; | 1687 Index *pPk; |
| 1712 int nPk; | 1688 int nPk; |
| 1713 int i, j; | 1689 int i, j; |
| 1714 sqlite3 *db = pParse->db; | 1690 sqlite3 *db = pParse->db; |
| 1715 Vdbe *v = pParse->pVdbe; | 1691 Vdbe *v = pParse->pVdbe; |
| 1716 | 1692 |
| 1693 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) |
| 1694 */ |
| 1695 if( !db->init.imposterTable ){ |
| 1696 for(i=0; i<pTab->nCol; i++){ |
| 1697 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){ |
| 1698 pTab->aCol[i].notNull = OE_Abort; |
| 1699 } |
| 1700 } |
| 1701 } |
| 1702 |
| 1703 /* The remaining transformations only apply to b-tree tables, not to |
| 1704 ** virtual tables */ |
| 1705 if( IN_DECLARE_VTAB ) return; |
| 1706 |
| 1717 /* Convert the OP_CreateTable opcode that would normally create the | 1707 /* Convert the OP_CreateTable opcode that would normally create the |
| 1718 ** root-page for the table into an OP_CreateIndex opcode. The index | 1708 ** root-page for the table into an OP_CreateIndex opcode. The index |
| 1719 ** created will become the PRIMARY KEY index. | 1709 ** created will become the PRIMARY KEY index. |
| 1720 */ | 1710 */ |
| 1721 if( pParse->addrCrTab ){ | 1711 if( pParse->addrCrTab ){ |
| 1722 assert( v ); | 1712 assert( v ); |
| 1723 sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex); | 1713 sqlite3VdbeChangeOpcode(v, pParse->addrCrTab, OP_CreateIndex); |
| 1724 } | 1714 } |
| 1725 | 1715 |
| 1726 /* Locate the PRIMARY KEY index. Or, if this table was originally | 1716 /* Locate the PRIMARY KEY index. Or, if this table was originally |
| 1727 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. | 1717 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
| 1728 */ | 1718 */ |
| 1729 if( pTab->iPKey>=0 ){ | 1719 if( pTab->iPKey>=0 ){ |
| 1730 ExprList *pList; | 1720 ExprList *pList; |
| 1731 Token ipkToken; | 1721 Token ipkToken; |
| 1732 ipkToken.z = pTab->aCol[pTab->iPKey].zName; | 1722 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); |
| 1733 ipkToken.n = sqlite3Strlen30(ipkToken.z); | |
| 1734 pList = sqlite3ExprListAppend(pParse, 0, | 1723 pList = sqlite3ExprListAppend(pParse, 0, |
| 1735 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); | 1724 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
| 1736 if( pList==0 ) return; | 1725 if( pList==0 ) return; |
| 1737 pList->a[0].sortOrder = pParse->iPkSortOrder; | 1726 pList->a[0].sortOrder = pParse->iPkSortOrder; |
| 1738 assert( pParse->pNewTable==pTab ); | 1727 assert( pParse->pNewTable==pTab ); |
| 1739 pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0); | 1728 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 1740 if( pPk==0 ) return; | 1729 SQLITE_IDXTYPE_PRIMARYKEY); |
| 1741 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY; | 1730 if( db->mallocFailed ) return; |
| 1731 pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1742 pTab->iPKey = -1; | 1732 pTab->iPKey = -1; |
| 1743 }else{ | 1733 }else{ |
| 1744 pPk = sqlite3PrimaryKeyIndex(pTab); | 1734 pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1745 | 1735 |
| 1746 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master | 1736 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master |
| 1747 ** table entry. This is only required if currently generating VDBE | 1737 ** table entry. This is only required if currently generating VDBE |
| 1748 ** code for a CREATE TABLE (not when parsing one as part of reading | 1738 ** code for a CREATE TABLE (not when parsing one as part of reading |
| 1749 ** a database schema). */ | 1739 ** a database schema). */ |
| 1750 if( v ){ | 1740 if( v ){ |
| 1751 assert( db->init.busy==0 ); | 1741 assert( db->init.busy==0 ); |
| 1752 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); | 1742 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto); |
| 1753 } | 1743 } |
| 1754 | 1744 |
| 1755 /* | 1745 /* |
| 1756 ** Remove all redundant columns from the PRIMARY KEY. For example, change | 1746 ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 1757 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later | 1747 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 1758 ** code assumes the PRIMARY KEY contains no repeated columns. | 1748 ** code assumes the PRIMARY KEY contains no repeated columns. |
| 1759 */ | 1749 */ |
| 1760 for(i=j=1; i<pPk->nKeyCol; i++){ | 1750 for(i=j=1; i<pPk->nKeyCol; i++){ |
| 1761 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ | 1751 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ |
| 1762 pPk->nColumn--; | 1752 pPk->nColumn--; |
| 1763 }else{ | 1753 }else{ |
| 1764 pPk->aiColumn[j++] = pPk->aiColumn[i]; | 1754 pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 1765 } | 1755 } |
| 1766 } | 1756 } |
| 1767 pPk->nKeyCol = j; | 1757 pPk->nKeyCol = j; |
| 1768 } | 1758 } |
| 1759 assert( pPk!=0 ); |
| 1769 pPk->isCovering = 1; | 1760 pPk->isCovering = 1; |
| 1770 assert( pPk!=0 ); | 1761 if( !db->init.imposterTable ) pPk->uniqNotNull = 1; |
| 1771 nPk = pPk->nKeyCol; | 1762 nPk = pPk->nKeyCol; |
| 1772 | 1763 |
| 1773 /* Make sure every column of the PRIMARY KEY is NOT NULL. (Except, | |
| 1774 ** do not enforce this for imposter tables.) */ | |
| 1775 if( !db->init.imposterTable ){ | |
| 1776 for(i=0; i<nPk; i++){ | |
| 1777 pTab->aCol[pPk->aiColumn[i]].notNull = OE_Abort; | |
| 1778 } | |
| 1779 pPk->uniqNotNull = 1; | |
| 1780 } | |
| 1781 | |
| 1782 /* The root page of the PRIMARY KEY is the table root page */ | 1764 /* The root page of the PRIMARY KEY is the table root page */ |
| 1783 pPk->tnum = pTab->tnum; | 1765 pPk->tnum = pTab->tnum; |
| 1784 | 1766 |
| 1785 /* Update the in-memory representation of all UNIQUE indices by converting | 1767 /* Update the in-memory representation of all UNIQUE indices by converting |
| 1786 ** the final rowid column into one or more columns of the PRIMARY KEY. | 1768 ** the final rowid column into one or more columns of the PRIMARY KEY. |
| 1787 */ | 1769 */ |
| 1788 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ | 1770 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1789 int n; | 1771 int n; |
| 1790 if( IsPrimaryKeyIndex(pIdx) ) continue; | 1772 if( IsPrimaryKeyIndex(pIdx) ) continue; |
| 1791 for(i=n=0; i<nPk; i++){ | 1773 for(i=n=0; i<nPk; i++){ |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1866 p = pParse->pNewTable; | 1848 p = pParse->pNewTable; |
| 1867 if( p==0 ) return; | 1849 if( p==0 ) return; |
| 1868 | 1850 |
| 1869 assert( !db->init.busy || !pSelect ); | 1851 assert( !db->init.busy || !pSelect ); |
| 1870 | 1852 |
| 1871 /* If the db->init.busy is 1 it means we are reading the SQL off the | 1853 /* If the db->init.busy is 1 it means we are reading the SQL off the |
| 1872 ** "sqlite_master" or "sqlite_temp_master" table on the disk. | 1854 ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
| 1873 ** So do not write to the disk again. Extract the root page number | 1855 ** So do not write to the disk again. Extract the root page number |
| 1874 ** for the table from the db->init.newTnum field. (The page number | 1856 ** for the table from the db->init.newTnum field. (The page number |
| 1875 ** should have been put there by the sqliteOpenCb routine.) | 1857 ** should have been put there by the sqliteOpenCb routine.) |
| 1858 ** |
| 1859 ** If the root page number is 1, that means this is the sqlite_master |
| 1860 ** table itself. So mark it read-only. |
| 1876 */ | 1861 */ |
| 1877 if( db->init.busy ){ | 1862 if( db->init.busy ){ |
| 1878 p->tnum = db->init.newTnum; | 1863 p->tnum = db->init.newTnum; |
| 1864 if( p->tnum==1 ) p->tabFlags |= TF_Readonly; |
| 1879 } | 1865 } |
| 1880 | 1866 |
| 1881 /* Special processing for WITHOUT ROWID Tables */ | 1867 /* Special processing for WITHOUT ROWID Tables */ |
| 1882 if( tabOpts & TF_WithoutRowid ){ | 1868 if( tabOpts & TF_WithoutRowid ){ |
| 1883 if( (p->tabFlags & TF_Autoincrement) ){ | 1869 if( (p->tabFlags & TF_Autoincrement) ){ |
| 1884 sqlite3ErrorMsg(pParse, | 1870 sqlite3ErrorMsg(pParse, |
| 1885 "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); | 1871 "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
| 1886 return; | 1872 return; |
| 1887 } | 1873 } |
| 1888 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ | 1874 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1969 regRowid = ++pParse->nMem; | 1955 regRowid = ++pParse->nMem; |
| 1970 assert(pParse->nTab==1); | 1956 assert(pParse->nTab==1); |
| 1971 sqlite3MayAbort(pParse); | 1957 sqlite3MayAbort(pParse); |
| 1972 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); | 1958 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
| 1973 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); | 1959 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
| 1974 pParse->nTab = 2; | 1960 pParse->nTab = 2; |
| 1975 addrTop = sqlite3VdbeCurrentAddr(v) + 1; | 1961 addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 1976 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); | 1962 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
| 1977 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); | 1963 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 1978 sqlite3Select(pParse, pSelect, &dest); | 1964 sqlite3Select(pParse, pSelect, &dest); |
| 1979 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); | 1965 sqlite3VdbeEndCoroutine(v, regYield); |
| 1980 sqlite3VdbeJumpHere(v, addrTop - 1); | 1966 sqlite3VdbeJumpHere(v, addrTop - 1); |
| 1981 if( pParse->nErr ) return; | 1967 if( pParse->nErr ) return; |
| 1982 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); | 1968 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); |
| 1983 if( pSelTab==0 ) return; | 1969 if( pSelTab==0 ) return; |
| 1984 assert( p->aCol==0 ); | 1970 assert( p->aCol==0 ); |
| 1985 p->nCol = pSelTab->nCol; | 1971 p->nCol = pSelTab->nCol; |
| 1986 p->aCol = pSelTab->aCol; | 1972 p->aCol = pSelTab->aCol; |
| 1987 pSelTab->nCol = 0; | 1973 pSelTab->nCol = 0; |
| 1988 pSelTab->aCol = 0; | 1974 pSelTab->aCol = 0; |
| 1989 sqlite3DeleteTable(db, pSelTab); | 1975 sqlite3DeleteTable(db, pSelTab); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 2011 } | 1997 } |
| 2012 | 1998 |
| 2013 /* A slot for the record has already been allocated in the | 1999 /* A slot for the record has already been allocated in the |
| 2014 ** SQLITE_MASTER table. We just need to update that slot with all | 2000 ** SQLITE_MASTER table. We just need to update that slot with all |
| 2015 ** the information we've collected. | 2001 ** the information we've collected. |
| 2016 */ | 2002 */ |
| 2017 sqlite3NestedParse(pParse, | 2003 sqlite3NestedParse(pParse, |
| 2018 "UPDATE %Q.%s " | 2004 "UPDATE %Q.%s " |
| 2019 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " | 2005 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " |
| 2020 "WHERE rowid=#%d", | 2006 "WHERE rowid=#%d", |
| 2021 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), | 2007 db->aDb[iDb].zDbSName, MASTER_NAME, |
| 2022 zType, | 2008 zType, |
| 2023 p->zName, | 2009 p->zName, |
| 2024 p->zName, | 2010 p->zName, |
| 2025 pParse->regRoot, | 2011 pParse->regRoot, |
| 2026 zStmt, | 2012 zStmt, |
| 2027 pParse->regRowid | 2013 pParse->regRowid |
| 2028 ); | 2014 ); |
| 2029 sqlite3DbFree(db, zStmt); | 2015 sqlite3DbFree(db, zStmt); |
| 2030 sqlite3ChangeCookie(pParse, iDb); | 2016 sqlite3ChangeCookie(pParse, iDb); |
| 2031 | 2017 |
| 2032 #ifndef SQLITE_OMIT_AUTOINCREMENT | 2018 #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2033 /* Check to see if we need to create an sqlite_sequence table for | 2019 /* Check to see if we need to create an sqlite_sequence table for |
| 2034 ** keeping track of autoincrement keys. | 2020 ** keeping track of autoincrement keys. |
| 2035 */ | 2021 */ |
| 2036 if( p->tabFlags & TF_Autoincrement ){ | 2022 if( (p->tabFlags & TF_Autoincrement)!=0 ){ |
| 2037 Db *pDb = &db->aDb[iDb]; | 2023 Db *pDb = &db->aDb[iDb]; |
| 2038 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 2024 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2039 if( pDb->pSchema->pSeqTab==0 ){ | 2025 if( pDb->pSchema->pSeqTab==0 ){ |
| 2040 sqlite3NestedParse(pParse, | 2026 sqlite3NestedParse(pParse, |
| 2041 "CREATE TABLE %Q.sqlite_sequence(name,seq)", | 2027 "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
| 2042 pDb->zName | 2028 pDb->zDbSName |
| 2043 ); | 2029 ); |
| 2044 } | 2030 } |
| 2045 } | 2031 } |
| 2046 #endif | 2032 #endif |
| 2047 | 2033 |
| 2048 /* Reparse everything to update our internal data structures */ | 2034 /* Reparse everything to update our internal data structures */ |
| 2049 sqlite3VdbeAddParseSchemaOp(v, iDb, | 2035 sqlite3VdbeAddParseSchemaOp(v, iDb, |
| 2050 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); | 2036 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName)); |
| 2051 } | 2037 } |
| 2052 | 2038 |
| 2053 | 2039 |
| 2054 /* Add the table to the in-memory representation of the database. | 2040 /* Add the table to the in-memory representation of the database. |
| 2055 */ | 2041 */ |
| 2056 if( db->init.busy ){ | 2042 if( db->init.busy ){ |
| 2057 Table *pOld; | 2043 Table *pOld; |
| 2058 Schema *pSchema = p->pSchema; | 2044 Schema *pSchema = p->pSchema; |
| 2059 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 2045 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2060 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); | 2046 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); |
| 2061 if( pOld ){ | 2047 if( pOld ){ |
| 2062 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ | 2048 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 2063 db->mallocFailed = 1; | 2049 sqlite3OomFault(db); |
| 2064 return; | 2050 return; |
| 2065 } | 2051 } |
| 2066 pParse->pNewTable = 0; | 2052 pParse->pNewTable = 0; |
| 2067 db->flags |= SQLITE_InternChanges; | 2053 db->flags |= SQLITE_InternChanges; |
| 2068 | 2054 |
| 2069 #ifndef SQLITE_OMIT_ALTERTABLE | 2055 #ifndef SQLITE_OMIT_ALTERTABLE |
| 2070 if( !p->pSelect ){ | 2056 if( !p->pSelect ){ |
| 2071 const char *zName = (const char *)pParse->sNameToken.z; | 2057 const char *zName = (const char *)pParse->sNameToken.z; |
| 2072 int nName; | 2058 int nName; |
| 2073 assert( !pSelect && pCons && pEnd ); | 2059 assert( !pSelect && pCons && pEnd ); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2156 ** The Table structure pTable is really a VIEW. Fill in the names of | 2142 ** The Table structure pTable is really a VIEW. Fill in the names of |
| 2157 ** the columns of the view in the pTable structure. Return the number | 2143 ** the columns of the view in the pTable structure. Return the number |
| 2158 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. | 2144 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
| 2159 */ | 2145 */ |
| 2160 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ | 2146 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 2161 Table *pSelTab; /* A fake table from which we get the result set */ | 2147 Table *pSelTab; /* A fake table from which we get the result set */ |
| 2162 Select *pSel; /* Copy of the SELECT that implements the view */ | 2148 Select *pSel; /* Copy of the SELECT that implements the view */ |
| 2163 int nErr = 0; /* Number of errors encountered */ | 2149 int nErr = 0; /* Number of errors encountered */ |
| 2164 int n; /* Temporarily holds the number of cursors assigned */ | 2150 int n; /* Temporarily holds the number of cursors assigned */ |
| 2165 sqlite3 *db = pParse->db; /* Database connection for malloc errors */ | 2151 sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
| 2152 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2166 sqlite3_xauth xAuth; /* Saved xAuth pointer */ | 2153 sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
| 2167 u8 bEnabledLA; /* Saved db->lookaside.bEnabled state */ | 2154 #endif |
| 2168 | 2155 |
| 2169 assert( pTable ); | 2156 assert( pTable ); |
| 2170 | 2157 |
| 2171 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2158 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2172 if( sqlite3VtabCallConnect(pParse, pTable) ){ | 2159 if( sqlite3VtabCallConnect(pParse, pTable) ){ |
| 2173 return SQLITE_ERROR; | 2160 return SQLITE_ERROR; |
| 2174 } | 2161 } |
| 2175 if( IsVirtual(pTable) ) return 0; | 2162 if( IsVirtual(pTable) ) return 0; |
| 2176 #endif | 2163 #endif |
| 2177 | 2164 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 2203 assert( pTable->nCol>=0 ); | 2190 assert( pTable->nCol>=0 ); |
| 2204 | 2191 |
| 2205 /* If we get this far, it means we need to compute the table names. | 2192 /* If we get this far, it means we need to compute the table names. |
| 2206 ** Note that the call to sqlite3ResultSetOfSelect() will expand any | 2193 ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
| 2207 ** "*" elements in the results set of the view and will assign cursors | 2194 ** "*" elements in the results set of the view and will assign cursors |
| 2208 ** to the elements of the FROM clause. But we do not want these changes | 2195 ** to the elements of the FROM clause. But we do not want these changes |
| 2209 ** to be permanent. So the computation is done on a copy of the SELECT | 2196 ** to be permanent. So the computation is done on a copy of the SELECT |
| 2210 ** statement that defines the view. | 2197 ** statement that defines the view. |
| 2211 */ | 2198 */ |
| 2212 assert( pTable->pSelect ); | 2199 assert( pTable->pSelect ); |
| 2213 bEnabledLA = db->lookaside.bEnabled; | 2200 pSel = sqlite3SelectDup(db, pTable->pSelect, 0); |
| 2214 if( pTable->pCheck ){ | 2201 if( pSel ){ |
| 2215 db->lookaside.bEnabled = 0; | 2202 n = pParse->nTab; |
| 2216 sqlite3ColumnsFromExprList(pParse, pTable->pCheck, | 2203 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 2217 &pTable->nCol, &pTable->aCol); | 2204 pTable->nCol = -1; |
| 2218 }else{ | 2205 db->lookaside.bDisable++; |
| 2219 pSel = sqlite3SelectDup(db, pTable->pSelect, 0); | |
| 2220 if( pSel ){ | |
| 2221 n = pParse->nTab; | |
| 2222 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); | |
| 2223 pTable->nCol = -1; | |
| 2224 db->lookaside.bEnabled = 0; | |
| 2225 #ifndef SQLITE_OMIT_AUTHORIZATION | 2206 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2226 xAuth = db->xAuth; | 2207 xAuth = db->xAuth; |
| 2227 db->xAuth = 0; | 2208 db->xAuth = 0; |
| 2228 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); | 2209 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
| 2229 db->xAuth = xAuth; | 2210 db->xAuth = xAuth; |
| 2230 #else | 2211 #else |
| 2231 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); | 2212 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); |
| 2232 #endif | 2213 #endif |
| 2233 pParse->nTab = n; | 2214 pParse->nTab = n; |
| 2234 if( pSelTab ){ | 2215 if( pTable->pCheck ){ |
| 2235 assert( pTable->aCol==0 ); | 2216 /* CREATE VIEW name(arglist) AS ... |
| 2236 pTable->nCol = pSelTab->nCol; | 2217 ** The names of the columns in the table are taken from |
| 2237 pTable->aCol = pSelTab->aCol; | 2218 ** arglist which is stored in pTable->pCheck. The pCheck field |
| 2238 pSelTab->nCol = 0; | 2219 ** normally holds CHECK constraints on an ordinary table, but for |
| 2239 pSelTab->aCol = 0; | 2220 ** a VIEW it holds the list of column names. |
| 2240 sqlite3DeleteTable(db, pSelTab); | 2221 */ |
| 2241 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); | 2222 sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
| 2242 }else{ | 2223 &pTable->nCol, &pTable->aCol); |
| 2243 pTable->nCol = 0; | 2224 if( db->mallocFailed==0 |
| 2244 nErr++; | 2225 && pParse->nErr==0 |
| 2226 && pTable->nCol==pSel->pEList->nExpr |
| 2227 ){ |
| 2228 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel); |
| 2245 } | 2229 } |
| 2246 sqlite3SelectDelete(db, pSel); | 2230 }else if( pSelTab ){ |
| 2247 } else { | 2231 /* CREATE VIEW name AS... without an argument list. Construct |
| 2232 ** the column names from the SELECT statement that defines the view. |
| 2233 */ |
| 2234 assert( pTable->aCol==0 ); |
| 2235 pTable->nCol = pSelTab->nCol; |
| 2236 pTable->aCol = pSelTab->aCol; |
| 2237 pSelTab->nCol = 0; |
| 2238 pSelTab->aCol = 0; |
| 2239 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
| 2240 }else{ |
| 2241 pTable->nCol = 0; |
| 2248 nErr++; | 2242 nErr++; |
| 2249 } | 2243 } |
| 2244 sqlite3DeleteTable(db, pSelTab); |
| 2245 sqlite3SelectDelete(db, pSel); |
| 2246 db->lookaside.bDisable--; |
| 2247 } else { |
| 2248 nErr++; |
| 2250 } | 2249 } |
| 2251 db->lookaside.bEnabled = bEnabledLA; | |
| 2252 pTable->pSchema->schemaFlags |= DB_UnresetViews; | 2250 pTable->pSchema->schemaFlags |= DB_UnresetViews; |
| 2253 #endif /* SQLITE_OMIT_VIEW */ | 2251 #endif /* SQLITE_OMIT_VIEW */ |
| 2254 return nErr; | 2252 return nErr; |
| 2255 } | 2253 } |
| 2256 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ | 2254 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 2257 | 2255 |
| 2258 #ifndef SQLITE_OMIT_VIEW | 2256 #ifndef SQLITE_OMIT_VIEW |
| 2259 /* | 2257 /* |
| 2260 ** Clear the column names from every VIEW in database idx. | 2258 ** Clear the column names from every VIEW in database idx. |
| 2261 */ | 2259 */ |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2321 | 2319 |
| 2322 /* | 2320 /* |
| 2323 ** Write code to erase the table with root-page iTable from database iDb. | 2321 ** Write code to erase the table with root-page iTable from database iDb. |
| 2324 ** Also write code to modify the sqlite_master table and internal schema | 2322 ** Also write code to modify the sqlite_master table and internal schema |
| 2325 ** if a root-page of another table is moved by the btree-layer whilst | 2323 ** if a root-page of another table is moved by the btree-layer whilst |
| 2326 ** erasing iTable (this can happen with an auto-vacuum database). | 2324 ** erasing iTable (this can happen with an auto-vacuum database). |
| 2327 */ | 2325 */ |
| 2328 static void destroyRootPage(Parse *pParse, int iTable, int iDb){ | 2326 static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 2329 Vdbe *v = sqlite3GetVdbe(pParse); | 2327 Vdbe *v = sqlite3GetVdbe(pParse); |
| 2330 int r1 = sqlite3GetTempReg(pParse); | 2328 int r1 = sqlite3GetTempReg(pParse); |
| 2329 assert( iTable>1 ); |
| 2331 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); | 2330 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
| 2332 sqlite3MayAbort(pParse); | 2331 sqlite3MayAbort(pParse); |
| 2333 #ifndef SQLITE_OMIT_AUTOVACUUM | 2332 #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2334 /* OP_Destroy stores an in integer r1. If this integer | 2333 /* OP_Destroy stores an in integer r1. If this integer |
| 2335 ** is non-zero, then it is the root page number of a table moved to | 2334 ** is non-zero, then it is the root page number of a table moved to |
| 2336 ** location iTable. The following code modifies the sqlite_master table to | 2335 ** location iTable. The following code modifies the sqlite_master table to |
| 2337 ** reflect this. | 2336 ** reflect this. |
| 2338 ** | 2337 ** |
| 2339 ** The "#NNN" in the SQL is a special constant that means whatever value | 2338 ** The "#NNN" in the SQL is a special constant that means whatever value |
| 2340 ** is in register NNN. See grammar rules associated with the TK_REGISTER | 2339 ** is in register NNN. See grammar rules associated with the TK_REGISTER |
| 2341 ** token for additional information. | 2340 ** token for additional information. |
| 2342 */ | 2341 */ |
| 2343 sqlite3NestedParse(pParse, | 2342 sqlite3NestedParse(pParse, |
| 2344 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", | 2343 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", |
| 2345 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1); | 2344 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1); |
| 2346 #endif | 2345 #endif |
| 2347 sqlite3ReleaseTempReg(pParse, r1); | 2346 sqlite3ReleaseTempReg(pParse, r1); |
| 2348 } | 2347 } |
| 2349 | 2348 |
| 2350 /* | 2349 /* |
| 2351 ** Write VDBE code to erase table pTab and all associated indices on disk. | 2350 ** Write VDBE code to erase table pTab and all associated indices on disk. |
| 2352 ** Code to update the sqlite_master tables and internal schema definitions | 2351 ** Code to update the sqlite_master tables and internal schema definitions |
| 2353 ** in case a root-page belonging to another table is moved by the btree layer | 2352 ** in case a root-page belonging to another table is moved by the btree layer |
| 2354 ** is also added (this can happen with an auto-vacuum database). | 2353 ** is also added (this can happen with an auto-vacuum database). |
| 2355 */ | 2354 */ |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2411 ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) | 2410 ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
| 2412 ** after a DROP INDEX or DROP TABLE command. | 2411 ** after a DROP INDEX or DROP TABLE command. |
| 2413 */ | 2412 */ |
| 2414 static void sqlite3ClearStatTables( | 2413 static void sqlite3ClearStatTables( |
| 2415 Parse *pParse, /* The parsing context */ | 2414 Parse *pParse, /* The parsing context */ |
| 2416 int iDb, /* The database number */ | 2415 int iDb, /* The database number */ |
| 2417 const char *zType, /* "idx" or "tbl" */ | 2416 const char *zType, /* "idx" or "tbl" */ |
| 2418 const char *zName /* Name of index or table */ | 2417 const char *zName /* Name of index or table */ |
| 2419 ){ | 2418 ){ |
| 2420 int i; | 2419 int i; |
| 2421 const char *zDbName = pParse->db->aDb[iDb].zName; | 2420 const char *zDbName = pParse->db->aDb[iDb].zDbSName; |
| 2422 for(i=1; i<=4; i++){ | 2421 for(i=1; i<=4; i++){ |
| 2423 char zTab[24]; | 2422 char zTab[24]; |
| 2424 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); | 2423 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); |
| 2425 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ | 2424 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ |
| 2426 sqlite3NestedParse(pParse, | 2425 sqlite3NestedParse(pParse, |
| 2427 "DELETE FROM %Q.%s WHERE %s=%Q", | 2426 "DELETE FROM %Q.%s WHERE %s=%Q", |
| 2428 zDbName, zTab, zType, zName | 2427 zDbName, zTab, zType, zName |
| 2429 ); | 2428 ); |
| 2430 } | 2429 } |
| 2431 } | 2430 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2464 | 2463 |
| 2465 #ifndef SQLITE_OMIT_AUTOINCREMENT | 2464 #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2466 /* Remove any entries of the sqlite_sequence table associated with | 2465 /* Remove any entries of the sqlite_sequence table associated with |
| 2467 ** the table being dropped. This is done before the table is dropped | 2466 ** the table being dropped. This is done before the table is dropped |
| 2468 ** at the btree level, in case the sqlite_sequence table needs to | 2467 ** at the btree level, in case the sqlite_sequence table needs to |
| 2469 ** move as a result of the drop (can happen in auto-vacuum mode). | 2468 ** move as a result of the drop (can happen in auto-vacuum mode). |
| 2470 */ | 2469 */ |
| 2471 if( pTab->tabFlags & TF_Autoincrement ){ | 2470 if( pTab->tabFlags & TF_Autoincrement ){ |
| 2472 sqlite3NestedParse(pParse, | 2471 sqlite3NestedParse(pParse, |
| 2473 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", | 2472 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
| 2474 pDb->zName, pTab->zName | 2473 pDb->zDbSName, pTab->zName |
| 2475 ); | 2474 ); |
| 2476 } | 2475 } |
| 2477 #endif | 2476 #endif |
| 2478 | 2477 |
| 2479 /* Drop all SQLITE_MASTER table and index entries that refer to the | 2478 /* Drop all SQLITE_MASTER table and index entries that refer to the |
| 2480 ** table. The program name loops through the master table and deletes | 2479 ** table. The program name loops through the master table and deletes |
| 2481 ** every row that refers to a table of the same name as the one being | 2480 ** every row that refers to a table of the same name as the one being |
| 2482 ** dropped. Triggers are handled separately because a trigger can be | 2481 ** dropped. Triggers are handled separately because a trigger can be |
| 2483 ** created in the temp database that refers to a table in another | 2482 ** created in the temp database that refers to a table in another |
| 2484 ** database. | 2483 ** database. |
| 2485 */ | 2484 */ |
| 2486 sqlite3NestedParse(pParse, | 2485 sqlite3NestedParse(pParse, |
| 2487 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", | 2486 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
| 2488 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); | 2487 pDb->zDbSName, MASTER_NAME, pTab->zName); |
| 2489 if( !isView && !IsVirtual(pTab) ){ | 2488 if( !isView && !IsVirtual(pTab) ){ |
| 2490 destroyTable(pParse, pTab); | 2489 destroyTable(pParse, pTab); |
| 2491 } | 2490 } |
| 2492 | 2491 |
| 2493 /* Remove the table entry from SQLite's internal schema and modify | 2492 /* Remove the table entry from SQLite's internal schema and modify |
| 2494 ** the schema cookie. | 2493 ** the schema cookie. |
| 2495 */ | 2494 */ |
| 2496 if( IsVirtual(pTab) ){ | 2495 if( IsVirtual(pTab) ){ |
| 2497 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); | 2496 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
| 2498 } | 2497 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 2511 sqlite3 *db = pParse->db; | 2510 sqlite3 *db = pParse->db; |
| 2512 int iDb; | 2511 int iDb; |
| 2513 | 2512 |
| 2514 if( db->mallocFailed ){ | 2513 if( db->mallocFailed ){ |
| 2515 goto exit_drop_table; | 2514 goto exit_drop_table; |
| 2516 } | 2515 } |
| 2517 assert( pParse->nErr==0 ); | 2516 assert( pParse->nErr==0 ); |
| 2518 assert( pName->nSrc==1 ); | 2517 assert( pName->nSrc==1 ); |
| 2519 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; | 2518 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
| 2520 if( noErr ) db->suppressErr++; | 2519 if( noErr ) db->suppressErr++; |
| 2520 assert( isView==0 || isView==LOCATE_VIEW ); |
| 2521 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); | 2521 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
| 2522 if( noErr ) db->suppressErr--; | 2522 if( noErr ) db->suppressErr--; |
| 2523 | 2523 |
| 2524 if( pTab==0 ){ | 2524 if( pTab==0 ){ |
| 2525 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); | 2525 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 2526 goto exit_drop_table; | 2526 goto exit_drop_table; |
| 2527 } | 2527 } |
| 2528 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 2528 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 2529 assert( iDb>=0 && iDb<db->nDb ); | 2529 assert( iDb>=0 && iDb<db->nDb ); |
| 2530 | 2530 |
| 2531 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure | 2531 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
| 2532 ** it is initialized. | 2532 ** it is initialized. |
| 2533 */ | 2533 */ |
| 2534 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ | 2534 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 2535 goto exit_drop_table; | 2535 goto exit_drop_table; |
| 2536 } | 2536 } |
| 2537 #ifndef SQLITE_OMIT_AUTHORIZATION | 2537 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2538 { | 2538 { |
| 2539 int code; | 2539 int code; |
| 2540 const char *zTab = SCHEMA_TABLE(iDb); | 2540 const char *zTab = SCHEMA_TABLE(iDb); |
| 2541 const char *zDb = db->aDb[iDb].zName; | 2541 const char *zDb = db->aDb[iDb].zDbSName; |
| 2542 const char *zArg2 = 0; | 2542 const char *zArg2 = 0; |
| 2543 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ | 2543 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
| 2544 goto exit_drop_table; | 2544 goto exit_drop_table; |
| 2545 } | 2545 } |
| 2546 if( isView ){ | 2546 if( isView ){ |
| 2547 if( !OMIT_TEMPDB && iDb==1 ){ | 2547 if( !OMIT_TEMPDB && iDb==1 ){ |
| 2548 code = SQLITE_DROP_TEMP_VIEW; | 2548 code = SQLITE_DROP_TEMP_VIEW; |
| 2549 }else{ | 2549 }else{ |
| 2550 code = SQLITE_DROP_VIEW; | 2550 code = SQLITE_DROP_VIEW; |
| 2551 } | 2551 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2706 } | 2706 } |
| 2707 pFKey->isDeferred = 0; | 2707 pFKey->isDeferred = 0; |
| 2708 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ | 2708 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
| 2709 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ | 2709 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
| 2710 | 2710 |
| 2711 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); | 2711 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
| 2712 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, | 2712 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
| 2713 pFKey->zTo, (void *)pFKey | 2713 pFKey->zTo, (void *)pFKey |
| 2714 ); | 2714 ); |
| 2715 if( pNextTo==pFKey ){ | 2715 if( pNextTo==pFKey ){ |
| 2716 db->mallocFailed = 1; | 2716 sqlite3OomFault(db); |
| 2717 goto fk_end; | 2717 goto fk_end; |
| 2718 } | 2718 } |
| 2719 if( pNextTo ){ | 2719 if( pNextTo ){ |
| 2720 assert( pNextTo->pPrevTo==0 ); | 2720 assert( pNextTo->pPrevTo==0 ); |
| 2721 pFKey->pNextTo = pNextTo; | 2721 pFKey->pNextTo = pNextTo; |
| 2722 pNextTo->pPrevTo = pFKey; | 2722 pNextTo->pPrevTo = pFKey; |
| 2723 } | 2723 } |
| 2724 | 2724 |
| 2725 /* Link the foreign key to the table as the last step. | 2725 /* Link the foreign key to the table as the last step. |
| 2726 */ | 2726 */ |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2772 int tnum; /* Root page of index */ | 2772 int tnum; /* Root page of index */ |
| 2773 int iPartIdxLabel; /* Jump to this label to skip a row */ | 2773 int iPartIdxLabel; /* Jump to this label to skip a row */ |
| 2774 Vdbe *v; /* Generate code into this virtual machine */ | 2774 Vdbe *v; /* Generate code into this virtual machine */ |
| 2775 KeyInfo *pKey; /* KeyInfo for index */ | 2775 KeyInfo *pKey; /* KeyInfo for index */ |
| 2776 int regRecord; /* Register holding assembled index record */ | 2776 int regRecord; /* Register holding assembled index record */ |
| 2777 sqlite3 *db = pParse->db; /* The database connection */ | 2777 sqlite3 *db = pParse->db; /* The database connection */ |
| 2778 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); | 2778 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
| 2779 | 2779 |
| 2780 #ifndef SQLITE_OMIT_AUTHORIZATION | 2780 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 2781 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, | 2781 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
| 2782 db->aDb[iDb].zName ) ){ | 2782 db->aDb[iDb].zDbSName ) ){ |
| 2783 return; | 2783 return; |
| 2784 } | 2784 } |
| 2785 #endif | 2785 #endif |
| 2786 | 2786 |
| 2787 /* Require a write-lock on the table to perform this operation */ | 2787 /* Require a write-lock on the table to perform this operation */ |
| 2788 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); | 2788 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 2789 | 2789 |
| 2790 v = sqlite3GetVdbe(pParse); | 2790 v = sqlite3GetVdbe(pParse); |
| 2791 if( v==0 ) return; | 2791 if( v==0 ) return; |
| 2792 if( memRootPage>=0 ){ | 2792 if( memRootPage>=0 ){ |
| 2793 tnum = memRootPage; | 2793 tnum = memRootPage; |
| 2794 }else{ | 2794 }else{ |
| 2795 tnum = pIndex->tnum; | 2795 tnum = pIndex->tnum; |
| 2796 } | 2796 } |
| 2797 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); | 2797 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
| 2798 assert( pKey!=0 || db->mallocFailed || pParse->nErr ); |
| 2798 | 2799 |
| 2799 /* Open the sorter cursor if we are to use one. */ | 2800 /* Open the sorter cursor if we are to use one. */ |
| 2800 iSorter = pParse->nTab++; | 2801 iSorter = pParse->nTab++; |
| 2801 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) | 2802 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
| 2802 sqlite3KeyInfoRef(pKey), P4_KEYINFO); | 2803 sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
| 2803 | 2804 |
| 2804 /* Open the table. Loop through all rows of the table, inserting index | 2805 /* Open the table. Loop through all rows of the table, inserting index |
| 2805 ** records into the sorter. */ | 2806 ** records into the sorter. */ |
| 2806 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); | 2807 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 2807 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); | 2808 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
| 2808 regRecord = sqlite3GetTempReg(pParse); | 2809 regRecord = sqlite3GetTempReg(pParse); |
| 2809 | 2810 |
| 2810 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); | 2811 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
| 2811 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); | 2812 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
| 2812 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); | 2813 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
| 2813 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); | 2814 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
| 2814 sqlite3VdbeJumpHere(v, addr1); | 2815 sqlite3VdbeJumpHere(v, addr1); |
| 2815 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); | 2816 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
| 2816 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, | 2817 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, |
| 2817 (char *)pKey, P4_KEYINFO); | 2818 (char *)pKey, P4_KEYINFO); |
| 2818 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); | 2819 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
| 2819 | 2820 |
| 2820 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); | 2821 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
| 2821 assert( pKey!=0 || db->mallocFailed || pParse->nErr ); | 2822 if( IsUniqueIndex(pIndex) ){ |
| 2822 if( IsUniqueIndex(pIndex) && pKey!=0 ){ | |
| 2823 int j2 = sqlite3VdbeCurrentAddr(v) + 3; | 2823 int j2 = sqlite3VdbeCurrentAddr(v) + 3; |
| 2824 sqlite3VdbeGoto(v, j2); | 2824 sqlite3VdbeGoto(v, j2); |
| 2825 addr2 = sqlite3VdbeCurrentAddr(v); | 2825 addr2 = sqlite3VdbeCurrentAddr(v); |
| 2826 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, | 2826 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
| 2827 pIndex->nKeyCol); VdbeCoverage(v); | 2827 pIndex->nKeyCol); VdbeCoverage(v); |
| 2828 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); | 2828 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
| 2829 }else{ | 2829 }else{ |
| 2830 addr2 = sqlite3VdbeCurrentAddr(v); | 2830 addr2 = sqlite3VdbeCurrentAddr(v); |
| 2831 } | 2831 } |
| 2832 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); | 2832 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
| 2833 sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); | 2833 sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); |
| 2834 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); | 2834 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
| 2835 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); | 2835 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2836 sqlite3ReleaseTempReg(pParse, regRecord); | 2836 sqlite3ReleaseTempReg(pParse, regRecord); |
| 2837 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); | 2837 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
| 2838 sqlite3VdbeJumpHere(v, addr1); | 2838 sqlite3VdbeJumpHere(v, addr1); |
| 2839 | 2839 |
| 2840 sqlite3VdbeAddOp1(v, OP_Close, iTab); | 2840 sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 2841 sqlite3VdbeAddOp1(v, OP_Close, iIdx); | 2841 sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
| 2842 sqlite3VdbeAddOp1(v, OP_Close, iSorter); | 2842 sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
| 2843 } | 2843 } |
| 2844 | 2844 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2881 ** Create a new index for an SQL table. pName1.pName2 is the name of the index | 2881 ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 2882 ** and pTblList is the name of the table that is to be indexed. Both will | 2882 ** and pTblList is the name of the table that is to be indexed. Both will |
| 2883 ** be NULL for a primary key or an index that is created to satisfy a | 2883 ** be NULL for a primary key or an index that is created to satisfy a |
| 2884 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable | 2884 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
| 2885 ** as the table to be indexed. pParse->pNewTable is a table that is | 2885 ** as the table to be indexed. pParse->pNewTable is a table that is |
| 2886 ** currently being constructed by a CREATE TABLE statement. | 2886 ** currently being constructed by a CREATE TABLE statement. |
| 2887 ** | 2887 ** |
| 2888 ** pList is a list of columns to be indexed. pList will be NULL if this | 2888 ** pList is a list of columns to be indexed. pList will be NULL if this |
| 2889 ** is a primary key or unique-constraint on the most recent column added | 2889 ** is a primary key or unique-constraint on the most recent column added |
| 2890 ** to the table currently under construction. | 2890 ** to the table currently under construction. |
| 2891 ** | |
| 2892 ** If the index is created successfully, return a pointer to the new Index | |
| 2893 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index | |
| 2894 ** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY) | |
| 2895 */ | 2891 */ |
| 2896 Index *sqlite3CreateIndex( | 2892 void sqlite3CreateIndex( |
| 2897 Parse *pParse, /* All information about this parse */ | 2893 Parse *pParse, /* All information about this parse */ |
| 2898 Token *pName1, /* First part of index name. May be NULL */ | 2894 Token *pName1, /* First part of index name. May be NULL */ |
| 2899 Token *pName2, /* Second part of index name. May be NULL */ | 2895 Token *pName2, /* Second part of index name. May be NULL */ |
| 2900 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ | 2896 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
| 2901 ExprList *pList, /* A list of columns to be indexed */ | 2897 ExprList *pList, /* A list of columns to be indexed */ |
| 2902 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ | 2898 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 2903 Token *pStart, /* The CREATE token that begins this statement */ | 2899 Token *pStart, /* The CREATE token that begins this statement */ |
| 2904 Expr *pPIWhere, /* WHERE clause for partial indices */ | 2900 Expr *pPIWhere, /* WHERE clause for partial indices */ |
| 2905 int sortOrder, /* Sort order of primary key when pList==NULL */ | 2901 int sortOrder, /* Sort order of primary key when pList==NULL */ |
| 2906 int ifNotExist /* Omit error if index already exists */ | 2902 int ifNotExist, /* Omit error if index already exists */ |
| 2903 u8 idxType /* The index type */ |
| 2907 ){ | 2904 ){ |
| 2908 Index *pRet = 0; /* Pointer to return */ | |
| 2909 Table *pTab = 0; /* Table to be indexed */ | 2905 Table *pTab = 0; /* Table to be indexed */ |
| 2910 Index *pIndex = 0; /* The index to be created */ | 2906 Index *pIndex = 0; /* The index to be created */ |
| 2911 char *zName = 0; /* Name of the index */ | 2907 char *zName = 0; /* Name of the index */ |
| 2912 int nName; /* Number of characters in zName */ | 2908 int nName; /* Number of characters in zName */ |
| 2913 int i, j; | 2909 int i, j; |
| 2914 DbFixer sFix; /* For assigning database names to pTable */ | 2910 DbFixer sFix; /* For assigning database names to pTable */ |
| 2915 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ | 2911 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
| 2916 sqlite3 *db = pParse->db; | 2912 sqlite3 *db = pParse->db; |
| 2917 Db *pDb; /* The specific table containing the indexed database */ | 2913 Db *pDb; /* The specific table containing the indexed database */ |
| 2918 int iDb; /* Index of the database that is being written */ | 2914 int iDb; /* Index of the database that is being written */ |
| 2919 Token *pName = 0; /* Unqualified name of the index to create */ | 2915 Token *pName = 0; /* Unqualified name of the index to create */ |
| 2920 struct ExprList_item *pListItem; /* For looping over pList */ | 2916 struct ExprList_item *pListItem; /* For looping over pList */ |
| 2921 int nExtra = 0; /* Space allocated for zExtra[] */ | 2917 int nExtra = 0; /* Space allocated for zExtra[] */ |
| 2922 int nExtraCol; /* Number of extra columns needed */ | 2918 int nExtraCol; /* Number of extra columns needed */ |
| 2923 char *zExtra = 0; /* Extra space after the Index object */ | 2919 char *zExtra = 0; /* Extra space after the Index object */ |
| 2924 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ | 2920 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
| 2925 | 2921 |
| 2926 if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){ | 2922 if( db->mallocFailed || pParse->nErr>0 ){ |
| 2923 goto exit_create_index; |
| 2924 } |
| 2925 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
| 2927 goto exit_create_index; | 2926 goto exit_create_index; |
| 2928 } | 2927 } |
| 2929 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ | 2928 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 2930 goto exit_create_index; | 2929 goto exit_create_index; |
| 2931 } | 2930 } |
| 2932 | 2931 |
| 2933 /* | 2932 /* |
| 2934 ** Find the table that is to be indexed. Return early if not found. | 2933 ** Find the table that is to be indexed. Return early if not found. |
| 2935 */ | 2934 */ |
| 2936 if( pTblName!=0 ){ | 2935 if( pTblName!=0 ){ |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3025 assert( pName->z!=0 ); | 3024 assert( pName->z!=0 ); |
| 3026 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ | 3025 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 3027 goto exit_create_index; | 3026 goto exit_create_index; |
| 3028 } | 3027 } |
| 3029 if( !db->init.busy ){ | 3028 if( !db->init.busy ){ |
| 3030 if( sqlite3FindTable(db, zName, 0)!=0 ){ | 3029 if( sqlite3FindTable(db, zName, 0)!=0 ){ |
| 3031 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); | 3030 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 3032 goto exit_create_index; | 3031 goto exit_create_index; |
| 3033 } | 3032 } |
| 3034 } | 3033 } |
| 3035 if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){ | 3034 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| 3036 if( !ifNotExist ){ | 3035 if( !ifNotExist ){ |
| 3037 sqlite3ErrorMsg(pParse, "index %s already exists", zName); | 3036 sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 3038 }else{ | 3037 }else{ |
| 3039 assert( !db->init.busy ); | 3038 assert( !db->init.busy ); |
| 3040 sqlite3CodeVerifySchema(pParse, iDb); | 3039 sqlite3CodeVerifySchema(pParse, iDb); |
| 3041 } | 3040 } |
| 3042 goto exit_create_index; | 3041 goto exit_create_index; |
| 3043 } | 3042 } |
| 3044 }else{ | 3043 }else{ |
| 3045 int n; | 3044 int n; |
| 3046 Index *pLoop; | 3045 Index *pLoop; |
| 3047 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} | 3046 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
| 3048 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); | 3047 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); |
| 3049 if( zName==0 ){ | 3048 if( zName==0 ){ |
| 3050 goto exit_create_index; | 3049 goto exit_create_index; |
| 3051 } | 3050 } |
| 3051 |
| 3052 /* Automatic index names generated from within sqlite3_declare_vtab() |
| 3053 ** must have names that are distinct from normal automatic index names. |
| 3054 ** The following statement converts "sqlite3_autoindex..." into |
| 3055 ** "sqlite3_butoindex..." in order to make the names distinct. |
| 3056 ** The "vtab_err.test" test demonstrates the need of this statement. */ |
| 3057 if( IN_DECLARE_VTAB ) zName[7]++; |
| 3052 } | 3058 } |
| 3053 | 3059 |
| 3054 /* Check for authorization to create an index. | 3060 /* Check for authorization to create an index. |
| 3055 */ | 3061 */ |
| 3056 #ifndef SQLITE_OMIT_AUTHORIZATION | 3062 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3057 { | 3063 { |
| 3058 const char *zDb = pDb->zName; | 3064 const char *zDb = pDb->zDbSName; |
| 3059 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ | 3065 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
| 3060 goto exit_create_index; | 3066 goto exit_create_index; |
| 3061 } | 3067 } |
| 3062 i = SQLITE_CREATE_INDEX; | 3068 i = SQLITE_CREATE_INDEX; |
| 3063 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; | 3069 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
| 3064 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ | 3070 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
| 3065 goto exit_create_index; | 3071 goto exit_create_index; |
| 3066 } | 3072 } |
| 3067 } | 3073 } |
| 3068 #endif | 3074 #endif |
| 3069 | 3075 |
| 3070 /* If pList==0, it means this routine was called to make a primary | 3076 /* If pList==0, it means this routine was called to make a primary |
| 3071 ** key out of the last column added to the table under construction. | 3077 ** key out of the last column added to the table under construction. |
| 3072 ** So create a fake list to simulate this. | 3078 ** So create a fake list to simulate this. |
| 3073 */ | 3079 */ |
| 3074 if( pList==0 ){ | 3080 if( pList==0 ){ |
| 3075 Token prevCol; | 3081 Token prevCol; |
| 3076 prevCol.z = pTab->aCol[pTab->nCol-1].zName; | 3082 sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName); |
| 3077 prevCol.n = sqlite3Strlen30(prevCol.z); | |
| 3078 pList = sqlite3ExprListAppend(pParse, 0, | 3083 pList = sqlite3ExprListAppend(pParse, 0, |
| 3079 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); | 3084 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
| 3080 if( pList==0 ) goto exit_create_index; | 3085 if( pList==0 ) goto exit_create_index; |
| 3081 assert( pList->nExpr==1 ); | 3086 assert( pList->nExpr==1 ); |
| 3082 sqlite3ExprListSetSortOrder(pList, sortOrder); | 3087 sqlite3ExprListSetSortOrder(pList, sortOrder); |
| 3083 }else{ | 3088 }else{ |
| 3084 sqlite3ExprListCheckLength(pParse, pList, "index"); | 3089 sqlite3ExprListCheckLength(pParse, pList, "index"); |
| 3085 } | 3090 } |
| 3086 | 3091 |
| 3087 /* Figure out how many bytes of space are required to store explicitly | 3092 /* Figure out how many bytes of space are required to store explicitly |
| (...skipping 18 matching lines...) Expand all Loading... |
| 3106 goto exit_create_index; | 3111 goto exit_create_index; |
| 3107 } | 3112 } |
| 3108 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); | 3113 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); |
| 3109 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); | 3114 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); |
| 3110 pIndex->zName = zExtra; | 3115 pIndex->zName = zExtra; |
| 3111 zExtra += nName + 1; | 3116 zExtra += nName + 1; |
| 3112 memcpy(pIndex->zName, zName, nName+1); | 3117 memcpy(pIndex->zName, zName, nName+1); |
| 3113 pIndex->pTable = pTab; | 3118 pIndex->pTable = pTab; |
| 3114 pIndex->onError = (u8)onError; | 3119 pIndex->onError = (u8)onError; |
| 3115 pIndex->uniqNotNull = onError!=OE_None; | 3120 pIndex->uniqNotNull = onError!=OE_None; |
| 3116 pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE; | 3121 pIndex->idxType = idxType; |
| 3117 pIndex->pSchema = db->aDb[iDb].pSchema; | 3122 pIndex->pSchema = db->aDb[iDb].pSchema; |
| 3118 pIndex->nKeyCol = pList->nExpr; | 3123 pIndex->nKeyCol = pList->nExpr; |
| 3119 if( pPIWhere ){ | 3124 if( pPIWhere ){ |
| 3120 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); | 3125 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); |
| 3121 pIndex->pPartIdxWhere = pPIWhere; | 3126 pIndex->pPartIdxWhere = pPIWhere; |
| 3122 pPIWhere = 0; | 3127 pPIWhere = 0; |
| 3123 } | 3128 } |
| 3124 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 3129 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 3125 | 3130 |
| 3126 /* Check to see if we should honor DESC requests on index columns | 3131 /* Check to see if we should honor DESC requests on index columns |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3216 } | 3221 } |
| 3217 } | 3222 } |
| 3218 assert( i==pIndex->nColumn ); | 3223 assert( i==pIndex->nColumn ); |
| 3219 }else{ | 3224 }else{ |
| 3220 pIndex->aiColumn[i] = XN_ROWID; | 3225 pIndex->aiColumn[i] = XN_ROWID; |
| 3221 pIndex->azColl[i] = sqlite3StrBINARY; | 3226 pIndex->azColl[i] = sqlite3StrBINARY; |
| 3222 } | 3227 } |
| 3223 sqlite3DefaultRowEst(pIndex); | 3228 sqlite3DefaultRowEst(pIndex); |
| 3224 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); | 3229 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
| 3225 | 3230 |
| 3231 /* If this index contains every column of its table, then mark |
| 3232 ** it as a covering index */ |
| 3233 assert( HasRowid(pTab) |
| 3234 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 ); |
| 3235 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ |
| 3236 pIndex->isCovering = 1; |
| 3237 for(j=0; j<pTab->nCol; j++){ |
| 3238 if( j==pTab->iPKey ) continue; |
| 3239 if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue; |
| 3240 pIndex->isCovering = 0; |
| 3241 break; |
| 3242 } |
| 3243 } |
| 3244 |
| 3226 if( pTab==pParse->pNewTable ){ | 3245 if( pTab==pParse->pNewTable ){ |
| 3227 /* This routine has been called to create an automatic index as a | 3246 /* This routine has been called to create an automatic index as a |
| 3228 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or | 3247 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
| 3229 ** a PRIMARY KEY or UNIQUE clause following the column definitions. | 3248 ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
| 3230 ** i.e. one of: | 3249 ** i.e. one of: |
| 3231 ** | 3250 ** |
| 3232 ** CREATE TABLE t(x PRIMARY KEY, y); | 3251 ** CREATE TABLE t(x PRIMARY KEY, y); |
| 3233 ** CREATE TABLE t(x, y, UNIQUE(x, y)); | 3252 ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
| 3234 ** | 3253 ** |
| 3235 ** Either way, check to see if the table already has such an index. If | 3254 ** Either way, check to see if the table already has such an index. If |
| (...skipping 17 matching lines...) Expand all Loading... |
| 3253 assert( IsUniqueIndex(pIndex) ); | 3272 assert( IsUniqueIndex(pIndex) ); |
| 3254 | 3273 |
| 3255 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; | 3274 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
| 3256 for(k=0; k<pIdx->nKeyCol; k++){ | 3275 for(k=0; k<pIdx->nKeyCol; k++){ |
| 3257 const char *z1; | 3276 const char *z1; |
| 3258 const char *z2; | 3277 const char *z2; |
| 3259 assert( pIdx->aiColumn[k]>=0 ); | 3278 assert( pIdx->aiColumn[k]>=0 ); |
| 3260 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; | 3279 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
| 3261 z1 = pIdx->azColl[k]; | 3280 z1 = pIdx->azColl[k]; |
| 3262 z2 = pIndex->azColl[k]; | 3281 z2 = pIndex->azColl[k]; |
| 3263 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; | 3282 if( sqlite3StrICmp(z1, z2) ) break; |
| 3264 } | 3283 } |
| 3265 if( k==pIdx->nKeyCol ){ | 3284 if( k==pIdx->nKeyCol ){ |
| 3266 if( pIdx->onError!=pIndex->onError ){ | 3285 if( pIdx->onError!=pIndex->onError ){ |
| 3267 /* This constraint creates the same index as a previous | 3286 /* This constraint creates the same index as a previous |
| 3268 ** constraint specified somewhere in the CREATE TABLE statement. | 3287 ** constraint specified somewhere in the CREATE TABLE statement. |
| 3269 ** However the ON CONFLICT clauses are different. If both this | 3288 ** However the ON CONFLICT clauses are different. If both this |
| 3270 ** constraint and the previous equivalent constraint have explicit | 3289 ** constraint and the previous equivalent constraint have explicit |
| 3271 ** ON CONFLICT clauses this is an error. Otherwise, use the | 3290 ** ON CONFLICT clauses this is an error. Otherwise, use the |
| 3272 ** explicitly specified behavior for the index. | 3291 ** explicitly specified behavior for the index. |
| 3273 */ | 3292 */ |
| 3274 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ | 3293 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
| 3275 sqlite3ErrorMsg(pParse, | 3294 sqlite3ErrorMsg(pParse, |
| 3276 "conflicting ON CONFLICT clauses specified", 0); | 3295 "conflicting ON CONFLICT clauses specified", 0); |
| 3277 } | 3296 } |
| 3278 if( pIdx->onError==OE_Default ){ | 3297 if( pIdx->onError==OE_Default ){ |
| 3279 pIdx->onError = pIndex->onError; | 3298 pIdx->onError = pIndex->onError; |
| 3280 } | 3299 } |
| 3281 } | 3300 } |
| 3282 pRet = pIdx; | 3301 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; |
| 3283 goto exit_create_index; | 3302 goto exit_create_index; |
| 3284 } | 3303 } |
| 3285 } | 3304 } |
| 3286 } | 3305 } |
| 3287 | 3306 |
| 3288 /* Link the new Index structure to its table and to the other | 3307 /* Link the new Index structure to its table and to the other |
| 3289 ** in-memory database structures. | 3308 ** in-memory database structures. |
| 3290 */ | 3309 */ |
| 3291 assert( pParse->nErr==0 ); | 3310 assert( pParse->nErr==0 ); |
| 3292 if( db->init.busy ){ | 3311 if( db->init.busy ){ |
| 3293 Index *p; | 3312 Index *p; |
| 3313 assert( !IN_DECLARE_VTAB ); |
| 3294 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); | 3314 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
| 3295 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, | 3315 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
| 3296 pIndex->zName, pIndex); | 3316 pIndex->zName, pIndex); |
| 3297 if( p ){ | 3317 if( p ){ |
| 3298 assert( p==pIndex ); /* Malloc must have failed */ | 3318 assert( p==pIndex ); /* Malloc must have failed */ |
| 3299 db->mallocFailed = 1; | 3319 sqlite3OomFault(db); |
| 3300 goto exit_create_index; | 3320 goto exit_create_index; |
| 3301 } | 3321 } |
| 3302 db->flags |= SQLITE_InternChanges; | 3322 db->flags |= SQLITE_InternChanges; |
| 3303 if( pTblName!=0 ){ | 3323 if( pTblName!=0 ){ |
| 3304 pIndex->tnum = db->init.newTnum; | 3324 pIndex->tnum = db->init.newTnum; |
| 3305 } | 3325 } |
| 3306 } | 3326 } |
| 3307 | 3327 |
| 3308 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the | 3328 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
| 3309 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then | 3329 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3349 }else{ | 3369 }else{ |
| 3350 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ | 3370 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
| 3351 /* zStmt = sqlite3MPrintf(""); */ | 3371 /* zStmt = sqlite3MPrintf(""); */ |
| 3352 zStmt = 0; | 3372 zStmt = 0; |
| 3353 } | 3373 } |
| 3354 | 3374 |
| 3355 /* Add an entry in sqlite_master for this index | 3375 /* Add an entry in sqlite_master for this index |
| 3356 */ | 3376 */ |
| 3357 sqlite3NestedParse(pParse, | 3377 sqlite3NestedParse(pParse, |
| 3358 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", | 3378 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", |
| 3359 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), | 3379 db->aDb[iDb].zDbSName, MASTER_NAME, |
| 3360 pIndex->zName, | 3380 pIndex->zName, |
| 3361 pTab->zName, | 3381 pTab->zName, |
| 3362 iMem, | 3382 iMem, |
| 3363 zStmt | 3383 zStmt |
| 3364 ); | 3384 ); |
| 3365 sqlite3DbFree(db, zStmt); | 3385 sqlite3DbFree(db, zStmt); |
| 3366 | 3386 |
| 3367 /* Fill the index with data and reparse the schema. Code an OP_Expire | 3387 /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 3368 ** to invalidate all pre-compiled statements. | 3388 ** to invalidate all pre-compiled statements. |
| 3369 */ | 3389 */ |
| 3370 if( pTblName ){ | 3390 if( pTblName ){ |
| 3371 sqlite3RefillIndex(pParse, pIndex, iMem); | 3391 sqlite3RefillIndex(pParse, pIndex, iMem); |
| 3372 sqlite3ChangeCookie(pParse, iDb); | 3392 sqlite3ChangeCookie(pParse, iDb); |
| 3373 sqlite3VdbeAddParseSchemaOp(v, iDb, | 3393 sqlite3VdbeAddParseSchemaOp(v, iDb, |
| 3374 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); | 3394 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName)); |
| 3375 sqlite3VdbeAddOp1(v, OP_Expire, 0); | 3395 sqlite3VdbeAddOp0(v, OP_Expire); |
| 3376 } | 3396 } |
| 3377 | 3397 |
| 3378 sqlite3VdbeJumpHere(v, pIndex->tnum); | 3398 sqlite3VdbeJumpHere(v, pIndex->tnum); |
| 3379 } | 3399 } |
| 3380 | 3400 |
| 3381 /* When adding an index to the list of indices for a table, make | 3401 /* When adding an index to the list of indices for a table, make |
| 3382 ** sure all indices labeled OE_Replace come after all those labeled | 3402 ** sure all indices labeled OE_Replace come after all those labeled |
| 3383 ** OE_Ignore. This is necessary for the correct constraint check | 3403 ** OE_Ignore. This is necessary for the correct constraint check |
| 3384 ** processing (in sqlite3GenerateConstraintChecks()) as part of | 3404 ** processing (in sqlite3GenerateConstraintChecks()) as part of |
| 3385 ** UPDATE and INSERT statements. | 3405 ** UPDATE and INSERT statements. |
| 3386 */ | 3406 */ |
| 3387 if( db->init.busy || pTblName==0 ){ | 3407 if( db->init.busy || pTblName==0 ){ |
| 3388 if( onError!=OE_Replace || pTab->pIndex==0 | 3408 if( onError!=OE_Replace || pTab->pIndex==0 |
| 3389 || pTab->pIndex->onError==OE_Replace){ | 3409 || pTab->pIndex->onError==OE_Replace){ |
| 3390 pIndex->pNext = pTab->pIndex; | 3410 pIndex->pNext = pTab->pIndex; |
| 3391 pTab->pIndex = pIndex; | 3411 pTab->pIndex = pIndex; |
| 3392 }else{ | 3412 }else{ |
| 3393 Index *pOther = pTab->pIndex; | 3413 Index *pOther = pTab->pIndex; |
| 3394 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ | 3414 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
| 3395 pOther = pOther->pNext; | 3415 pOther = pOther->pNext; |
| 3396 } | 3416 } |
| 3397 pIndex->pNext = pOther->pNext; | 3417 pIndex->pNext = pOther->pNext; |
| 3398 pOther->pNext = pIndex; | 3418 pOther->pNext = pIndex; |
| 3399 } | 3419 } |
| 3400 pRet = pIndex; | |
| 3401 pIndex = 0; | 3420 pIndex = 0; |
| 3402 } | 3421 } |
| 3403 | 3422 |
| 3404 /* Clean up before exiting */ | 3423 /* Clean up before exiting */ |
| 3405 exit_create_index: | 3424 exit_create_index: |
| 3406 if( pIndex ) freeIndex(db, pIndex); | 3425 if( pIndex ) freeIndex(db, pIndex); |
| 3407 sqlite3ExprDelete(db, pPIWhere); | 3426 sqlite3ExprDelete(db, pPIWhere); |
| 3408 sqlite3ExprListDelete(db, pList); | 3427 sqlite3ExprListDelete(db, pList); |
| 3409 sqlite3SrcListDelete(db, pTblName); | 3428 sqlite3SrcListDelete(db, pTblName); |
| 3410 sqlite3DbFree(db, zName); | 3429 sqlite3DbFree(db, zName); |
| 3411 return pRet; | |
| 3412 } | 3430 } |
| 3413 | 3431 |
| 3414 /* | 3432 /* |
| 3415 ** Fill the Index.aiRowEst[] array with default information - information | 3433 ** Fill the Index.aiRowEst[] array with default information - information |
| 3416 ** to be used when we have not run the ANALYZE command. | 3434 ** to be used when we have not run the ANALYZE command. |
| 3417 ** | 3435 ** |
| 3418 ** aiRowEst[0] is supposed to contain the number of elements in the index. | 3436 ** aiRowEst[0] is supposed to contain the number of elements in the index. |
| 3419 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the | 3437 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 3420 ** number of rows in the table that match any particular value of the | 3438 ** number of rows in the table that match any particular value of the |
| 3421 ** first column of the index. aiRowEst[2] is an estimate of the number | 3439 ** first column of the index. aiRowEst[2] is an estimate of the number |
| 3422 ** of rows that match any particular combination of the first 2 columns | 3440 ** of rows that match any particular combination of the first 2 columns |
| 3423 ** of the index. And so forth. It must always be the case that | 3441 ** of the index. And so forth. It must always be the case that |
| 3424 * | 3442 * |
| 3425 ** aiRowEst[N]<=aiRowEst[N-1] | 3443 ** aiRowEst[N]<=aiRowEst[N-1] |
| 3426 ** aiRowEst[N]>=1 | 3444 ** aiRowEst[N]>=1 |
| 3427 ** | 3445 ** |
| 3428 ** Apart from that, we have little to go on besides intuition as to | 3446 ** Apart from that, we have little to go on besides intuition as to |
| 3429 ** how aiRowEst[] should be initialized. The numbers generated here | 3447 ** how aiRowEst[] should be initialized. The numbers generated here |
| 3430 ** are based on typical values found in actual indices. | 3448 ** are based on typical values found in actual indices. |
| 3431 */ | 3449 */ |
| 3432 void sqlite3DefaultRowEst(Index *pIdx){ | 3450 void sqlite3DefaultRowEst(Index *pIdx){ |
| 3433 /* 10, 9, 8, 7, 6 */ | 3451 /* 10, 9, 8, 7, 6 */ |
| 3434 LogEst aVal[] = { 33, 32, 30, 28, 26 }; | 3452 LogEst aVal[] = { 33, 32, 30, 28, 26 }; |
| 3435 LogEst *a = pIdx->aiRowLogEst; | 3453 LogEst *a = pIdx->aiRowLogEst; |
| 3436 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); | 3454 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); |
| 3437 int i; | 3455 int i; |
| 3438 | 3456 |
| 3439 /* Set the first entry (number of rows in the index) to the estimated | 3457 /* Set the first entry (number of rows in the index) to the estimated |
| 3440 ** number of rows in the table. Or 10, if the estimated number of rows | 3458 ** number of rows in the table, or half the number of rows in the table |
| 3441 ** in the table is less than that. */ | 3459 ** for a partial index. But do not let the estimate drop below 10. */ |
| 3442 a[0] = pIdx->pTable->nRowLogEst; | 3460 a[0] = pIdx->pTable->nRowLogEst; |
| 3443 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); | 3461 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) ); |
| 3462 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) ); |
| 3444 | 3463 |
| 3445 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is | 3464 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
| 3446 ** 6 and each subsequent value (if any) is 5. */ | 3465 ** 6 and each subsequent value (if any) is 5. */ |
| 3447 memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); | 3466 memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
| 3448 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ | 3467 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ |
| 3449 a[i] = 23; assert( 23==sqlite3LogEst(5) ); | 3468 a[i] = 23; assert( 23==sqlite3LogEst(5) ); |
| 3450 } | 3469 } |
| 3451 | 3470 |
| 3452 assert( 0==sqlite3LogEst(1) ); | 3471 assert( 0==sqlite3LogEst(1) ); |
| 3453 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; | 3472 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 3484 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ | 3503 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
| 3485 sqlite3ErrorMsg(pParse, "index associated with UNIQUE " | 3504 sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
| 3486 "or PRIMARY KEY constraint cannot be dropped", 0); | 3505 "or PRIMARY KEY constraint cannot be dropped", 0); |
| 3487 goto exit_drop_index; | 3506 goto exit_drop_index; |
| 3488 } | 3507 } |
| 3489 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); | 3508 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
| 3490 #ifndef SQLITE_OMIT_AUTHORIZATION | 3509 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3491 { | 3510 { |
| 3492 int code = SQLITE_DROP_INDEX; | 3511 int code = SQLITE_DROP_INDEX; |
| 3493 Table *pTab = pIndex->pTable; | 3512 Table *pTab = pIndex->pTable; |
| 3494 const char *zDb = db->aDb[iDb].zName; | 3513 const char *zDb = db->aDb[iDb].zDbSName; |
| 3495 const char *zTab = SCHEMA_TABLE(iDb); | 3514 const char *zTab = SCHEMA_TABLE(iDb); |
| 3496 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ | 3515 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
| 3497 goto exit_drop_index; | 3516 goto exit_drop_index; |
| 3498 } | 3517 } |
| 3499 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; | 3518 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; |
| 3500 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ | 3519 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
| 3501 goto exit_drop_index; | 3520 goto exit_drop_index; |
| 3502 } | 3521 } |
| 3503 } | 3522 } |
| 3504 #endif | 3523 #endif |
| 3505 | 3524 |
| 3506 /* Generate code to remove the index and from the master table */ | 3525 /* Generate code to remove the index and from the master table */ |
| 3507 v = sqlite3GetVdbe(pParse); | 3526 v = sqlite3GetVdbe(pParse); |
| 3508 if( v ){ | 3527 if( v ){ |
| 3509 sqlite3BeginWriteOperation(pParse, 1, iDb); | 3528 sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 3510 sqlite3NestedParse(pParse, | 3529 sqlite3NestedParse(pParse, |
| 3511 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", | 3530 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", |
| 3512 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName | 3531 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName |
| 3513 ); | 3532 ); |
| 3514 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); | 3533 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
| 3515 sqlite3ChangeCookie(pParse, iDb); | 3534 sqlite3ChangeCookie(pParse, iDb); |
| 3516 destroyRootPage(pParse, pIndex->tnum, iDb); | 3535 destroyRootPage(pParse, pIndex->tnum, iDb); |
| 3517 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); | 3536 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
| 3518 } | 3537 } |
| 3519 | 3538 |
| 3520 exit_drop_index: | 3539 exit_drop_index: |
| 3521 sqlite3SrcListDelete(db, pName); | 3540 sqlite3SrcListDelete(db, pName); |
| 3522 } | 3541 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3645 | 3664 |
| 3646 /* Sanity checking on calling parameters */ | 3665 /* Sanity checking on calling parameters */ |
| 3647 assert( iStart>=0 ); | 3666 assert( iStart>=0 ); |
| 3648 assert( nExtra>=1 ); | 3667 assert( nExtra>=1 ); |
| 3649 assert( pSrc!=0 ); | 3668 assert( pSrc!=0 ); |
| 3650 assert( iStart<=pSrc->nSrc ); | 3669 assert( iStart<=pSrc->nSrc ); |
| 3651 | 3670 |
| 3652 /* Allocate additional space if needed */ | 3671 /* Allocate additional space if needed */ |
| 3653 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ | 3672 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
| 3654 SrcList *pNew; | 3673 SrcList *pNew; |
| 3655 int nAlloc = pSrc->nSrc+nExtra; | 3674 int nAlloc = pSrc->nSrc*2+nExtra; |
| 3656 int nGot; | 3675 int nGot; |
| 3657 pNew = sqlite3DbRealloc(db, pSrc, | 3676 pNew = sqlite3DbRealloc(db, pSrc, |
| 3658 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); | 3677 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); |
| 3659 if( pNew==0 ){ | 3678 if( pNew==0 ){ |
| 3660 assert( db->mallocFailed ); | 3679 assert( db->mallocFailed ); |
| 3661 return pSrc; | 3680 return pSrc; |
| 3662 } | 3681 } |
| 3663 pSrc = pNew; | 3682 pSrc = pNew; |
| 3664 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; | 3683 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; |
| 3665 pSrc->nAlloc = nGot; | 3684 pSrc->nAlloc = nGot; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3718 ** before being added to the SrcList. | 3737 ** before being added to the SrcList. |
| 3719 */ | 3738 */ |
| 3720 SrcList *sqlite3SrcListAppend( | 3739 SrcList *sqlite3SrcListAppend( |
| 3721 sqlite3 *db, /* Connection to notify of malloc failures */ | 3740 sqlite3 *db, /* Connection to notify of malloc failures */ |
| 3722 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ | 3741 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ |
| 3723 Token *pTable, /* Table to append */ | 3742 Token *pTable, /* Table to append */ |
| 3724 Token *pDatabase /* Database of the table */ | 3743 Token *pDatabase /* Database of the table */ |
| 3725 ){ | 3744 ){ |
| 3726 struct SrcList_item *pItem; | 3745 struct SrcList_item *pItem; |
| 3727 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ | 3746 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ |
| 3747 assert( db!=0 ); |
| 3728 if( pList==0 ){ | 3748 if( pList==0 ){ |
| 3729 pList = sqlite3DbMallocZero(db, sizeof(SrcList) ); | 3749 pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) ); |
| 3730 if( pList==0 ) return 0; | 3750 if( pList==0 ) return 0; |
| 3731 pList->nAlloc = 1; | 3751 pList->nAlloc = 1; |
| 3752 pList->nSrc = 1; |
| 3753 memset(&pList->a[0], 0, sizeof(pList->a[0])); |
| 3754 pList->a[0].iCursor = -1; |
| 3755 }else{ |
| 3756 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); |
| 3732 } | 3757 } |
| 3733 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); | |
| 3734 if( db->mallocFailed ){ | 3758 if( db->mallocFailed ){ |
| 3735 sqlite3SrcListDelete(db, pList); | 3759 sqlite3SrcListDelete(db, pList); |
| 3736 return 0; | 3760 return 0; |
| 3737 } | 3761 } |
| 3738 pItem = &pList->a[pList->nSrc-1]; | 3762 pItem = &pList->a[pList->nSrc-1]; |
| 3739 if( pDatabase && pDatabase->z==0 ){ | 3763 if( pDatabase && pDatabase->z==0 ){ |
| 3740 pDatabase = 0; | 3764 pDatabase = 0; |
| 3741 } | 3765 } |
| 3742 if( pDatabase ){ | 3766 if( pDatabase ){ |
| 3743 Token *pTemp = pDatabase; | 3767 Token *pTemp = pDatabase; |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3902 if( p ){ | 3926 if( p ){ |
| 3903 int i; | 3927 int i; |
| 3904 for(i=p->nSrc-1; i>0; i--){ | 3928 for(i=p->nSrc-1; i>0; i--){ |
| 3905 p->a[i].fg.jointype = p->a[i-1].fg.jointype; | 3929 p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
| 3906 } | 3930 } |
| 3907 p->a[0].fg.jointype = 0; | 3931 p->a[0].fg.jointype = 0; |
| 3908 } | 3932 } |
| 3909 } | 3933 } |
| 3910 | 3934 |
| 3911 /* | 3935 /* |
| 3912 ** Begin a transaction | 3936 ** Generate VDBE code for a BEGIN statement. |
| 3913 */ | 3937 */ |
| 3914 void sqlite3BeginTransaction(Parse *pParse, int type){ | 3938 void sqlite3BeginTransaction(Parse *pParse, int type){ |
| 3915 sqlite3 *db; | 3939 sqlite3 *db; |
| 3916 Vdbe *v; | 3940 Vdbe *v; |
| 3917 int i; | 3941 int i; |
| 3918 | 3942 |
| 3919 assert( pParse!=0 ); | 3943 assert( pParse!=0 ); |
| 3920 db = pParse->db; | 3944 db = pParse->db; |
| 3921 assert( db!=0 ); | 3945 assert( db!=0 ); |
| 3922 /* if( db->aDb[0].pBt==0 ) return; */ | |
| 3923 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ | 3946 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ |
| 3924 return; | 3947 return; |
| 3925 } | 3948 } |
| 3926 v = sqlite3GetVdbe(pParse); | 3949 v = sqlite3GetVdbe(pParse); |
| 3927 if( !v ) return; | 3950 if( !v ) return; |
| 3928 if( type!=TK_DEFERRED ){ | 3951 if( type!=TK_DEFERRED ){ |
| 3929 for(i=0; i<db->nDb; i++){ | 3952 for(i=0; i<db->nDb; i++){ |
| 3930 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); | 3953 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); |
| 3931 sqlite3VdbeUsesBtree(v, i); | 3954 sqlite3VdbeUsesBtree(v, i); |
| 3932 } | 3955 } |
| 3933 } | 3956 } |
| 3934 sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0); | 3957 sqlite3VdbeAddOp0(v, OP_AutoCommit); |
| 3935 } | 3958 } |
| 3936 | 3959 |
| 3937 /* | 3960 /* |
| 3938 ** Commit a transaction | 3961 ** Generate VDBE code for a COMMIT statement. |
| 3939 */ | 3962 */ |
| 3940 void sqlite3CommitTransaction(Parse *pParse){ | 3963 void sqlite3CommitTransaction(Parse *pParse){ |
| 3941 Vdbe *v; | 3964 Vdbe *v; |
| 3942 | 3965 |
| 3943 assert( pParse!=0 ); | 3966 assert( pParse!=0 ); |
| 3944 assert( pParse->db!=0 ); | 3967 assert( pParse->db!=0 ); |
| 3945 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ | 3968 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ |
| 3946 return; | 3969 return; |
| 3947 } | 3970 } |
| 3948 v = sqlite3GetVdbe(pParse); | 3971 v = sqlite3GetVdbe(pParse); |
| 3949 if( v ){ | 3972 if( v ){ |
| 3950 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0); | 3973 sqlite3VdbeAddOp1(v, OP_AutoCommit, 1); |
| 3951 } | 3974 } |
| 3952 } | 3975 } |
| 3953 | 3976 |
| 3954 /* | 3977 /* |
| 3955 ** Rollback a transaction | 3978 ** Generate VDBE code for a ROLLBACK statement. |
| 3956 */ | 3979 */ |
| 3957 void sqlite3RollbackTransaction(Parse *pParse){ | 3980 void sqlite3RollbackTransaction(Parse *pParse){ |
| 3958 Vdbe *v; | 3981 Vdbe *v; |
| 3959 | 3982 |
| 3960 assert( pParse!=0 ); | 3983 assert( pParse!=0 ); |
| 3961 assert( pParse->db!=0 ); | 3984 assert( pParse->db!=0 ); |
| 3962 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ | 3985 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ |
| 3963 return; | 3986 return; |
| 3964 } | 3987 } |
| 3965 v = sqlite3GetVdbe(pParse); | 3988 v = sqlite3GetVdbe(pParse); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4007 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); | 4030 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); |
| 4008 if( rc!=SQLITE_OK ){ | 4031 if( rc!=SQLITE_OK ){ |
| 4009 sqlite3ErrorMsg(pParse, "unable to open a temporary database " | 4032 sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 4010 "file for storing temporary tables"); | 4033 "file for storing temporary tables"); |
| 4011 pParse->rc = rc; | 4034 pParse->rc = rc; |
| 4012 return 1; | 4035 return 1; |
| 4013 } | 4036 } |
| 4014 db->aDb[1].pBt = pBt; | 4037 db->aDb[1].pBt = pBt; |
| 4015 assert( db->aDb[1].pSchema ); | 4038 assert( db->aDb[1].pSchema ); |
| 4016 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ | 4039 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
| 4017 db->mallocFailed = 1; | 4040 sqlite3OomFault(db); |
| 4018 return 1; | 4041 return 1; |
| 4019 } | 4042 } |
| 4020 } | 4043 } |
| 4021 return 0; | 4044 return 0; |
| 4022 } | 4045 } |
| 4023 | 4046 |
| 4024 /* | 4047 /* |
| 4025 ** Record the fact that the schema cookie will need to be verified | 4048 ** Record the fact that the schema cookie will need to be verified |
| 4026 ** for database iDb. The code to actually verify the schema cookie | 4049 ** for database iDb. The code to actually verify the schema cookie |
| 4027 ** will occur at the end of the top-level VDBE and will be generated | 4050 ** will occur at the end of the top-level VDBE and will be generated |
| 4028 ** later, by sqlite3FinishCoding(). | 4051 ** later, by sqlite3FinishCoding(). |
| 4029 */ | 4052 */ |
| 4030 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ | 4053 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
| 4031 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 4054 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 4032 sqlite3 *db = pToplevel->db; | |
| 4033 | 4055 |
| 4034 assert( iDb>=0 && iDb<db->nDb ); | 4056 assert( iDb>=0 && iDb<pParse->db->nDb ); |
| 4035 assert( db->aDb[iDb].pBt!=0 || iDb==1 ); | 4057 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 ); |
| 4036 assert( iDb<SQLITE_MAX_ATTACHED+2 ); | 4058 assert( iDb<SQLITE_MAX_ATTACHED+2 ); |
| 4037 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | 4059 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) ); |
| 4038 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ | 4060 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ |
| 4039 DbMaskSet(pToplevel->cookieMask, iDb); | 4061 DbMaskSet(pToplevel->cookieMask, iDb); |
| 4040 pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie; | |
| 4041 if( !OMIT_TEMPDB && iDb==1 ){ | 4062 if( !OMIT_TEMPDB && iDb==1 ){ |
| 4042 sqlite3OpenTempDatabase(pToplevel); | 4063 sqlite3OpenTempDatabase(pToplevel); |
| 4043 } | 4064 } |
| 4044 } | 4065 } |
| 4045 } | 4066 } |
| 4046 | 4067 |
| 4047 /* | 4068 /* |
| 4048 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each | 4069 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
| 4049 ** attached database. Otherwise, invoke it for the database named zDb only. | 4070 ** attached database. Otherwise, invoke it for the database named zDb only. |
| 4050 */ | 4071 */ |
| 4051 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ | 4072 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ |
| 4052 sqlite3 *db = pParse->db; | 4073 sqlite3 *db = pParse->db; |
| 4053 int i; | 4074 int i; |
| 4054 for(i=0; i<db->nDb; i++){ | 4075 for(i=0; i<db->nDb; i++){ |
| 4055 Db *pDb = &db->aDb[i]; | 4076 Db *pDb = &db->aDb[i]; |
| 4056 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){ | 4077 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ |
| 4057 sqlite3CodeVerifySchema(pParse, i); | 4078 sqlite3CodeVerifySchema(pParse, i); |
| 4058 } | 4079 } |
| 4059 } | 4080 } |
| 4060 } | 4081 } |
| 4061 | 4082 |
| 4062 /* | 4083 /* |
| 4063 ** Generate VDBE code that prepares for doing an operation that | 4084 ** Generate VDBE code that prepares for doing an operation that |
| 4064 ** might change the database. | 4085 ** might change the database. |
| 4065 ** | 4086 ** |
| 4066 ** This routine starts a new transaction if we are not already within | 4087 ** This routine starts a new transaction if we are not already within |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4124 char *p4, /* Error message */ | 4145 char *p4, /* Error message */ |
| 4125 i8 p4type, /* P4_STATIC or P4_TRANSIENT */ | 4146 i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 4126 u8 p5Errmsg /* P5_ErrMsg type */ | 4147 u8 p5Errmsg /* P5_ErrMsg type */ |
| 4127 ){ | 4148 ){ |
| 4128 Vdbe *v = sqlite3GetVdbe(pParse); | 4149 Vdbe *v = sqlite3GetVdbe(pParse); |
| 4129 assert( (errCode&0xff)==SQLITE_CONSTRAINT ); | 4150 assert( (errCode&0xff)==SQLITE_CONSTRAINT ); |
| 4130 if( onError==OE_Abort ){ | 4151 if( onError==OE_Abort ){ |
| 4131 sqlite3MayAbort(pParse); | 4152 sqlite3MayAbort(pParse); |
| 4132 } | 4153 } |
| 4133 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); | 4154 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
| 4134 if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg); | 4155 sqlite3VdbeChangeP5(v, p5Errmsg); |
| 4135 } | 4156 } |
| 4136 | 4157 |
| 4137 /* | 4158 /* |
| 4138 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. | 4159 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. |
| 4139 */ | 4160 */ |
| 4140 void sqlite3UniqueConstraint( | 4161 void sqlite3UniqueConstraint( |
| 4141 Parse *pParse, /* Parsing context */ | 4162 Parse *pParse, /* Parsing context */ |
| 4142 int onError, /* Constraint type */ | 4163 int onError, /* Constraint type */ |
| 4143 Index *pIdx /* The index that triggers the constraint */ | 4164 Index *pIdx /* The index that triggers the constraint */ |
| 4144 ){ | 4165 ){ |
| 4145 char *zErr; | 4166 char *zErr; |
| 4146 int j; | 4167 int j; |
| 4147 StrAccum errMsg; | 4168 StrAccum errMsg; |
| 4148 Table *pTab = pIdx->pTable; | 4169 Table *pTab = pIdx->pTable; |
| 4149 | 4170 |
| 4150 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); | 4171 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); |
| 4151 if( pIdx->aColExpr ){ | 4172 if( pIdx->aColExpr ){ |
| 4152 sqlite3XPrintf(&errMsg, 0, "index '%q'", pIdx->zName); | 4173 sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName); |
| 4153 }else{ | 4174 }else{ |
| 4154 for(j=0; j<pIdx->nKeyCol; j++){ | 4175 for(j=0; j<pIdx->nKeyCol; j++){ |
| 4155 char *zCol; | 4176 char *zCol; |
| 4156 assert( pIdx->aiColumn[j]>=0 ); | 4177 assert( pIdx->aiColumn[j]>=0 ); |
| 4157 zCol = pTab->aCol[pIdx->aiColumn[j]].zName; | 4178 zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 4158 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); | 4179 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); |
| 4159 sqlite3XPrintf(&errMsg, 0, "%s.%s", pTab->zName, zCol); | 4180 sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol); |
| 4160 } | 4181 } |
| 4161 } | 4182 } |
| 4162 zErr = sqlite3StrAccumFinish(&errMsg); | 4183 zErr = sqlite3StrAccumFinish(&errMsg); |
| 4163 sqlite3HaltConstraint(pParse, | 4184 sqlite3HaltConstraint(pParse, |
| 4164 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY | 4185 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
| 4165 : SQLITE_CONSTRAINT_UNIQUE, | 4186 : SQLITE_CONSTRAINT_UNIQUE, |
| 4166 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); | 4187 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
| 4167 } | 4188 } |
| 4168 | 4189 |
| 4169 | 4190 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4293 reindexDatabases(pParse, zColl); | 4314 reindexDatabases(pParse, zColl); |
| 4294 sqlite3DbFree(db, zColl); | 4315 sqlite3DbFree(db, zColl); |
| 4295 return; | 4316 return; |
| 4296 } | 4317 } |
| 4297 sqlite3DbFree(db, zColl); | 4318 sqlite3DbFree(db, zColl); |
| 4298 } | 4319 } |
| 4299 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); | 4320 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 4300 if( iDb<0 ) return; | 4321 if( iDb<0 ) return; |
| 4301 z = sqlite3NameFromToken(db, pObjName); | 4322 z = sqlite3NameFromToken(db, pObjName); |
| 4302 if( z==0 ) return; | 4323 if( z==0 ) return; |
| 4303 zDb = db->aDb[iDb].zName; | 4324 zDb = db->aDb[iDb].zDbSName; |
| 4304 pTab = sqlite3FindTable(db, z, zDb); | 4325 pTab = sqlite3FindTable(db, z, zDb); |
| 4305 if( pTab ){ | 4326 if( pTab ){ |
| 4306 reindexTable(pParse, pTab, 0); | 4327 reindexTable(pParse, pTab, 0); |
| 4307 sqlite3DbFree(db, z); | 4328 sqlite3DbFree(db, z); |
| 4308 return; | 4329 return; |
| 4309 } | 4330 } |
| 4310 pIndex = sqlite3FindIndex(db, z, zDb); | 4331 pIndex = sqlite3FindIndex(db, z, zDb); |
| 4311 sqlite3DbFree(db, z); | 4332 sqlite3DbFree(db, z); |
| 4312 if( pIndex ){ | 4333 if( pIndex ){ |
| 4313 sqlite3BeginWriteOperation(pParse, 0, iDb); | 4334 sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 4314 sqlite3RefillIndex(pParse, pIndex, -1); | 4335 sqlite3RefillIndex(pParse, pIndex, -1); |
| 4315 return; | 4336 return; |
| 4316 } | 4337 } |
| 4317 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); | 4338 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| 4318 } | 4339 } |
| 4319 #endif | 4340 #endif |
| 4320 | 4341 |
| 4321 /* | 4342 /* |
| 4322 ** Return a KeyInfo structure that is appropriate for the given Index. | 4343 ** Return a KeyInfo structure that is appropriate for the given Index. |
| 4323 ** | 4344 ** |
| 4324 ** The KeyInfo structure for an index is cached in the Index object. | |
| 4325 ** So there might be multiple references to the returned pointer. The | |
| 4326 ** caller should not try to modify the KeyInfo object. | |
| 4327 ** | |
| 4328 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object | 4345 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
| 4329 ** when it has finished using it. | 4346 ** when it has finished using it. |
| 4330 */ | 4347 */ |
| 4331 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ | 4348 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
| 4332 int i; | 4349 int i; |
| 4333 int nCol = pIdx->nColumn; | 4350 int nCol = pIdx->nColumn; |
| 4334 int nKey = pIdx->nKeyCol; | 4351 int nKey = pIdx->nKeyCol; |
| 4335 KeyInfo *pKey; | 4352 KeyInfo *pKey; |
| 4336 if( pParse->nErr ) return 0; | 4353 if( pParse->nErr ) return 0; |
| 4337 if( pIdx->uniqNotNull ){ | 4354 if( pIdx->uniqNotNull ){ |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4382 } | 4399 } |
| 4383 } | 4400 } |
| 4384 } | 4401 } |
| 4385 | 4402 |
| 4386 if( pWith ){ | 4403 if( pWith ){ |
| 4387 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); | 4404 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); |
| 4388 pNew = sqlite3DbRealloc(db, pWith, nByte); | 4405 pNew = sqlite3DbRealloc(db, pWith, nByte); |
| 4389 }else{ | 4406 }else{ |
| 4390 pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); | 4407 pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); |
| 4391 } | 4408 } |
| 4392 assert( zName!=0 || pNew==0 ); | 4409 assert( (pNew!=0 && zName!=0) || db->mallocFailed ); |
| 4393 assert( db->mallocFailed==0 || pNew==0 ); | |
| 4394 | 4410 |
| 4395 if( pNew==0 ){ | 4411 if( db->mallocFailed ){ |
| 4396 sqlite3ExprListDelete(db, pArglist); | 4412 sqlite3ExprListDelete(db, pArglist); |
| 4397 sqlite3SelectDelete(db, pQuery); | 4413 sqlite3SelectDelete(db, pQuery); |
| 4398 sqlite3DbFree(db, zName); | 4414 sqlite3DbFree(db, zName); |
| 4399 pNew = pWith; | 4415 pNew = pWith; |
| 4400 }else{ | 4416 }else{ |
| 4401 pNew->a[pNew->nCte].pSelect = pQuery; | 4417 pNew->a[pNew->nCte].pSelect = pQuery; |
| 4402 pNew->a[pNew->nCte].pCols = pArglist; | 4418 pNew->a[pNew->nCte].pCols = pArglist; |
| 4403 pNew->a[pNew->nCte].zName = zName; | 4419 pNew->a[pNew->nCte].zName = zName; |
| 4404 pNew->a[pNew->nCte].zCteErr = 0; | 4420 pNew->a[pNew->nCte].zCteErr = 0; |
| 4405 pNew->nCte++; | 4421 pNew->nCte++; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 4417 for(i=0; i<pWith->nCte; i++){ | 4433 for(i=0; i<pWith->nCte; i++){ |
| 4418 struct Cte *pCte = &pWith->a[i]; | 4434 struct Cte *pCte = &pWith->a[i]; |
| 4419 sqlite3ExprListDelete(db, pCte->pCols); | 4435 sqlite3ExprListDelete(db, pCte->pCols); |
| 4420 sqlite3SelectDelete(db, pCte->pSelect); | 4436 sqlite3SelectDelete(db, pCte->pSelect); |
| 4421 sqlite3DbFree(db, pCte->zName); | 4437 sqlite3DbFree(db, pCte->zName); |
| 4422 } | 4438 } |
| 4423 sqlite3DbFree(db, pWith); | 4439 sqlite3DbFree(db, pWith); |
| 4424 } | 4440 } |
| 4425 } | 4441 } |
| 4426 #endif /* !defined(SQLITE_OMIT_CTE) */ | 4442 #endif /* !defined(SQLITE_OMIT_CTE) */ |
| OLD | NEW |