drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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 |
| 14 | ** following kinds of SQL syntax: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 15 | ** |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 16 | ** CREATE TABLE |
| 17 | ** DROP TABLE |
| 18 | ** CREATE INDEX |
| 19 | ** DROP INDEX |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 20 | ** creating ID lists |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 21 | ** BEGIN TRANSACTION |
| 22 | ** COMMIT |
| 23 | ** ROLLBACK |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | */ |
| 25 | #include "sqliteInt.h" |
| 26 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 27 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 28 | /* |
| 29 | ** The TableLock structure is only used by the sqlite3TableLock() and |
| 30 | ** codeTableLocks() functions. |
| 31 | */ |
| 32 | struct TableLock { |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 33 | int iDb; /* The database containing the table to be locked */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 34 | Pgno iTab; /* The root page of the table to be locked */ |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 35 | u8 isWriteLock; /* True for write lock. False for a read lock */ |
| 36 | const char *zLockName; /* Name of the table */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* |
drh | d698bc1 | 2006-03-23 23:33:26 +0000 | [diff] [blame] | 40 | ** Record the fact that we want to lock a table at run-time. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 41 | ** |
drh | d698bc1 | 2006-03-23 23:33:26 +0000 | [diff] [blame] | 42 | ** The table to be locked has root page iTab and is found in database iDb. |
| 43 | ** A read or a write lock can be taken depending on isWritelock. |
| 44 | ** |
| 45 | ** This routine just records the fact that the lock is desired. The |
| 46 | ** code to make the lock occur is generated by a later call to |
| 47 | ** codeTableLocks() which occurs during sqlite3FinishCoding(). |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 48 | */ |
drh | 9430506 | 2021-05-17 11:19:32 +0000 | [diff] [blame] | 49 | static SQLITE_NOINLINE void lockTable( |
drh | d698bc1 | 2006-03-23 23:33:26 +0000 | [diff] [blame] | 50 | Parse *pParse, /* Parsing context */ |
| 51 | int iDb, /* Index of the database containing the table to lock */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 52 | Pgno iTab, /* Root page number of the table to be locked */ |
drh | d698bc1 | 2006-03-23 23:33:26 +0000 | [diff] [blame] | 53 | u8 isWriteLock, /* True for a write lock */ |
| 54 | const char *zName /* Name of the table to be locked */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 55 | ){ |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 56 | Parse *pToplevel; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 57 | int i; |
| 58 | int nBytes; |
| 59 | TableLock *p; |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 60 | assert( iDb>=0 ); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 61 | |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 62 | pToplevel = sqlite3ParseToplevel(pParse); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 63 | for(i=0; i<pToplevel->nTableLock; i++){ |
| 64 | p = &pToplevel->aTableLock[i]; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 65 | if( p->iDb==iDb && p->iTab==iTab ){ |
| 66 | p->isWriteLock = (p->isWriteLock || isWriteLock); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 71 | nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); |
| 72 | pToplevel->aTableLock = |
| 73 | sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); |
| 74 | if( pToplevel->aTableLock ){ |
| 75 | p = &pToplevel->aTableLock[pToplevel->nTableLock++]; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 76 | p->iDb = iDb; |
| 77 | p->iTab = iTab; |
| 78 | p->isWriteLock = isWriteLock; |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 79 | p->zLockName = zName; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 80 | }else{ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 81 | pToplevel->nTableLock = 0; |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 82 | sqlite3OomFault(pToplevel->db); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
drh | 9430506 | 2021-05-17 11:19:32 +0000 | [diff] [blame] | 85 | void sqlite3TableLock( |
| 86 | Parse *pParse, /* Parsing context */ |
| 87 | int iDb, /* Index of the database containing the table to lock */ |
| 88 | Pgno iTab, /* Root page number of the table to be locked */ |
| 89 | u8 isWriteLock, /* True for a write lock */ |
| 90 | const char *zName /* Name of the table to be locked */ |
| 91 | ){ |
drh | 95003fe | 2021-05-21 15:33:49 +0000 | [diff] [blame] | 92 | #ifdef SQLITE_OMIT_CONCURRENT |
drh | 9430506 | 2021-05-17 11:19:32 +0000 | [diff] [blame] | 93 | if( iDb==1 ) return; |
| 94 | if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; |
drh | 95003fe | 2021-05-21 15:33:49 +0000 | [diff] [blame] | 95 | #endif |
drh | 9430506 | 2021-05-17 11:19:32 +0000 | [diff] [blame] | 96 | lockTable(pParse, iDb, iTab, isWriteLock, zName); |
| 97 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | ** Code an OP_TableLock instruction for each table locked by the |
| 101 | ** statement (configured by calls to sqlite3TableLock()). |
| 102 | */ |
| 103 | static void codeTableLocks(Parse *pParse){ |
| 104 | int i; |
drh | f0b4174 | 2020-08-15 21:55:14 +0000 | [diff] [blame] | 105 | Vdbe *pVdbe = pParse->pVdbe; |
drh | 289a0c8 | 2020-08-15 22:23:00 +0000 | [diff] [blame] | 106 | assert( pVdbe!=0 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 107 | |
| 108 | for(i=0; i<pParse->nTableLock; i++){ |
| 109 | TableLock *p = &pParse->aTableLock[i]; |
| 110 | int p1 = p->iDb; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 111 | sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 112 | p->zLockName, P4_STATIC); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | #else |
| 116 | #define codeTableLocks(x) |
| 117 | #endif |
| 118 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 119 | /* |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 120 | ** Return TRUE if the given yDbMask object is empty - if it contains no |
| 121 | ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() |
| 122 | ** macros when SQLITE_MAX_ATTACHED is greater than 30. |
| 123 | */ |
| 124 | #if SQLITE_MAX_ATTACHED>30 |
| 125 | int sqlite3DbMaskAllZero(yDbMask m){ |
| 126 | int i; |
| 127 | for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; |
| 128 | return 1; |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 133 | ** This routine is called after a single SQL statement has been |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 134 | ** parsed and a VDBE program to execute that statement has been |
| 135 | ** prepared. This routine puts the finishing touches on the |
| 136 | ** VDBE program and resets the pParse structure for the next |
| 137 | ** parse. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 138 | ** |
| 139 | ** Note that if an error occurred, it might be the case that |
| 140 | ** no VDBE code was generated. |
| 141 | */ |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 142 | void sqlite3FinishCoding(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 143 | sqlite3 *db; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 144 | Vdbe *v; |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 145 | int iDb, i; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 146 | |
dan | f78baaf | 2012-12-06 19:37:22 +0000 | [diff] [blame] | 147 | assert( pParse->pToplevel==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 148 | db = pParse->db; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 149 | assert( db->pParse==pParse ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 150 | if( pParse->nested ) return; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 151 | if( pParse->nErr ){ |
drh | a5c9a70 | 2022-01-25 00:03:25 +0000 | [diff] [blame] | 152 | if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM; |
drh | d99d283 | 2015-04-17 15:58:33 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 155 | assert( db->mallocFailed==0 ); |
danielk1977 | 48d0d86 | 2005-02-01 03:09:52 +0000 | [diff] [blame] | 156 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 157 | /* Begin by generating some termination code at the end of the |
| 158 | ** vdbe program |
| 159 | */ |
drh | 02c4aa3 | 2021-02-04 13:44:42 +0000 | [diff] [blame] | 160 | v = pParse->pVdbe; |
| 161 | if( v==0 ){ |
| 162 | if( db->init.busy ){ |
| 163 | pParse->rc = SQLITE_DONE; |
| 164 | return; |
| 165 | } |
| 166 | v = sqlite3GetVdbe(pParse); |
| 167 | if( v==0 ) pParse->rc = SQLITE_ERROR; |
drh | c8af879 | 2021-01-01 22:06:17 +0000 | [diff] [blame] | 168 | } |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 169 | assert( !pParse->isMultiWrite |
| 170 | || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 171 | if( v ){ |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 172 | if( pParse->bReturning ){ |
| 173 | Returning *pReturning = pParse->u1.pReturning; |
| 174 | int addrRewind; |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 175 | int reg; |
| 176 | |
drh | 1a6bac0 | 2022-04-25 10:43:19 +0000 | [diff] [blame] | 177 | if( pReturning->nRetCol ){ |
drh | 3b26b2b | 2021-12-01 19:17:14 +0000 | [diff] [blame] | 178 | sqlite3VdbeAddOp0(v, OP_FkCheck); |
drh | 7132f43 | 2021-10-20 12:52:12 +0000 | [diff] [blame] | 179 | addrRewind = |
| 180 | sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur); |
| 181 | VdbeCoverage(v); |
| 182 | reg = pReturning->iRetReg; |
| 183 | for(i=0; i<pReturning->nRetCol; i++){ |
| 184 | sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i); |
| 185 | } |
| 186 | sqlite3VdbeAddOp2(v, OP_ResultRow, reg, i); |
| 187 | sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1); |
| 188 | VdbeCoverage(v); |
| 189 | sqlite3VdbeJumpHere(v, addrRewind); |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 190 | } |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 191 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 192 | sqlite3VdbeAddOp0(v, OP_Halt); |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 193 | |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 194 | #if SQLITE_USER_AUTHENTICATION |
| 195 | if( pParse->nTableLock>0 && db->init.busy==0 ){ |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 196 | sqlite3UserAuthInit(db); |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 197 | if( db->auth.authLevel<UAUTH_User ){ |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 198 | sqlite3ErrorMsg(pParse, "user not authenticated"); |
drh | 6ae3ab0 | 2016-08-23 14:42:15 +0000 | [diff] [blame] | 199 | pParse->rc = SQLITE_AUTH_USER; |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 200 | return; |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | #endif |
| 204 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 205 | /* The cookie mask contains one bit for each database file open. |
| 206 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 207 | ** set for each database that is used. Generate code to start a |
| 208 | ** transaction on each used database and to verify the schema cookie |
| 209 | ** on each used database. |
| 210 | */ |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 211 | assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 212 | sqlite3VdbeJumpHere(v, 0); |
| 213 | assert( db->nDb>0 ); |
| 214 | iDb = 0; |
| 215 | do{ |
| 216 | Schema *pSchema; |
| 217 | if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
| 218 | sqlite3VdbeUsesBtree(v, iDb); |
| 219 | pSchema = db->aDb[iDb].pSchema; |
| 220 | sqlite3VdbeAddOp4Int(v, |
| 221 | OP_Transaction, /* Opcode */ |
| 222 | iDb, /* P1 */ |
| 223 | DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
| 224 | pSchema->schema_cookie, /* P3 */ |
| 225 | pSchema->iGeneration /* P4 */ |
| 226 | ); |
| 227 | if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 228 | VdbeComment((v, |
| 229 | "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
| 230 | }while( ++iDb<db->nDb ); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 231 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 232 | for(i=0; i<pParse->nVtabLock; i++){ |
| 233 | char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 234 | sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 235 | } |
| 236 | pParse->nVtabLock = 0; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 237 | #endif |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 238 | |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 239 | /* Once all the cookies have been verified and transactions opened, |
| 240 | ** obtain the required table-locks. This is a no-op unless the |
| 241 | ** shared-cache feature is enabled. |
| 242 | */ |
| 243 | codeTableLocks(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 244 | |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 245 | /* Initialize any AUTOINCREMENT data structures required. |
| 246 | */ |
| 247 | sqlite3AutoincrementBegin(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 248 | |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 249 | /* Code constant expressions that where factored out of inner loops. |
| 250 | ** |
| 251 | ** The pConstExpr list might also contain expressions that we simply |
| 252 | ** want to keep around until the Parse object is deleted. Such |
| 253 | ** expressions have iConstExprReg==0. Do not generate code for |
| 254 | ** those expressions, of course. |
| 255 | */ |
| 256 | if( pParse->pConstExpr ){ |
| 257 | ExprList *pEL = pParse->pConstExpr; |
| 258 | pParse->okConstFactor = 0; |
| 259 | for(i=0; i<pEL->nExpr; i++){ |
| 260 | int iReg = pEL->a[i].u.iConstExprReg; |
| 261 | sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg); |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 262 | } |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 263 | } |
drh | 7bace9e | 2022-07-23 12:51:48 +0000 | [diff] [blame] | 264 | |
| 265 | if( pParse->bReturning ){ |
| 266 | Returning *pRet = pParse->u1.pReturning; |
| 267 | if( pRet->nRetCol ){ |
| 268 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* Finally, jump back to the beginning of the executable code. */ |
| 273 | sqlite3VdbeGoto(v, 1); |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 274 | } |
| 275 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 276 | /* Get the VDBE program ready for execution |
| 277 | */ |
drh | 1da88b5 | 2022-01-24 19:38:56 +0000 | [diff] [blame] | 278 | assert( v!=0 || pParse->nErr ); |
| 279 | assert( db->mallocFailed==0 || pParse->nErr ); |
| 280 | if( pParse->nErr==0 ){ |
drh | 3492dd7 | 2009-09-14 23:47:24 +0000 | [diff] [blame] | 281 | /* A minimum of one cursor is required if autoincrement is used |
| 282 | * See ticket [a696379c1f08866] */ |
drh | 04ab586 | 2018-12-01 21:13:41 +0000 | [diff] [blame] | 283 | assert( pParse->pAinc==0 || pParse->nTab>0 ); |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 284 | sqlite3VdbeMakeReady(v, pParse); |
danielk1977 | 441daf6 | 2005-02-01 03:46:43 +0000 | [diff] [blame] | 285 | pParse->rc = SQLITE_DONE; |
drh | e294da0 | 2010-02-25 23:44:15 +0000 | [diff] [blame] | 286 | }else{ |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 287 | pParse->rc = SQLITE_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | /* |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 292 | ** Run the parser and code generator recursively in order to generate |
| 293 | ** code for the SQL statement given onto the end of the pParse context |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 294 | ** currently under construction. Notes: |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 295 | ** |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 296 | ** * The final OP_Halt is not appended and other initialization |
| 297 | ** and finalization steps are omitted because those are handling by the |
| 298 | ** outermost parser. |
| 299 | ** |
| 300 | ** * Built-in SQL functions always take precedence over application-defined |
| 301 | ** SQL functions. In other words, it is not possible to override a |
| 302 | ** built-in function. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 303 | */ |
| 304 | void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 305 | va_list ap; |
| 306 | char *zSql; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 307 | sqlite3 *db = pParse->db; |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 308 | u32 savedDbFlags = db->mDbFlags; |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 309 | char saveBuf[PARSE_TAIL_SZ]; |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 310 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 311 | if( pParse->nErr ) return; |
drh | 12e1eb3 | 2022-12-29 18:54:15 +0000 | [diff] [blame] | 312 | if( pParse->eParseMode ) return; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 313 | assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
| 314 | va_start(ap, zFormat); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 315 | zSql = sqlite3VMPrintf(db, zFormat, ap); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 316 | va_end(ap); |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 317 | if( zSql==0 ){ |
drh | 480c572 | 2019-02-22 16:18:12 +0000 | [diff] [blame] | 318 | /* This can result either from an OOM or because the formatted string |
| 319 | ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set |
| 320 | ** an error */ |
| 321 | if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG; |
drh | a2b6806 | 2019-03-28 04:03:17 +0000 | [diff] [blame] | 322 | pParse->nErr++; |
drh | 480c572 | 2019-02-22 16:18:12 +0000 | [diff] [blame] | 323 | return; |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 324 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 325 | pParse->nested++; |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 326 | memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 327 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 328 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 329 | sqlite3RunParser(pParse, zSql); |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 330 | db->mDbFlags = savedDbFlags; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 331 | sqlite3DbFree(db, zSql); |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 332 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 333 | pParse->nested--; |
| 334 | } |
| 335 | |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 336 | #if SQLITE_USER_AUTHENTICATION |
| 337 | /* |
| 338 | ** Return TRUE if zTable is the name of the system table that stores the |
| 339 | ** list of users and their access credentials. |
| 340 | */ |
| 341 | int sqlite3UserAuthTable(const char *zTable){ |
| 342 | return sqlite3_stricmp(zTable, "sqlite_user")==0; |
| 343 | } |
| 344 | #endif |
| 345 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 346 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 347 | ** Locate the in-memory structure that describes a particular database |
| 348 | ** table given the name of that table and (optionally) the name of the |
| 349 | ** database containing the table. Return NULL if not found. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 350 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 351 | ** If zDatabase is 0, all databases are searched for the table and the |
| 352 | ** first matching table is returned. (No checking for duplicate table |
| 353 | ** names is done.) The search order is TEMP first, then MAIN, then any |
| 354 | ** auxiliary databases added using the ATTACH command. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 355 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 356 | ** See also sqlite3LocateTable(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 357 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 358 | Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 359 | Table *p = 0; |
| 360 | int i; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 361 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 362 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 363 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 364 | #if SQLITE_USER_AUTHENTICATION |
| 365 | /* Only the admin user is allowed to know that the sqlite_user table |
| 366 | ** exists */ |
drh | e933b83 | 2014-09-10 17:34:28 +0000 | [diff] [blame] | 367 | if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 368 | return 0; |
| 369 | } |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 370 | #endif |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 371 | if( zDatabase ){ |
| 372 | for(i=0; i<db->nDb; i++){ |
| 373 | if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 374 | } |
| 375 | if( i>=db->nDb ){ |
| 376 | /* No match against the official names. But always match "main" |
| 377 | ** to schema 0 as a legacy fallback. */ |
| 378 | if( sqlite3StrICmp(zDatabase,"main")==0 ){ |
| 379 | i = 0; |
| 380 | }else{ |
| 381 | return 0; |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 382 | } |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 383 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 384 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 385 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 386 | if( i==1 ){ |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 387 | if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 |
| 388 | || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 |
| 389 | || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 390 | ){ |
| 391 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 392 | LEGACY_TEMP_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 393 | } |
| 394 | }else{ |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 395 | if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 396 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 397 | LEGACY_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 400 | } |
| 401 | }else{ |
| 402 | /* Match against TEMP first */ |
| 403 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); |
| 404 | if( p ) return p; |
| 405 | /* The main database is second */ |
| 406 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); |
| 407 | if( p ) return p; |
| 408 | /* Attached databases are in order of attachment */ |
| 409 | for(i=2; i<db->nDb; i++){ |
| 410 | assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 411 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 412 | if( p ) break; |
| 413 | } |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 414 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 415 | if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ |
| 416 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE); |
| 417 | }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 418 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 419 | LEGACY_TEMP_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 422 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 423 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 427 | ** Locate the in-memory structure that describes a particular database |
| 428 | ** table given the name of that table and (optionally) the name of the |
| 429 | ** database containing the table. Return NULL if not found. Also leave an |
| 430 | ** error message in pParse->zErrMsg. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 431 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 432 | ** The difference between this routine and sqlite3FindTable() is that this |
| 433 | ** routine leaves an error message in pParse->zErrMsg where |
| 434 | ** sqlite3FindTable() does not. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 435 | */ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 436 | Table *sqlite3LocateTable( |
| 437 | Parse *pParse, /* context in which to report errors */ |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 438 | u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 439 | const char *zName, /* Name of the table we are looking for */ |
| 440 | const char *zDbase /* Name of the database. Might be NULL */ |
| 441 | ){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 442 | Table *p; |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 443 | sqlite3 *db = pParse->db; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 444 | |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 445 | /* Read the database schema. If an error occurs, leave an error message |
| 446 | ** and code in pParse and return NULL. */ |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 447 | if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 |
| 448 | && SQLITE_OK!=sqlite3ReadSchema(pParse) |
| 449 | ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 453 | p = sqlite3FindTable(db, zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 454 | if( p==0 ){ |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 455 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 456 | /* If zName is the not the name of a table in the schema created using |
| 457 | ** CREATE, then check to see if it is the name of an virtual table that |
| 458 | ** can be an eponymous virtual table. */ |
drh | 7424aef | 2022-10-01 13:17:53 +0000 | [diff] [blame] | 459 | if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){ |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 460 | Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 461 | if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 462 | pMod = sqlite3PragmaVtabRegister(db, zName); |
| 463 | } |
| 464 | if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
dan | bd24e8f | 2021-07-08 18:29:25 +0000 | [diff] [blame] | 465 | testcase( pMod->pEpoTab==0 ); |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 466 | return pMod->pEpoTab; |
| 467 | } |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 468 | } |
| 469 | #endif |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 470 | if( flags & LOCATE_NOERR ) return 0; |
| 471 | pParse->checkSchema = 1; |
drh | 7424aef | 2022-10-01 13:17:53 +0000 | [diff] [blame] | 472 | }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){ |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 473 | p = 0; |
| 474 | } |
| 475 | |
| 476 | if( p==0 ){ |
| 477 | const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| 478 | if( zDbase ){ |
| 479 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
| 480 | }else{ |
| 481 | sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 482 | } |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 483 | }else{ |
| 484 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 485 | } |
dan | fab1d40 | 2015-11-26 15:51:55 +0000 | [diff] [blame] | 486 | |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 487 | return p; |
| 488 | } |
| 489 | |
| 490 | /* |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 491 | ** Locate the table identified by *p. |
| 492 | ** |
| 493 | ** This is a wrapper around sqlite3LocateTable(). The difference between |
| 494 | ** sqlite3LocateTable() and this function is that this function restricts |
| 495 | ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
| 496 | ** non-NULL if it is part of a view or trigger program definition. See |
| 497 | ** sqlite3FixSrcList() for details. |
| 498 | */ |
| 499 | Table *sqlite3LocateTableItem( |
| 500 | Parse *pParse, |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 501 | u32 flags, |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 502 | SrcItem *p |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 503 | ){ |
| 504 | const char *zDb; |
| 505 | assert( p->pSchema==0 || p->zDatabase==0 ); |
| 506 | if( p->pSchema ){ |
| 507 | int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 508 | zDb = pParse->db->aDb[iDb].zDbSName; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 509 | }else{ |
| 510 | zDb = p->zDatabase; |
| 511 | } |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 512 | return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 516 | ** Return the preferred table name for system tables. Translate legacy |
| 517 | ** names into the new preferred names, as appropriate. |
| 518 | */ |
| 519 | const char *sqlite3PreferredTableName(const char *zName){ |
| 520 | if( sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 521 | if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 ){ |
| 522 | return PREFERRED_SCHEMA_TABLE; |
| 523 | } |
| 524 | if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){ |
| 525 | return PREFERRED_TEMP_SCHEMA_TABLE; |
| 526 | } |
| 527 | } |
| 528 | return zName; |
| 529 | } |
| 530 | |
| 531 | /* |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 532 | ** Locate the in-memory structure that describes |
| 533 | ** a particular index given the name of that index |
| 534 | ** and the name of the database that contains the index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 535 | ** Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 536 | ** |
| 537 | ** If zDatabase is 0, all databases are searched for the |
| 538 | ** table and the first matching index is returned. (No checking |
| 539 | ** for duplicate index names is done.) The search order is |
| 540 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 541 | ** using the ATTACH command. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 542 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 543 | Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 544 | Index *p = 0; |
| 545 | int i; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 546 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 547 | assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 548 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 549 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 550 | Schema *pSchema = db->aDb[j].pSchema; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 551 | assert( pSchema ); |
dan | 465c2b8 | 2020-03-21 15:10:40 +0000 | [diff] [blame] | 552 | if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 553 | assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 554 | p = sqlite3HashFind(&pSchema->idxHash, zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 555 | if( p ) break; |
| 556 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 557 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 561 | ** Reclaim the memory used by an index |
| 562 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 563 | void sqlite3FreeIndex(sqlite3 *db, Index *p){ |
drh | 92aa5ea | 2009-09-11 14:05:06 +0000 | [diff] [blame] | 564 | #ifndef SQLITE_OMIT_ANALYZE |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 565 | sqlite3DeleteIndexSamples(db, p); |
drh | 92aa5ea | 2009-09-11 14:05:06 +0000 | [diff] [blame] | 566 | #endif |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 567 | sqlite3ExprDelete(db, p->pPartIdxWhere); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 568 | sqlite3ExprListDelete(db, p->aColExpr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 569 | sqlite3DbFree(db, p->zColAff); |
mistachkin | 5905f86 | 2015-12-31 19:04:42 +0000 | [diff] [blame] | 570 | if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); |
drh | 175b8f0 | 2019-08-08 15:24:17 +0000 | [diff] [blame] | 571 | #ifdef SQLITE_ENABLE_STAT4 |
drh | 75b170b | 2014-10-04 00:07:44 +0000 | [diff] [blame] | 572 | sqlite3_free(p->aiRowEst); |
| 573 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 574 | sqlite3DbFree(db, p); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 578 | ** For the index called zIdxName which is found in the database iDb, |
| 579 | ** unlike that index from its Table then remove the index from |
| 580 | ** the index hash table and free all memory structures associated |
| 581 | ** with the index. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 582 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 583 | void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 584 | Index *pIndex; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 585 | Hash *pHash; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 586 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 587 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 588 | pHash = &db->aDb[iDb].pSchema->idxHash; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 589 | pIndex = sqlite3HashInsert(pHash, zIdxName, 0); |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 590 | if( ALWAYS(pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 591 | if( pIndex->pTable->pIndex==pIndex ){ |
| 592 | pIndex->pTable->pIndex = pIndex->pNext; |
| 593 | }else{ |
| 594 | Index *p; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 595 | /* Justification of ALWAYS(); The index must be on the list of |
| 596 | ** indices. */ |
| 597 | p = pIndex->pTable->pIndex; |
| 598 | while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
| 599 | if( ALWAYS(p && p->pNext==pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 600 | p->pNext = pIndex->pNext; |
| 601 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 602 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 603 | sqlite3FreeIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 604 | } |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 605 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 609 | ** Look through the list of open database files in db->aDb[] and if |
| 610 | ** any have been closed, remove them from the list. Reallocate the |
| 611 | ** db->aDb[] structure to a smaller size, if possible. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 612 | ** |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 613 | ** Entry 0 (the "main" database) and entry 1 (the "temp" database) |
| 614 | ** are never candidates for being collapsed. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 615 | */ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 616 | void sqlite3CollapseDatabaseArray(sqlite3 *db){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 617 | int i, j; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 618 | for(i=j=2; i<db->nDb; i++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 619 | struct Db *pDb = &db->aDb[i]; |
| 620 | if( pDb->pBt==0 ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 621 | sqlite3DbFree(db, pDb->zDbSName); |
| 622 | pDb->zDbSName = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 623 | continue; |
| 624 | } |
| 625 | if( j<i ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 626 | db->aDb[j] = db->aDb[i]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 627 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 628 | j++; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 629 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 630 | db->nDb = j; |
| 631 | if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 632 | memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 633 | sqlite3DbFree(db, db->aDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 634 | db->aDb = db->aDbStatic; |
| 635 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 639 | ** Reset the schema for the database at index iDb. Also reset the |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 640 | ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. |
| 641 | ** Deferred resets may be run by calling with iDb<0. |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 642 | */ |
| 643 | void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 644 | int i; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 645 | assert( iDb<db->nDb ); |
| 646 | |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 647 | if( iDb>=0 ){ |
| 648 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 649 | DbSetProperty(db, iDb, DB_ResetWanted); |
| 650 | DbSetProperty(db, 1, DB_ResetWanted); |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 651 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 652 | } |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 653 | |
| 654 | if( db->nSchemaLock==0 ){ |
| 655 | for(i=0; i<db->nDb; i++){ |
| 656 | if( DbHasProperty(db, i, DB_ResetWanted) ){ |
| 657 | sqlite3SchemaClear(db->aDb[i].pSchema); |
| 658 | } |
| 659 | } |
| 660 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
| 664 | ** Erase all schema information from all attached databases (including |
| 665 | ** "main" and "temp") for a single database connection. |
| 666 | */ |
| 667 | void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ |
| 668 | int i; |
| 669 | sqlite3BtreeEnterAll(db); |
| 670 | for(i=0; i<db->nDb; i++){ |
| 671 | Db *pDb = &db->aDb[i]; |
| 672 | if( pDb->pSchema ){ |
dan | 63e50b9 | 2018-11-27 19:47:55 +0000 | [diff] [blame] | 673 | if( db->nSchemaLock==0 ){ |
| 674 | sqlite3SchemaClear(pDb->pSchema); |
| 675 | }else{ |
| 676 | DbSetProperty(db, i, DB_ResetWanted); |
| 677 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 680 | db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 681 | sqlite3VtabUnlockList(db); |
| 682 | sqlite3BtreeLeaveAll(db); |
dan | 63e50b9 | 2018-11-27 19:47:55 +0000 | [diff] [blame] | 683 | if( db->nSchemaLock==0 ){ |
| 684 | sqlite3CollapseDatabaseArray(db); |
| 685 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 689 | ** This routine is called when a commit occurs. |
| 690 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 691 | void sqlite3CommitInternalChanges(sqlite3 *db){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 692 | db->mDbFlags &= ~DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /* |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 696 | ** Set the expression associated with a column. This is usually |
| 697 | ** the DEFAULT value, but might also be the expression that computes |
| 698 | ** the value for a generated column. |
| 699 | */ |
| 700 | void sqlite3ColumnSetExpr( |
| 701 | Parse *pParse, /* Parsing context */ |
| 702 | Table *pTab, /* The table containing the column */ |
| 703 | Column *pCol, /* The column to receive the new DEFAULT expression */ |
| 704 | Expr *pExpr /* The new default expression */ |
| 705 | ){ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 706 | ExprList *pList; |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 707 | assert( IsOrdinaryTable(pTab) ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 708 | pList = pTab->u.tab.pDfltList; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 709 | if( pCol->iDflt==0 |
drh | 324f91a | 2021-08-04 14:50:23 +0000 | [diff] [blame] | 710 | || NEVER(pList==0) |
| 711 | || NEVER(pList->nExpr<pCol->iDflt) |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 712 | ){ |
| 713 | pCol->iDflt = pList==0 ? 1 : pList->nExpr+1; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 714 | pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr); |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 715 | }else{ |
| 716 | sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr); |
| 717 | pList->a[pCol->iDflt-1].pExpr = pExpr; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | ** Return the expression associated with a column. The expression might be |
| 723 | ** the DEFAULT clause or the AS clause of a generated column. |
| 724 | ** Return NULL if the column has no associated expression. |
| 725 | */ |
| 726 | Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){ |
| 727 | if( pCol->iDflt==0 ) return 0; |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 728 | if( NEVER(!IsOrdinaryTable(pTab)) ) return 0; |
drh | 324f91a | 2021-08-04 14:50:23 +0000 | [diff] [blame] | 729 | if( NEVER(pTab->u.tab.pDfltList==0) ) return 0; |
| 730 | if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 731 | return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /* |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 735 | ** Set the collating sequence name for a column. |
| 736 | */ |
| 737 | void sqlite3ColumnSetColl( |
| 738 | sqlite3 *db, |
| 739 | Column *pCol, |
| 740 | const char *zColl |
| 741 | ){ |
drh | 913306a | 2021-11-26 17:10:18 +0000 | [diff] [blame] | 742 | i64 nColl; |
| 743 | i64 n; |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 744 | char *zNew; |
| 745 | assert( zColl!=0 ); |
| 746 | n = sqlite3Strlen30(pCol->zCnName) + 1; |
| 747 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 748 | n += sqlite3Strlen30(pCol->zCnName+n) + 1; |
| 749 | } |
| 750 | nColl = sqlite3Strlen30(zColl) + 1; |
| 751 | zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n); |
| 752 | if( zNew ){ |
| 753 | pCol->zCnName = zNew; |
| 754 | memcpy(pCol->zCnName + n, zColl, nColl); |
| 755 | pCol->colFlags |= COLFLAG_HASCOLL; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | ** Return the collating squence name for a column |
| 761 | */ |
| 762 | const char *sqlite3ColumnColl(Column *pCol){ |
| 763 | const char *z; |
| 764 | if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0; |
| 765 | z = pCol->zCnName; |
| 766 | while( *z ){ z++; } |
| 767 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 768 | do{ z++; }while( *z ); |
| 769 | } |
| 770 | return z+1; |
| 771 | } |
| 772 | |
| 773 | /* |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 774 | ** Delete memory allocated for the column names of a table or view (the |
| 775 | ** Table.aCol[] array). |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 776 | */ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 777 | void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 778 | int i; |
| 779 | Column *pCol; |
| 780 | assert( pTable!=0 ); |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 781 | assert( db!=0 ); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 782 | if( (pCol = pTable->aCol)!=0 ){ |
| 783 | for(i=0; i<pTable->nCol; i++, pCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 784 | assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) ); |
| 785 | sqlite3DbFree(db, pCol->zCnName); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 786 | } |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 787 | sqlite3DbNNFreeNN(db, pTable->aCol); |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 788 | if( IsOrdinaryTable(pTable) ){ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 789 | sqlite3ExprListDelete(db, pTable->u.tab.pDfltList); |
| 790 | } |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 791 | if( db->pnBytesFreed==0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 792 | pTable->aCol = 0; |
| 793 | pTable->nCol = 0; |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 794 | if( IsOrdinaryTable(pTable) ){ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 795 | pTable->u.tab.pDfltList = 0; |
| 796 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 797 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 798 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 802 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 803 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 804 | ** |
| 805 | ** This routine just deletes the data structure. It does not unlink |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 806 | ** the table data structure from the hash table. But it does destroy |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 807 | ** memory structures of the indices and foreign keys associated with |
| 808 | ** the table. |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 809 | ** |
| 810 | ** The db parameter is optional. It is needed if the Table object |
| 811 | ** contains lookaside memory. (Table objects in the schema do not use |
| 812 | ** lookaside memory, but some ephemeral Table objects do.) Or the |
| 813 | ** db parameter can be used with db->pnBytesFreed to measure the memory |
| 814 | ** used by the Table object. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 815 | */ |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 816 | static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 817 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 818 | |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 819 | #ifdef SQLITE_DEBUG |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 820 | /* Record the number of outstanding lookaside allocations in schema Tables |
dan | bedf84c | 2019-07-08 13:45:02 +0000 | [diff] [blame] | 821 | ** prior to doing any free() operations. Since schema Tables do not use |
| 822 | ** lookaside, this number should not change. |
| 823 | ** |
| 824 | ** If malloc has already failed, it may be that it failed while allocating |
| 825 | ** a Table object that was going to be marked ephemeral. So do not check |
| 826 | ** that no lookaside memory is used in this case either. */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 827 | int nLookaside = 0; |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 828 | assert( db!=0 ); |
| 829 | if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 830 | nLookaside = sqlite3LookasideUsed(db, 0); |
| 831 | } |
| 832 | #endif |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 833 | |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 834 | /* Delete all indices associated with this table. */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 835 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 836 | pNext = pIndex->pNext; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 837 | assert( pIndex->pSchema==pTable->pSchema |
| 838 | || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 839 | if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 840 | char *zName = pIndex->zName; |
| 841 | TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 842 | &pIndex->pSchema->idxHash, zName, 0 |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 843 | ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 844 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 845 | assert( pOld==pIndex || pOld==0 ); |
| 846 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 847 | sqlite3FreeIndex(db, pIndex); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 848 | } |
| 849 | |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 850 | if( IsOrdinaryTable(pTable) ){ |
| 851 | sqlite3FkDelete(db, pTable); |
| 852 | } |
| 853 | #ifndef SQLITE_OMIT_VIRTUAL_TABLE |
| 854 | else if( IsVirtual(pTable) ){ |
| 855 | sqlite3VtabClear(db, pTable); |
| 856 | } |
| 857 | #endif |
| 858 | else{ |
| 859 | assert( IsView(pTable) ); |
| 860 | sqlite3SelectDelete(db, pTable->u.view.pSelect); |
| 861 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 862 | |
| 863 | /* Delete the Table structure itself. |
| 864 | */ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 865 | sqlite3DeleteColumnNames(db, pTable); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 866 | sqlite3DbFree(db, pTable->zName); |
| 867 | sqlite3DbFree(db, pTable->zColAff); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 868 | sqlite3ExprListDelete(db, pTable->pCheck); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 869 | sqlite3DbFree(db, pTable); |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 870 | |
| 871 | /* Verify that no lookaside memory was used by schema tables */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 872 | assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 873 | } |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 874 | void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 875 | /* Do not delete the table until the reference count reaches zero. */ |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 876 | assert( db!=0 ); |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 877 | if( !pTable ) return; |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 878 | if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return; |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 879 | deleteTable(db, pTable); |
| 880 | } |
| 881 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 882 | |
| 883 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 884 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 885 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 886 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 887 | void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 888 | Table *p; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 889 | Db *pDb; |
| 890 | |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 891 | assert( db!=0 ); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 892 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 972a231 | 2009-12-08 14:34:08 +0000 | [diff] [blame] | 893 | assert( zTabName ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 894 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 972a231 | 2009-12-08 14:34:08 +0000 | [diff] [blame] | 895 | testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 896 | pDb = &db->aDb[iDb]; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 897 | p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 898 | sqlite3DeleteTable(db, p); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 899 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 903 | ** Given a token, return a string that consists of the text of that |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 904 | ** token. Space to hold the returned string |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 905 | ** is obtained from sqliteMalloc() and must be freed by the calling |
| 906 | ** function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 907 | ** |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 908 | ** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
| 909 | ** surround the body of the token are removed. |
| 910 | ** |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 911 | ** Tokens are often just pointers into the original SQL text and so |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 912 | ** are not \000 terminated and are not persistent. The returned string |
| 913 | ** is \000 terminated and is persistent. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 914 | */ |
drh | b6dad52 | 2021-09-24 16:14:47 +0000 | [diff] [blame] | 915 | char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 916 | char *zName; |
| 917 | if( pName ){ |
drh | b6dad52 | 2021-09-24 16:14:47 +0000 | [diff] [blame] | 918 | zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 919 | sqlite3Dequote(zName); |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 920 | }else{ |
| 921 | zName = 0; |
| 922 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 923 | return zName; |
| 924 | } |
| 925 | |
| 926 | /* |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 927 | ** Open the sqlite_schema table stored in database number iDb for |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 928 | ** writing. The table is opened using cursor 0. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 929 | */ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 930 | void sqlite3OpenSchemaTable(Parse *p, int iDb){ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 931 | Vdbe *v = sqlite3GetVdbe(p); |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 932 | sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, LEGACY_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 933 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 934 | if( p->nTab==0 ){ |
| 935 | p->nTab = 1; |
| 936 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | /* |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 940 | ** Parameter zName points to a nul-terminated buffer containing the name |
| 941 | ** of a database ("main", "temp" or the name of an attached db). This |
| 942 | ** function returns the index of the named database in db->aDb[], or |
| 943 | ** -1 if the named db cannot be found. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 944 | */ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 945 | int sqlite3FindDbName(sqlite3 *db, const char *zName){ |
| 946 | int i = -1; /* Database number */ |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 947 | if( zName ){ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 948 | Db *pDb; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 949 | for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
drh | 2951809 | 2016-12-24 19:37:16 +0000 | [diff] [blame] | 950 | if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; |
| 951 | /* "main" is always an acceptable alias for the primary database |
| 952 | ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ |
| 953 | if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 956 | return i; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 957 | } |
| 958 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 959 | /* |
| 960 | ** The token *pName contains the name of a database (either "main" or |
| 961 | ** "temp" or the name of an attached db). This routine returns the |
| 962 | ** index of the named database in db->aDb[], or -1 if the named db |
| 963 | ** does not exist. |
| 964 | */ |
| 965 | int sqlite3FindDb(sqlite3 *db, Token *pName){ |
| 966 | int i; /* Database number */ |
| 967 | char *zName; /* Name we are searching for */ |
| 968 | zName = sqlite3NameFromToken(db, pName); |
| 969 | i = sqlite3FindDbName(db, zName); |
| 970 | sqlite3DbFree(db, zName); |
| 971 | return i; |
| 972 | } |
| 973 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 974 | /* The table or view or trigger name is passed to this routine via tokens |
| 975 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 976 | ** |
| 977 | ** CREATE TABLE xxx.yyy (...); |
| 978 | ** |
| 979 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 980 | ** the table name is not fully qualified, i.e.: |
| 981 | ** |
| 982 | ** CREATE TABLE yyy(...); |
| 983 | ** |
| 984 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 985 | ** |
| 986 | ** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
| 987 | ** pName2) that stores the unqualified table name. The index of the |
| 988 | ** database "xxx" is returned. |
| 989 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 990 | int sqlite3TwoPartName( |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 991 | Parse *pParse, /* Parsing and code generating context */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 992 | Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 993 | Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
| 994 | Token **pUnqual /* Write the unqualified object name here */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 995 | ){ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 996 | int iDb; /* Database holding the object */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 997 | sqlite3 *db = pParse->db; |
| 998 | |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 999 | assert( pName2!=0 ); |
| 1000 | if( pName2->n>0 ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1001 | if( db->init.busy ) { |
| 1002 | sqlite3ErrorMsg(pParse, "corrupt database"); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1003 | return -1; |
| 1004 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1005 | *pUnqual = pName2; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 1006 | iDb = sqlite3FindDb(db, pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1007 | if( iDb<0 ){ |
| 1008 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1009 | return -1; |
| 1010 | } |
| 1011 | }else{ |
drh | d36bcec | 2021-05-29 21:50:05 +0000 | [diff] [blame] | 1012 | assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 1013 | || (db->mDbFlags & DBFLAG_Vacuum)!=0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1014 | iDb = db->init.iDb; |
| 1015 | *pUnqual = pName1; |
| 1016 | } |
| 1017 | return iDb; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
drh | 0f1c2eb | 2018-11-03 17:31:48 +0000 | [diff] [blame] | 1021 | ** True if PRAGMA writable_schema is ON |
| 1022 | */ |
| 1023 | int sqlite3WritableSchema(sqlite3 *db){ |
| 1024 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); |
| 1025 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1026 | SQLITE_WriteSchema ); |
| 1027 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1028 | SQLITE_Defensive ); |
| 1029 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1030 | (SQLITE_WriteSchema|SQLITE_Defensive) ); |
| 1031 | return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1035 | ** This routine is used to check if the UTF-8 string zName is a legal |
| 1036 | ** unqualified name for a new schema object (table, index, view or |
| 1037 | ** trigger). All names are legal except those that begin with the string |
| 1038 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
| 1039 | ** is reserved for internal use. |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1040 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1041 | ** When parsing the sqlite_schema table, this routine also checks to |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1042 | ** make sure the "type", "name", and "tbl_name" columns are consistent |
| 1043 | ** with the SQL. |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1044 | */ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1045 | int sqlite3CheckObjectName( |
| 1046 | Parse *pParse, /* Parsing context */ |
| 1047 | const char *zName, /* Name of the object to check */ |
| 1048 | const char *zType, /* Type of this object */ |
| 1049 | const char *zTblName /* Parent table name for triggers and indexes */ |
| 1050 | ){ |
| 1051 | sqlite3 *db = pParse->db; |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 1052 | if( sqlite3WritableSchema(db) |
| 1053 | || db->init.imposterTable |
| 1054 | || !sqlite3Config.bExtraSchemaChecks |
| 1055 | ){ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1056 | /* Skip these error checks for writable_schema=ON */ |
| 1057 | return SQLITE_OK; |
| 1058 | } |
| 1059 | if( db->init.busy ){ |
| 1060 | if( sqlite3_stricmp(zType, db->init.azInit[0]) |
| 1061 | || sqlite3_stricmp(zName, db->init.azInit[1]) |
| 1062 | || sqlite3_stricmp(zTblName, db->init.azInit[2]) |
| 1063 | ){ |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 1064 | sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ |
| 1065 | return SQLITE_ERROR; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1066 | } |
| 1067 | }else{ |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 1068 | if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) |
| 1069 | || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1070 | ){ |
| 1071 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", |
| 1072 | zName); |
| 1073 | return SQLITE_ERROR; |
| 1074 | } |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 1075 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1076 | } |
| 1077 | return SQLITE_OK; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1081 | ** Return the PRIMARY KEY index of a table |
| 1082 | */ |
| 1083 | Index *sqlite3PrimaryKeyIndex(Table *pTab){ |
| 1084 | Index *p; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 1085 | for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1086 | return p; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1090 | ** Convert an table column number into a index column number. That is, |
| 1091 | ** for the column iCol in the table (as defined by the CREATE TABLE statement) |
| 1092 | ** find the (first) offset of that column in index pIdx. Or return -1 |
| 1093 | ** if column iCol is not used in index pIdx. |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1094 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1095 | i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1096 | int i; |
| 1097 | for(i=0; i<pIdx->nColumn; i++){ |
| 1098 | if( iCol==pIdx->aiColumn[i] ) return i; |
| 1099 | } |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1103 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1104 | /* Convert a storage column number into a table column number. |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1105 | ** |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1106 | ** The storage column number (0,1,2,....) is the index of the value |
| 1107 | ** as it appears in the record on disk. The true column number |
| 1108 | ** is the index (0,1,2,...) of the column in the CREATE TABLE statement. |
| 1109 | ** |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1110 | ** The storage column number is less than the table column number if |
| 1111 | ** and only there are VIRTUAL columns to the left. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1112 | ** |
| 1113 | ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1114 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1115 | i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1116 | if( pTab->tabFlags & TF_HasVirtual ){ |
| 1117 | int i; |
| 1118 | for(i=0; i<=iCol; i++){ |
| 1119 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++; |
| 1120 | } |
| 1121 | } |
| 1122 | return iCol; |
| 1123 | } |
| 1124 | #endif |
| 1125 | |
| 1126 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1127 | /* Convert a table column number into a storage column number. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1128 | ** |
| 1129 | ** The storage column number (0,1,2,....) is the index of the value |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1130 | ** as it appears in the record on disk. Or, if the input column is |
| 1131 | ** the N-th virtual column (zero-based) then the storage number is |
| 1132 | ** the number of non-virtual columns in the table plus N. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1133 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1134 | ** The true column number is the index (0,1,2,...) of the column in |
| 1135 | ** the CREATE TABLE statement. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1136 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1137 | ** If the input column is a VIRTUAL column, then it should not appear |
| 1138 | ** in storage. But the value sometimes is cached in registers that |
| 1139 | ** follow the range of registers used to construct storage. This |
| 1140 | ** avoids computing the same VIRTUAL column multiple times, and provides |
| 1141 | ** values for use by OP_Param opcodes in triggers. Hence, if the |
| 1142 | ** input column is a VIRTUAL table, put it after all the other columns. |
| 1143 | ** |
| 1144 | ** In the following, N means "normal column", S means STORED, and |
| 1145 | ** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: |
| 1146 | ** |
| 1147 | ** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); |
| 1148 | ** -- 0 1 2 3 4 5 6 7 8 |
| 1149 | ** |
| 1150 | ** Then the mapping from this function is as follows: |
| 1151 | ** |
| 1152 | ** INPUTS: 0 1 2 3 4 5 6 7 8 |
| 1153 | ** OUTPUTS: 0 1 6 2 3 7 4 5 8 |
| 1154 | ** |
| 1155 | ** So, in other words, this routine shifts all the virtual columns to |
| 1156 | ** the end. |
| 1157 | ** |
| 1158 | ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and |
drh | 7fe2fc0 | 2019-12-07 00:22:18 +0000 | [diff] [blame] | 1159 | ** this routine is a no-op macro. If the pTab does not have any virtual |
| 1160 | ** columns, then this routine is no-op that always return iCol. If iCol |
| 1161 | ** is negative (indicating the ROWID column) then this routine return iCol. |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1162 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1163 | i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1164 | int i; |
| 1165 | i16 n; |
| 1166 | assert( iCol<pTab->nCol ); |
drh | 7fe2fc0 | 2019-12-07 00:22:18 +0000 | [diff] [blame] | 1167 | if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1168 | for(i=0, n=0; i<iCol; i++){ |
| 1169 | if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++; |
| 1170 | } |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1171 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){ |
| 1172 | /* iCol is a virtual column itself */ |
| 1173 | return pTab->nNVCol + i - n; |
| 1174 | }else{ |
| 1175 | /* iCol is a normal or stored column */ |
| 1176 | return n; |
| 1177 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1178 | } |
| 1179 | #endif |
| 1180 | |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1181 | /* |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1182 | ** Insert a single OP_JournalMode query opcode in order to force the |
| 1183 | ** prepared statement to return false for sqlite3_stmt_readonly(). This |
| 1184 | ** is used by CREATE TABLE IF NOT EXISTS and similar if the table already |
| 1185 | ** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS |
| 1186 | ** will return false for sqlite3_stmt_readonly() even if that statement |
| 1187 | ** is a read-only no-op. |
| 1188 | */ |
| 1189 | static void sqlite3ForceNotReadOnly(Parse *pParse){ |
| 1190 | int iReg = ++pParse->nMem; |
| 1191 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1192 | if( v ){ |
| 1193 | sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY); |
dan | a8f249f | 2021-05-20 11:42:51 +0000 | [diff] [blame] | 1194 | sqlite3VdbeUsesBtree(v, 0); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1199 | ** Begin constructing a new table representation in memory. This is |
| 1200 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1201 | ** to a CREATE TABLE statement. In particular, this routine is called |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1202 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1203 | ** flag is true if the table should be stored in the auxiliary database |
| 1204 | ** file instead of in the main database file. This is normally the case |
| 1205 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1206 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1207 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1208 | ** The new table record is initialized and put in pParse->pNewTable. |
| 1209 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 1210 | ** routines will be called to add more information to this record. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1211 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1212 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1213 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1214 | void sqlite3StartTable( |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1215 | Parse *pParse, /* Parser context */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1216 | Token *pName1, /* First part of the name of the table or view */ |
| 1217 | Token *pName2, /* Second part of the name of the table or view */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1218 | int isTemp, /* True if this is a TEMP table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1219 | int isView, /* True if this is a VIEW */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 1220 | int isVirtual, /* True if this is a VIRTUAL table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1221 | int noErr /* Do nothing if table already exists */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1222 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1223 | Table *pTable; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1224 | char *zName = 0; /* The name of the new table */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1225 | sqlite3 *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1226 | Vdbe *v; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1227 | int iDb; /* Database number to create the table in */ |
| 1228 | Token *pName; /* Unqualified name of the table to create */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1229 | |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1230 | if( db->init.busy && db->init.newTnum==1 ){ |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1231 | /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */ |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1232 | iDb = db->init.iDb; |
| 1233 | zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); |
| 1234 | pName = pName1; |
| 1235 | }else{ |
| 1236 | /* The common case */ |
| 1237 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 1238 | if( iDb<0 ) return; |
| 1239 | if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
| 1240 | /* If creating a temp table, the name may not be qualified. Unless |
| 1241 | ** the database name is "temp" anyway. */ |
| 1242 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 1243 | return; |
| 1244 | } |
| 1245 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 1246 | zName = sqlite3NameFromToken(db, pName); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1247 | if( IN_RENAME_OBJECT ){ |
| 1248 | sqlite3RenameTokenMap(pParse, (void*)zName, pName); |
| 1249 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1250 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1251 | pParse->sNameToken = *pName; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1252 | if( zName==0 ) return; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1253 | if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1254 | goto begin_table_error; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1255 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1256 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1257 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1258 | assert( isTemp==0 || isTemp==1 ); |
| 1259 | assert( isView==0 || isView==1 ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1260 | { |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1261 | static const u8 aCode[] = { |
| 1262 | SQLITE_CREATE_TABLE, |
| 1263 | SQLITE_CREATE_TEMP_TABLE, |
| 1264 | SQLITE_CREATE_VIEW, |
| 1265 | SQLITE_CREATE_TEMP_VIEW |
| 1266 | }; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1267 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1268 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1269 | goto begin_table_error; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1270 | } |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1271 | if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], |
| 1272 | zName, 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1273 | goto begin_table_error; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | #endif |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1277 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1278 | /* Make sure the new table name does not collide with an existing |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 1279 | ** index or table name in the same database. Issue an error message if |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1280 | ** it does. The exception is if the statement being parsed was passed |
| 1281 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 1282 | ** and types will be used, so there is no need to test for namespace |
| 1283 | ** collisions. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1284 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1285 | if( !IN_SPECIAL_PARSE ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1286 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1287 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 1288 | goto begin_table_error; |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1289 | } |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1290 | pTable = sqlite3FindTable(db, zName, zDb); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1291 | if( pTable ){ |
| 1292 | if( !noErr ){ |
larrybr | 6e85b27 | 2022-02-07 01:09:49 +0000 | [diff] [blame] | 1293 | sqlite3ErrorMsg(pParse, "%s %T already exists", |
| 1294 | (IsView(pTable)? "view" : "table"), pName); |
dan | 7687c83 | 2011-04-09 15:39:02 +0000 | [diff] [blame] | 1295 | }else{ |
drh | 33c59ec | 2015-04-19 20:39:17 +0000 | [diff] [blame] | 1296 | assert( !db->init.busy || CORRUPT_DB ); |
dan | 7687c83 | 2011-04-09 15:39:02 +0000 | [diff] [blame] | 1297 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1298 | sqlite3ForceNotReadOnly(pParse); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1299 | } |
| 1300 | goto begin_table_error; |
| 1301 | } |
drh | 8a8a0d1 | 2010-09-28 20:26:44 +0000 | [diff] [blame] | 1302 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1303 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 1304 | goto begin_table_error; |
| 1305 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1306 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1307 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 1308 | pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1309 | if( pTable==0 ){ |
drh | 4df86af | 2016-02-04 11:48:00 +0000 | [diff] [blame] | 1310 | assert( db->mallocFailed ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1311 | pParse->rc = SQLITE_NOMEM_BKPT; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1312 | pParse->nErr++; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1313 | goto begin_table_error; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1314 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1315 | pTable->zName = zName; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1316 | pTable->iPKey = -1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1317 | pTable->pSchema = db->aDb[iDb].pSchema; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 1318 | pTable->nTabRef = 1; |
drh | d1417ee | 2017-06-06 18:20:43 +0000 | [diff] [blame] | 1319 | #ifdef SQLITE_DEFAULT_ROWEST |
| 1320 | pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); |
| 1321 | #else |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1322 | pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | d1417ee | 2017-06-06 18:20:43 +0000 | [diff] [blame] | 1323 | #endif |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1324 | assert( pParse->pNewTable==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1325 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1326 | |
| 1327 | /* Begin generating the code that will insert the table record into |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 1328 | ** the schema table. Note in particular that we must go ahead |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1329 | ** and allocate the record number for the table entry now. Before any |
| 1330 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 1331 | ** indices to be created and the table record must come before the |
| 1332 | ** indices. Hence, the record number for the table must be allocated |
| 1333 | ** now. |
| 1334 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1335 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1336 | int addr1; |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 1337 | int fileFormat; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1338 | int reg1, reg2, reg3; |
drh | 3c03afd | 2015-09-09 13:28:06 +0000 | [diff] [blame] | 1339 | /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ |
| 1340 | static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; |
drh | 0dd5cda | 2015-06-16 16:39:01 +0000 | [diff] [blame] | 1341 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 1342 | |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 1343 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1344 | if( isVirtual ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1345 | sqlite3VdbeAddOp0(v, OP_VBegin); |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 1346 | } |
| 1347 | #endif |
| 1348 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 1349 | /* If the file format and encoding in the database have not been set, |
| 1350 | ** set them now. |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 1351 | */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1352 | reg1 = pParse->regRowid = ++pParse->nMem; |
| 1353 | reg2 = pParse->regRoot = ++pParse->nMem; |
| 1354 | reg3 = ++pParse->nMem; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 1355 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1356 | sqlite3VdbeUsesBtree(v, iDb); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1357 | addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 1358 | fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 1359 | 1 : SQLITE_MAX_FILE_FORMAT; |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 1360 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); |
| 1361 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1362 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 1363 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1364 | /* This just creates a place-holder record in the sqlite_schema table. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1365 | ** The record created does not contain anything yet. It will be replaced |
| 1366 | ** by the real entry in code generated at sqlite3EndTable(). |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 1367 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 1368 | ** The rowid for the new entry is left in register pParse->regRowid. |
| 1369 | ** The root page number of the new table is left in reg pParse->regRoot. |
| 1370 | ** The rowid and root page number values are needed by the code that |
| 1371 | ** sqlite3EndTable will generate. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1372 | */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 1373 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 1374 | if( isView || isVirtual ){ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1375 | sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1376 | }else |
| 1377 | #endif |
| 1378 | { |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1379 | assert( !pParse->bReturning ); |
| 1380 | pParse->u1.addrCrTab = |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 1381 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1382 | } |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 1383 | sqlite3OpenSchemaTable(pParse, iDb); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1384 | sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); |
drh | 3c03afd | 2015-09-09 13:28:06 +0000 | [diff] [blame] | 1385 | sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1386 | sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); |
| 1387 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1388 | sqlite3VdbeAddOp0(v, OP_Close); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1389 | } |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* Normal (non-error) return. */ |
| 1392 | return; |
| 1393 | |
| 1394 | /* If an error occurs, we jump here */ |
| 1395 | begin_table_error: |
drh | c0495e8 | 2021-07-22 21:11:06 +0000 | [diff] [blame] | 1396 | pParse->checkSchema = 1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1397 | sqlite3DbFree(db, zName); |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1398 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1401 | /* Set properties of a table column based on the (magical) |
| 1402 | ** name of the column. |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 1403 | */ |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1404 | #if SQLITE_ENABLE_HIDDEN_COLUMNS |
drh | e611050 | 2015-12-31 15:34:03 +0000 | [diff] [blame] | 1405 | void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1406 | if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){ |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1407 | pCol->colFlags |= COLFLAG_HIDDEN; |
drh | 6f6e60d | 2021-02-18 15:45:34 +0000 | [diff] [blame] | 1408 | if( pTab ) pTab->tabFlags |= TF_HasHidden; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 1409 | }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ |
| 1410 | pTab->tabFlags |= TF_OOOHidden; |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1411 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1412 | } |
drh | e611050 | 2015-12-31 15:34:03 +0000 | [diff] [blame] | 1413 | #endif |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1414 | |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1415 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1416 | ** Name of the special TEMP trigger used to implement RETURNING. The |
| 1417 | ** name begins with "sqlite_" so that it is guaranteed not to collide |
| 1418 | ** with any application-generated triggers. |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1419 | */ |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1420 | #define RETURNING_TRIGGER_NAME "sqlite_returning" |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1421 | |
| 1422 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1423 | ** Clean up the data structures associated with the RETURNING clause. |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1424 | */ |
| 1425 | static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){ |
| 1426 | Hash *pHash; |
| 1427 | pHash = &(db->aDb[1].pSchema->trigHash); |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1428 | sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, 0); |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1429 | sqlite3ExprListDelete(db, pRet->pReturnEL); |
| 1430 | sqlite3DbFree(db, pRet); |
| 1431 | } |
| 1432 | |
| 1433 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1434 | ** Add the RETURNING clause to the parse currently underway. |
| 1435 | ** |
| 1436 | ** This routine creates a special TEMP trigger that will fire for each row |
| 1437 | ** of the DML statement. That TEMP trigger contains a single SELECT |
| 1438 | ** statement with a result set that is the argument of the RETURNING clause. |
| 1439 | ** The trigger has the Trigger.bReturning flag and an opcode of |
| 1440 | ** TK_RETURNING instead of TK_SELECT, so that the trigger code generator |
| 1441 | ** knows to handle it specially. The TEMP trigger is automatically |
| 1442 | ** removed at the end of the parse. |
| 1443 | ** |
| 1444 | ** When this routine is called, we do not yet know if the RETURNING clause |
| 1445 | ** is attached to a DELETE, INSERT, or UPDATE, so construct it as a |
| 1446 | ** RETURNING trigger instead. It will then be converted into the appropriate |
| 1447 | ** type on the first call to sqlite3TriggersExist(). |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1448 | */ |
| 1449 | void sqlite3AddReturning(Parse *pParse, ExprList *pList){ |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1450 | Returning *pRet; |
| 1451 | Hash *pHash; |
| 1452 | sqlite3 *db = pParse->db; |
drh | e1c9a4e | 2021-02-07 23:28:20 +0000 | [diff] [blame] | 1453 | if( pParse->pNewTrigger ){ |
| 1454 | sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger"); |
| 1455 | }else{ |
| 1456 | assert( pParse->bReturning==0 ); |
| 1457 | } |
drh | d086aa0 | 2021-01-29 21:31:59 +0000 | [diff] [blame] | 1458 | pParse->bReturning = 1; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1459 | pRet = sqlite3DbMallocZero(db, sizeof(*pRet)); |
| 1460 | if( pRet==0 ){ |
| 1461 | sqlite3ExprListDelete(db, pList); |
| 1462 | return; |
| 1463 | } |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1464 | pParse->u1.pReturning = pRet; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1465 | pRet->pParse = pParse; |
| 1466 | pRet->pReturnEL = pList; |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1467 | sqlite3ParserAddCleanup(pParse, |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1468 | (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet); |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 1469 | testcase( pParse->earlyCleanup ); |
drh | cf4108b | 2021-01-30 03:06:19 +0000 | [diff] [blame] | 1470 | if( db->mallocFailed ) return; |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1471 | pRet->retTrig.zName = RETURNING_TRIGGER_NAME; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1472 | pRet->retTrig.op = TK_RETURNING; |
| 1473 | pRet->retTrig.tr_tm = TRIGGER_AFTER; |
| 1474 | pRet->retTrig.bReturning = 1; |
| 1475 | pRet->retTrig.pSchema = db->aDb[1].pSchema; |
drh | a476768 | 2021-04-27 13:04:18 +0000 | [diff] [blame] | 1476 | pRet->retTrig.pTabSchema = db->aDb[1].pSchema; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1477 | pRet->retTrig.step_list = &pRet->retTStep; |
drh | dac9a5f | 2021-01-29 21:18:46 +0000 | [diff] [blame] | 1478 | pRet->retTStep.op = TK_RETURNING; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1479 | pRet->retTStep.pTrig = &pRet->retTrig; |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1480 | pRet->retTStep.pExprList = pList; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1481 | pHash = &(db->aDb[1].pSchema->trigHash); |
drh | e1c9a4e | 2021-02-07 23:28:20 +0000 | [diff] [blame] | 1482 | assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr ); |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1483 | if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig) |
drh | 0166df0 | 2021-01-30 12:07:32 +0000 | [diff] [blame] | 1484 | ==&pRet->retTrig ){ |
| 1485 | sqlite3OomFault(db); |
| 1486 | } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1487 | } |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 1488 | |
| 1489 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1490 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1491 | ** |
| 1492 | ** The parser calls this routine once for each column declaration |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1493 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1494 | ** first to get things going. Then this routine is called for each |
| 1495 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1496 | */ |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1497 | void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1498 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1499 | int i; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 1500 | char *z; |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1501 | char *zType; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1502 | Column *pCol; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1503 | sqlite3 *db = pParse->db; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1504 | u8 hName; |
drh | 7b3c514 | 2021-07-30 12:47:35 +0000 | [diff] [blame] | 1505 | Column *aNew; |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1506 | u8 eType = COLTYPE_CUSTOM; |
| 1507 | u8 szEst = 1; |
| 1508 | char affinity = SQLITE_AFF_BLOB; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1509 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1510 | if( (p = pParse->pNewTable)==0 ) return; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1511 | if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 1512 | sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
| 1513 | return; |
| 1514 | } |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1515 | if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(&sName); |
drh | e48f261 | 2021-07-30 20:09:08 +0000 | [diff] [blame] | 1516 | |
| 1517 | /* Because keywords GENERATE ALWAYS can be converted into indentifiers |
| 1518 | ** by the parser, we can sometimes end up with a typename that ends |
| 1519 | ** with "generated always". Check for this case and omit the surplus |
| 1520 | ** text. */ |
| 1521 | if( sType.n>=16 |
| 1522 | && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0 |
| 1523 | ){ |
| 1524 | sType.n -= 6; |
| 1525 | while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 1526 | if( sType.n>=9 |
| 1527 | && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0 |
| 1528 | ){ |
| 1529 | sType.n -= 9; |
| 1530 | while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 1531 | } |
| 1532 | } |
| 1533 | |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1534 | /* Check for standard typenames. For standard typenames we will |
| 1535 | ** set the Column.eType field rather than storing the typename after |
| 1536 | ** the column name, in order to save space. */ |
| 1537 | if( sType.n>=3 ){ |
| 1538 | sqlite3DequoteToken(&sType); |
| 1539 | for(i=0; i<SQLITE_N_STDTYPE; i++){ |
| 1540 | if( sType.n==sqlite3StdTypeLen[i] |
| 1541 | && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0 |
| 1542 | ){ |
| 1543 | sType.n = 0; |
| 1544 | eType = i+1; |
| 1545 | affinity = sqlite3StdTypeAffinity[i]; |
| 1546 | if( affinity<=SQLITE_AFF_TEXT ) szEst = 5; |
| 1547 | break; |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
drh | 913306a | 2021-11-26 17:10:18 +0000 | [diff] [blame] | 1552 | z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) ); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1553 | if( z==0 ) return; |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1554 | if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName); |
| 1555 | memcpy(z, sName.z, sName.n); |
| 1556 | z[sName.n] = 0; |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1557 | sqlite3Dequote(z); |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1558 | hName = sqlite3StrIHash(z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1559 | for(i=0; i<p->nCol; i++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1560 | if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1561 | sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1562 | sqlite3DbFree(db, z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | } |
drh | 913306a | 2021-11-26 17:10:18 +0000 | [diff] [blame] | 1566 | aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0])); |
drh | 7b3c514 | 2021-07-30 12:47:35 +0000 | [diff] [blame] | 1567 | if( aNew==0 ){ |
| 1568 | sqlite3DbFree(db, z); |
| 1569 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1570 | } |
drh | 7b3c514 | 2021-07-30 12:47:35 +0000 | [diff] [blame] | 1571 | p->aCol = aNew; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1572 | pCol = &p->aCol[p->nCol]; |
| 1573 | memset(pCol, 0, sizeof(p->aCol[0])); |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1574 | pCol->zCnName = z; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1575 | pCol->hName = hName; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 1576 | sqlite3ColumnPropertiesFromName(p, pCol); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1577 | |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1578 | if( sType.n==0 ){ |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1579 | /* If there is no type specified, columns have the default affinity |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1580 | ** 'BLOB' with a default size of 4 bytes. */ |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1581 | pCol->affinity = affinity; |
drh | b70f2ea | 2021-08-18 12:05:22 +0000 | [diff] [blame] | 1582 | pCol->eCType = eType; |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1583 | pCol->szEst = szEst; |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1584 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1585 | if( affinity==SQLITE_AFF_BLOB ){ |
| 1586 | if( 4>=sqlite3GlobalConfig.szSorterRef ){ |
| 1587 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1588 | } |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1589 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1590 | #endif |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1591 | }else{ |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 1592 | zType = z + sqlite3Strlen30(z) + 1; |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1593 | memcpy(zType, sType.z, sType.n); |
| 1594 | zType[sType.n] = 0; |
drh | a6dddd9 | 2016-04-18 15:46:14 +0000 | [diff] [blame] | 1595 | sqlite3Dequote(zType); |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1596 | pCol->affinity = sqlite3AffinityType(zType, pCol); |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1597 | pCol->colFlags |= COLFLAG_HASTYPE; |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1598 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1599 | p->nCol++; |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 1600 | p->nNVCol++; |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 1601 | pParse->constraintName.n = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1605 | ** This routine is called by the parser while in the middle of |
| 1606 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 1607 | ** been seen on a column. This routine sets the notNull flag on |
| 1608 | ** the column currently under construction. |
| 1609 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1610 | void sqlite3AddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1611 | Table *p; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1612 | Column *pCol; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1613 | p = pParse->pNewTable; |
| 1614 | if( p==0 || NEVER(p->nCol<1) ) return; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1615 | pCol = &p->aCol[p->nCol-1]; |
| 1616 | pCol->notNull = (u8)onError; |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1617 | p->tabFlags |= TF_HasNotNull; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created |
| 1620 | ** on this column. */ |
| 1621 | if( pCol->colFlags & COLFLAG_UNIQUE ){ |
| 1622 | Index *pIdx; |
| 1623 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1624 | assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); |
| 1625 | if( pIdx->aiColumn[0]==p->nCol-1 ){ |
| 1626 | pIdx->uniqNotNull = 1; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /* |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1633 | ** Scan the column type name zType (length nType) and return the |
| 1634 | ** associated affinity type. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1635 | ** |
| 1636 | ** This routine does a case-independent search of zType for the |
| 1637 | ** substrings in the following table. If one of the substrings is |
| 1638 | ** found, the corresponding affinity is returned. If zType contains |
| 1639 | ** more than one of the substrings, entries toward the top of |
| 1640 | ** the table take priority. For example, if zType is 'BLOBINT', |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1641 | ** SQLITE_AFF_INTEGER is returned. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1642 | ** |
| 1643 | ** Substring | Affinity |
| 1644 | ** -------------------------------- |
| 1645 | ** 'INT' | SQLITE_AFF_INTEGER |
| 1646 | ** 'CHAR' | SQLITE_AFF_TEXT |
| 1647 | ** 'CLOB' | SQLITE_AFF_TEXT |
| 1648 | ** 'TEXT' | SQLITE_AFF_TEXT |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1649 | ** 'BLOB' | SQLITE_AFF_BLOB |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1650 | ** 'REAL' | SQLITE_AFF_REAL |
| 1651 | ** 'FLOA' | SQLITE_AFF_REAL |
| 1652 | ** 'DOUB' | SQLITE_AFF_REAL |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1653 | ** |
| 1654 | ** If none of the substrings in the above table are found, |
| 1655 | ** SQLITE_AFF_NUMERIC is returned. |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1656 | */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1657 | char sqlite3AffinityType(const char *zIn, Column *pCol){ |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1658 | u32 h = 0; |
| 1659 | char aff = SQLITE_AFF_NUMERIC; |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1660 | const char *zChar = 0; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1661 | |
drh | 2f1e02e | 2016-03-09 02:12:44 +0000 | [diff] [blame] | 1662 | assert( zIn!=0 ); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1663 | while( zIn[0] ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1664 | h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1665 | zIn++; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1666 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1667 | aff = SQLITE_AFF_TEXT; |
| 1668 | zChar = zIn; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1669 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| 1670 | aff = SQLITE_AFF_TEXT; |
| 1671 | }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 1672 | aff = SQLITE_AFF_TEXT; |
| 1673 | }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1674 | && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1675 | aff = SQLITE_AFF_BLOB; |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1676 | if( zIn[0]=='(' ) zChar = zIn; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1677 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1678 | }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
| 1679 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1680 | aff = SQLITE_AFF_REAL; |
| 1681 | }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
| 1682 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1683 | aff = SQLITE_AFF_REAL; |
| 1684 | }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
| 1685 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1686 | aff = SQLITE_AFF_REAL; |
| 1687 | #endif |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1688 | }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1689 | aff = SQLITE_AFF_INTEGER; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1690 | break; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1691 | } |
| 1692 | } |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1693 | |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1694 | /* If pCol is not NULL, store an estimate of the field size. The |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1695 | ** estimate is scaled so that the size of an integer is 1. */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1696 | if( pCol ){ |
| 1697 | int v = 0; /* default size is approx 4 bytes */ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 1698 | if( aff<SQLITE_AFF_NUMERIC ){ |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1699 | if( zChar ){ |
| 1700 | while( zChar[0] ){ |
| 1701 | if( sqlite3Isdigit(zChar[0]) ){ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1702 | /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1703 | sqlite3GetInt32(zChar, &v); |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | zChar++; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1707 | } |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1708 | }else{ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1709 | v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1710 | } |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1711 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1712 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1713 | if( v>=sqlite3GlobalConfig.szSorterRef ){ |
| 1714 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1715 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1716 | #endif |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1717 | v = v/4 + 1; |
| 1718 | if( v>255 ) v = 255; |
| 1719 | pCol->szEst = v; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1720 | } |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1721 | return aff; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /* |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1725 | ** The expression is the default value for the most recently added column |
| 1726 | ** of the table currently under construction. |
| 1727 | ** |
| 1728 | ** Default value expressions must be constant. Raise an exception if this |
| 1729 | ** is not the case. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1730 | ** |
| 1731 | ** This routine is called by the parser while in the middle of |
| 1732 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1733 | */ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1734 | void sqlite3AddDefaultValue( |
| 1735 | Parse *pParse, /* Parsing context */ |
| 1736 | Expr *pExpr, /* The parsed expression of the default value */ |
| 1737 | const char *zStart, /* Start of the default value text */ |
| 1738 | const char *zEnd /* First character past end of defaut value text */ |
| 1739 | ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1740 | Table *p; |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1741 | Column *pCol; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1742 | sqlite3 *db = pParse->db; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1743 | p = pParse->pNewTable; |
| 1744 | if( p!=0 ){ |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 1745 | int isInit = db->init.busy && db->init.iDb!=1; |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1746 | pCol = &(p->aCol[p->nCol-1]); |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 1747 | if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){ |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1748 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1749 | pCol->zCnName); |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1750 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1751 | }else if( pCol->colFlags & COLFLAG_GENERATED ){ |
drh | ab0992f | 2019-10-23 03:53:10 +0000 | [diff] [blame] | 1752 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1753 | testcase( pCol->colFlags & COLFLAG_STORED ); |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1754 | sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column"); |
| 1755 | #endif |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1756 | }else{ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1757 | /* A copy of pExpr is used instead of the original, as pExpr contains |
drh | 424981d | 2018-03-28 15:56:55 +0000 | [diff] [blame] | 1758 | ** tokens that point to volatile memory. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1759 | */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1760 | Expr x, *pDfltExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1761 | memset(&x, 0, sizeof(x)); |
| 1762 | x.op = TK_SPAN; |
drh | 9b2e043 | 2017-12-27 19:43:22 +0000 | [diff] [blame] | 1763 | x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1764 | x.pLeft = pExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1765 | x.flags = EP_Skip; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1766 | pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1767 | sqlite3DbFree(db, x.u.zToken); |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1768 | sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr); |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1769 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1770 | } |
dan | 8900a48 | 2018-09-05 14:36:05 +0000 | [diff] [blame] | 1771 | if( IN_RENAME_OBJECT ){ |
| 1772 | sqlite3RenameExprUnmap(pParse, pExpr); |
| 1773 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1774 | sqlite3ExprDelete(db, pExpr); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /* |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1778 | ** Backwards Compatibility Hack: |
| 1779 | ** |
| 1780 | ** Historical versions of SQLite accepted strings as column names in |
| 1781 | ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
| 1782 | ** |
| 1783 | ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) |
| 1784 | ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); |
| 1785 | ** |
| 1786 | ** This is goofy. But to preserve backwards compatibility we continue to |
| 1787 | ** accept it. This routine does the necessary conversion. It converts |
| 1788 | ** the expression given in its argument from a TK_STRING into a TK_ID |
| 1789 | ** if the expression is just a TK_STRING with an optional COLLATE clause. |
drh | 6c59136 | 2019-04-27 20:16:42 +0000 | [diff] [blame] | 1790 | ** If the expression is anything other than TK_STRING, the expression is |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1791 | ** unchanged. |
| 1792 | */ |
| 1793 | static void sqlite3StringToId(Expr *p){ |
| 1794 | if( p->op==TK_STRING ){ |
| 1795 | p->op = TK_ID; |
| 1796 | }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ |
| 1797 | p->pLeft->op = TK_ID; |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | /* |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1802 | ** Tag the given column as being part of the PRIMARY KEY |
| 1803 | */ |
| 1804 | static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){ |
| 1805 | pCol->colFlags |= COLFLAG_PRIMKEY; |
| 1806 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1807 | if( pCol->colFlags & COLFLAG_GENERATED ){ |
| 1808 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1809 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 1810 | sqlite3ErrorMsg(pParse, |
| 1811 | "generated columns cannot be part of the PRIMARY KEY"); |
| 1812 | } |
| 1813 | #endif |
| 1814 | } |
| 1815 | |
| 1816 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1817 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 1818 | ** of columns that form the primary key. If pList is NULL, then the |
| 1819 | ** most recently added column of the table is the primary key. |
| 1820 | ** |
| 1821 | ** A table can have at most one primary key. If the table already has |
| 1822 | ** a primary key (and this is the second primary key) then create an |
| 1823 | ** error. |
| 1824 | ** |
| 1825 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1826 | ** then we will try to use that column as the rowid. Set the Table.iPKey |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1827 | ** field of the table under construction to be the index of the |
| 1828 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 1829 | ** no INTEGER PRIMARY KEY. |
| 1830 | ** |
| 1831 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 1832 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 1833 | */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1834 | void sqlite3AddPrimaryKey( |
| 1835 | Parse *pParse, /* Parsing context */ |
| 1836 | ExprList *pList, /* List of field names to be indexed */ |
| 1837 | int onError, /* What to do with a uniqueness conflict */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 1838 | int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
| 1839 | int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1840 | ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1841 | Table *pTab = pParse->pNewTable; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1842 | Column *pCol = 0; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1843 | int iCol = -1, i; |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1844 | int nTerm; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1845 | if( pTab==0 ) goto primary_key_exit; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1846 | if( pTab->tabFlags & TF_HasPrimaryKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1847 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1848 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1849 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1850 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1851 | pTab->tabFlags |= TF_HasPrimaryKey; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1852 | if( pList==0 ){ |
| 1853 | iCol = pTab->nCol - 1; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1854 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1855 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1856 | nTerm = 1; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1857 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1858 | nTerm = pList->nExpr; |
| 1859 | for(i=0; i<nTerm; i++){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1860 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 1861 | assert( pCExpr!=0 ); |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1862 | sqlite3StringToId(pCExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 1863 | if( pCExpr->op==TK_ID ){ |
drh | f975107 | 2021-10-07 13:40:29 +0000 | [diff] [blame] | 1864 | const char *zCName; |
| 1865 | assert( !ExprHasProperty(pCExpr, EP_IntValue) ); |
| 1866 | zCName = pCExpr->u.zToken; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1867 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1868 | if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){ |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1869 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1870 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1873 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1874 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1877 | if( nTerm==1 |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1878 | && pCol |
drh | b70f2ea | 2021-08-18 12:05:22 +0000 | [diff] [blame] | 1879 | && pCol->eCType==COLTYPE_INTEGER |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1880 | && sortOrder!=SQLITE_SO_DESC |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1881 | ){ |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1882 | if( IN_RENAME_OBJECT && pList ){ |
dan | 2381f6d | 2019-03-20 16:58:21 +0000 | [diff] [blame] | 1883 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 1884 | sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1885 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1886 | pTab->iPKey = iCol; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1887 | pTab->keyConf = (u8)onError; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1888 | assert( autoInc==0 || autoInc==1 ); |
| 1889 | pTab->tabFlags |= autoInc*TF_Autoincrement; |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 1890 | if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags; |
drh | 34ab941 | 2019-12-19 17:42:27 +0000 | [diff] [blame] | 1891 | (void)sqlite3HasExplicitNulls(pParse, pList); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1892 | }else if( autoInc ){ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1893 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1894 | sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1895 | "INTEGER PRIMARY KEY"); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1896 | #endif |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1897 | }else{ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1898 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
| 1899 | 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1900 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1901 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1902 | |
| 1903 | primary_key_exit: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1904 | sqlite3ExprListDelete(pParse->db, pList); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1905 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /* |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1909 | ** Add a new CHECK constraint to the table currently under construction. |
| 1910 | */ |
| 1911 | void sqlite3AddCheckConstraint( |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 1912 | Parse *pParse, /* Parsing context */ |
| 1913 | Expr *pCheckExpr, /* The check expression */ |
| 1914 | const char *zStart, /* Opening "(" */ |
| 1915 | const char *zEnd /* Closing ")" */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1916 | ){ |
| 1917 | #ifndef SQLITE_OMIT_CHECK |
| 1918 | Table *pTab = pParse->pNewTable; |
drh | c9bbb01 | 2014-05-21 08:48:18 +0000 | [diff] [blame] | 1919 | sqlite3 *db = pParse->db; |
| 1920 | if( pTab && !IN_DECLARE_VTAB |
| 1921 | && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) |
| 1922 | ){ |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1923 | pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); |
| 1924 | if( pParse->constraintName.n ){ |
| 1925 | sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 1926 | }else{ |
| 1927 | Token t; |
| 1928 | for(zStart++; sqlite3Isspace(zStart[0]); zStart++){} |
| 1929 | while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; } |
| 1930 | t.z = zStart; |
| 1931 | t.n = (int)(zEnd - t.z); |
| 1932 | sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1933 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1934 | }else |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1935 | #endif |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1936 | { |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1937 | sqlite3ExprDelete(pParse->db, pCheckExpr); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1938 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1942 | ** Set the collation function of the most recently parsed table column |
| 1943 | ** to the CollSeq given. |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1944 | */ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1945 | void sqlite3AddCollateType(Parse *pParse, Token *pToken){ |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1946 | Table *p; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1947 | int i; |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1948 | char *zColl; /* Dequoted name of collation sequence */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1949 | sqlite3 *db; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1950 | |
dan | 936a305 | 2020-10-12 15:27:50 +0000 | [diff] [blame] | 1951 | if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1952 | i = p->nCol-1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1953 | db = pParse->db; |
| 1954 | zColl = sqlite3NameFromToken(db, pToken); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1955 | if( !zColl ) return; |
| 1956 | |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1957 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1958 | Index *pIdx; |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 1959 | sqlite3ColumnSetColl(db, &p->aCol[i], zColl); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1960 | |
| 1961 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 1962 | ** then an index may have been created on this column before the |
| 1963 | ** collation type was added. Correct this if it is the case. |
| 1964 | */ |
| 1965 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1966 | assert( pIdx->nKeyCol==1 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1967 | if( pIdx->aiColumn[0]==i ){ |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 1968 | pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | } |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 1972 | sqlite3DbFree(db, zColl); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1975 | /* Change the most recently parsed column to be a GENERATED ALWAYS AS |
| 1976 | ** column. |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1977 | */ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1978 | void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){ |
| 1979 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1980 | u8 eType = COLFLAG_VIRTUAL; |
| 1981 | Table *pTab = pParse->pNewTable; |
| 1982 | Column *pCol; |
drh | f68bf5f | 2019-12-04 03:31:29 +0000 | [diff] [blame] | 1983 | if( pTab==0 ){ |
| 1984 | /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ |
| 1985 | goto generated_done; |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1986 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1987 | pCol = &(pTab->aCol[pTab->nCol-1]); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1988 | if( IN_DECLARE_VTAB ){ |
| 1989 | sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns"); |
| 1990 | goto generated_done; |
| 1991 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1992 | if( pCol->iDflt>0 ) goto generated_error; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1993 | if( pType ){ |
| 1994 | if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){ |
| 1995 | /* no-op */ |
| 1996 | }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){ |
| 1997 | eType = COLFLAG_STORED; |
| 1998 | }else{ |
| 1999 | goto generated_error; |
| 2000 | } |
| 2001 | } |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 2002 | if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2003 | pCol->colFlags |= eType; |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 2004 | assert( TF_HasVirtual==COLFLAG_VIRTUAL ); |
| 2005 | assert( TF_HasStored==COLFLAG_STORED ); |
| 2006 | pTab->tabFlags |= eType; |
drh | a0e16a2 | 2019-10-27 22:22:24 +0000 | [diff] [blame] | 2007 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 2008 | makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */ |
| 2009 | } |
drh | fe83892 | 2022-12-21 14:13:49 +0000 | [diff] [blame] | 2010 | if( ALWAYS(pExpr) && pExpr->op==TK_ID ){ |
| 2011 | /* The value of a generated column needs to be a real expression, not |
| 2012 | ** just a reference to another column, in order for covering index |
| 2013 | ** optimizations to work correctly. So if the value is not an expression, |
| 2014 | ** turn it into one by adding a unary "+" operator. */ |
| 2015 | pExpr = sqlite3PExpr(pParse, TK_UPLUS, pExpr, 0); |
| 2016 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2017 | sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 2018 | pExpr = 0; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2019 | goto generated_done; |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 2020 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2021 | generated_error: |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 2022 | sqlite3ErrorMsg(pParse, "error in generated column \"%s\"", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2023 | pCol->zCnName); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2024 | generated_done: |
| 2025 | sqlite3ExprDelete(pParse->db, pExpr); |
| 2026 | #else |
| 2027 | /* Throw and error for the GENERATED ALWAYS AS clause if the |
| 2028 | ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */ |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 2029 | sqlite3ErrorMsg(pParse, "generated columns not supported"); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2030 | sqlite3ExprDelete(pParse->db, pExpr); |
| 2031 | #endif |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 2034 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2035 | ** Generate code that will increment the schema cookie. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2036 | ** |
| 2037 | ** The schema cookie is used to determine when the schema for the |
| 2038 | ** database changes. After each schema change, the cookie value |
| 2039 | ** changes. When a process first reads the schema it records the |
| 2040 | ** cookie. Thereafter, whenever it goes to access the database, |
| 2041 | ** it checks the cookie to make sure the schema has not changed |
| 2042 | ** since it was last read. |
| 2043 | ** |
| 2044 | ** This plan is not completely bullet-proof. It is possible for |
| 2045 | ** the schema to change multiple times and for the cookie to be |
| 2046 | ** set back to prior value. But schema changes are infrequent |
| 2047 | ** and the probability of hitting the same cookie value is only |
| 2048 | ** 1 chance in 2^32. So we're safe enough. |
drh | 96fdcb4 | 2016-09-27 00:09:33 +0000 | [diff] [blame] | 2049 | ** |
| 2050 | ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments |
| 2051 | ** the schema-version whenever the schema changes. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2052 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2053 | void sqlite3ChangeCookie(Parse *pParse, int iDb){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2054 | sqlite3 *db = pParse->db; |
| 2055 | Vdbe *v = pParse->pVdbe; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2056 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 2057 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, |
drh | 3517b31 | 2018-04-09 00:46:42 +0000 | [diff] [blame] | 2058 | (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2062 | ** Measure the number of characters needed to output the given |
| 2063 | ** identifier. The number returned includes any quotes used |
| 2064 | ** but does not include the null terminator. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2065 | ** |
| 2066 | ** The estimate is conservative. It might be larger that what is |
| 2067 | ** really needed. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2068 | */ |
| 2069 | static int identLength(const char *z){ |
| 2070 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2071 | for(n=0; *z; n++, z++){ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2072 | if( *z=='"' ){ n++; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2073 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2074 | return n + 2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 2078 | ** The first parameter is a pointer to an output buffer. The second |
| 2079 | ** parameter is a pointer to an integer that contains the offset at |
| 2080 | ** which to write into the output buffer. This function copies the |
| 2081 | ** nul-terminated string pointed to by the third parameter, zSignedIdent, |
| 2082 | ** to the specified offset in the buffer and updates *pIdx to refer |
| 2083 | ** to the first byte after the last byte written before returning. |
| 2084 | ** |
| 2085 | ** If the string zSignedIdent consists entirely of alpha-numeric |
| 2086 | ** characters, does not begin with a digit and is not an SQL keyword, |
| 2087 | ** then it is copied to the output buffer exactly as it is. Otherwise, |
| 2088 | ** it is quoted using double-quotes. |
| 2089 | */ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2090 | static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2091 | unsigned char *zIdent = (unsigned char*)zSignedIdent; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2092 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2093 | i = *pIdx; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 2094 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2095 | for(j=0; zIdent[j]; j++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 2096 | if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2097 | } |
drh | c740752 | 2014-01-10 20:38:12 +0000 | [diff] [blame] | 2098 | needQuote = sqlite3Isdigit(zIdent[0]) |
| 2099 | || sqlite3KeywordCode(zIdent, j)!=TK_ID |
| 2100 | || zIdent[j]!=0 |
| 2101 | || j==0; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 2102 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2103 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2104 | for(j=0; zIdent[j]; j++){ |
| 2105 | z[i++] = zIdent[j]; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2106 | if( zIdent[j]=='"' ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2107 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2108 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2109 | z[i] = 0; |
| 2110 | *pIdx = i; |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | ** Generate a CREATE TABLE statement appropriate for the given |
| 2115 | ** table. Memory to hold the text of the statement is obtained |
| 2116 | ** from sqliteMalloc() and must be freed by the calling function. |
| 2117 | */ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 2118 | static char *createTableStmt(sqlite3 *db, Table *p){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2119 | int i, k, n; |
| 2120 | char *zStmt; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2121 | char *zSep, *zSep2, *zEnd; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2122 | Column *pCol; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2123 | n = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2124 | for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2125 | n += identLength(pCol->zCnName) + 5; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2126 | } |
| 2127 | n += identLength(p->zName); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2128 | if( n<50 ){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2129 | zSep = ""; |
| 2130 | zSep2 = ","; |
| 2131 | zEnd = ")"; |
| 2132 | }else{ |
| 2133 | zSep = "\n "; |
| 2134 | zSep2 = ",\n "; |
| 2135 | zEnd = "\n)"; |
| 2136 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2137 | n += 35 + 6*p->nCol; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2138 | zStmt = sqlite3DbMallocRaw(0, n); |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 2139 | if( zStmt==0 ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2140 | sqlite3OomFault(db); |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 2141 | return 0; |
| 2142 | } |
drh | bdb339f | 2009-02-02 18:03:21 +0000 | [diff] [blame] | 2143 | sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2144 | k = sqlite3Strlen30(zStmt); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2145 | identPut(zStmt, &k, p->zName); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2146 | zStmt[k++] = '('; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2147 | for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2148 | static const char * const azType[] = { |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2149 | /* SQLITE_AFF_BLOB */ "", |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 2150 | /* SQLITE_AFF_TEXT */ " TEXT", |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2151 | /* SQLITE_AFF_NUMERIC */ " NUM", |
| 2152 | /* SQLITE_AFF_INTEGER */ " INT", |
drh | 00d6b27 | 2022-12-15 20:03:08 +0000 | [diff] [blame] | 2153 | /* SQLITE_AFF_REAL */ " REAL", |
| 2154 | /* SQLITE_AFF_FLEXNUM */ " NUM", |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2155 | }; |
| 2156 | int len; |
| 2157 | const char *zType; |
| 2158 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2159 | sqlite3_snprintf(n-k, &zStmt[k], zSep); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2160 | k += sqlite3Strlen30(&zStmt[k]); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2161 | zSep = zSep2; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2162 | identPut(zStmt, &k, pCol->zCnName); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2163 | assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
| 2164 | assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
| 2165 | testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 2166 | testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2167 | testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
| 2168 | testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
| 2169 | testcase( pCol->affinity==SQLITE_AFF_REAL ); |
drh | 00d6b27 | 2022-12-15 20:03:08 +0000 | [diff] [blame] | 2170 | testcase( pCol->affinity==SQLITE_AFF_FLEXNUM ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2171 | |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2172 | zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2173 | len = sqlite3Strlen30(zType); |
drh | 00d6b27 | 2022-12-15 20:03:08 +0000 | [diff] [blame] | 2174 | assert( pCol->affinity==SQLITE_AFF_BLOB |
| 2175 | || pCol->affinity==SQLITE_AFF_FLEXNUM |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2176 | || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2177 | memcpy(&zStmt[k], zType, len); |
| 2178 | k += len; |
| 2179 | assert( k<=n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2180 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2181 | sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2182 | return zStmt; |
| 2183 | } |
| 2184 | |
| 2185 | /* |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2186 | ** Resize an Index object to hold N columns total. Return SQLITE_OK |
| 2187 | ** on success and SQLITE_NOMEM on an OOM error. |
| 2188 | */ |
| 2189 | static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ |
| 2190 | char *zExtra; |
| 2191 | int nByte; |
| 2192 | if( pIdx->nColumn>=N ) return SQLITE_OK; |
| 2193 | assert( pIdx->isResized==0 ); |
dan | b5a6923 | 2020-09-15 20:48:30 +0000 | [diff] [blame] | 2194 | nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2195 | zExtra = sqlite3DbMallocZero(db, nByte); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 2196 | if( zExtra==0 ) return SQLITE_NOMEM_BKPT; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2197 | memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 2198 | pIdx->azColl = (const char**)zExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2199 | zExtra += sizeof(char*)*N; |
dan | b5a6923 | 2020-09-15 20:48:30 +0000 | [diff] [blame] | 2200 | memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1)); |
| 2201 | pIdx->aiRowLogEst = (LogEst*)zExtra; |
| 2202 | zExtra += sizeof(LogEst)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2203 | memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
| 2204 | pIdx->aiColumn = (i16*)zExtra; |
| 2205 | zExtra += sizeof(i16)*N; |
| 2206 | memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
| 2207 | pIdx->aSortOrder = (u8*)zExtra; |
| 2208 | pIdx->nColumn = N; |
| 2209 | pIdx->isResized = 1; |
| 2210 | return SQLITE_OK; |
| 2211 | } |
| 2212 | |
| 2213 | /* |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2214 | ** Estimate the total row width for a table. |
| 2215 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2216 | static void estimateTableWidth(Table *pTab){ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2217 | unsigned wTable = 0; |
| 2218 | const Column *pTabCol; |
| 2219 | int i; |
| 2220 | for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ |
| 2221 | wTable += pTabCol->szEst; |
| 2222 | } |
| 2223 | if( pTab->iPKey<0 ) wTable++; |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2224 | pTab->szTabRow = sqlite3LogEst(wTable*4); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | /* |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2228 | ** Estimate the average size of a row for an index. |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2229 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2230 | static void estimateIndexWidth(Index *pIdx){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2231 | unsigned wIndex = 0; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2232 | int i; |
| 2233 | const Column *aCol = pIdx->pTable->aCol; |
| 2234 | for(i=0; i<pIdx->nColumn; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2235 | i16 x = pIdx->aiColumn[i]; |
| 2236 | assert( x<pIdx->pTable->nCol ); |
| 2237 | wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2238 | } |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2239 | pIdx->szIdxRow = sqlite3LogEst(wIndex*4); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2242 | /* Return true if column number x is any of the first nCol entries of aiCol[]. |
| 2243 | ** This is used to determine if the column number x appears in any of the |
| 2244 | ** first nCol entries of an index. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2245 | */ |
| 2246 | static int hasColumn(const i16 *aiCol, int nCol, int x){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2247 | while( nCol-- > 0 ){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2248 | if( x==*(aiCol++) ){ |
| 2249 | return 1; |
| 2250 | } |
| 2251 | } |
| 2252 | return 0; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2256 | ** Return true if any of the first nKey entries of index pIdx exactly |
| 2257 | ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID |
| 2258 | ** PRIMARY KEY index. pIdx is an index on the same table. pIdx may |
| 2259 | ** or may not be the same index as pPk. |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2260 | ** |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2261 | ** The first nKey entries of pIdx are guaranteed to be ordinary columns, |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2262 | ** not a rowid or expression. |
| 2263 | ** |
| 2264 | ** This routine differs from hasColumn() in that both the column and the |
| 2265 | ** collating sequence must match for this routine, but for hasColumn() only |
| 2266 | ** the column name must match. |
| 2267 | */ |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2268 | static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2269 | int i, j; |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2270 | assert( nKey<=pIdx->nColumn ); |
| 2271 | assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) ); |
| 2272 | assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); |
| 2273 | assert( pPk->pTable->tabFlags & TF_WithoutRowid ); |
| 2274 | assert( pPk->pTable==pIdx->pTable ); |
| 2275 | testcase( pPk==pIdx ); |
| 2276 | j = pPk->aiColumn[iCol]; |
| 2277 | assert( j!=XN_ROWID && j!=XN_EXPR ); |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2278 | for(i=0; i<nKey; i++){ |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2279 | assert( pIdx->aiColumn[i]>=0 || j>=0 ); |
| 2280 | if( pIdx->aiColumn[i]==j |
| 2281 | && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2282 | ){ |
| 2283 | return 1; |
| 2284 | } |
| 2285 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2286 | return 0; |
| 2287 | } |
| 2288 | |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2289 | /* Recompute the colNotIdxed field of the Index. |
| 2290 | ** |
| 2291 | ** colNotIdxed is a bitmask that has a 0 bit representing each indexed |
drh | 5723c65 | 2022-10-22 13:49:35 +0000 | [diff] [blame] | 2292 | ** columns that are within the first 63 columns of the table and a 1 for |
| 2293 | ** all other bits (all columns that are not in the index). The |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2294 | ** high-order bit of colNotIdxed is always 1. All unindexed columns |
| 2295 | ** of the table have a 1. |
| 2296 | ** |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2297 | ** 2019-10-24: For the purpose of this computation, virtual columns are |
| 2298 | ** not considered to be covered by the index, even if they are in the |
| 2299 | ** index, because we do not trust the logic in whereIndexExprTrans() to be |
| 2300 | ** able to find all instances of a reference to the indexed table column |
| 2301 | ** and convert them into references to the index. Hence we always want |
| 2302 | ** the actual table at hand in order to recompute the virtual column, if |
| 2303 | ** necessary. |
| 2304 | ** |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2305 | ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask |
| 2306 | ** to determine if the index is covering index. |
| 2307 | */ |
| 2308 | static void recomputeColumnsNotIndexed(Index *pIdx){ |
| 2309 | Bitmask m = 0; |
| 2310 | int j; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2311 | Table *pTab = pIdx->pTable; |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2312 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 2313 | int x = pIdx->aiColumn[j]; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2314 | if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2315 | testcase( x==BMS-1 ); |
| 2316 | testcase( x==BMS-2 ); |
| 2317 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 2318 | } |
| 2319 | } |
| 2320 | pIdx->colNotIdxed = ~m; |
drh | 5723c65 | 2022-10-22 13:49:35 +0000 | [diff] [blame] | 2321 | assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2324 | /* |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2325 | ** This routine runs at the end of parsing a CREATE TABLE statement that |
| 2326 | ** has a WITHOUT ROWID clause. The job of this routine is to convert both |
| 2327 | ** internal schema data structures and the generated VDBE code so that they |
| 2328 | ** are appropriate for a WITHOUT ROWID table instead of a rowid table. |
| 2329 | ** Changes include: |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2330 | ** |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2331 | ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 2332 | ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY |
| 2333 | ** into BTREE_BLOBKEY. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2334 | ** (3) Bypass the creation of the sqlite_schema table entry |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2335 | ** for the PRIMARY KEY as the primary key index is now |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2336 | ** identified by the sqlite_schema table entry of the table itself. |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2337 | ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2338 | ** schema to the rootpage from the main table. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2339 | ** (5) Add all table columns to the PRIMARY KEY Index object |
| 2340 | ** so that the PRIMARY KEY is a covering index. The surplus |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2341 | ** columns are part of KeyInfo.nAllField and are not used for |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2342 | ** sorting or lookup or uniqueness checks. |
| 2343 | ** (6) Replace the rowid tail on all automatically generated UNIQUE |
| 2344 | ** indices with the PRIMARY KEY columns. |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2345 | ** |
| 2346 | ** For virtual tables, only (1) is performed. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2347 | */ |
| 2348 | static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ |
| 2349 | Index *pIdx; |
| 2350 | Index *pPk; |
| 2351 | int nPk; |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2352 | int nExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2353 | int i, j; |
| 2354 | sqlite3 *db = pParse->db; |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2355 | Vdbe *v = pParse->pVdbe; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2356 | |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2357 | /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) |
| 2358 | */ |
| 2359 | if( !db->init.imposterTable ){ |
| 2360 | for(i=0; i<pTab->nCol; i++){ |
drh | fd46ec6 | 2021-08-18 22:26:51 +0000 | [diff] [blame] | 2361 | if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 |
| 2362 | && (pTab->aCol[i].notNull==OE_None) |
| 2363 | ){ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2364 | pTab->aCol[i].notNull = OE_Abort; |
| 2365 | } |
| 2366 | } |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 2367 | pTab->tabFlags |= TF_HasNotNull; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 2370 | /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY |
| 2371 | ** into BTREE_BLOBKEY. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2372 | */ |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 2373 | assert( !pParse->bReturning ); |
| 2374 | if( pParse->u1.addrCrTab ){ |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2375 | assert( v ); |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 2376 | sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY); |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2379 | /* Locate the PRIMARY KEY index. Or, if this table was originally |
| 2380 | ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
| 2381 | */ |
| 2382 | if( pTab->iPKey>=0 ){ |
| 2383 | ExprList *pList; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 2384 | Token ipkToken; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2385 | sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 2386 | pList = sqlite3ExprListAppend(pParse, 0, |
| 2387 | sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 2388 | if( pList==0 ){ |
| 2389 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2390 | return; |
| 2391 | } |
dan | f9b0c45 | 2019-05-06 16:15:28 +0000 | [diff] [blame] | 2392 | if( IN_RENAME_OBJECT ){ |
| 2393 | sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); |
| 2394 | } |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 2395 | pList->a[0].fg.sortFlags = pParse->iPkSortOrder; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2396 | assert( pParse->pNewTable==pTab ); |
dan | f9b0c45 | 2019-05-06 16:15:28 +0000 | [diff] [blame] | 2397 | pTab->iPKey = -1; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2398 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 2399 | SQLITE_IDXTYPE_PRIMARYKEY); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 2400 | if( pParse->nErr ){ |
drh | 3bb9d75 | 2021-04-13 13:48:31 +0000 | [diff] [blame] | 2401 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2402 | return; |
| 2403 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 2404 | assert( db->mallocFailed==0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2405 | pPk = sqlite3PrimaryKeyIndex(pTab); |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2406 | assert( pPk->nKeyCol==1 ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 2407 | }else{ |
| 2408 | pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | f0c48b1 | 2019-02-11 01:58:34 +0000 | [diff] [blame] | 2409 | assert( pPk!=0 ); |
dan | c5b7358 | 2015-05-26 11:53:14 +0000 | [diff] [blame] | 2410 | |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2411 | /* |
| 2412 | ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 2413 | ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 2414 | ** code assumes the PRIMARY KEY contains no repeated columns. |
| 2415 | */ |
| 2416 | for(i=j=1; i<pPk->nKeyCol; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2417 | if( isDupColumn(pPk, j, pPk, i) ){ |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2418 | pPk->nColumn--; |
| 2419 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2420 | testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2421 | pPk->azColl[j] = pPk->azColl[i]; |
| 2422 | pPk->aSortOrder[j] = pPk->aSortOrder[i]; |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2423 | pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 2424 | } |
| 2425 | } |
| 2426 | pPk->nKeyCol = j; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2427 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2428 | assert( pPk!=0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2429 | pPk->isCovering = 1; |
| 2430 | if( !db->init.imposterTable ) pPk->uniqNotNull = 1; |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2431 | nPk = pPk->nColumn = pPk->nKeyCol; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2432 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2433 | /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema |
drh | df94966 | 2017-07-30 18:40:52 +0000 | [diff] [blame] | 2434 | ** table entry. This is only required if currently generating VDBE |
| 2435 | ** code for a CREATE TABLE (not when parsing one as part of reading |
| 2436 | ** a database schema). */ |
| 2437 | if( v && pPk->tnum>0 ){ |
| 2438 | assert( db->init.busy==0 ); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2439 | sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto); |
drh | df94966 | 2017-07-30 18:40:52 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2442 | /* The root page of the PRIMARY KEY is the table root page */ |
| 2443 | pPk->tnum = pTab->tnum; |
| 2444 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2445 | /* Update the in-memory representation of all UNIQUE indices by converting |
| 2446 | ** the final rowid column into one or more columns of the PRIMARY KEY. |
| 2447 | */ |
| 2448 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2449 | int n; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2450 | if( IsPrimaryKeyIndex(pIdx) ) continue; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2451 | for(i=n=0; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2452 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2453 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
| 2454 | n++; |
| 2455 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2456 | } |
drh | 5a9a37b | 2013-11-05 17:30:04 +0000 | [diff] [blame] | 2457 | if( n==0 ){ |
| 2458 | /* This index is a superset of the primary key */ |
| 2459 | pIdx->nColumn = pIdx->nKeyCol; |
| 2460 | continue; |
| 2461 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2462 | if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; |
| 2463 | for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2464 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2465 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2466 | pIdx->aiColumn[j] = pPk->aiColumn[i]; |
| 2467 | pIdx->azColl[j] = pPk->azColl[i]; |
drh | bf9ff25 | 2019-05-14 00:43:13 +0000 | [diff] [blame] | 2468 | if( pPk->aSortOrder[i] ){ |
| 2469 | /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */ |
| 2470 | pIdx->bAscKeyBug = 1; |
| 2471 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2472 | j++; |
| 2473 | } |
| 2474 | } |
drh | 00012df | 2013-11-05 01:59:07 +0000 | [diff] [blame] | 2475 | assert( pIdx->nColumn>=pIdx->nKeyCol+n ); |
| 2476 | assert( pIdx->nColumn>=j ); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /* Add all table columns to the PRIMARY KEY index |
| 2480 | */ |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2481 | nExtra = 0; |
| 2482 | for(i=0; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2483 | if( !hasColumn(pPk->aiColumn, nPk, i) |
| 2484 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2485 | } |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2486 | if( resizeIndexObject(db, pPk, nPk+nExtra) ) return; |
| 2487 | for(i=0, j=nPk; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2488 | if( !hasColumn(pPk->aiColumn, j, i) |
| 2489 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 |
| 2490 | ){ |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2491 | assert( j<pPk->nColumn ); |
| 2492 | pPk->aiColumn[j] = i; |
| 2493 | pPk->azColl[j] = sqlite3StrBINARY; |
| 2494 | j++; |
| 2495 | } |
| 2496 | } |
| 2497 | assert( pPk->nColumn==j ); |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2498 | assert( pTab->nNVCol<=j ); |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2499 | recomputeColumnsNotIndexed(pPk); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2502 | |
| 2503 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2504 | /* |
| 2505 | ** Return true if pTab is a virtual table and zName is a shadow table name |
| 2506 | ** for that virtual table. |
| 2507 | */ |
| 2508 | int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ |
| 2509 | int nName; /* Length of zName */ |
| 2510 | Module *pMod; /* Module for the virtual table */ |
| 2511 | |
| 2512 | if( !IsVirtual(pTab) ) return 0; |
| 2513 | nName = sqlite3Strlen30(pTab->zName); |
| 2514 | if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; |
| 2515 | if( zName[nName]!='_' ) return 0; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2516 | pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2517 | if( pMod==0 ) return 0; |
| 2518 | if( pMod->pModule->iVersion<3 ) return 0; |
| 2519 | if( pMod->pModule->xShadowName==0 ) return 0; |
| 2520 | return pMod->pModule->xShadowName(zName+nName+1); |
| 2521 | } |
| 2522 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 2523 | |
dan | f6e015f | 2018-11-28 08:02:28 +0000 | [diff] [blame] | 2524 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2525 | /* |
drh | ddfec00 | 2021-11-04 00:51:53 +0000 | [diff] [blame] | 2526 | ** Table pTab is a virtual table. If it the virtual table implementation |
| 2527 | ** exists and has an xShadowName method, then loop over all other ordinary |
| 2528 | ** tables within the same schema looking for shadow tables of pTab, and mark |
| 2529 | ** any shadow tables seen using the TF_Shadow flag. |
| 2530 | */ |
| 2531 | void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){ |
| 2532 | int nName; /* Length of pTab->zName */ |
| 2533 | Module *pMod; /* Module for the virtual table */ |
| 2534 | HashElem *k; /* For looping through the symbol table */ |
| 2535 | |
| 2536 | assert( IsVirtual(pTab) ); |
| 2537 | pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); |
| 2538 | if( pMod==0 ) return; |
| 2539 | if( NEVER(pMod->pModule==0) ) return; |
drh | 62561b8 | 2021-11-06 10:59:27 +0000 | [diff] [blame] | 2540 | if( pMod->pModule->iVersion<3 ) return; |
drh | ddfec00 | 2021-11-04 00:51:53 +0000 | [diff] [blame] | 2541 | if( pMod->pModule->xShadowName==0 ) return; |
| 2542 | assert( pTab->zName!=0 ); |
| 2543 | nName = sqlite3Strlen30(pTab->zName); |
| 2544 | for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 2545 | Table *pOther = sqliteHashData(k); |
| 2546 | assert( pOther->zName!=0 ); |
| 2547 | if( !IsOrdinaryTable(pOther) ) continue; |
| 2548 | if( pOther->tabFlags & TF_Shadow ) continue; |
| 2549 | if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0 |
| 2550 | && pOther->zName[nName]=='_' |
| 2551 | && pMod->pModule->xShadowName(pOther->zName+nName+1) |
| 2552 | ){ |
| 2553 | pOther->tabFlags |= TF_Shadow; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 2558 | |
| 2559 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2560 | /* |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2561 | ** Return true if zName is a shadow table name in the current database |
| 2562 | ** connection. |
| 2563 | ** |
| 2564 | ** zName is temporarily modified while this routine is running, but is |
| 2565 | ** restored to its original value prior to this routine returning. |
| 2566 | */ |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 2567 | int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2568 | char *zTail; /* Pointer to the last "_" in zName */ |
| 2569 | Table *pTab; /* Table that zName is a shadow of */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2570 | zTail = strrchr(zName, '_'); |
| 2571 | if( zTail==0 ) return 0; |
| 2572 | *zTail = 0; |
| 2573 | pTab = sqlite3FindTable(db, zName, 0); |
| 2574 | *zTail = '_'; |
| 2575 | if( pTab==0 ) return 0; |
| 2576 | if( !IsVirtual(pTab) ) return 0; |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2577 | return sqlite3IsShadowTableOf(db, pTab, zName); |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2578 | } |
dan | f6e015f | 2018-11-28 08:02:28 +0000 | [diff] [blame] | 2579 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2580 | |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2581 | |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2582 | #ifdef SQLITE_DEBUG |
| 2583 | /* |
| 2584 | ** Mark all nodes of an expression as EP_Immutable, indicating that |
| 2585 | ** they should not be changed. Expressions attached to a table or |
| 2586 | ** index definition are tagged this way to help ensure that we do |
| 2587 | ** not pass them into code generator routines by mistake. |
| 2588 | */ |
| 2589 | static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){ |
drh | 3547e49 | 2022-12-23 14:49:24 +0000 | [diff] [blame] | 2590 | (void)pWalker; |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2591 | ExprSetVVAProperty(pExpr, EP_Immutable); |
| 2592 | return WRC_Continue; |
| 2593 | } |
| 2594 | static void markExprListImmutable(ExprList *pList){ |
| 2595 | if( pList ){ |
| 2596 | Walker w; |
| 2597 | memset(&w, 0, sizeof(w)); |
| 2598 | w.xExprCallback = markImmutableExprStep; |
| 2599 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 2600 | w.xSelectCallback2 = 0; |
| 2601 | sqlite3WalkExprList(&w, pList); |
| 2602 | } |
| 2603 | } |
| 2604 | #else |
| 2605 | #define markExprListImmutable(X) /* no-op */ |
| 2606 | #endif /* SQLITE_DEBUG */ |
| 2607 | |
| 2608 | |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2609 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2610 | ** This routine is called to report the final ")" that terminates |
| 2611 | ** a CREATE TABLE statement. |
| 2612 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2613 | ** The table structure that other action routines have been building |
| 2614 | ** is added to the internal hash tables, assuming no errors have |
| 2615 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2616 | ** |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2617 | ** An entry for the table is made in the schema table on disk, unless |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2618 | ** this is a temporary table or db->init.busy==1. When db->init.busy==1 |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2619 | ** it means we are reading the sqlite_schema table because we just |
| 2620 | ** connected to the database or because the sqlite_schema table has |
drh | ddba9e5 | 2005-03-19 01:41:21 +0000 | [diff] [blame] | 2621 | ** recently changed, so the entry for this table already exists in |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2622 | ** the sqlite_schema table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2623 | ** |
| 2624 | ** If the pSelect argument is not NULL, it means that this routine |
| 2625 | ** was called to create a table generated from a |
| 2626 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 2627 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2628 | */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2629 | void sqlite3EndTable( |
| 2630 | Parse *pParse, /* Parse context */ |
| 2631 | Token *pCons, /* The ',' token after the last column defn. */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2632 | Token *pEnd, /* The ')' before options in the CREATE TABLE */ |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2633 | u32 tabOpts, /* Extra table options. Usually 0. */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2634 | Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
| 2635 | ){ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2636 | Table *p; /* The new table */ |
| 2637 | sqlite3 *db = pParse->db; /* The database connection */ |
| 2638 | int iDb; /* Database in which the table lives */ |
| 2639 | Index *pIdx; /* An implied index of the table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2640 | |
drh | 027616d | 2015-08-08 22:47:47 +0000 | [diff] [blame] | 2641 | if( pEnd==0 && pSelect==0 ){ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2642 | return; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2643 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 2644 | p = pParse->pNewTable; |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2645 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2646 | |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 2647 | if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2648 | p->tabFlags |= TF_Shadow; |
| 2649 | } |
| 2650 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2651 | /* If the db->init.busy is 1 it means we are reading the SQL off the |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2652 | ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2653 | ** So do not write to the disk again. Extract the root page number |
| 2654 | ** for the table from the db->init.newTnum field. (The page number |
| 2655 | ** should have been put there by the sqliteOpenCb routine.) |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2656 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2657 | ** If the root page number is 1, that means this is the sqlite_schema |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2658 | ** table itself. So mark it read-only. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2659 | */ |
| 2660 | if( db->init.busy ){ |
drh | 54e3f94 | 2021-10-11 15:54:05 +0000 | [diff] [blame] | 2661 | if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){ |
drh | 1e9c47b | 2018-03-16 20:15:58 +0000 | [diff] [blame] | 2662 | sqlite3ErrorMsg(pParse, ""); |
| 2663 | return; |
| 2664 | } |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2665 | p->tnum = db->init.newTnum; |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2666 | if( p->tnum==1 ) p->tabFlags |= TF_Readonly; |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2669 | /* Special processing for tables that include the STRICT keyword: |
| 2670 | ** |
| 2671 | ** * Do not allow custom column datatypes. Every column must have |
| 2672 | ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB. |
| 2673 | ** |
| 2674 | ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY, |
| 2675 | ** then all columns of the PRIMARY KEY must have a NOT NULL |
| 2676 | ** constraint. |
| 2677 | */ |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2678 | if( tabOpts & TF_Strict ){ |
| 2679 | int ii; |
| 2680 | p->tabFlags |= TF_Strict; |
| 2681 | for(ii=0; ii<p->nCol; ii++){ |
drh | ab16578 | 2021-08-19 00:24:43 +0000 | [diff] [blame] | 2682 | Column *pCol = &p->aCol[ii]; |
drh | b9fd010 | 2021-08-23 10:28:02 +0000 | [diff] [blame] | 2683 | if( pCol->eCType==COLTYPE_CUSTOM ){ |
| 2684 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 2685 | sqlite3ErrorMsg(pParse, |
| 2686 | "unknown datatype for %s.%s: \"%s\"", |
| 2687 | p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "") |
| 2688 | ); |
| 2689 | }else{ |
| 2690 | sqlite3ErrorMsg(pParse, "missing datatype for %s.%s", |
| 2691 | p->zName, pCol->zCnName); |
| 2692 | } |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2693 | return; |
drh | b9fd010 | 2021-08-23 10:28:02 +0000 | [diff] [blame] | 2694 | }else if( pCol->eCType==COLTYPE_ANY ){ |
| 2695 | pCol->affinity = SQLITE_AFF_BLOB; |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2696 | } |
drh | ab16578 | 2021-08-19 00:24:43 +0000 | [diff] [blame] | 2697 | if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0 |
| 2698 | && p->iPKey!=ii |
| 2699 | && pCol->notNull == OE_None |
| 2700 | ){ |
drh | ab16578 | 2021-08-19 00:24:43 +0000 | [diff] [blame] | 2701 | pCol->notNull = OE_Abort; |
| 2702 | p->tabFlags |= TF_HasNotNull; |
| 2703 | } |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
drh | 3cbd2b7 | 2019-02-19 13:51:58 +0000 | [diff] [blame] | 2707 | assert( (p->tabFlags & TF_HasPrimaryKey)==0 |
| 2708 | || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 ); |
| 2709 | assert( (p->tabFlags & TF_HasPrimaryKey)!=0 |
| 2710 | || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) ); |
| 2711 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2712 | /* Special processing for WITHOUT ROWID Tables */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2713 | if( tabOpts & TF_WithoutRowid ){ |
drh | d2fe335 | 2013-11-09 18:15:35 +0000 | [diff] [blame] | 2714 | if( (p->tabFlags & TF_Autoincrement) ){ |
| 2715 | sqlite3ErrorMsg(pParse, |
| 2716 | "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
| 2717 | return; |
| 2718 | } |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2719 | if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
drh | d2fe335 | 2013-11-09 18:15:35 +0000 | [diff] [blame] | 2720 | sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2721 | return; |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 2722 | } |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2723 | p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 2724 | convertToWithoutRowidTable(pParse, p); |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 2725 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2726 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2727 | |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2728 | #ifndef SQLITE_OMIT_CHECK |
| 2729 | /* Resolve names in all CHECK constraint expressions. |
| 2730 | */ |
| 2731 | if( p->pCheck ){ |
drh | 3780be1 | 2013-07-31 19:05:22 +0000 | [diff] [blame] | 2732 | sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 +0000 | [diff] [blame] | 2733 | if( pParse->nErr ){ |
| 2734 | /* If errors are seen, delete the CHECK constraints now, else they might |
| 2735 | ** actually be used if PRAGMA writable_schema=ON is set. */ |
| 2736 | sqlite3ExprListDelete(db, p->pCheck); |
| 2737 | p->pCheck = 0; |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2738 | }else{ |
| 2739 | markExprListImmutable(p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 +0000 | [diff] [blame] | 2740 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2741 | } |
| 2742 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2743 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2744 | if( p->tabFlags & TF_HasGenerated ){ |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2745 | int ii, nNG = 0; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2746 | testcase( p->tabFlags & TF_HasVirtual ); |
| 2747 | testcase( p->tabFlags & TF_HasStored ); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2748 | for(ii=0; ii<p->nCol; ii++){ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 +0000 | [diff] [blame] | 2749 | u32 colFlags = p->aCol[ii].colFlags; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2750 | if( (colFlags & COLFLAG_GENERATED)!=0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2751 | Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]); |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2752 | testcase( colFlags & COLFLAG_VIRTUAL ); |
| 2753 | testcase( colFlags & COLFLAG_STORED ); |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2754 | if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){ |
| 2755 | /* If there are errors in resolving the expression, change the |
| 2756 | ** expression to a NULL. This prevents code generators that operate |
| 2757 | ** on the expression from inserting extra parts into the expression |
| 2758 | ** tree that have been allocated from lookaside memory, which is |
drh | 2d58b7f | 2020-01-17 23:27:41 +0000 | [diff] [blame] | 2759 | ** illegal in a schema and will lead to errors or heap corruption |
| 2760 | ** when the database connection closes. */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2761 | sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii], |
| 2762 | sqlite3ExprAlloc(db, TK_NULL, 0, 0)); |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2763 | } |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2764 | }else{ |
| 2765 | nNG++; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2766 | } |
drh | 2c40a3e | 2019-10-29 01:26:24 +0000 | [diff] [blame] | 2767 | } |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2768 | if( nNG==0 ){ |
| 2769 | sqlite3ErrorMsg(pParse, "must have at least one non-generated column"); |
drh | 2c40a3e | 2019-10-29 01:26:24 +0000 | [diff] [blame] | 2770 | return; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2774 | |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2775 | /* Estimate the average row size for the table and for all implied indices */ |
| 2776 | estimateTableWidth(p); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2777 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2778 | estimateIndexWidth(pIdx); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2781 | /* If not initializing, then create a record for the new table |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2782 | ** in the schema table of the database. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2783 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2784 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 2785 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2786 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2787 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2788 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2789 | Vdbe *v; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2790 | char *zType; /* "view" or "table" */ |
| 2791 | char *zType2; /* "VIEW" or "TABLE" */ |
| 2792 | char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2793 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2794 | v = sqlite3GetVdbe(pParse); |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2795 | if( NEVER(v==0) ) return; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2796 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2797 | sqlite3VdbeAddOp1(v, OP_Close, 0); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 2798 | |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2799 | /* |
| 2800 | ** Initialize zType for the new view or table. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2801 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2802 | if( IsOrdinaryTable(p) ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2803 | /* A regular table */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2804 | zType = "table"; |
| 2805 | zType2 = "TABLE"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2806 | #ifndef SQLITE_OMIT_VIEW |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2807 | }else{ |
| 2808 | /* A view */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2809 | zType = "view"; |
| 2810 | zType2 = "VIEW"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2811 | #endif |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2812 | } |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2813 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2814 | /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
| 2815 | ** statement to populate the new table. The root-page number for the |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2816 | ** new table is in register pParse->regRoot. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2817 | ** |
| 2818 | ** Once the SELECT has been coded by sqlite3Select(), it is in a |
| 2819 | ** suitable state to query for the column names and types to be used |
| 2820 | ** by the new table. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 2821 | ** |
| 2822 | ** A shared-cache write-lock is not required to write to the new table, |
| 2823 | ** as a schema-lock must have already been obtained to create it. Since |
| 2824 | ** a schema-lock excludes all other database users, the write-lock would |
| 2825 | ** be redundant. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2826 | */ |
| 2827 | if( pSelect ){ |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2828 | SelectDest dest; /* Where the SELECT should store results */ |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2829 | int regYield; /* Register holding co-routine entry-point */ |
| 2830 | int addrTop; /* Top of the co-routine */ |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2831 | int regRec; /* A record to be insert into the new table */ |
| 2832 | int regRowid; /* Rowid of the next row to insert */ |
| 2833 | int addrInsLoop; /* Top of the loop for inserting rows */ |
| 2834 | Table *pSelTab; /* A table that describes the SELECT results */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2835 | |
drh | fde3043 | 2022-03-10 21:04:49 +0000 | [diff] [blame] | 2836 | if( IN_SPECIAL_PARSE ){ |
| 2837 | pParse->rc = SQLITE_ERROR; |
| 2838 | pParse->nErr++; |
| 2839 | return; |
| 2840 | } |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2841 | regYield = ++pParse->nMem; |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2842 | regRec = ++pParse->nMem; |
| 2843 | regRowid = ++pParse->nMem; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 2844 | assert(pParse->nTab==1); |
drh | 0dd5cda | 2015-06-16 16:39:01 +0000 | [diff] [blame] | 2845 | sqlite3MayAbort(pParse); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2846 | sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 2847 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2848 | pParse->nTab = 2; |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2849 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 2850 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2851 | if( pParse->nErr ) return; |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 2852 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2853 | if( pSelTab==0 ) return; |
| 2854 | assert( p->aCol==0 ); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 2855 | p->nCol = p->nNVCol = pSelTab->nCol; |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2856 | p->aCol = pSelTab->aCol; |
| 2857 | pSelTab->nCol = 0; |
| 2858 | pSelTab->aCol = 0; |
| 2859 | sqlite3DeleteTable(db, pSelTab); |
drh | 755b0fd | 2017-12-23 12:33:40 +0000 | [diff] [blame] | 2860 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 2861 | sqlite3Select(pParse, pSelect, &dest); |
drh | 5060a67 | 2017-12-25 13:43:54 +0000 | [diff] [blame] | 2862 | if( pParse->nErr ) return; |
drh | 755b0fd | 2017-12-23 12:33:40 +0000 | [diff] [blame] | 2863 | sqlite3VdbeEndCoroutine(v, regYield); |
| 2864 | sqlite3VdbeJumpHere(v, addrTop - 1); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2865 | addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
| 2866 | VdbeCoverage(v); |
| 2867 | sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
| 2868 | sqlite3TableAffinity(v, p, 0); |
| 2869 | sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); |
| 2870 | sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2871 | sqlite3VdbeGoto(v, addrInsLoop); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2872 | sqlite3VdbeJumpHere(v, addrInsLoop); |
| 2873 | sqlite3VdbeAddOp1(v, OP_Close, 1); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2874 | } |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2875 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2876 | /* Compute the complete text of the CREATE statement */ |
| 2877 | if( pSelect ){ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 2878 | zStmt = createTableStmt(db, p); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2879 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 2880 | Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; |
| 2881 | n = (int)(pEnd2->z - pParse->sNameToken.z); |
| 2882 | if( pEnd2->z[0]!=';' ) n += pEnd2->n; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2883 | zStmt = sqlite3MPrintf(db, |
| 2884 | "CREATE %s %.*s", zType2, n, pParse->sNameToken.z |
| 2885 | ); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | /* A slot for the record has already been allocated in the |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2889 | ** schema table. We just need to update that slot with all |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2890 | ** the information we've collected. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2891 | */ |
| 2892 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 2893 | "UPDATE %Q." LEGACY_SCHEMA_TABLE |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2894 | " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q" |
| 2895 | " WHERE rowid=#%d", |
| 2896 | db->aDb[iDb].zDbSName, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2897 | zType, |
| 2898 | p->zName, |
| 2899 | p->zName, |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2900 | pParse->regRoot, |
| 2901 | zStmt, |
| 2902 | pParse->regRowid |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2903 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2904 | sqlite3DbFree(db, zStmt); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2905 | sqlite3ChangeCookie(pParse, iDb); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2906 | |
| 2907 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2908 | /* Check to see if we need to create an sqlite_sequence table for |
| 2909 | ** keeping track of autoincrement keys. |
| 2910 | */ |
dan | 755ed41 | 2021-04-07 12:02:30 +0000 | [diff] [blame] | 2911 | if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2912 | Db *pDb = &db->aDb[iDb]; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2913 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2914 | if( pDb->pSchema->pSeqTab==0 ){ |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2915 | sqlite3NestedParse(pParse, |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 2916 | "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 2917 | pDb->zDbSName |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2918 | ); |
| 2919 | } |
| 2920 | } |
| 2921 | #endif |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2922 | |
| 2923 | /* Reparse everything to update our internal data structures */ |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 2924 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 2925 | sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2926 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* Add the table to the in-memory representation of the database. |
| 2929 | */ |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 2930 | if( db->init.busy ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2931 | Table *pOld; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2932 | Schema *pSchema = p->pSchema; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2933 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 2934 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 2935 | pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2936 | if( pOld ){ |
| 2937 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2938 | sqlite3OomFault(db); |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2939 | return; |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2940 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2941 | pParse->pNewTable = 0; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2942 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | d4b6469 | 2021-04-06 16:16:15 +0000 | [diff] [blame] | 2943 | |
| 2944 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 2945 | ** then record a pointer to this table in the main database structure |
| 2946 | ** so that INSERT can find the table easily. */ |
| 2947 | assert( !pParse->nested ); |
| 2948 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2949 | if( strcmp(p->zName, "sqlite_sequence")==0 ){ |
| 2950 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2951 | p->pSchema->pSeqTab = p; |
| 2952 | } |
| 2953 | #endif |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2954 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2955 | |
| 2956 | #ifndef SQLITE_OMIT_ALTERTABLE |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2957 | if( !pSelect && IsOrdinaryTable(p) ){ |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2958 | assert( pCons && pEnd ); |
| 2959 | if( pCons->z==0 ){ |
| 2960 | pCons = pEnd; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2961 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2962 | p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2963 | } |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2964 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2967 | #ifndef SQLITE_OMIT_VIEW |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2968 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2969 | ** The parser calls this routine in order to create a new VIEW |
| 2970 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2971 | void sqlite3CreateView( |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2972 | Parse *pParse, /* The parsing context */ |
| 2973 | Token *pBegin, /* The CREATE token that begins the statement */ |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 2974 | Token *pName1, /* The token that holds the name of the view */ |
| 2975 | Token *pName2, /* The token that holds the name of the view */ |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2976 | ExprList *pCNames, /* Optional list of view column names */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 2977 | Select *pSelect, /* A SELECT statement that will become the new view */ |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 2978 | int isTemp, /* TRUE for a TEMPORARY view */ |
| 2979 | int noErr /* Suppress error messages if VIEW already exists */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2980 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2981 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2982 | int n; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 2983 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2984 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 2985 | DbFixer sFix; |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 2986 | Token *pName = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2987 | int iDb; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2988 | sqlite3 *db = pParse->db; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2989 | |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 2990 | if( pParse->nVar>0 ){ |
| 2991 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
drh | 32498f1 | 2015-09-26 11:15:44 +0000 | [diff] [blame] | 2992 | goto create_view_fail; |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 2993 | } |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 2994 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2995 | p = pParse->pNewTable; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2996 | if( p==0 || pParse->nErr ) goto create_view_fail; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 2997 | |
| 2998 | /* Legacy versions of SQLite allowed the use of the magic "rowid" column |
| 2999 | ** on a view, even though views do not have rowids. The following flag |
| 3000 | ** setting fixes this problem. But the fix can be disabled by compiling |
| 3001 | ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that |
| 3002 | ** depend upon the old buggy behavior. */ |
| 3003 | #ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
drh | a6c54de | 2021-04-06 19:13:44 +0000 | [diff] [blame] | 3004 | p->tabFlags |= TF_NoVisibleRowid; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 3005 | #endif |
| 3006 | |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3007 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3008 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
drh | d100f69 | 2013-10-03 15:39:44 +0000 | [diff] [blame] | 3009 | sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3010 | if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 3011 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 3012 | /* Make a copy of the entire SELECT statement that defines the view. |
| 3013 | ** This will force all the Expr.token.z values to be dynamically |
| 3014 | ** allocated rather than point to the input string - which means that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 3015 | ** they will persist after the current sqlite3_exec() call returns. |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 3016 | */ |
dan | 3809696 | 2019-12-09 08:13:43 +0000 | [diff] [blame] | 3017 | pSelect->selFlags |= SF_View; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3018 | if( IN_RENAME_OBJECT ){ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3019 | p->u.view.pSelect = pSelect; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 3020 | pSelect = 0; |
| 3021 | }else{ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3022 | p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 3023 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3024 | p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3025 | p->eTabType = TABTYP_VIEW; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3026 | if( db->mallocFailed ) goto create_view_fail; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 3027 | |
| 3028 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 3029 | ** the end. |
| 3030 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3031 | sEnd = pParse->sLastToken; |
drh | 6116ee4 | 2018-01-10 00:40:06 +0000 | [diff] [blame] | 3032 | assert( sEnd.z[0]!=0 || sEnd.n==0 ); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3033 | if( sEnd.z[0]!=';' ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3034 | sEnd.z += sEnd.n; |
| 3035 | } |
| 3036 | sEnd.n = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3037 | n = (int)(sEnd.z - pBegin->z); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3038 | assert( n>0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3039 | z = pBegin->z; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3040 | while( sqlite3Isspace(z[n-1]) ){ n--; } |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3041 | sEnd.z = &z[n-1]; |
| 3042 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 3043 | |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3044 | /* Use sqlite3EndTable() to add the view to the schema table */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 3045 | sqlite3EndTable(pParse, 0, &sEnd, 0, 0); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3046 | |
| 3047 | create_view_fail: |
| 3048 | sqlite3SelectDelete(db, pSelect); |
dan | e8ab40d | 2018-09-12 08:51:48 +0000 | [diff] [blame] | 3049 | if( IN_RENAME_OBJECT ){ |
| 3050 | sqlite3RenameExprlistUnmap(pParse, pCNames); |
| 3051 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3052 | sqlite3ExprListDelete(db, pCNames); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3053 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3054 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3055 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3056 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3057 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3058 | /* |
| 3059 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 3060 | ** the columns of the view in the pTable structure. Return the number |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 3061 | ** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3062 | */ |
drh | ef69d2b | 2022-07-25 22:31:04 +0000 | [diff] [blame] | 3063 | static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){ |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 3064 | Table *pSelTab; /* A fake table from which we get the result set */ |
| 3065 | Select *pSel; /* Copy of the SELECT that implements the view */ |
| 3066 | int nErr = 0; /* Number of errors encountered */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3067 | sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
drh | 424981d | 2018-03-28 15:56:55 +0000 | [diff] [blame] | 3068 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 3069 | int rc; |
| 3070 | #endif |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3071 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 32c6a48 | 2014-09-11 13:44:52 +0000 | [diff] [blame] | 3072 | sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 3073 | #endif |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3074 | |
| 3075 | assert( pTable ); |
| 3076 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3077 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ddfec00 | 2021-11-04 00:51:53 +0000 | [diff] [blame] | 3078 | if( IsVirtual(pTable) ){ |
| 3079 | db->nSchemaLock++; |
| 3080 | rc = sqlite3VtabCallConnect(pParse, pTable); |
| 3081 | db->nSchemaLock--; |
| 3082 | return rc; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3083 | } |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3084 | #endif |
| 3085 | |
| 3086 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3087 | /* A positive nCol means the columns names for this view are |
drh | 509a630 | 2022-07-25 23:01:41 +0000 | [diff] [blame] | 3088 | ** already known. This routine is not called unless either the |
| 3089 | ** table is virtual or nCol is zero. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3090 | */ |
drh | 509a630 | 2022-07-25 23:01:41 +0000 | [diff] [blame] | 3091 | assert( pTable->nCol<=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3092 | |
| 3093 | /* A negative nCol is a special marker meaning that we are currently |
| 3094 | ** trying to compute the column names. If we enter this routine with |
| 3095 | ** a negative nCol, it means two or more views form a loop, like this: |
| 3096 | ** |
| 3097 | ** CREATE VIEW one AS SELECT * FROM two; |
| 3098 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 3099 | ** |
drh | 768578e | 2009-05-12 00:40:12 +0000 | [diff] [blame] | 3100 | ** Actually, the error above is now caught prior to reaching this point. |
| 3101 | ** But the following test is still important as it does come up |
| 3102 | ** in the following: |
| 3103 | ** |
| 3104 | ** CREATE TABLE main.ex1(a); |
| 3105 | ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; |
| 3106 | ** SELECT * FROM temp.ex1; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3107 | */ |
| 3108 | if( pTable->nCol<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3109 | sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3110 | return 1; |
| 3111 | } |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 3112 | assert( pTable->nCol>=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3113 | |
| 3114 | /* If we get this far, it means we need to compute the table names. |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 3115 | ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
| 3116 | ** "*" elements in the results set of the view and will assign cursors |
| 3117 | ** to the elements of the FROM clause. But we do not want these changes |
| 3118 | ** to be permanent. So the computation is done on a copy of the SELECT |
| 3119 | ** statement that defines the view. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3120 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3121 | assert( IsView(pTable) ); |
| 3122 | pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3123 | if( pSel ){ |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 3124 | u8 eParseMode = pParse->eParseMode; |
drh | 7f41756 | 2022-04-25 14:49:48 +0000 | [diff] [blame] | 3125 | int nTab = pParse->nTab; |
| 3126 | int nSelect = pParse->nSelect; |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 3127 | pParse->eParseMode = PARSE_MODE_NORMAL; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3128 | sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 3129 | pTable->nCol = -1; |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 3130 | DisableLookaside; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 3131 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3132 | xAuth = db->xAuth; |
| 3133 | db->xAuth = 0; |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3134 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3135 | db->xAuth = xAuth; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 3136 | #else |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3137 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 3138 | #endif |
drh | 7f41756 | 2022-04-25 14:49:48 +0000 | [diff] [blame] | 3139 | pParse->nTab = nTab; |
| 3140 | pParse->nSelect = nSelect; |
dan | 5d59102 | 2019-12-28 08:26:47 +0000 | [diff] [blame] | 3141 | if( pSelTab==0 ){ |
| 3142 | pTable->nCol = 0; |
| 3143 | nErr++; |
| 3144 | }else if( pTable->pCheck ){ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3145 | /* CREATE VIEW name(arglist) AS ... |
| 3146 | ** The names of the columns in the table are taken from |
| 3147 | ** arglist which is stored in pTable->pCheck. The pCheck field |
| 3148 | ** normally holds CHECK constraints on an ordinary table, but for |
| 3149 | ** a VIEW it holds the list of column names. |
| 3150 | */ |
| 3151 | sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
| 3152 | &pTable->nCol, &pTable->aCol); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 3153 | if( pParse->nErr==0 |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3154 | && pTable->nCol==pSel->pEList->nExpr |
| 3155 | ){ |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 3156 | assert( db->mallocFailed==0 ); |
drh | 9e66087 | 2022-12-13 15:54:43 +0000 | [diff] [blame] | 3157 | sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3158 | } |
dan | 5d59102 | 2019-12-28 08:26:47 +0000 | [diff] [blame] | 3159 | }else{ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3160 | /* CREATE VIEW name AS... without an argument list. Construct |
| 3161 | ** the column names from the SELECT statement that defines the view. |
| 3162 | */ |
| 3163 | assert( pTable->aCol==0 ); |
| 3164 | pTable->nCol = pSelTab->nCol; |
| 3165 | pTable->aCol = pSelTab->aCol; |
dan | 3dc864b | 2021-02-25 16:55:47 +0000 | [diff] [blame] | 3166 | pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3167 | pSelTab->nCol = 0; |
| 3168 | pSelTab->aCol = 0; |
| 3169 | assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3170 | } |
drh | 0383661 | 2019-11-02 17:59:10 +0000 | [diff] [blame] | 3171 | pTable->nNVCol = pTable->nCol; |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 3172 | sqlite3DeleteTable(db, pSelTab); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3173 | sqlite3SelectDelete(db, pSel); |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 3174 | EnableLookaside; |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 3175 | pParse->eParseMode = eParseMode; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3176 | } else { |
| 3177 | nErr++; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3178 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3179 | pTable->pSchema->schemaFlags |= DB_UnresetViews; |
dan | 77f3f40 | 2018-07-09 18:55:44 +0000 | [diff] [blame] | 3180 | if( db->mallocFailed ){ |
| 3181 | sqlite3DeleteColumnNames(db, pTable); |
dan | 77f3f40 | 2018-07-09 18:55:44 +0000 | [diff] [blame] | 3182 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3183 | #endif /* SQLITE_OMIT_VIEW */ |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3184 | return nErr; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3185 | } |
drh | ef69d2b | 2022-07-25 22:31:04 +0000 | [diff] [blame] | 3186 | int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 3187 | assert( pTable!=0 ); |
| 3188 | if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0; |
| 3189 | return viewGetColumnNames(pParse, pTable); |
| 3190 | } |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3191 | #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3192 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3193 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3194 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3195 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3196 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3197 | static void sqliteViewResetAll(sqlite3 *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3198 | HashElem *i; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3199 | assert( sqlite3SchemaMutexHeld(db, idx, 0) ); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3200 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3201 | for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3202 | Table *pTab = sqliteHashData(i); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3203 | if( IsView(pTab) ){ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 3204 | sqlite3DeleteColumnNames(db, pTab); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3205 | } |
| 3206 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3207 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3208 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3209 | #else |
| 3210 | # define sqliteViewResetAll(A,B) |
| 3211 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3212 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3213 | /* |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3214 | ** This function is called by the VDBE to adjust the internal schema |
| 3215 | ** used by SQLite when the btree layer moves a table root page. The |
| 3216 | ** root-page of a table or index in database iDb has changed from iFrom |
| 3217 | ** to iTo. |
drh | 6205d4a | 2006-03-24 03:36:26 +0000 | [diff] [blame] | 3218 | ** |
| 3219 | ** Ticket #1728: The symbol table might still contain information |
| 3220 | ** on tables and/or indices that are the process of being deleted. |
| 3221 | ** If you are unlucky, one of those deleted indices or tables might |
| 3222 | ** have the same rootpage number as the real table or index that is |
| 3223 | ** being moved. So we cannot stop searching after the first match |
| 3224 | ** because the first match might be for one of the deleted indices |
| 3225 | ** or tables and not the table/index that is actually being moved. |
| 3226 | ** We must continue looping until all tables and indices with |
| 3227 | ** rootpage==iFrom have been converted to have a rootpage of iTo |
| 3228 | ** in order to be certain that we got the right one. |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3229 | */ |
| 3230 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3231 | void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3232 | HashElem *pElem; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3233 | Hash *pHash; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 3234 | Db *pDb; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3235 | |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 3236 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 3237 | pDb = &db->aDb[iDb]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3238 | pHash = &pDb->pSchema->tblHash; |
| 3239 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3240 | Table *pTab = sqliteHashData(pElem); |
| 3241 | if( pTab->tnum==iFrom ){ |
| 3242 | pTab->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3243 | } |
| 3244 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3245 | pHash = &pDb->pSchema->idxHash; |
| 3246 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3247 | Index *pIdx = sqliteHashData(pElem); |
| 3248 | if( pIdx->tnum==iFrom ){ |
| 3249 | pIdx->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3250 | } |
| 3251 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3252 | } |
| 3253 | #endif |
| 3254 | |
| 3255 | /* |
| 3256 | ** Write code to erase the table with root-page iTable from database iDb. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3257 | ** Also write code to modify the sqlite_schema table and internal schema |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3258 | ** if a root-page of another table is moved by the btree-layer whilst |
| 3259 | ** erasing iTable (this can happen with an auto-vacuum database). |
| 3260 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3261 | static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 3262 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3263 | int r1 = sqlite3GetTempReg(pParse); |
drh | 1991888 | 2020-07-30 23:47:00 +0000 | [diff] [blame] | 3264 | if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3265 | sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3266 | sqlite3MayAbort(pParse); |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 3267 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3268 | /* OP_Destroy stores an in integer r1. If this integer |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3269 | ** is non-zero, then it is the root page number of a table moved to |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3270 | ** location iTable. The following code modifies the sqlite_schema table to |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3271 | ** reflect this. |
| 3272 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 3273 | ** The "#NNN" in the SQL is a special constant that means whatever value |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 3274 | ** is in register NNN. See grammar rules associated with the TK_REGISTER |
| 3275 | ** token for additional information. |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3276 | */ |
danielk1977 | 63e3e9f | 2004-11-05 09:19:27 +0000 | [diff] [blame] | 3277 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 3278 | "UPDATE %Q." LEGACY_SCHEMA_TABLE |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3279 | " SET rootpage=%d WHERE #%d AND rootpage=#%d", |
| 3280 | pParse->db->aDb[iDb].zDbSName, iTable, r1, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3281 | #endif |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3282 | sqlite3ReleaseTempReg(pParse, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | /* |
| 3286 | ** Write VDBE code to erase table pTab and all associated indices on disk. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3287 | ** Code to update the sqlite_schema tables and internal schema definitions |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3288 | ** in case a root-page belonging to another table is moved by the btree layer |
| 3289 | ** is also added (this can happen with an auto-vacuum database). |
| 3290 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3291 | static void destroyTable(Parse *pParse, Table *pTab){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3292 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
| 3293 | ** is not defined), then it is important to call OP_Destroy on the |
| 3294 | ** table and index root-pages in order, starting with the numerically |
| 3295 | ** largest root-page number. This guarantees that none of the root-pages |
| 3296 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
| 3297 | ** following were coded: |
| 3298 | ** |
| 3299 | ** OP_Destroy 4 0 |
| 3300 | ** ... |
| 3301 | ** OP_Destroy 5 0 |
| 3302 | ** |
| 3303 | ** and root page 5 happened to be the largest root-page number in the |
| 3304 | ** database, then root page 5 would be moved to page 4 by the |
| 3305 | ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
| 3306 | ** a free-list page. |
| 3307 | */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3308 | Pgno iTab = pTab->tnum; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 3309 | Pgno iDestroyed = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3310 | |
| 3311 | while( 1 ){ |
| 3312 | Index *pIdx; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 3313 | Pgno iLargest = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3314 | |
| 3315 | if( iDestroyed==0 || iTab<iDestroyed ){ |
| 3316 | iLargest = iTab; |
| 3317 | } |
| 3318 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3319 | Pgno iIdx = pIdx->tnum; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3320 | assert( pIdx->pSchema==pTab->pSchema ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3321 | if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
| 3322 | iLargest = iIdx; |
| 3323 | } |
| 3324 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3325 | if( iLargest==0 ){ |
| 3326 | return; |
| 3327 | }else{ |
| 3328 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 3329 | assert( iDb>=0 && iDb<pParse->db->nDb ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3330 | destroyRootPage(pParse, iLargest, iDb); |
| 3331 | iDestroyed = iLargest; |
| 3332 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3333 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | /* |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3337 | ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3338 | ** after a DROP INDEX or DROP TABLE command. |
| 3339 | */ |
| 3340 | static void sqlite3ClearStatTables( |
| 3341 | Parse *pParse, /* The parsing context */ |
| 3342 | int iDb, /* The database number */ |
| 3343 | const char *zType, /* "idx" or "tbl" */ |
| 3344 | const char *zName /* Name of index or table */ |
| 3345 | ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3346 | int i; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3347 | const char *zDbName = pParse->db->aDb[iDb].zDbSName; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 3348 | for(i=1; i<=4; i++){ |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3349 | char zTab[24]; |
| 3350 | sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); |
| 3351 | if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3352 | sqlite3NestedParse(pParse, |
| 3353 | "DELETE FROM %Q.%s WHERE %s=%Q", |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3354 | zDbName, zTab, zType, zName |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3355 | ); |
| 3356 | } |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3361 | ** Generate code to drop a table. |
| 3362 | */ |
| 3363 | void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 3364 | Vdbe *v; |
| 3365 | sqlite3 *db = pParse->db; |
| 3366 | Trigger *pTrigger; |
| 3367 | Db *pDb = &db->aDb[iDb]; |
| 3368 | |
| 3369 | v = sqlite3GetVdbe(pParse); |
| 3370 | assert( v!=0 ); |
| 3371 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 3372 | |
| 3373 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3374 | if( IsVirtual(pTab) ){ |
| 3375 | sqlite3VdbeAddOp0(v, OP_VBegin); |
| 3376 | } |
| 3377 | #endif |
| 3378 | |
| 3379 | /* Drop all triggers associated with the table being dropped. Code |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3380 | ** is generated to remove entries from sqlite_schema and/or |
| 3381 | ** sqlite_temp_schema if required. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3382 | */ |
| 3383 | pTrigger = sqlite3TriggerList(pParse, pTab); |
| 3384 | while( pTrigger ){ |
| 3385 | assert( pTrigger->pSchema==pTab->pSchema || |
| 3386 | pTrigger->pSchema==db->aDb[1].pSchema ); |
| 3387 | sqlite3DropTriggerPtr(pParse, pTrigger); |
| 3388 | pTrigger = pTrigger->pNext; |
| 3389 | } |
| 3390 | |
| 3391 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 3392 | /* Remove any entries of the sqlite_sequence table associated with |
| 3393 | ** the table being dropped. This is done before the table is dropped |
| 3394 | ** at the btree level, in case the sqlite_sequence table needs to |
| 3395 | ** move as a result of the drop (can happen in auto-vacuum mode). |
| 3396 | */ |
| 3397 | if( pTab->tabFlags & TF_Autoincrement ){ |
| 3398 | sqlite3NestedParse(pParse, |
| 3399 | "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3400 | pDb->zDbSName, pTab->zName |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3401 | ); |
| 3402 | } |
| 3403 | #endif |
| 3404 | |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3405 | /* Drop all entries in the schema table that refer to the |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3406 | ** table. The program name loops through the schema table and deletes |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3407 | ** every row that refers to a table of the same name as the one being |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3408 | ** dropped. Triggers are handled separately because a trigger can be |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3409 | ** created in the temp database that refers to a table in another |
| 3410 | ** database. |
| 3411 | */ |
| 3412 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 3413 | "DELETE FROM %Q." LEGACY_SCHEMA_TABLE |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3414 | " WHERE tbl_name=%Q and type!='trigger'", |
| 3415 | pDb->zDbSName, pTab->zName); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3416 | if( !isView && !IsVirtual(pTab) ){ |
| 3417 | destroyTable(pParse, pTab); |
| 3418 | } |
| 3419 | |
| 3420 | /* Remove the table entry from SQLite's internal schema and modify |
| 3421 | ** the schema cookie. |
| 3422 | */ |
| 3423 | if( IsVirtual(pTab) ){ |
| 3424 | sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
dan | 1d4b164 | 2018-12-28 17:45:08 +0000 | [diff] [blame] | 3425 | sqlite3MayAbort(pParse); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3426 | } |
| 3427 | sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 3428 | sqlite3ChangeCookie(pParse, iDb); |
| 3429 | sqliteViewResetAll(db, iDb); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3433 | ** Return TRUE if shadow tables should be read-only in the current |
| 3434 | ** context. |
| 3435 | */ |
| 3436 | int sqlite3ReadOnlyShadowTables(sqlite3 *db){ |
| 3437 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3438 | if( (db->flags & SQLITE_Defensive)!=0 |
| 3439 | && db->pVtabCtx==0 |
| 3440 | && db->nVdbeExec==0 |
dan | 7398365 | 2021-07-19 14:00:29 +0000 | [diff] [blame] | 3441 | && !sqlite3VtabInSync(db) |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3442 | ){ |
| 3443 | return 1; |
| 3444 | } |
| 3445 | #endif |
| 3446 | return 0; |
| 3447 | } |
| 3448 | |
| 3449 | /* |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3450 | ** Return true if it is not allowed to drop the given table |
| 3451 | */ |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3452 | static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){ |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3453 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 3454 | if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0; |
| 3455 | if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; |
| 3456 | return 1; |
| 3457 | } |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3458 | if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ |
| 3459 | return 1; |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3460 | } |
dan | 35c7312 | 2021-11-06 18:22:50 +0000 | [diff] [blame] | 3461 | if( pTab->tabFlags & TF_Eponymous ){ |
| 3462 | return 1; |
| 3463 | } |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3464 | return 0; |
| 3465 | } |
| 3466 | |
| 3467 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3468 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3469 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3470 | */ |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3471 | void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3472 | Table *pTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3473 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3474 | sqlite3 *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 3475 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3476 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3477 | if( db->mallocFailed ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 3478 | goto exit_drop_table; |
| 3479 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3480 | assert( pParse->nErr==0 ); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3481 | assert( pName->nSrc==1 ); |
drh | 7520996 | 2015-04-19 22:31:45 +0000 | [diff] [blame] | 3482 | if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 3483 | if( noErr ) db->suppressErr++; |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 3484 | assert( isView==0 || isView==LOCATE_VIEW ); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 3485 | pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 3486 | if( noErr ) db->suppressErr--; |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3487 | |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3488 | if( pTab==0 ){ |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 3489 | if( noErr ){ |
| 3490 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 3491 | sqlite3ForceNotReadOnly(pParse); |
| 3492 | } |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3493 | goto exit_drop_table; |
| 3494 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3495 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3496 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | b5258c3 | 2007-10-04 18:11:15 +0000 | [diff] [blame] | 3497 | |
| 3498 | /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
| 3499 | ** it is initialized. |
| 3500 | */ |
| 3501 | if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 3502 | goto exit_drop_table; |
| 3503 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3504 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3505 | { |
| 3506 | int code; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3507 | const char *zTab = SCHEMA_TABLE(iDb); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3508 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3509 | const char *zArg2 = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3510 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3511 | goto exit_drop_table; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3512 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3513 | if( isView ){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3514 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3515 | code = SQLITE_DROP_TEMP_VIEW; |
| 3516 | }else{ |
| 3517 | code = SQLITE_DROP_VIEW; |
| 3518 | } |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3519 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3520 | }else if( IsVirtual(pTab) ){ |
| 3521 | code = SQLITE_DROP_VTABLE; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 3522 | zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3523 | #endif |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3524 | }else{ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3525 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3526 | code = SQLITE_DROP_TEMP_TABLE; |
| 3527 | }else{ |
| 3528 | code = SQLITE_DROP_TABLE; |
| 3529 | } |
| 3530 | } |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3531 | if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3532 | goto exit_drop_table; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3533 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3534 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 3535 | goto exit_drop_table; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 3536 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3537 | } |
| 3538 | #endif |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3539 | if( tableMayNotBeDropped(db, pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3540 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3541 | goto exit_drop_table; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3542 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3543 | |
| 3544 | #ifndef SQLITE_OMIT_VIEW |
| 3545 | /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
| 3546 | ** on a table. |
| 3547 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3548 | if( isView && !IsView(pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3549 | sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
| 3550 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3551 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3552 | if( !isView && IsView(pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3553 | sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
| 3554 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3555 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3556 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3557 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3558 | /* Generate code to remove the table from the schema table |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 3559 | ** on disk. |
| 3560 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3561 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3562 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 3563 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
mistachkin | 0fc2da3 | 2018-07-20 20:56:22 +0000 | [diff] [blame] | 3564 | if( !isView ){ |
| 3565 | sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 3566 | sqlite3FkDropTable(pParse, pName, pTab); |
| 3567 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3568 | sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3569 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3570 | |
| 3571 | exit_drop_table: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3572 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3576 | ** This routine is called to create a new foreign key on the table |
| 3577 | ** currently under construction. pFromCol determines which columns |
| 3578 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 3579 | ** connect the key to the last column inserted. pTo is the name of |
drh | bd50a92 | 2013-11-03 02:27:58 +0000 | [diff] [blame] | 3580 | ** the table referred to (a.k.a the "parent" table). pToCol is a list |
| 3581 | ** of tables in the parent pTo table. flags contains all |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3582 | ** information about the conflict resolution algorithms specified |
| 3583 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 3584 | ** |
| 3585 | ** An FKey structure is created and added to the table currently |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3586 | ** under construction in the pParse->pNewTable field. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3587 | ** |
| 3588 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3589 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3590 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3591 | void sqlite3CreateForeignKey( |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3592 | Parse *pParse, /* Parsing context */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3593 | ExprList *pFromCol, /* Columns in this table that point to other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3594 | Token *pTo, /* Name of the other table */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3595 | ExprList *pToCol, /* Columns in the other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3596 | int flags /* Conflict resolution algorithms. */ |
| 3597 | ){ |
danielk1977 | 1857693 | 2008-08-06 13:47:40 +0000 | [diff] [blame] | 3598 | sqlite3 *db = pParse->db; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3599 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 3600 | FKey *pFKey = 0; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3601 | FKey *pNextTo; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3602 | Table *p = pParse->pNewTable; |
drh | 913306a | 2021-11-26 17:10:18 +0000 | [diff] [blame] | 3603 | i64 nByte; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3604 | int i; |
| 3605 | int nCol; |
| 3606 | char *z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3607 | |
| 3608 | assert( pTo!=0 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3609 | if( p==0 || IN_DECLARE_VTAB ) goto fk_end; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3610 | if( pFromCol==0 ){ |
| 3611 | int iCol = p->nCol-1; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3612 | if( NEVER(iCol<0) ) goto fk_end; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3613 | if( pToCol && pToCol->nExpr!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3614 | sqlite3ErrorMsg(pParse, "foreign key on %s" |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3615 | " should reference only one column of table %T", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 3616 | p->aCol[iCol].zCnName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3617 | goto fk_end; |
| 3618 | } |
| 3619 | nCol = 1; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3620 | }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3621 | sqlite3ErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3622 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3623 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3624 | goto fk_end; |
| 3625 | }else{ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3626 | nCol = pFromCol->nExpr; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3627 | } |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3628 | nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3629 | if( pToCol ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3630 | for(i=0; i<pToCol->nExpr; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3631 | nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3634 | pFKey = sqlite3DbMallocZero(db, nByte ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3635 | if( pFKey==0 ){ |
| 3636 | goto fk_end; |
| 3637 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3638 | pFKey->pFrom = p; |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 3639 | assert( IsOrdinaryTable(p) ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3640 | pFKey->pNextFrom = p->u.tab.pFKey; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3641 | z = (char*)&pFKey->aCol[nCol]; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 3642 | pFKey->zTo = z; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3643 | if( IN_RENAME_OBJECT ){ |
| 3644 | sqlite3RenameTokenMap(pParse, (void*)z, pTo); |
| 3645 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3646 | memcpy(z, pTo->z, pTo->n); |
| 3647 | z[pTo->n] = 0; |
danielk1977 | 70d9e9c | 2009-04-24 18:06:09 +0000 | [diff] [blame] | 3648 | sqlite3Dequote(z); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3649 | z += pTo->n+1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3650 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3651 | if( pFromCol==0 ){ |
| 3652 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 3653 | }else{ |
| 3654 | for(i=0; i<nCol; i++){ |
| 3655 | int j; |
| 3656 | for(j=0; j<p->nCol; j++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 3657 | if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3658 | pFKey->aCol[i].iFrom = j; |
| 3659 | break; |
| 3660 | } |
| 3661 | } |
| 3662 | if( j>=p->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3663 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3664 | "unknown column \"%s\" in foreign key definition", |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3665 | pFromCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3666 | goto fk_end; |
| 3667 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3668 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3669 | sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3670 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3671 | } |
| 3672 | } |
| 3673 | if( pToCol ){ |
| 3674 | for(i=0; i<nCol; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3675 | int n = sqlite3Strlen30(pToCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3676 | pFKey->aCol[i].zCol = z; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3677 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3678 | sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName); |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 3679 | } |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3680 | memcpy(z, pToCol->a[i].zEName, n); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3681 | z[n] = 0; |
| 3682 | z += n+1; |
| 3683 | } |
| 3684 | } |
| 3685 | pFKey->isDeferred = 0; |
dan | 8099ce6 | 2009-09-23 08:43:35 +0000 | [diff] [blame] | 3686 | pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
| 3687 | pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3688 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3689 | assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3690 | pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 3691 | pFKey->zTo, (void *)pFKey |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3692 | ); |
dan | f59c5ca | 2009-09-22 16:55:38 +0000 | [diff] [blame] | 3693 | if( pNextTo==pFKey ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 3694 | sqlite3OomFault(db); |
dan | f59c5ca | 2009-09-22 16:55:38 +0000 | [diff] [blame] | 3695 | goto fk_end; |
| 3696 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3697 | if( pNextTo ){ |
| 3698 | assert( pNextTo->pPrevTo==0 ); |
| 3699 | pFKey->pNextTo = pNextTo; |
| 3700 | pNextTo->pPrevTo = pFKey; |
| 3701 | } |
| 3702 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3703 | /* Link the foreign key to the table as the last step. |
| 3704 | */ |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 3705 | assert( IsOrdinaryTable(p) ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3706 | p->u.tab.pFKey = pFKey; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3707 | pFKey = 0; |
| 3708 | |
| 3709 | fk_end: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3710 | sqlite3DbFree(db, pFKey); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3711 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3712 | sqlite3ExprListDelete(db, pFromCol); |
| 3713 | sqlite3ExprListDelete(db, pToCol); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | /* |
| 3717 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 3718 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 3719 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 3720 | ** The behavior of the most recently created foreign key is adjusted |
| 3721 | ** accordingly. |
| 3722 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3723 | void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3724 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3725 | Table *pTab; |
| 3726 | FKey *pFKey; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3727 | if( (pTab = pParse->pNewTable)==0 ) return; |
drh | 78b2fa8 | 2021-10-07 12:11:20 +0000 | [diff] [blame] | 3728 | if( NEVER(!IsOrdinaryTable(pTab)) ) return; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3729 | if( (pFKey = pTab->u.tab.pFKey)==0 ) return; |
drh | 4c42983 | 2009-10-12 22:30:49 +0000 | [diff] [blame] | 3730 | assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3731 | pFKey->isDeferred = (u8)isDeferred; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3732 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | /* |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3736 | ** Generate code that will erase and refill index *pIdx. This is |
| 3737 | ** used to initialize a newly created index or to recompute the |
| 3738 | ** content of an index in response to a REINDEX command. |
| 3739 | ** |
| 3740 | ** if memRootPage is not negative, it means that the index is newly |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3741 | ** created. The register specified by memRootPage contains the |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3742 | ** root page number of the index. If memRootPage is negative, then |
| 3743 | ** the index already exists and must be cleared before being refilled and |
| 3744 | ** the root page number of the index is taken from pIndex->tnum. |
| 3745 | */ |
| 3746 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 3747 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 3748 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 3749 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3750 | int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3751 | int addr1; /* Address of top of loop */ |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3752 | int addr2; /* Address to jump to for next iteration */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3753 | Pgno tnum; /* Root page of index */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3754 | int iPartIdxLabel; /* Jump to this label to skip a row */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3755 | Vdbe *v; /* Generate code into this virtual machine */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3756 | KeyInfo *pKey; /* KeyInfo for index */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3757 | int regRecord; /* Register holding assembled index record */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3758 | sqlite3 *db = pParse->db; /* The database connection */ |
| 3759 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3760 | |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 3761 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3762 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3763 | db->aDb[iDb].zDbSName ) ){ |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
| 3766 | #endif |
| 3767 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 3768 | /* Require a write-lock on the table to perform this operation */ |
| 3769 | sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 3770 | |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3771 | v = sqlite3GetVdbe(pParse); |
| 3772 | if( v==0 ) return; |
| 3773 | if( memRootPage>=0 ){ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3774 | tnum = (Pgno)memRootPage; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3775 | }else{ |
| 3776 | tnum = pIndex->tnum; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3777 | } |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3778 | pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 3779 | assert( pKey!=0 || pParse->nErr ); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3780 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3781 | /* Open the sorter cursor if we are to use one. */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3782 | iSorter = pParse->nTab++; |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3783 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3784 | sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3785 | |
| 3786 | /* Open the table. Loop through all rows of the table, inserting index |
| 3787 | ** records into the sorter. */ |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 3788 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3789 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 3790 | regRecord = sqlite3GetTempReg(pParse); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3791 | sqlite3MultiWrite(pParse); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3792 | |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 3793 | sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3794 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
drh | 8774451 | 2014-04-13 19:15:49 +0000 | [diff] [blame] | 3795 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3796 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3797 | sqlite3VdbeJumpHere(v, addr1); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3798 | if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3799 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3800 | (char *)pKey, P4_KEYINFO); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3801 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
| 3802 | |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3803 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
drh | 60de73e | 2016-04-05 15:59:23 +0000 | [diff] [blame] | 3804 | if( IsUniqueIndex(pIndex) ){ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3805 | int j2 = sqlite3VdbeGoto(v, 1); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3806 | addr2 = sqlite3VdbeCurrentAddr(v); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3807 | sqlite3VdbeVerifyAbortable(v, OE_Abort); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 3808 | sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 3809 | pIndex->nKeyCol); VdbeCoverage(v); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 3810 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3811 | sqlite3VdbeJumpHere(v, j2); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3812 | }else{ |
dan | 7ed6c06 | 2019-05-21 16:32:41 +0000 | [diff] [blame] | 3813 | /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not |
| 3814 | ** abort. The exception is if one of the indexed expressions contains a |
| 3815 | ** user function that throws an exception when it is evaluated. But the |
| 3816 | ** overhead of adding a statement journal to a CREATE INDEX statement is |
| 3817 | ** very small (since most of the pages written do not contain content that |
| 3818 | ** needs to be restored if the statement aborts), so we call |
| 3819 | ** sqlite3MayAbort() for all CREATE INDEX statements. */ |
dan | ef14abb | 2019-05-21 14:42:24 +0000 | [diff] [blame] | 3820 | sqlite3MayAbort(pParse); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3821 | addr2 = sqlite3VdbeCurrentAddr(v); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3822 | } |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 3823 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
drh | bf9ff25 | 2019-05-14 00:43:13 +0000 | [diff] [blame] | 3824 | if( !pIndex->bAscKeyBug ){ |
| 3825 | /* This OP_SeekEnd opcode makes index insert for a REINDEX go much |
| 3826 | ** faster by avoiding unnecessary seeks. But the optimization does |
| 3827 | ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables |
| 3828 | ** with DESC primary keys, since those indexes have there keys in |
| 3829 | ** a different order from the main table. |
| 3830 | ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf |
| 3831 | */ |
| 3832 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); |
| 3833 | } |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 3834 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3835 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 3836 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3837 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 3838 | sqlite3VdbeJumpHere(v, addr1); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3839 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3840 | sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 3841 | sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3842 | sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | /* |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3846 | ** Allocate heap space to hold an Index object with nCol columns. |
| 3847 | ** |
| 3848 | ** Increase the allocation size to provide an extra nExtra bytes |
| 3849 | ** of 8-byte aligned space after the Index object and return a |
| 3850 | ** pointer to this extra space in *ppExtra. |
| 3851 | */ |
| 3852 | Index *sqlite3AllocateIndexObject( |
| 3853 | sqlite3 *db, /* Database connection */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3854 | i16 nCol, /* Total number of columns in the index */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3855 | int nExtra, /* Number of bytes of extra space to alloc */ |
| 3856 | char **ppExtra /* Pointer to the "extra" space */ |
| 3857 | ){ |
| 3858 | Index *p; /* Allocated index object */ |
| 3859 | int nByte; /* Bytes of space for Index object + arrays */ |
| 3860 | |
| 3861 | nByte = ROUND8(sizeof(Index)) + /* Index structure */ |
| 3862 | ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3863 | ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3864 | sizeof(i16)*nCol + /* Index.aiColumn */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3865 | sizeof(u8)*nCol); /* Index.aSortOrder */ |
| 3866 | p = sqlite3DbMallocZero(db, nByte + nExtra); |
| 3867 | if( p ){ |
| 3868 | char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 3869 | p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3870 | p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); |
| 3871 | p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3872 | p->aSortOrder = (u8*)pExtra; |
| 3873 | p->nColumn = nCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3874 | p->nKeyCol = nCol - 1; |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3875 | *ppExtra = ((char*)p) + nByte; |
| 3876 | } |
| 3877 | return p; |
| 3878 | } |
| 3879 | |
| 3880 | /* |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3881 | ** If expression list pList contains an expression that was parsed with |
| 3882 | ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in |
| 3883 | ** pParse and return non-zero. Otherwise, return zero. |
| 3884 | */ |
| 3885 | int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ |
| 3886 | if( pList ){ |
| 3887 | int i; |
| 3888 | for(i=0; i<pList->nExpr; i++){ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 3889 | if( pList->a[i].fg.bNulls ){ |
| 3890 | u8 sf = pList->a[i].fg.sortFlags; |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3891 | sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", |
| 3892 | (sf==0 || sf==3) ? "FIRST" : "LAST" |
| 3893 | ); |
| 3894 | return 1; |
| 3895 | } |
| 3896 | } |
| 3897 | } |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
| 3901 | /* |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3902 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 3903 | ** and pTblList is the name of the table that is to be indexed. Both will |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3904 | ** be NULL for a primary key or an index that is created to satisfy a |
| 3905 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 3906 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 3907 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3908 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 3909 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 3910 | ** is a primary key or unique-constraint on the most recent column added |
| 3911 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3912 | */ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3913 | void sqlite3CreateIndex( |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3914 | Parse *pParse, /* All information about this parse */ |
| 3915 | Token *pName1, /* First part of index name. May be NULL */ |
| 3916 | Token *pName2, /* Second part of index name. May be NULL */ |
| 3917 | SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3918 | ExprList *pList, /* A list of columns to be indexed */ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3919 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 1c55ba0 | 2007-07-02 19:31:27 +0000 | [diff] [blame] | 3920 | Token *pStart, /* The CREATE token that begins this statement */ |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 3921 | Expr *pPIWhere, /* WHERE clause for partial indices */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 3922 | int sortOrder, /* Sort order of primary key when pList==NULL */ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3923 | int ifNotExist, /* Omit error if index already exists */ |
| 3924 | u8 idxType /* The index type */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3925 | ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3926 | Table *pTab = 0; /* Table to be indexed */ |
| 3927 | Index *pIndex = 0; /* The index to be created */ |
| 3928 | char *zName = 0; /* Name of the index */ |
| 3929 | int nName; /* Number of characters in zName */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 3930 | int i, j; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3931 | DbFixer sFix; /* For assigning database names to pTable */ |
| 3932 | int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3933 | sqlite3 *db = pParse->db; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3934 | Db *pDb; /* The specific table containing the indexed database */ |
| 3935 | int iDb; /* Index of the database that is being written */ |
| 3936 | Token *pName = 0; /* Unqualified name of the index to create */ |
| 3937 | struct ExprList_item *pListItem; /* For looping over pList */ |
drh | c28c4e5 | 2013-10-03 19:21:41 +0000 | [diff] [blame] | 3938 | int nExtra = 0; /* Space allocated for zExtra[] */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3939 | int nExtraCol; /* Number of extra columns needed */ |
drh | 47b927d | 2013-12-03 00:11:40 +0000 | [diff] [blame] | 3940 | char *zExtra = 0; /* Extra space after the Index object */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3941 | Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3942 | |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 3943 | assert( db->pParse==pParse ); |
| 3944 | if( pParse->nErr ){ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3945 | goto exit_create_index; |
| 3946 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 3947 | assert( db->mallocFailed==0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3948 | if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3949 | goto exit_create_index; |
| 3950 | } |
| 3951 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 3952 | goto exit_create_index; |
| 3953 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3954 | if( sqlite3HasExplicitNulls(pParse, pList) ){ |
| 3955 | goto exit_create_index; |
| 3956 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3957 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3958 | /* |
| 3959 | ** Find the table that is to be indexed. Return early if not found. |
| 3960 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3961 | if( pTblName!=0 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* Use the two-part index name to determine the database |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3964 | ** to search for the table. 'Fix' the table name to this db |
| 3965 | ** before looking up the table. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3966 | */ |
| 3967 | assert( pName1 && pName2 ); |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3968 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3969 | if( iDb<0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3970 | assert( pName && pName->z ); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3971 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3972 | #ifndef SQLITE_OMIT_TEMPDB |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 3973 | /* If the index name was unqualified, check if the table |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3974 | ** is a temp table. If so, set the database to 1. Do not do this |
| 3975 | ** if initialising a database schema. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3976 | */ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3977 | if( !db->init.busy ){ |
| 3978 | pTab = sqlite3SrcListLookup(pParse, pTblName); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3979 | if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3980 | iDb = 1; |
| 3981 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3982 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3983 | #endif |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3984 | |
drh | d100f69 | 2013-10-03 15:39:44 +0000 | [diff] [blame] | 3985 | sqlite3FixInit(&sFix, pParse, iDb, "index", pName); |
| 3986 | if( sqlite3FixSrcList(&sFix, pTblName) ){ |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 3987 | /* Because the parser constructs pTblName from a single identifier, |
| 3988 | ** sqlite3FixSrcList can never fail. */ |
| 3989 | assert(0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3990 | } |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 3991 | pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); |
drh | c31c7c1 | 2012-10-08 23:25:07 +0000 | [diff] [blame] | 3992 | assert( db->mallocFailed==0 || pTab==0 ); |
| 3993 | if( pTab==0 ) goto exit_create_index; |
drh | 989b116 | 2013-08-01 22:27:26 +0000 | [diff] [blame] | 3994 | if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ |
| 3995 | sqlite3ErrorMsg(pParse, |
| 3996 | "cannot create a TEMP index on non-TEMP table \"%s\"", |
| 3997 | pTab->zName); |
| 3998 | goto exit_create_index; |
| 3999 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4000 | if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4001 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 4002 | assert( pName==0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4003 | assert( pStart==0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4004 | pTab = pParse->pNewTable; |
drh | a6370df | 2006-01-04 21:40:06 +0000 | [diff] [blame] | 4005 | if( !pTab ) goto exit_create_index; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4006 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4007 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4008 | pDb = &db->aDb[iDb]; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 4009 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4010 | assert( pTab!=0 ); |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 4011 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
drh | 3a3a03f | 2014-09-11 16:36:43 +0000 | [diff] [blame] | 4012 | && db->init.busy==0 |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 4013 | && pTblName!=0 |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 4014 | #if SQLITE_USER_AUTHENTICATION |
| 4015 | && sqlite3UserAuthTable(pTab->zName)==0 |
| 4016 | #endif |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4017 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4018 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 4019 | goto exit_create_index; |
| 4020 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 4021 | #ifndef SQLITE_OMIT_VIEW |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 4022 | if( IsView(pTab) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4023 | sqlite3ErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 4024 | goto exit_create_index; |
| 4025 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 4026 | #endif |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 +0000 | [diff] [blame] | 4027 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4028 | if( IsVirtual(pTab) ){ |
| 4029 | sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); |
| 4030 | goto exit_create_index; |
| 4031 | } |
| 4032 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4033 | |
| 4034 | /* |
| 4035 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4036 | ** index or table with the same name. |
| 4037 | ** |
| 4038 | ** Exception: If we are reading the names of permanent indices from the |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 4039 | ** sqlite_schema table (because some other process changed the schema) and |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4040 | ** one of the index names collides with the name of a temporary table or |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4041 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 4042 | ** |
| 4043 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 4044 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 4045 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4046 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4047 | if( pName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4048 | zName = sqlite3NameFromToken(db, pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 4049 | if( zName==0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4050 | assert( pName->z!=0 ); |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 4051 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4052 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 4053 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4054 | if( !IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4055 | if( !db->init.busy ){ |
drh | 626bcc8 | 2022-08-09 16:13:21 +0000 | [diff] [blame] | 4056 | if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4057 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 4058 | goto exit_create_index; |
| 4059 | } |
| 4060 | } |
| 4061 | if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| 4062 | if( !ifNotExist ){ |
| 4063 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 4064 | }else{ |
| 4065 | assert( !db->init.busy ); |
| 4066 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 4067 | sqlite3ForceNotReadOnly(pParse); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4068 | } |
danielk1977 | d45a031 | 2007-03-13 16:32:25 +0000 | [diff] [blame] | 4069 | goto exit_create_index; |
| 4070 | } |
| 4071 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 4072 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 4073 | int n; |
| 4074 | Index *pLoop; |
| 4075 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 4076 | zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 4077 | if( zName==0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 4078 | goto exit_create_index; |
| 4079 | } |
drh | 0aafa9c | 2016-08-05 14:35:47 +0000 | [diff] [blame] | 4080 | |
| 4081 | /* Automatic index names generated from within sqlite3_declare_vtab() |
| 4082 | ** must have names that are distinct from normal automatic index names. |
| 4083 | ** The following statement converts "sqlite3_autoindex..." into |
| 4084 | ** "sqlite3_butoindex..." in order to make the names distinct. |
| 4085 | ** The "vtab_err.test" test demonstrates the need of this statement. */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4086 | if( IN_SPECIAL_PARSE ) zName[7]++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4089 | /* Check for authorization to create an index. |
| 4090 | */ |
| 4091 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4092 | if( !IN_RENAME_OBJECT ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4093 | const char *zDb = pDb->zDbSName; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4094 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 4095 | goto exit_create_index; |
| 4096 | } |
| 4097 | i = SQLITE_CREATE_INDEX; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4098 | if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4099 | if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 4100 | goto exit_create_index; |
| 4101 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4102 | } |
| 4103 | #endif |
| 4104 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4105 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 4106 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4107 | ** So create a fake list to simulate this. |
| 4108 | */ |
| 4109 | if( pList==0 ){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4110 | Token prevCol; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 4111 | Column *pCol = &pTab->aCol[pTab->nCol-1]; |
| 4112 | pCol->colFlags |= COLFLAG_UNIQUE; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 4113 | sqlite3TokenInit(&prevCol, pCol->zCnName); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4114 | pList = sqlite3ExprListAppend(pParse, 0, |
| 4115 | sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4116 | if( pList==0 ) goto exit_create_index; |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 4117 | assert( pList->nExpr==1 ); |
dan | 5b32bdf | 2019-08-17 15:47:32 +0000 | [diff] [blame] | 4118 | sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4119 | }else{ |
| 4120 | sqlite3ExprListCheckLength(pParse, pList, "index"); |
drh | 8fe25c6 | 2019-03-31 21:09:33 +0000 | [diff] [blame] | 4121 | if( pParse->nErr ) goto exit_create_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4124 | /* Figure out how many bytes of space are required to store explicitly |
| 4125 | ** specified collation sequence names. |
| 4126 | */ |
| 4127 | for(i=0; i<pList->nExpr; i++){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4128 | Expr *pExpr = pList->a[i].pExpr; |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 4129 | assert( pExpr!=0 ); |
| 4130 | if( pExpr->op==TK_COLLATE ){ |
drh | f975107 | 2021-10-07 13:40:29 +0000 | [diff] [blame] | 4131 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
dan | 911ce41 | 2013-05-15 15:16:50 +0000 | [diff] [blame] | 4132 | nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4133 | } |
| 4134 | } |
| 4135 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4136 | /* |
| 4137 | ** Allocate the index structure. |
| 4138 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4139 | nName = sqlite3Strlen30(zName); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4140 | nExtraCol = pPk ? pPk->nKeyCol : 1; |
drh | 8fe25c6 | 2019-03-31 21:09:33 +0000 | [diff] [blame] | 4141 | assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4142 | pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 4143 | nName + nExtra + 1, &zExtra); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4144 | if( db->mallocFailed ){ |
| 4145 | goto exit_create_index; |
| 4146 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4147 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); |
drh | e09b84c | 2011-11-14 02:53:54 +0000 | [diff] [blame] | 4148 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 4149 | pIndex->zName = zExtra; |
| 4150 | zExtra += nName + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 4151 | memcpy(pIndex->zName, zName, nName+1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4152 | pIndex->pTable = pTab; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4153 | pIndex->onError = (u8)onError; |
drh | 9eade08 | 2013-10-24 14:16:10 +0000 | [diff] [blame] | 4154 | pIndex->uniqNotNull = onError!=OE_None; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 4155 | pIndex->idxType = idxType; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4156 | pIndex->pSchema = db->aDb[iDb].pSchema; |
drh | 72ffd09 | 2013-10-30 15:52:32 +0000 | [diff] [blame] | 4157 | pIndex->nKeyCol = pList->nExpr; |
drh | 3780be1 | 2013-07-31 19:05:22 +0000 | [diff] [blame] | 4158 | if( pPIWhere ){ |
| 4159 | sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); |
| 4160 | pIndex->pPartIdxWhere = pPIWhere; |
| 4161 | pPIWhere = 0; |
| 4162 | } |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 4163 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4164 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4165 | /* Check to see if we should honor DESC requests on index columns |
| 4166 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4167 | if( pDb->pSchema->file_format>=4 ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4168 | sortOrderMask = -1; /* Honor DESC */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4169 | }else{ |
| 4170 | sortOrderMask = 0; /* Ignore DESC */ |
| 4171 | } |
| 4172 | |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4173 | /* Analyze the list of expressions that form the terms of the index and |
| 4174 | ** report any errors. In the common case where the expression is exactly |
| 4175 | ** a table column, store that column in aiColumn[]. For general expressions, |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4176 | ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4177 | ** |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4178 | ** TODO: Issue a warning if two or more columns of the index are identical. |
| 4179 | ** TODO: Issue a warning if the table primary key is used as part of the |
| 4180 | ** index key. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4181 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4182 | pListItem = pList->a; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4183 | if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4184 | pIndex->aColExpr = pList; |
| 4185 | pList = 0; |
| 4186 | } |
| 4187 | for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4188 | Expr *pCExpr; /* The i-th index expression */ |
| 4189 | int requestedSortOrder; /* ASC or DESC on the i-th expression */ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4190 | const char *zColl; /* Collation sequence name */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4191 | |
drh | edb04ed | 2015-09-04 12:54:01 +0000 | [diff] [blame] | 4192 | sqlite3StringToId(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4193 | sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); |
| 4194 | if( pParse->nErr ) goto exit_create_index; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4195 | pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4196 | if( pCExpr->op!=TK_COLUMN ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4197 | if( pTab==pParse->pNewTable ){ |
| 4198 | sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " |
| 4199 | "UNIQUE constraints"); |
| 4200 | goto exit_create_index; |
| 4201 | } |
| 4202 | if( pIndex->aColExpr==0 ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4203 | pIndex->aColExpr = pList; |
| 4204 | pList = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4205 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4206 | j = XN_EXPR; |
| 4207 | pIndex->aiColumn[i] = XN_EXPR; |
drh | 8492653 | 2015-08-31 19:38:42 +0000 | [diff] [blame] | 4208 | pIndex->uniqNotNull = 0; |
drh | 4bc1cc1 | 2022-10-13 21:08:34 +0000 | [diff] [blame] | 4209 | pIndex->bHasExpr = 1; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4210 | }else{ |
| 4211 | j = pCExpr->iColumn; |
| 4212 | assert( j<=0x7fff ); |
| 4213 | if( j<0 ){ |
| 4214 | j = pTab->iPKey; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 4215 | }else{ |
| 4216 | if( pTab->aCol[j].notNull==0 ){ |
| 4217 | pIndex->uniqNotNull = 0; |
| 4218 | } |
| 4219 | if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ |
| 4220 | pIndex->bHasVCol = 1; |
drh | 0853584 | 2022-10-17 14:30:01 +0000 | [diff] [blame] | 4221 | pIndex->bHasExpr = 1; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 4222 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4223 | } |
| 4224 | pIndex->aiColumn[i] = (i16)j; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4225 | } |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4226 | zColl = 0; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4227 | if( pListItem->pExpr->op==TK_COLLATE ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4228 | int nColl; |
drh | f975107 | 2021-10-07 13:40:29 +0000 | [diff] [blame] | 4229 | assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) ); |
dan | 911ce41 | 2013-05-15 15:16:50 +0000 | [diff] [blame] | 4230 | zColl = pListItem->pExpr->u.zToken; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4231 | nColl = sqlite3Strlen30(zColl) + 1; |
| 4232 | assert( nExtra>=nColl ); |
| 4233 | memcpy(zExtra, zColl, nColl); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4234 | zColl = zExtra; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4235 | zExtra += nColl; |
| 4236 | nExtra -= nColl; |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4237 | }else if( j>=0 ){ |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 4238 | zColl = sqlite3ColumnColl(&pTab->aCol[j]); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 4239 | } |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4240 | if( !zColl ) zColl = sqlite3StrBINARY; |
drh | b7f24de | 2009-05-13 17:35:23 +0000 | [diff] [blame] | 4241 | if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 4242 | goto exit_create_index; |
| 4243 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4244 | pIndex->azColl[i] = zColl; |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 4245 | requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4246 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4247 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4248 | |
| 4249 | /* Append the table key to the end of the index. For WITHOUT ROWID |
| 4250 | ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For |
| 4251 | ** normal tables (when pPk==0) this will be the rowid. |
| 4252 | */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4253 | if( pPk ){ |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4254 | for(j=0; j<pPk->nKeyCol; j++){ |
| 4255 | int x = pPk->aiColumn[j]; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4256 | assert( x>=0 ); |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 4257 | if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4258 | pIndex->nColumn--; |
| 4259 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 4260 | testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4261 | pIndex->aiColumn[i] = x; |
| 4262 | pIndex->azColl[i] = pPk->azColl[j]; |
| 4263 | pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
| 4264 | i++; |
| 4265 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4266 | } |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4267 | assert( i==pIndex->nColumn ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4268 | }else{ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4269 | pIndex->aiColumn[i] = XN_ROWID; |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4270 | pIndex->azColl[i] = sqlite3StrBINARY; |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4271 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4272 | sqlite3DefaultRowEst(pIndex); |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 4273 | if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4274 | |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4275 | /* If this index contains every column of its table, then mark |
| 4276 | ** it as a covering index */ |
| 4277 | assert( HasRowid(pTab) |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 4278 | || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 4279 | recomputeColumnsNotIndexed(pIndex); |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4280 | if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ |
| 4281 | pIndex->isCovering = 1; |
| 4282 | for(j=0; j<pTab->nCol; j++){ |
| 4283 | if( j==pTab->iPKey ) continue; |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 4284 | if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4285 | pIndex->isCovering = 0; |
| 4286 | break; |
| 4287 | } |
| 4288 | } |
| 4289 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4290 | if( pTab==pParse->pNewTable ){ |
| 4291 | /* This routine has been called to create an automatic index as a |
| 4292 | ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
| 4293 | ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
| 4294 | ** i.e. one of: |
| 4295 | ** |
| 4296 | ** CREATE TABLE t(x PRIMARY KEY, y); |
| 4297 | ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
| 4298 | ** |
| 4299 | ** Either way, check to see if the table already has such an index. If |
| 4300 | ** so, don't bother creating this one. This only applies to |
| 4301 | ** automatically created indices. Users can do as they wish with |
| 4302 | ** explicit indices. |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4303 | ** |
| 4304 | ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent |
| 4305 | ** (and thus suppressing the second one) even if they have different |
| 4306 | ** sort orders. |
| 4307 | ** |
| 4308 | ** If there are different collating sequences or if the columns of |
| 4309 | ** the constraint occur in different orders, then the constraints are |
| 4310 | ** considered distinct and both result in separate indices. |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4311 | */ |
| 4312 | Index *pIdx; |
| 4313 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 4314 | int k; |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4315 | assert( IsUniqueIndex(pIdx) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4316 | assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4317 | assert( IsUniqueIndex(pIndex) ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4318 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4319 | if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
| 4320 | for(k=0; k<pIdx->nKeyCol; k++){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4321 | const char *z1; |
| 4322 | const char *z2; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4323 | assert( pIdx->aiColumn[k]>=0 ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4324 | if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4325 | z1 = pIdx->azColl[k]; |
| 4326 | z2 = pIndex->azColl[k]; |
drh | c41c132 | 2016-02-11 13:30:36 +0000 | [diff] [blame] | 4327 | if( sqlite3StrICmp(z1, z2) ) break; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4328 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4329 | if( k==pIdx->nKeyCol ){ |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 4330 | if( pIdx->onError!=pIndex->onError ){ |
| 4331 | /* This constraint creates the same index as a previous |
| 4332 | ** constraint specified somewhere in the CREATE TABLE statement. |
| 4333 | ** However the ON CONFLICT clauses are different. If both this |
| 4334 | ** constraint and the previous equivalent constraint have explicit |
| 4335 | ** ON CONFLICT clauses this is an error. Otherwise, use the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 4336 | ** explicitly specified behavior for the index. |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 4337 | */ |
| 4338 | if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
| 4339 | sqlite3ErrorMsg(pParse, |
| 4340 | "conflicting ON CONFLICT clauses specified", 0); |
| 4341 | } |
| 4342 | if( pIdx->onError==OE_Default ){ |
| 4343 | pIdx->onError = pIndex->onError; |
| 4344 | } |
| 4345 | } |
drh | 273bfe9 | 2016-06-02 16:22:53 +0000 | [diff] [blame] | 4346 | if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; |
drh | 885eeb6 | 2019-01-09 02:02:24 +0000 | [diff] [blame] | 4347 | if( IN_RENAME_OBJECT ){ |
| 4348 | pIndex->pNext = pParse->pNewIndex; |
| 4349 | pParse->pNewIndex = pIndex; |
| 4350 | pIndex = 0; |
| 4351 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4352 | goto exit_create_index; |
| 4353 | } |
| 4354 | } |
| 4355 | } |
| 4356 | |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4357 | if( !IN_RENAME_OBJECT ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 4358 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4359 | /* Link the new Index structure to its table and to the other |
| 4360 | ** in-memory database structures. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4361 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4362 | assert( pParse->nErr==0 ); |
| 4363 | if( db->init.busy ){ |
| 4364 | Index *p; |
| 4365 | assert( !IN_SPECIAL_PARSE ); |
| 4366 | assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4367 | if( pTblName!=0 ){ |
| 4368 | pIndex->tnum = db->init.newTnum; |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 4369 | if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ |
drh | 8bf4126 | 2019-01-30 19:50:07 +0000 | [diff] [blame] | 4370 | sqlite3ErrorMsg(pParse, "invalid rootpage"); |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 4371 | pParse->rc = SQLITE_CORRUPT_BKPT; |
| 4372 | goto exit_create_index; |
| 4373 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4374 | } |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 4375 | p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4376 | pIndex->zName, pIndex); |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 4377 | if( p ){ |
drh | 8a9789b | 2013-08-01 03:36:59 +0000 | [diff] [blame] | 4378 | assert( p==pIndex ); /* Malloc must have failed */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4379 | sqlite3OomFault(db); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 4380 | goto exit_create_index; |
drh | 8a9789b | 2013-08-01 03:36:59 +0000 | [diff] [blame] | 4381 | } |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4382 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4383 | } |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4384 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4385 | /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
| 4386 | ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
| 4387 | ** emit code to allocate the index rootpage on disk and make an entry for |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 4388 | ** the index in the sqlite_schema table and populate the index with |
| 4389 | ** content. But, do not do this if we are simply reading the sqlite_schema |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4390 | ** table to parse the schema, or if this index is the PRIMARY KEY index |
| 4391 | ** of a WITHOUT ROWID table. |
| 4392 | ** |
| 4393 | ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY |
| 4394 | ** or UNIQUE index in a CREATE TABLE statement. Since the table |
| 4395 | ** has just been created, it contains no data and the index initialization |
| 4396 | ** step can be skipped. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4397 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4398 | else if( HasRowid(pTab) || pTblName!=0 ){ |
| 4399 | Vdbe *v; |
| 4400 | char *zStmt; |
| 4401 | int iMem = ++pParse->nMem; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4402 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4403 | v = sqlite3GetVdbe(pParse); |
| 4404 | if( v==0 ) goto exit_create_index; |
| 4405 | |
| 4406 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 4407 | |
| 4408 | /* Create the rootpage for the index using CreateIndex. But before |
| 4409 | ** doing so, code a Noop instruction and store its address in |
| 4410 | ** Index.tnum. This is required in case this index is actually a |
| 4411 | ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In |
| 4412 | ** that case the convertToWithoutRowidTable() routine will replace |
| 4413 | ** the Noop with a Goto to jump over the VDBE code generated below. */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4414 | pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4415 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); |
| 4416 | |
| 4417 | /* Gather the complete text of the CREATE INDEX statement into |
| 4418 | ** the zStmt variable |
| 4419 | */ |
drh | 55f66b3 | 2019-07-16 19:44:32 +0000 | [diff] [blame] | 4420 | assert( pName!=0 || pStart==0 ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4421 | if( pStart ){ |
| 4422 | int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; |
| 4423 | if( pName->z[n-1]==';' ) n--; |
| 4424 | /* A named index with an explicit CREATE INDEX statement */ |
| 4425 | zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", |
| 4426 | onError==OE_None ? "" : " UNIQUE", n, pName->z); |
| 4427 | }else{ |
| 4428 | /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
| 4429 | /* zStmt = sqlite3MPrintf(""); */ |
| 4430 | zStmt = 0; |
| 4431 | } |
| 4432 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 4433 | /* Add an entry in sqlite_schema for this index |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4434 | */ |
| 4435 | sqlite3NestedParse(pParse, |
drh | fd4bf77 | 2021-12-03 14:43:49 +0000 | [diff] [blame] | 4436 | "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", |
| 4437 | db->aDb[iDb].zDbSName, |
| 4438 | pIndex->zName, |
| 4439 | pTab->zName, |
| 4440 | iMem, |
| 4441 | zStmt |
| 4442 | ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4443 | sqlite3DbFree(db, zStmt); |
| 4444 | |
| 4445 | /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 4446 | ** to invalidate all pre-compiled statements. |
| 4447 | */ |
| 4448 | if( pTblName ){ |
| 4449 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 4450 | sqlite3ChangeCookie(pParse, iDb); |
| 4451 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 4452 | sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4453 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); |
| 4454 | } |
| 4455 | |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4456 | sqlite3VdbeJumpHere(v, (int)pIndex->tnum); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4457 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4458 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4459 | if( db->init.busy || pTblName==0 ){ |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4460 | pIndex->pNext = pTab->pIndex; |
| 4461 | pTab->pIndex = pIndex; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4462 | pIndex = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4463 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4464 | else if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4465 | assert( pParse->pNewIndex==0 ); |
| 4466 | pParse->pNewIndex = pIndex; |
| 4467 | pIndex = 0; |
| 4468 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4469 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4470 | /* Clean up before exiting */ |
| 4471 | exit_create_index: |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4472 | if( pIndex ) sqlite3FreeIndex(db, pIndex); |
drh | 97060e5 | 2021-03-21 17:52:47 +0000 | [diff] [blame] | 4473 | if( pTab ){ |
| 4474 | /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list. |
| 4475 | ** The list was already ordered when this routine was entered, so at this |
| 4476 | ** point at most a single index (the newly added index) will be out of |
| 4477 | ** order. So we have to reorder at most one index. */ |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 4478 | Index **ppFrom; |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4479 | Index *pThis; |
| 4480 | for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ |
| 4481 | Index *pNext; |
| 4482 | if( pThis->onError!=OE_Replace ) continue; |
| 4483 | while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ |
| 4484 | *ppFrom = pNext; |
| 4485 | pThis->pNext = pNext->pNext; |
| 4486 | pNext->pNext = pThis; |
| 4487 | ppFrom = &pNext->pNext; |
| 4488 | } |
| 4489 | break; |
| 4490 | } |
drh | 97060e5 | 2021-03-21 17:52:47 +0000 | [diff] [blame] | 4491 | #ifdef SQLITE_DEBUG |
| 4492 | /* Verify that all REPLACE indexes really are now at the end |
| 4493 | ** of the index list. In other words, no other index type ever |
| 4494 | ** comes after a REPLACE index on the list. */ |
| 4495 | for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){ |
| 4496 | assert( pThis->onError!=OE_Replace |
| 4497 | || pThis->pNext==0 |
| 4498 | || pThis->pNext->onError==OE_Replace ); |
| 4499 | } |
| 4500 | #endif |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4501 | } |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 4502 | sqlite3ExprDelete(db, pPIWhere); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4503 | sqlite3ExprListDelete(db, pList); |
| 4504 | sqlite3SrcListDelete(db, pTblName); |
| 4505 | sqlite3DbFree(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4509 | ** Fill the Index.aiRowEst[] array with default information - information |
drh | 91124b3 | 2005-08-18 18:15:05 +0000 | [diff] [blame] | 4510 | ** to be used when we have not run the ANALYZE command. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4511 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4512 | ** aiRowEst[0] is supposed to contain the number of elements in the index. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4513 | ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 4514 | ** number of rows in the table that match any particular value of the |
| 4515 | ** first column of the index. aiRowEst[2] is an estimate of the number |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4516 | ** of rows that match any particular combination of the first 2 columns |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4517 | ** of the index. And so forth. It must always be the case that |
| 4518 | * |
| 4519 | ** aiRowEst[N]<=aiRowEst[N-1] |
| 4520 | ** aiRowEst[N]>=1 |
| 4521 | ** |
| 4522 | ** Apart from that, we have little to go on besides intuition as to |
| 4523 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 4524 | ** are based on typical values found in actual indices. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4525 | */ |
| 4526 | void sqlite3DefaultRowEst(Index *pIdx){ |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4527 | /* 10, 9, 8, 7, 6 */ |
| 4528 | static const LogEst aVal[] = { 33, 32, 30, 28, 26 }; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4529 | LogEst *a = pIdx->aiRowLogEst; |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4530 | LogEst x; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4531 | int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4532 | int i; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4533 | |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 4534 | /* Indexes with default row estimates should not have stat1 data */ |
| 4535 | assert( !pIdx->hasStat1 ); |
| 4536 | |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4537 | /* Set the first entry (number of rows in the index) to the estimated |
drh | 8dc570b | 2016-06-08 18:07:21 +0000 | [diff] [blame] | 4538 | ** number of rows in the table, or half the number of rows in the table |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4539 | ** for a partial index. |
| 4540 | ** |
| 4541 | ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 |
| 4542 | ** table but other parts we are having to guess at, then do not let the |
| 4543 | ** estimated number of rows in the table be less than 1000 (LogEst 99). |
| 4544 | ** Failure to do this can cause the indexes for which we do not have |
drh | 8c1fbe8 | 2020-08-11 17:20:02 +0000 | [diff] [blame] | 4545 | ** stat1 data to be ignored by the query planner. |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4546 | */ |
| 4547 | x = pIdx->pTable->nRowLogEst; |
| 4548 | assert( 99==sqlite3LogEst(1000) ); |
| 4549 | if( x<99 ){ |
| 4550 | pIdx->pTable->nRowLogEst = x = 99; |
| 4551 | } |
drh | 5f086dd | 2021-04-29 13:37:36 +0000 | [diff] [blame] | 4552 | if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4553 | a[0] = x; |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4554 | |
| 4555 | /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
| 4556 | ** 6 and each subsequent value (if any) is 5. */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4557 | memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4558 | for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ |
| 4559 | a[i] = 23; assert( 23==sqlite3LogEst(5) ); |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4560 | } |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4561 | |
| 4562 | assert( 0==sqlite3LogEst(1) ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4563 | if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 4567 | ** This routine will drop an existing named index. This routine |
| 4568 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4569 | */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4570 | void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4571 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4572 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 4573 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4574 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4575 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4576 | if( db->mallocFailed ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 4577 | goto exit_drop_index; |
| 4578 | } |
drh | 3cdb139 | 2022-01-24 12:48:54 +0000 | [diff] [blame] | 4579 | assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4580 | assert( pName->nSrc==1 ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 4581 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 4582 | goto exit_drop_index; |
| 4583 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4584 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4585 | if( pIndex==0 ){ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4586 | if( !ifExists ){ |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 4587 | sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 4588 | }else{ |
| 4589 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 4590 | sqlite3ForceNotReadOnly(pParse); |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4591 | } |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 4592 | pParse->checkSchema = 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4593 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4594 | } |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4595 | if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4596 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 4597 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4598 | goto exit_drop_index; |
| 4599 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4600 | iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4601 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 4602 | { |
| 4603 | int code = SQLITE_DROP_INDEX; |
| 4604 | Table *pTab = pIndex->pTable; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4605 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4606 | const char *zTab = SCHEMA_TABLE(iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4607 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4608 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4609 | } |
drh | 93fd542 | 2021-06-14 20:41:20 +0000 | [diff] [blame] | 4610 | if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4611 | if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4612 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4613 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 4614 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4615 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4616 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4617 | /* Generate code to remove the index and from the schema table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4618 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4619 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4620 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4621 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 +0000 | [diff] [blame] | 4622 | "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 4623 | db->aDb[iDb].zDbSName, pIndex->zName |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4624 | ); |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 4625 | sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4626 | sqlite3ChangeCookie(pParse, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4627 | destroyRootPage(pParse, pIndex->tnum, iDb); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4628 | sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4631 | exit_drop_index: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4632 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | /* |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4636 | ** pArray is a pointer to an array of objects. Each object in the |
| 4637 | ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() |
| 4638 | ** to extend the array so that there is space for a new object at the end. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4639 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4640 | ** When this function is called, *pnEntry contains the current size of |
| 4641 | ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes |
| 4642 | ** in total). |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4643 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4644 | ** If the realloc() is successful (i.e. if no OOM condition occurs), the |
| 4645 | ** space allocated for the new object is zeroed, *pnEntry updated to |
| 4646 | ** reflect the new size of the array and a pointer to the new allocation |
| 4647 | ** returned. *pIdx is set to the index of the new array entry in this case. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4648 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4649 | ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains |
| 4650 | ** unchanged and a copy of pArray returned. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4651 | */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4652 | void *sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4653 | sqlite3 *db, /* Connection to notify of malloc failures */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4654 | void *pArray, /* Array of objects. Might be reallocated */ |
| 4655 | int szEntry, /* Size of each object in the array */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4656 | int *pnEntry, /* Number of objects currently in use */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4657 | int *pIdx /* Write the index of a new slot here */ |
| 4658 | ){ |
| 4659 | char *z; |
drh | f6ad201 | 2019-04-13 14:07:57 +0000 | [diff] [blame] | 4660 | sqlite3_int64 n = *pIdx = *pnEntry; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4661 | if( (n & (n-1))==0 ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 4662 | sqlite3_int64 sz = (n==0) ? 1 : 2*n; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4663 | void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4664 | if( pNew==0 ){ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4665 | *pIdx = -1; |
| 4666 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4667 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4668 | pArray = pNew; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4669 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4670 | z = (char*)pArray; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4671 | memset(&z[n * szEntry], 0, szEntry); |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4672 | ++*pnEntry; |
| 4673 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
| 4676 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4677 | ** Append a new element to the given IdList. Create a new IdList if |
| 4678 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4679 | ** |
| 4680 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4681 | */ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 4682 | IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ |
| 4683 | sqlite3 *db = pParse->db; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4684 | int i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4685 | if( pList==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4686 | pList = sqlite3DbMallocZero(db, sizeof(IdList) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4687 | if( pList==0 ) return 0; |
drh | a99e325 | 2022-04-15 15:47:14 +0000 | [diff] [blame] | 4688 | }else{ |
| 4689 | IdList *pNew; |
| 4690 | pNew = sqlite3DbRealloc(db, pList, |
| 4691 | sizeof(IdList) + pList->nId*sizeof(pList->a)); |
| 4692 | if( pNew==0 ){ |
| 4693 | sqlite3IdListDelete(db, pList); |
| 4694 | return 0; |
| 4695 | } |
| 4696 | pList = pNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4697 | } |
drh | a99e325 | 2022-04-15 15:47:14 +0000 | [diff] [blame] | 4698 | i = pList->nId++; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4699 | pList->a[i].zName = sqlite3NameFromToken(db, pToken); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4700 | if( IN_RENAME_OBJECT && pList->a[i].zName ){ |
dan | 07e9523 | 2018-08-21 16:32:53 +0000 | [diff] [blame] | 4701 | sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 4702 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4703 | return pList; |
| 4704 | } |
| 4705 | |
| 4706 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4707 | ** Delete an IdList. |
| 4708 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4709 | void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4710 | int i; |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 4711 | assert( db!=0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4712 | if( pList==0 ) return; |
drh | f80bb19 | 2022-04-18 19:48:31 +0000 | [diff] [blame] | 4713 | assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4714 | for(i=0; i<pList->nId; i++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4715 | sqlite3DbFree(db, pList->a[i].zName); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4716 | } |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 4717 | sqlite3DbNNFreeNN(db, pList); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | /* |
| 4721 | ** Return the index in pList of the identifier named zId. Return -1 |
| 4722 | ** if not found. |
| 4723 | */ |
| 4724 | int sqlite3IdListIndex(IdList *pList, const char *zName){ |
| 4725 | int i; |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4726 | assert( pList!=0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4727 | for(i=0; i<pList->nId; i++){ |
| 4728 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 4729 | } |
| 4730 | return -1; |
| 4731 | } |
| 4732 | |
| 4733 | /* |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4734 | ** Maximum size of a SrcList object. |
| 4735 | ** The SrcList object is used to represent the FROM clause of a |
| 4736 | ** SELECT statement, and the query planner cannot deal with more |
| 4737 | ** than 64 tables in a join. So any value larger than 64 here |
| 4738 | ** is sufficient for most uses. Smaller values, like say 10, are |
| 4739 | ** appropriate for small and memory-limited applications. |
| 4740 | */ |
| 4741 | #ifndef SQLITE_MAX_SRCLIST |
| 4742 | # define SQLITE_MAX_SRCLIST 200 |
| 4743 | #endif |
| 4744 | |
| 4745 | /* |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4746 | ** Expand the space allocated for the given SrcList object by |
| 4747 | ** creating nExtra new slots beginning at iStart. iStart is zero based. |
| 4748 | ** New slots are zeroed. |
| 4749 | ** |
| 4750 | ** For example, suppose a SrcList initially contains two entries: A,B. |
| 4751 | ** To append 3 new entries onto the end, do this: |
| 4752 | ** |
| 4753 | ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); |
| 4754 | ** |
| 4755 | ** After the call above it would contain: A, B, nil, nil, nil. |
| 4756 | ** If the iStart argument had been 1 instead of 2, then the result |
| 4757 | ** would have been: A, nil, nil, nil, B. To prepend the new slots, |
| 4758 | ** the iStart value would be 0. The result then would |
| 4759 | ** be: nil, nil, nil, A, B. |
| 4760 | ** |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4761 | ** If a memory allocation fails or the SrcList becomes too large, leave |
| 4762 | ** the original SrcList unchanged, return NULL, and leave an error message |
| 4763 | ** in pParse. |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4764 | */ |
| 4765 | SrcList *sqlite3SrcListEnlarge( |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4766 | Parse *pParse, /* Parsing context into which errors are reported */ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4767 | SrcList *pSrc, /* The SrcList to be enlarged */ |
| 4768 | int nExtra, /* Number of new slots to add to pSrc->a[] */ |
| 4769 | int iStart /* Index in pSrc->a[] of first new slot */ |
| 4770 | ){ |
| 4771 | int i; |
| 4772 | |
| 4773 | /* Sanity checking on calling parameters */ |
| 4774 | assert( iStart>=0 ); |
| 4775 | assert( nExtra>=1 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4776 | assert( pSrc!=0 ); |
| 4777 | assert( iStart<=pSrc->nSrc ); |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4778 | |
| 4779 | /* Allocate additional space if needed */ |
drh | fc5717c | 2014-03-05 19:04:46 +0000 | [diff] [blame] | 4780 | if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4781 | SrcList *pNew; |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 4782 | sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4783 | sqlite3 *db = pParse->db; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4784 | |
| 4785 | if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4786 | sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", |
| 4787 | SQLITE_MAX_SRCLIST); |
| 4788 | return 0; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4789 | } |
| 4790 | if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4791 | pNew = sqlite3DbRealloc(db, pSrc, |
| 4792 | sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); |
| 4793 | if( pNew==0 ){ |
| 4794 | assert( db->mallocFailed ); |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4795 | return 0; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4796 | } |
| 4797 | pSrc = pNew; |
drh | d0ee3a1 | 2019-02-06 01:18:36 +0000 | [diff] [blame] | 4798 | pSrc->nAlloc = nAlloc; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | /* Move existing slots that come after the newly inserted slots |
| 4802 | ** out of the way */ |
| 4803 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| 4804 | pSrc->a[i+nExtra] = pSrc->a[i]; |
| 4805 | } |
drh | 6d1626e | 2014-03-05 15:52:43 +0000 | [diff] [blame] | 4806 | pSrc->nSrc += nExtra; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4807 | |
| 4808 | /* Zero the newly allocated slots */ |
| 4809 | memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
| 4810 | for(i=iStart; i<iStart+nExtra; i++){ |
| 4811 | pSrc->a[i].iCursor = -1; |
| 4812 | } |
| 4813 | |
| 4814 | /* Return a pointer to the enlarged SrcList */ |
| 4815 | return pSrc; |
| 4816 | } |
| 4817 | |
| 4818 | |
| 4819 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4820 | ** Append a new table name to the given SrcList. Create a new SrcList if |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 4821 | ** need be. A new entry is created in the SrcList even if pTable is NULL. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4822 | ** |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4823 | ** A SrcList is returned, or NULL if there is an OOM error or if the |
| 4824 | ** SrcList grows to large. The returned |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4825 | ** SrcList might be the same as the SrcList that was input or it might be |
| 4826 | ** a new one. If an OOM error does occurs, then the prior value of pList |
| 4827 | ** that is input to this routine is automatically freed. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4828 | ** |
| 4829 | ** If pDatabase is not null, it means that the table has an optional |
| 4830 | ** database name prefix. Like this: "database.table". The pDatabase |
| 4831 | ** points to the table name and the pTable points to the database name. |
| 4832 | ** The SrcList.a[].zName field is filled with the table name which might |
| 4833 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 4834 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 4835 | ** or with NULL if no database is specified. |
| 4836 | ** |
| 4837 | ** In other words, if call like this: |
| 4838 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4839 | ** sqlite3SrcListAppend(D,A,B,0); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4840 | ** |
| 4841 | ** Then B is a table name and the database name is unspecified. If called |
| 4842 | ** like this: |
| 4843 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4844 | ** sqlite3SrcListAppend(D,A,B,C); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4845 | ** |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4846 | ** Then C is the table name and B is the database name. If C is defined |
| 4847 | ** then so is B. In other words, we never have a case where: |
| 4848 | ** |
| 4849 | ** sqlite3SrcListAppend(D,A,0,C); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 4850 | ** |
| 4851 | ** Both pTable and pDatabase are assumed to be quoted. They are dequoted |
| 4852 | ** before being added to the SrcList. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4853 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4854 | SrcList *sqlite3SrcListAppend( |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4855 | Parse *pParse, /* Parsing context, in which errors are reported */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4856 | SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ |
| 4857 | Token *pTable, /* Table to append */ |
| 4858 | Token *pDatabase /* Database of the table */ |
| 4859 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4860 | SrcItem *pItem; |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4861 | sqlite3 *db; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4862 | assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4863 | assert( pParse!=0 ); |
| 4864 | assert( pParse->db!=0 ); |
| 4865 | db = pParse->db; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4866 | if( pList==0 ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4867 | pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4868 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 4869 | pList->nAlloc = 1; |
drh | ac178b3 | 2016-12-14 11:14:13 +0000 | [diff] [blame] | 4870 | pList->nSrc = 1; |
| 4871 | memset(&pList->a[0], 0, sizeof(pList->a[0])); |
| 4872 | pList->a[0].iCursor = -1; |
| 4873 | }else{ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4874 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); |
| 4875 | if( pNew==0 ){ |
| 4876 | sqlite3SrcListDelete(db, pList); |
| 4877 | return 0; |
| 4878 | }else{ |
| 4879 | pList = pNew; |
| 4880 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4881 | } |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4882 | pItem = &pList->a[pList->nSrc-1]; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4883 | if( pDatabase && pDatabase->z==0 ){ |
| 4884 | pDatabase = 0; |
| 4885 | } |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4886 | if( pDatabase ){ |
drh | 169a689 | 2017-07-06 01:02:09 +0000 | [diff] [blame] | 4887 | pItem->zName = sqlite3NameFromToken(db, pDatabase); |
| 4888 | pItem->zDatabase = sqlite3NameFromToken(db, pTable); |
| 4889 | }else{ |
| 4890 | pItem->zName = sqlite3NameFromToken(db, pTable); |
| 4891 | pItem->zDatabase = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4892 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4893 | return pList; |
| 4894 | } |
| 4895 | |
| 4896 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4897 | ** Assign VdbeCursor index numbers to all tables in a SrcList |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4898 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4899 | void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4900 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4901 | SrcItem *pItem; |
drh | 9da977f | 2021-04-20 12:14:12 +0000 | [diff] [blame] | 4902 | assert( pList || pParse->db->mallocFailed ); |
| 4903 | if( ALWAYS(pList) ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4904 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 4905 | if( pItem->iCursor>=0 ) continue; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4906 | pItem->iCursor = pParse->nTab++; |
| 4907 | if( pItem->pSelect ){ |
| 4908 | sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); |
| 4909 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4910 | } |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4915 | ** Delete an entire SrcList including all its substructure. |
| 4916 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4917 | void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4918 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4919 | SrcItem *pItem; |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 4920 | assert( db!=0 ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4921 | if( pList==0 ) return; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 4922 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 4923 | if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase); |
| 4924 | if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName); |
| 4925 | if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias); |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4926 | if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
| 4927 | if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 4928 | sqlite3DeleteTable(db, pItem->pTab); |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4929 | if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect); |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4930 | if( pItem->fg.isUsing ){ |
| 4931 | sqlite3IdListDelete(db, pItem->u3.pUsing); |
| 4932 | }else if( pItem->u3.pOn ){ |
| 4933 | sqlite3ExprDelete(db, pItem->u3.pOn); |
| 4934 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4935 | } |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 4936 | sqlite3DbNNFreeNN(db, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 4939 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4940 | ** This routine is called by the parser to add a new term to the |
| 4941 | ** end of a growing FROM clause. The "p" parameter is the part of |
| 4942 | ** the FROM clause that has already been constructed. "p" is NULL |
| 4943 | ** if this is the first term of the FROM clause. pTable and pDatabase |
| 4944 | ** are the name of the table and database named in the FROM clause term. |
| 4945 | ** pDatabase is NULL if the database name qualifier is missing - the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4946 | ** usual case. If the term has an alias, then pAlias points to the |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4947 | ** alias token. If the term is a subquery, then pSubquery is the |
| 4948 | ** SELECT statement that the subquery encodes. The pTable and |
| 4949 | ** pDatabase parameters are NULL for subqueries. The pOn and pUsing |
| 4950 | ** parameters are the content of the ON and USING clauses. |
| 4951 | ** |
| 4952 | ** Return a new SrcList which encodes is the FROM with the new |
| 4953 | ** term added. |
| 4954 | */ |
| 4955 | SrcList *sqlite3SrcListAppendFromTerm( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4956 | Parse *pParse, /* Parsing context */ |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4957 | SrcList *p, /* The left part of the FROM clause already seen */ |
| 4958 | Token *pTable, /* Name of the table to add to the FROM clause */ |
| 4959 | Token *pDatabase, /* Name of the database containing pTable */ |
| 4960 | Token *pAlias, /* The right-hand side of the AS subexpression */ |
| 4961 | Select *pSubquery, /* A subquery used in place of a table name */ |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4962 | OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */ |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4963 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4964 | SrcItem *pItem; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4965 | sqlite3 *db = pParse->db; |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4966 | if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){ |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4967 | sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4968 | (pOnUsing->pOn ? "ON" : "USING") |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4969 | ); |
| 4970 | goto append_from_error; |
| 4971 | } |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4972 | p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 4973 | if( p==0 ){ |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4974 | goto append_from_error; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4975 | } |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 4976 | assert( p->nSrc>0 ); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4977 | pItem = &p->a[p->nSrc-1]; |
drh | a488ec9 | 2018-09-07 18:52:25 +0000 | [diff] [blame] | 4978 | assert( (pTable==0)==(pDatabase==0) ); |
| 4979 | assert( pItem->zName==0 || pDatabase!=0 ); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4980 | if( IN_RENAME_OBJECT && pItem->zName ){ |
drh | a488ec9 | 2018-09-07 18:52:25 +0000 | [diff] [blame] | 4981 | Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4982 | sqlite3RenameTokenMap(pParse, pItem->zName, pToken); |
| 4983 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4984 | assert( pAlias!=0 ); |
| 4985 | if( pAlias->n ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4986 | pItem->zAlias = sqlite3NameFromToken(db, pAlias); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4987 | } |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 4988 | if( pSubquery ){ |
| 4989 | pItem->pSelect = pSubquery; |
| 4990 | if( pSubquery->selFlags & SF_NestedFrom ){ |
| 4991 | pItem->fg.isNestedFrom = 1; |
| 4992 | } |
| 4993 | } |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4994 | assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 ); |
| 4995 | assert( pItem->fg.isUsing==0 ); |
| 4996 | if( pOnUsing==0 ){ |
| 4997 | pItem->u3.pOn = 0; |
| 4998 | }else if( pOnUsing->pUsing ){ |
| 4999 | pItem->fg.isUsing = 1; |
| 5000 | pItem->u3.pUsing = pOnUsing->pUsing; |
| 5001 | }else{ |
| 5002 | pItem->u3.pOn = pOnUsing->pOn; |
| 5003 | } |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 5004 | return p; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 5005 | |
drh | 46658d7 | 2021-12-08 18:50:30 +0000 | [diff] [blame] | 5006 | append_from_error: |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 5007 | assert( p==0 ); |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 5008 | sqlite3ClearOnOrUsing(db, pOnUsing); |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 5009 | sqlite3SelectDelete(db, pSubquery); |
| 5010 | return 0; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | /* |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5014 | ** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
| 5015 | ** element of the source-list passed as the second argument. |
| 5016 | */ |
| 5017 | void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 5018 | assert( pIndexedBy!=0 ); |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 5019 | if( p && pIndexedBy->n>0 ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5020 | SrcItem *pItem; |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 5021 | assert( p->nSrc>0 ); |
| 5022 | pItem = &p->a[p->nSrc-1]; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 5023 | assert( pItem->fg.notIndexed==0 ); |
| 5024 | assert( pItem->fg.isIndexedBy==0 ); |
| 5025 | assert( pItem->fg.isTabFunc==0 ); |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5026 | if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
| 5027 | /* A "NOT INDEXED" clause was supplied. See parse.y |
| 5028 | ** construct "indexed_opt" for details. */ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 5029 | pItem->fg.notIndexed = 1; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5030 | }else{ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 5031 | pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 5032 | pItem->fg.isIndexedBy = 1; |
drh | dbfbb5a | 2021-10-07 23:04:50 +0000 | [diff] [blame] | 5033 | assert( pItem->fg.isCte==0 ); /* No collision on union u2 */ |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5034 | } |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | /* |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 5039 | ** Append the contents of SrcList p2 to SrcList p1 and return the resulting |
| 5040 | ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 |
| 5041 | ** are deleted by this function. |
| 5042 | */ |
| 5043 | SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){ |
dan | 8b023cf | 2020-04-30 18:28:40 +0000 | [diff] [blame] | 5044 | assert( p1 && p1->nSrc==1 ); |
| 5045 | if( p2 ){ |
| 5046 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1); |
| 5047 | if( pNew==0 ){ |
| 5048 | sqlite3SrcListDelete(pParse->db, p2); |
| 5049 | }else{ |
| 5050 | p1 = pNew; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5051 | memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem)); |
drh | 525326e | 2020-07-15 21:53:53 +0000 | [diff] [blame] | 5052 | sqlite3DbFree(pParse->db, p2); |
drh | d973268 | 2022-04-18 10:09:29 +0000 | [diff] [blame] | 5053 | p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype); |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | return p1; |
| 5057 | } |
| 5058 | |
| 5059 | /* |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 5060 | ** Add the list of function arguments to the SrcList entry for a |
| 5061 | ** table-valued-function. |
| 5062 | */ |
| 5063 | void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 5064 | if( p ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5065 | SrcItem *pItem = &p->a[p->nSrc-1]; |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 5066 | assert( pItem->fg.notIndexed==0 ); |
| 5067 | assert( pItem->fg.isIndexedBy==0 ); |
| 5068 | assert( pItem->fg.isTabFunc==0 ); |
| 5069 | pItem->u1.pFuncArg = pList; |
| 5070 | pItem->fg.isTabFunc = 1; |
drh | d8b1bfc | 2015-08-20 23:21:34 +0000 | [diff] [blame] | 5071 | }else{ |
| 5072 | sqlite3ExprListDelete(pParse->db, pList); |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 5073 | } |
| 5074 | } |
| 5075 | |
| 5076 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 5077 | ** When building up a FROM clause in the parser, the join operator |
| 5078 | ** is initially attached to the left operand. But the code generator |
| 5079 | ** expects the join operator to be on the right operand. This routine |
| 5080 | ** Shifts all join operators from left to right for an entire FROM |
| 5081 | ** clause. |
| 5082 | ** |
| 5083 | ** Example: Suppose the join is like this: |
| 5084 | ** |
| 5085 | ** A natural cross join B |
| 5086 | ** |
| 5087 | ** The operator is "natural cross join". The A and B operands are stored |
| 5088 | ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
| 5089 | ** operator with A. This routine shifts that operator over to B. |
drh | 62ed36b | 2022-04-10 20:28:41 +0000 | [diff] [blame] | 5090 | ** |
| 5091 | ** Additional changes: |
| 5092 | ** |
| 5093 | ** * All tables to the left of the right-most RIGHT JOIN are tagged with |
| 5094 | ** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the |
| 5095 | ** code generator can easily tell that the table is part of |
| 5096 | ** the left operand of at least one RIGHT JOIN. |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 5097 | */ |
drh | fdc621a | 2022-04-16 19:13:16 +0000 | [diff] [blame] | 5098 | void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){ |
drh | 6dab33b | 2022-04-21 19:25:51 +0000 | [diff] [blame] | 5099 | (void)pParse; |
drh | 62ed36b | 2022-04-10 20:28:41 +0000 | [diff] [blame] | 5100 | if( p && p->nSrc>1 ){ |
| 5101 | int i = p->nSrc-1; |
drh | a76ac88 | 2022-04-08 19:20:12 +0000 | [diff] [blame] | 5102 | u8 allFlags = 0; |
drh | 62ed36b | 2022-04-10 20:28:41 +0000 | [diff] [blame] | 5103 | do{ |
drh | a76ac88 | 2022-04-08 19:20:12 +0000 | [diff] [blame] | 5104 | allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
drh | 62ed36b | 2022-04-10 20:28:41 +0000 | [diff] [blame] | 5105 | }while( (--i)>0 ); |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 5106 | p->a[0].fg.jointype = 0; |
drh | a76ac88 | 2022-04-08 19:20:12 +0000 | [diff] [blame] | 5107 | |
| 5108 | /* All terms to the left of a RIGHT JOIN should be tagged with the |
| 5109 | ** JT_LTORJ flags */ |
| 5110 | if( allFlags & JT_RIGHT ){ |
| 5111 | for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){} |
| 5112 | i--; |
| 5113 | assert( i>=0 ); |
| 5114 | do{ |
drh | fdc621a | 2022-04-16 19:13:16 +0000 | [diff] [blame] | 5115 | p->a[i].fg.jointype |= JT_LTORJ; |
| 5116 | }while( (--i)>=0 ); |
drh | a76ac88 | 2022-04-08 19:20:12 +0000 | [diff] [blame] | 5117 | } |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 5118 | } |
| 5119 | } |
| 5120 | |
| 5121 | /* |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 5122 | ** Generate VDBE code for a BEGIN statement. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5123 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 5124 | void sqlite3BeginTransaction(Parse *pParse, int type){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 5125 | sqlite3 *db; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5126 | Vdbe *v; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 5127 | int i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5128 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5129 | assert( pParse!=0 ); |
| 5130 | db = pParse->db; |
| 5131 | assert( db!=0 ); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5132 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ |
| 5133 | return; |
| 5134 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5135 | v = sqlite3GetVdbe(pParse); |
| 5136 | if( !v ) return; |
dan | 3d39434 | 2015-07-27 19:31:45 +0000 | [diff] [blame] | 5137 | if( type==TK_IMMEDIATE || type==TK_EXCLUSIVE ){ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 5138 | for(i=0; i<db->nDb; i++){ |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 5139 | int eTxnType; |
| 5140 | Btree *pBt = db->aDb[i].pBt; |
| 5141 | if( pBt && sqlite3BtreeIsReadonly(pBt) ){ |
| 5142 | eTxnType = 0; /* Read txn */ |
| 5143 | }else if( type==TK_EXCLUSIVE ){ |
| 5144 | eTxnType = 2; /* Exclusive txn */ |
| 5145 | }else{ |
| 5146 | eTxnType = 1; /* Write txn */ |
| 5147 | } |
| 5148 | sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5149 | sqlite3VdbeUsesBtree(v, i); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 5150 | } |
| 5151 | } |
dan | bf3cf57 | 2015-08-24 19:56:04 +0000 | [diff] [blame] | 5152 | sqlite3VdbeAddOp3(v, OP_AutoCommit, 0, 0, (type==TK_CONCURRENT)); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | /* |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5156 | ** Generate VDBE code for a COMMIT or ROLLBACK statement. |
| 5157 | ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise |
| 5158 | ** code is generated for a COMMIT. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5159 | */ |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5160 | void sqlite3EndTransaction(Parse *pParse, int eType){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5161 | Vdbe *v; |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5162 | int isRollback; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5163 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5164 | assert( pParse!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5165 | assert( pParse->db!=0 ); |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5166 | assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); |
| 5167 | isRollback = eType==TK_ROLLBACK; |
| 5168 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, |
| 5169 | isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5170 | return; |
| 5171 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5172 | v = sqlite3GetVdbe(pParse); |
| 5173 | if( v ){ |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5174 | sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 5175 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5176 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 5177 | |
| 5178 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 5179 | ** This function is called by the parser when it parses a command to create, |
| 5180 | ** release or rollback an SQL savepoint. |
| 5181 | */ |
| 5182 | void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 5183 | char *zName = sqlite3NameFromToken(pParse->db, pName); |
| 5184 | if( zName ){ |
| 5185 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 5186 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | 558814f | 2010-06-02 05:53:53 +0000 | [diff] [blame] | 5187 | static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 5188 | assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
| 5189 | #endif |
| 5190 | if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ |
| 5191 | sqlite3DbFree(pParse->db, zName); |
| 5192 | return; |
| 5193 | } |
| 5194 | sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | /* |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5199 | ** Make sure the TEMP database is open and available for use. Return |
| 5200 | ** the number of errors. Leave any error messages in the pParse structure. |
| 5201 | */ |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 5202 | int sqlite3OpenTempDatabase(Parse *pParse){ |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5203 | sqlite3 *db = pParse->db; |
| 5204 | if( db->aDb[1].pBt==0 && !pParse->explain ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5205 | int rc; |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 5206 | Btree *pBt; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5207 | static const int flags = |
| 5208 | SQLITE_OPEN_READWRITE | |
| 5209 | SQLITE_OPEN_CREATE | |
| 5210 | SQLITE_OPEN_EXCLUSIVE | |
| 5211 | SQLITE_OPEN_DELETEONCLOSE | |
| 5212 | SQLITE_OPEN_TEMP_DB; |
| 5213 | |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 5214 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5215 | if( rc!=SQLITE_OK ){ |
| 5216 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 5217 | "file for storing temporary tables"); |
| 5218 | pParse->rc = rc; |
| 5219 | return 1; |
| 5220 | } |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 5221 | db->aDb[1].pBt = pBt; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 5222 | assert( db->aDb[1].pSchema ); |
drh | e937df8 | 2020-05-07 01:56:57 +0000 | [diff] [blame] | 5223 | if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 5224 | sqlite3OomFault(db); |
drh | 7c9c986 | 2010-01-31 14:18:21 +0000 | [diff] [blame] | 5225 | return 1; |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 5226 | } |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5227 | } |
| 5228 | return 0; |
| 5229 | } |
| 5230 | |
| 5231 | /* |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 5232 | ** Record the fact that the schema cookie will need to be verified |
| 5233 | ** for database iDb. The code to actually verify the schema cookie |
| 5234 | ** will occur at the end of the top-level VDBE and will be generated |
| 5235 | ** later, by sqlite3FinishCoding(). |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5236 | */ |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5237 | static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){ |
| 5238 | assert( iDb>=0 && iDb<pToplevel->db->nDb ); |
| 5239 | assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 ); |
drh | 099b385 | 2021-03-10 16:35:37 +0000 | [diff] [blame] | 5240 | assert( iDb<SQLITE_MAX_DB ); |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5241 | assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5242 | if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ |
| 5243 | DbMaskSet(pToplevel->cookieMask, iDb); |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 5244 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 5245 | sqlite3OpenTempDatabase(pToplevel); |
| 5246 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5247 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5248 | } |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5249 | void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
| 5250 | sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb); |
| 5251 | } |
| 5252 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5253 | |
| 5254 | /* |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 5255 | ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
| 5256 | ** attached database. Otherwise, invoke it for the database named zDb only. |
| 5257 | */ |
| 5258 | void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ |
| 5259 | sqlite3 *db = pParse->db; |
| 5260 | int i; |
| 5261 | for(i=0; i<db->nDb; i++){ |
| 5262 | Db *pDb = &db->aDb[i]; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5263 | if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 5264 | sqlite3CodeVerifySchema(pParse, i); |
| 5265 | } |
| 5266 | } |
| 5267 | } |
| 5268 | |
| 5269 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 5270 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 5271 | ** might change the database. |
| 5272 | ** |
| 5273 | ** This routine starts a new transaction if we are not already within |
| 5274 | ** a transaction. If we are already within a transaction, then a checkpoint |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 5275 | ** is set if the setStatement parameter is true. A checkpoint should |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 5276 | ** be set for operations that might fail (due to a constraint) part of |
| 5277 | ** the way through and which will need to undo some writes without having to |
| 5278 | ** rollback the whole transaction. For operations where all constraints |
| 5279 | ** can be checked before any changes are made to the database, it is never |
| 5280 | ** necessary to undo a write and the checkpoint should not be set. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 5281 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 5282 | void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5283 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5284 | sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5285 | DbMaskSet(pToplevel->writeMask, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5286 | pToplevel->isMultiWrite |= setStatement; |
| 5287 | } |
| 5288 | |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 5289 | /* |
| 5290 | ** Indicate that the statement currently under construction might write |
| 5291 | ** more than one entry (example: deleting one row then inserting another, |
| 5292 | ** inserting multiple rows in a table, or inserting a row and index entries.) |
| 5293 | ** If an abort occurs after some of these writes have completed, then it will |
| 5294 | ** be necessary to undo the completed writes. |
| 5295 | */ |
| 5296 | void sqlite3MultiWrite(Parse *pParse){ |
| 5297 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5298 | pToplevel->isMultiWrite = 1; |
| 5299 | } |
| 5300 | |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5301 | /* |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 5302 | ** The code generator calls this routine if is discovers that it is |
| 5303 | ** possible to abort a statement prior to completion. In order to |
| 5304 | ** perform this abort without corrupting the database, we need to make |
| 5305 | ** sure that the statement is protected by a statement transaction. |
| 5306 | ** |
| 5307 | ** Technically, we only need to set the mayAbort flag if the |
| 5308 | ** isMultiWrite flag was previously set. There is a time dependency |
| 5309 | ** such that the abort must occur after the multiwrite. This makes |
| 5310 | ** some statements involving the REPLACE conflict resolution algorithm |
| 5311 | ** go a little faster. But taking advantage of this time dependency |
| 5312 | ** makes it more difficult to prove that the code is correct (in |
| 5313 | ** particular, it prevents us from writing an effective |
| 5314 | ** implementation of sqlite3AssertMayAbort()) and so we have chosen |
| 5315 | ** to take the safe route and skip the optimization. |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5316 | */ |
| 5317 | void sqlite3MayAbort(Parse *pParse){ |
| 5318 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5319 | pToplevel->mayAbort = 1; |
| 5320 | } |
| 5321 | |
| 5322 | /* |
| 5323 | ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
| 5324 | ** error. The onError parameter determines which (if any) of the statement |
| 5325 | ** and/or current transaction is rolled back. |
| 5326 | */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5327 | void sqlite3HaltConstraint( |
| 5328 | Parse *pParse, /* Parsing context */ |
| 5329 | int errCode, /* extended error code */ |
| 5330 | int onError, /* Constraint type */ |
| 5331 | char *p4, /* Error message */ |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5332 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 5333 | u8 p5Errmsg /* P5_ErrMsg type */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5334 | ){ |
drh | 289a0c8 | 2020-08-15 22:23:00 +0000 | [diff] [blame] | 5335 | Vdbe *v; |
| 5336 | assert( pParse->pVdbe!=0 ); |
| 5337 | v = sqlite3GetVdbe(pParse); |
drh | 9e5fdc4 | 2020-05-08 19:02:21 +0000 | [diff] [blame] | 5338 | assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5339 | if( onError==OE_Abort ){ |
| 5340 | sqlite3MayAbort(pParse); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5341 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5342 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 5343 | sqlite3VdbeChangeP5(v, p5Errmsg); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | /* |
| 5347 | ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. |
| 5348 | */ |
| 5349 | void sqlite3UniqueConstraint( |
| 5350 | Parse *pParse, /* Parsing context */ |
| 5351 | int onError, /* Constraint type */ |
| 5352 | Index *pIdx /* The index that triggers the constraint */ |
| 5353 | ){ |
| 5354 | char *zErr; |
| 5355 | int j; |
| 5356 | StrAccum errMsg; |
| 5357 | Table *pTab = pIdx->pTable; |
| 5358 | |
drh | 86ec1ed | 2019-04-10 00:58:07 +0000 | [diff] [blame] | 5359 | sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, |
| 5360 | pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5361 | if( pIdx->aColExpr ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 5362 | sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5363 | }else{ |
| 5364 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 5365 | char *zCol; |
| 5366 | assert( pIdx->aiColumn[j]>=0 ); |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 5367 | zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName; |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 5368 | if( j ) sqlite3_str_append(&errMsg, ", ", 2); |
| 5369 | sqlite3_str_appendall(&errMsg, pTab->zName); |
| 5370 | sqlite3_str_append(&errMsg, ".", 1); |
| 5371 | sqlite3_str_appendall(&errMsg, zCol); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5372 | } |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5373 | } |
| 5374 | zErr = sqlite3StrAccumFinish(&errMsg); |
| 5375 | sqlite3HaltConstraint(pParse, |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 5376 | IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
| 5377 | : SQLITE_CONSTRAINT_UNIQUE, |
dan | 93889d9 | 2013-11-06 16:28:59 +0000 | [diff] [blame] | 5378 | onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | |
| 5382 | /* |
| 5383 | ** Code an OP_Halt due to non-unique rowid. |
| 5384 | */ |
| 5385 | void sqlite3RowidConstraint( |
| 5386 | Parse *pParse, /* Parsing context */ |
| 5387 | int onError, /* Conflict resolution algorithm */ |
| 5388 | Table *pTab /* The table with the non-unique rowid */ |
| 5389 | ){ |
| 5390 | char *zMsg; |
| 5391 | int rc; |
| 5392 | if( pTab->iPKey>=0 ){ |
| 5393 | zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 5394 | pTab->aCol[pTab->iPKey].zCnName); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5395 | rc = SQLITE_CONSTRAINT_PRIMARYKEY; |
| 5396 | }else{ |
| 5397 | zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); |
| 5398 | rc = SQLITE_CONSTRAINT_ROWID; |
| 5399 | } |
| 5400 | sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, |
| 5401 | P5_ConstraintUnique); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5404 | /* |
| 5405 | ** Check to see if pIndex uses the collating sequence pColl. Return |
| 5406 | ** true if it does and false if it does not. |
| 5407 | */ |
| 5408 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5409 | static int collationMatch(const char *zColl, Index *pIndex){ |
| 5410 | int i; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 5411 | assert( zColl!=0 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5412 | for(i=0; i<pIndex->nColumn; i++){ |
| 5413 | const char *z = pIndex->azColl[i]; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5414 | assert( z!=0 || pIndex->aiColumn[i]<0 ); |
| 5415 | if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5416 | return 1; |
| 5417 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5418 | } |
| 5419 | return 0; |
| 5420 | } |
| 5421 | #endif |
| 5422 | |
| 5423 | /* |
| 5424 | ** Recompute all indices of pTab that use the collating sequence pColl. |
| 5425 | ** If pColl==0 then recompute all indices of pTab. |
| 5426 | */ |
| 5427 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5428 | static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
dan | e6370e9 | 2019-01-11 17:41:23 +0000 | [diff] [blame] | 5429 | if( !IsVirtual(pTab) ){ |
| 5430 | Index *pIndex; /* An index associated with pTab */ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5431 | |
dan | e6370e9 | 2019-01-11 17:41:23 +0000 | [diff] [blame] | 5432 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 5433 | if( zColl==0 || collationMatch(zColl, pIndex) ){ |
| 5434 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 5435 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5436 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5437 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5438 | } |
| 5439 | } |
| 5440 | } |
| 5441 | #endif |
| 5442 | |
| 5443 | /* |
| 5444 | ** Recompute all indices of all tables in all databases where the |
| 5445 | ** indices use the collating sequence pColl. If pColl==0 then recompute |
| 5446 | ** all indices everywhere. |
| 5447 | */ |
| 5448 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5449 | static void reindexDatabases(Parse *pParse, char const *zColl){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5450 | Db *pDb; /* A single database */ |
| 5451 | int iDb; /* The database index number */ |
| 5452 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5453 | HashElem *k; /* For looping over tables in pDb */ |
| 5454 | Table *pTab; /* A table in the database */ |
| 5455 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 5456 | assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5457 | for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 5458 | assert( pDb!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5459 | for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5460 | pTab = (Table*)sqliteHashData(k); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5461 | reindexTable(pParse, pTab, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | } |
| 5465 | #endif |
| 5466 | |
| 5467 | /* |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 5468 | ** Generate code for the REINDEX command. |
| 5469 | ** |
| 5470 | ** REINDEX -- 1 |
| 5471 | ** REINDEX <collation> -- 2 |
| 5472 | ** REINDEX ?<database>.?<tablename> -- 3 |
| 5473 | ** REINDEX ?<database>.?<indexname> -- 4 |
| 5474 | ** |
| 5475 | ** Form 1 causes all indices in all attached databases to be rebuilt. |
| 5476 | ** Form 2 rebuilds all indices in all databases that use the named |
| 5477 | ** collating function. Forms 3 and 4 rebuild the named index or all |
| 5478 | ** indices associated with the named table. |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5479 | */ |
| 5480 | #ifndef SQLITE_OMIT_REINDEX |
| 5481 | void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
| 5482 | CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
| 5483 | char *z; /* Name of a table or index */ |
| 5484 | const char *zDb; /* Name of the database */ |
| 5485 | Table *pTab; /* A table in the database */ |
| 5486 | Index *pIndex; /* An index associated with pTab */ |
| 5487 | int iDb; /* The database index number */ |
| 5488 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5489 | Token *pObjName; /* Name of the table or index to be reindexed */ |
| 5490 | |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 5491 | /* Read the database schema. If an error occurs, leave an error message |
| 5492 | ** and code in pParse and return NULL. */ |
| 5493 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e63739a | 2005-01-27 00:33:37 +0000 | [diff] [blame] | 5494 | return; |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 5497 | if( pName1==0 ){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5498 | reindexDatabases(pParse, 0); |
| 5499 | return; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5500 | }else if( NEVER(pName2==0) || pName2->z==0 ){ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 5501 | char *zColl; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5502 | assert( pName1->z ); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 5503 | zColl = sqlite3NameFromToken(pParse->db, pName1); |
| 5504 | if( !zColl ) return; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 5505 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5506 | if( pColl ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5507 | reindexDatabases(pParse, zColl); |
| 5508 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5509 | return; |
| 5510 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5511 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5512 | } |
| 5513 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 5514 | if( iDb<0 ) return; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5515 | z = sqlite3NameFromToken(db, pObjName); |
drh | 84f3112 | 2007-05-12 15:00:14 +0000 | [diff] [blame] | 5516 | if( z==0 ) return; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5517 | zDb = db->aDb[iDb].zDbSName; |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5518 | pTab = sqlite3FindTable(db, z, zDb); |
| 5519 | if( pTab ){ |
| 5520 | reindexTable(pParse, pTab, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5521 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5522 | return; |
| 5523 | } |
| 5524 | pIndex = sqlite3FindIndex(db, z, zDb); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5525 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5526 | if( pIndex ){ |
| 5527 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5528 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5529 | return; |
| 5530 | } |
| 5531 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| 5532 | } |
| 5533 | #endif |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5534 | |
| 5535 | /* |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5536 | ** Return a KeyInfo structure that is appropriate for the given Index. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5537 | ** |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5538 | ** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
| 5539 | ** when it has finished using it. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5540 | */ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5541 | KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5542 | int i; |
| 5543 | int nCol = pIdx->nColumn; |
| 5544 | int nKey = pIdx->nKeyCol; |
| 5545 | KeyInfo *pKey; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5546 | if( pParse->nErr ) return 0; |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5547 | if( pIdx->uniqNotNull ){ |
| 5548 | pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); |
| 5549 | }else{ |
| 5550 | pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 5551 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5552 | if( pKey ){ |
| 5553 | assert( sqlite3KeyInfoIsWriteable(pKey) ); |
| 5554 | for(i=0; i<nCol; i++){ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 5555 | const char *zColl = pIdx->azColl[i]; |
| 5556 | pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5557 | sqlite3LocateCollSeq(pParse, zColl); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 5558 | pKey->aSortFlags[i] = pIdx->aSortOrder[i]; |
| 5559 | assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5560 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5561 | if( pParse->nErr ){ |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 5562 | assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); |
| 5563 | if( pIdx->bNoQuery==0 ){ |
| 5564 | /* Deactivate the index because it contains an unknown collating |
| 5565 | ** sequence. The only way to reactive the index is to reload the |
| 5566 | ** schema. Adding the missing collating sequence later does not |
| 5567 | ** reactive the index. The application had the chance to register |
| 5568 | ** the missing index using the collation-needed callback. For |
| 5569 | ** simplicity, SQLite will not give the application a second chance. |
| 5570 | */ |
| 5571 | pIdx->bNoQuery = 1; |
| 5572 | pParse->rc = SQLITE_ERROR_RETRY; |
| 5573 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5574 | sqlite3KeyInfoUnref(pKey); |
| 5575 | pKey = 0; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5576 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5577 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5578 | return pKey; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5579 | } |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5580 | |
| 5581 | #ifndef SQLITE_OMIT_CTE |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5582 | /* |
| 5583 | ** Create a new CTE object |
| 5584 | */ |
| 5585 | Cte *sqlite3CteNew( |
| 5586 | Parse *pParse, /* Parsing context */ |
| 5587 | Token *pName, /* Name of the common-table */ |
| 5588 | ExprList *pArglist, /* Optional column name list for the table */ |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5589 | Select *pQuery, /* Query used to initialize the table */ |
| 5590 | u8 eM10d /* The MATERIALIZED flag */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5591 | ){ |
| 5592 | Cte *pNew; |
| 5593 | sqlite3 *db = pParse->db; |
| 5594 | |
| 5595 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew)); |
| 5596 | assert( pNew!=0 || db->mallocFailed ); |
| 5597 | |
| 5598 | if( db->mallocFailed ){ |
| 5599 | sqlite3ExprListDelete(db, pArglist); |
| 5600 | sqlite3SelectDelete(db, pQuery); |
| 5601 | }else{ |
| 5602 | pNew->pSelect = pQuery; |
| 5603 | pNew->pCols = pArglist; |
| 5604 | pNew->zName = sqlite3NameFromToken(pParse->db, pName); |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5605 | pNew->eM10d = eM10d; |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5606 | } |
| 5607 | return pNew; |
| 5608 | } |
| 5609 | |
| 5610 | /* |
| 5611 | ** Clear information from a Cte object, but do not deallocate storage |
| 5612 | ** for the object itself. |
| 5613 | */ |
| 5614 | static void cteClear(sqlite3 *db, Cte *pCte){ |
| 5615 | assert( pCte!=0 ); |
| 5616 | sqlite3ExprListDelete(db, pCte->pCols); |
| 5617 | sqlite3SelectDelete(db, pCte->pSelect); |
| 5618 | sqlite3DbFree(db, pCte->zName); |
| 5619 | } |
| 5620 | |
| 5621 | /* |
| 5622 | ** Free the contents of the CTE object passed as the second argument. |
| 5623 | */ |
| 5624 | void sqlite3CteDelete(sqlite3 *db, Cte *pCte){ |
| 5625 | assert( pCte!=0 ); |
| 5626 | cteClear(db, pCte); |
| 5627 | sqlite3DbFree(db, pCte); |
| 5628 | } |
| 5629 | |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5630 | /* |
| 5631 | ** This routine is invoked once per CTE by the parser while parsing a |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5632 | ** WITH clause. The CTE described by teh third argument is added to |
| 5633 | ** the WITH clause of the second argument. If the second argument is |
| 5634 | ** NULL, then a new WITH argument is created. |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5635 | */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5636 | With *sqlite3WithAdd( |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5637 | Parse *pParse, /* Parsing context */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5638 | With *pWith, /* Existing WITH clause, or NULL */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5639 | Cte *pCte /* CTE to add to the WITH clause */ |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5640 | ){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5641 | sqlite3 *db = pParse->db; |
| 5642 | With *pNew; |
| 5643 | char *zName; |
| 5644 | |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5645 | if( pCte==0 ){ |
| 5646 | return pWith; |
| 5647 | } |
| 5648 | |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5649 | /* Check that the CTE name is unique within this WITH clause. If |
| 5650 | ** not, store an error in the Parse structure. */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5651 | zName = pCte->zName; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5652 | if( zName && pWith ){ |
| 5653 | int i; |
| 5654 | for(i=0; i<pWith->nCte; i++){ |
| 5655 | if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ |
drh | 727a99f | 2014-01-16 21:59:51 +0000 | [diff] [blame] | 5656 | sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5657 | } |
| 5658 | } |
| 5659 | } |
| 5660 | |
| 5661 | if( pWith ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 5662 | sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5663 | pNew = sqlite3DbRealloc(db, pWith, nByte); |
| 5664 | }else{ |
| 5665 | pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); |
| 5666 | } |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 5667 | assert( (pNew!=0 && zName!=0) || db->mallocFailed ); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5668 | |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 5669 | if( db->mallocFailed ){ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5670 | sqlite3CteDelete(db, pCte); |
dan | a9f5c13 | 2014-01-13 16:36:40 +0000 | [diff] [blame] | 5671 | pNew = pWith; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5672 | }else{ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5673 | pNew->a[pNew->nCte++] = *pCte; |
| 5674 | sqlite3DbFree(db, pCte); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
| 5677 | return pNew; |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5680 | /* |
| 5681 | ** Free the contents of the With object passed as the second argument. |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5682 | */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5683 | void sqlite3WithDelete(sqlite3 *db, With *pWith){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5684 | if( pWith ){ |
| 5685 | int i; |
| 5686 | for(i=0; i<pWith->nCte; i++){ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5687 | cteClear(db, &pWith->a[i]); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5688 | } |
| 5689 | sqlite3DbFree(db, pWith); |
| 5690 | } |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5691 | } |
| 5692 | #endif /* !defined(SQLITE_OMIT_CTE) */ |