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 | ){ |
| 92 | if( iDb==1 ) return; |
| 93 | if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; |
| 94 | lockTable(pParse, iDb, iTab, isWriteLock, zName); |
| 95 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | ** Code an OP_TableLock instruction for each table locked by the |
| 99 | ** statement (configured by calls to sqlite3TableLock()). |
| 100 | */ |
| 101 | static void codeTableLocks(Parse *pParse){ |
| 102 | int i; |
drh | f0b4174 | 2020-08-15 21:55:14 +0000 | [diff] [blame] | 103 | Vdbe *pVdbe = pParse->pVdbe; |
drh | 289a0c8 | 2020-08-15 22:23:00 +0000 | [diff] [blame] | 104 | assert( pVdbe!=0 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 105 | |
| 106 | for(i=0; i<pParse->nTableLock; i++){ |
| 107 | TableLock *p = &pParse->aTableLock[i]; |
| 108 | int p1 = p->iDb; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 109 | sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 110 | p->zLockName, P4_STATIC); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | #else |
| 114 | #define codeTableLocks(x) |
| 115 | #endif |
| 116 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 117 | /* |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 118 | ** Return TRUE if the given yDbMask object is empty - if it contains no |
| 119 | ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() |
| 120 | ** macros when SQLITE_MAX_ATTACHED is greater than 30. |
| 121 | */ |
| 122 | #if SQLITE_MAX_ATTACHED>30 |
| 123 | int sqlite3DbMaskAllZero(yDbMask m){ |
| 124 | int i; |
| 125 | for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; |
| 126 | return 1; |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 131 | ** This routine is called after a single SQL statement has been |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 132 | ** parsed and a VDBE program to execute that statement has been |
| 133 | ** prepared. This routine puts the finishing touches on the |
| 134 | ** VDBE program and resets the pParse structure for the next |
| 135 | ** parse. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 136 | ** |
| 137 | ** Note that if an error occurred, it might be the case that |
| 138 | ** no VDBE code was generated. |
| 139 | */ |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 140 | void sqlite3FinishCoding(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 141 | sqlite3 *db; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 142 | Vdbe *v; |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 143 | |
dan | f78baaf | 2012-12-06 19:37:22 +0000 | [diff] [blame] | 144 | assert( pParse->pToplevel==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 145 | db = pParse->db; |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 146 | if( pParse->nested ) return; |
drh | d99d283 | 2015-04-17 15:58:33 +0000 | [diff] [blame] | 147 | if( db->mallocFailed || pParse->nErr ){ |
| 148 | if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; |
| 149 | return; |
| 150 | } |
danielk1977 | 48d0d86 | 2005-02-01 03:09:52 +0000 | [diff] [blame] | 151 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 152 | /* Begin by generating some termination code at the end of the |
| 153 | ** vdbe program |
| 154 | */ |
drh | 02c4aa3 | 2021-02-04 13:44:42 +0000 | [diff] [blame] | 155 | v = pParse->pVdbe; |
| 156 | if( v==0 ){ |
| 157 | if( db->init.busy ){ |
| 158 | pParse->rc = SQLITE_DONE; |
| 159 | return; |
| 160 | } |
| 161 | v = sqlite3GetVdbe(pParse); |
| 162 | if( v==0 ) pParse->rc = SQLITE_ERROR; |
drh | c8af879 | 2021-01-01 22:06:17 +0000 | [diff] [blame] | 163 | } |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 164 | assert( !pParse->isMultiWrite |
| 165 | || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 166 | if( v ){ |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 167 | if( pParse->bReturning ){ |
| 168 | Returning *pReturning = pParse->u1.pReturning; |
| 169 | int addrRewind; |
| 170 | int i; |
| 171 | int reg; |
| 172 | |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 173 | addrRewind = |
| 174 | sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur); |
drh | 6859d32 | 2021-02-18 01:02:03 +0000 | [diff] [blame] | 175 | VdbeCoverage(v); |
drh | 552562c | 2021-02-04 20:52:20 +0000 | [diff] [blame] | 176 | reg = pReturning->iRetReg; |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 177 | for(i=0; i<pReturning->nRetCol; i++){ |
| 178 | sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i); |
| 179 | } |
| 180 | sqlite3VdbeAddOp2(v, OP_ResultRow, reg, i); |
| 181 | sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1); |
drh | 6859d32 | 2021-02-18 01:02:03 +0000 | [diff] [blame] | 182 | VdbeCoverage(v); |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 183 | sqlite3VdbeJumpHere(v, addrRewind); |
| 184 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 185 | sqlite3VdbeAddOp0(v, OP_Halt); |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 186 | |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 187 | #if SQLITE_USER_AUTHENTICATION |
| 188 | if( pParse->nTableLock>0 && db->init.busy==0 ){ |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 189 | sqlite3UserAuthInit(db); |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 190 | if( db->auth.authLevel<UAUTH_User ){ |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 191 | sqlite3ErrorMsg(pParse, "user not authenticated"); |
drh | 6ae3ab0 | 2016-08-23 14:42:15 +0000 | [diff] [blame] | 192 | pParse->rc = SQLITE_AUTH_USER; |
drh | 7883ecf | 2014-09-11 16:19:31 +0000 | [diff] [blame] | 193 | return; |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | #endif |
| 197 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 198 | /* The cookie mask contains one bit for each database file open. |
| 199 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 200 | ** set for each database that is used. Generate code to start a |
| 201 | ** transaction on each used database and to verify the schema cookie |
| 202 | ** on each used database. |
| 203 | */ |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 204 | if( db->mallocFailed==0 |
| 205 | && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr) |
| 206 | ){ |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 207 | int iDb, i; |
| 208 | assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 209 | sqlite3VdbeJumpHere(v, 0); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 210 | for(iDb=0; iDb<db->nDb; iDb++){ |
drh | 1d96cc6 | 2016-09-30 18:35:36 +0000 | [diff] [blame] | 211 | Schema *pSchema; |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 212 | if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 213 | sqlite3VdbeUsesBtree(v, iDb); |
drh | 1d96cc6 | 2016-09-30 18:35:36 +0000 | [diff] [blame] | 214 | pSchema = db->aDb[iDb].pSchema; |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 215 | sqlite3VdbeAddOp4Int(v, |
| 216 | OP_Transaction, /* Opcode */ |
| 217 | iDb, /* P1 */ |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 218 | DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
drh | 1d96cc6 | 2016-09-30 18:35:36 +0000 | [diff] [blame] | 219 | pSchema->schema_cookie, /* P3 */ |
| 220 | pSchema->iGeneration /* P4 */ |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 221 | ); |
| 222 | if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
dan | 076e0f9 | 2015-09-28 15:20:58 +0000 | [diff] [blame] | 223 | VdbeComment((v, |
| 224 | "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 225 | } |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 226 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 227 | for(i=0; i<pParse->nVtabLock; i++){ |
| 228 | char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 229 | sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 230 | } |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 231 | pParse->nVtabLock = 0; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 +0000 | [diff] [blame] | 232 | #endif |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 233 | |
| 234 | /* Once all the cookies have been verified and transactions opened, |
| 235 | ** obtain the required table-locks. This is a no-op unless the |
| 236 | ** shared-cache feature is enabled. |
| 237 | */ |
| 238 | codeTableLocks(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 239 | |
| 240 | /* Initialize any AUTOINCREMENT data structures required. |
| 241 | */ |
| 242 | sqlite3AutoincrementBegin(pParse); |
| 243 | |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 244 | /* Code constant expressions that where factored out of inner loops. |
| 245 | ** |
| 246 | ** The pConstExpr list might also contain expressions that we simply |
| 247 | ** want to keep around until the Parse object is deleted. Such |
| 248 | ** expressions have iConstExprReg==0. Do not generate code for |
| 249 | ** those expressions, of course. |
| 250 | */ |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 251 | if( pParse->pConstExpr ){ |
| 252 | ExprList *pEL = pParse->pConstExpr; |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 253 | pParse->okConstFactor = 0; |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 254 | for(i=0; i<pEL->nExpr; i++){ |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 255 | int iReg = pEL->a[i].u.iConstExprReg; |
| 256 | if( iReg>0 ){ |
| 257 | sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg); |
| 258 | } |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 262 | if( pParse->bReturning ){ |
| 263 | Returning *pRet = pParse->u1.pReturning; |
| 264 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol); |
| 265 | } |
| 266 | |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 267 | /* Finally, jump back to the beginning of the executable code. */ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 268 | sqlite3VdbeGoto(v, 1); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 269 | } |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 270 | } |
| 271 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 272 | /* Get the VDBE program ready for execution |
| 273 | */ |
drh | 126a6e2 | 2015-04-15 04:10:50 +0000 | [diff] [blame] | 274 | if( v && pParse->nErr==0 && !db->mallocFailed ){ |
drh | 3492dd7 | 2009-09-14 23:47:24 +0000 | [diff] [blame] | 275 | /* A minimum of one cursor is required if autoincrement is used |
| 276 | * See ticket [a696379c1f08866] */ |
drh | 04ab586 | 2018-12-01 21:13:41 +0000 | [diff] [blame] | 277 | assert( pParse->pAinc==0 || pParse->nTab>0 ); |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 278 | sqlite3VdbeMakeReady(v, pParse); |
danielk1977 | 441daf6 | 2005-02-01 03:46:43 +0000 | [diff] [blame] | 279 | pParse->rc = SQLITE_DONE; |
drh | e294da0 | 2010-02-25 23:44:15 +0000 | [diff] [blame] | 280 | }else{ |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 281 | pParse->rc = SQLITE_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | /* |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 286 | ** Run the parser and code generator recursively in order to generate |
| 287 | ** code for the SQL statement given onto the end of the pParse context |
| 288 | ** currently under construction. When the parser is run recursively |
| 289 | ** this way, the final OP_Halt is not appended and other initialization |
| 290 | ** and finalization steps are omitted because those are handling by the |
| 291 | ** outermost parser. |
| 292 | ** |
| 293 | ** Not everything is nestable. This facility is designed to permit |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 294 | ** INSERT, UPDATE, and DELETE operations against the schema table. Use |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 295 | ** care if you decide to try to use this routine for some other purposes. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 296 | */ |
| 297 | void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 298 | va_list ap; |
| 299 | char *zSql; |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 300 | char *zErrMsg = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 301 | sqlite3 *db = pParse->db; |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 302 | char saveBuf[PARSE_TAIL_SZ]; |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 303 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 304 | if( pParse->nErr ) return; |
| 305 | assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
| 306 | va_start(ap, zFormat); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 307 | zSql = sqlite3VMPrintf(db, zFormat, ap); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 308 | va_end(ap); |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 309 | if( zSql==0 ){ |
drh | 480c572 | 2019-02-22 16:18:12 +0000 | [diff] [blame] | 310 | /* This can result either from an OOM or because the formatted string |
| 311 | ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set |
| 312 | ** an error */ |
| 313 | if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG; |
drh | a2b6806 | 2019-03-28 04:03:17 +0000 | [diff] [blame] | 314 | pParse->nErr++; |
drh | 480c572 | 2019-02-22 16:18:12 +0000 | [diff] [blame] | 315 | return; |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 316 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 317 | pParse->nested++; |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 318 | memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 319 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 320 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 321 | sqlite3DbFree(db, zErrMsg); |
| 322 | sqlite3DbFree(db, zSql); |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 323 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 324 | pParse->nested--; |
| 325 | } |
| 326 | |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 327 | #if SQLITE_USER_AUTHENTICATION |
| 328 | /* |
| 329 | ** Return TRUE if zTable is the name of the system table that stores the |
| 330 | ** list of users and their access credentials. |
| 331 | */ |
| 332 | int sqlite3UserAuthTable(const char *zTable){ |
| 333 | return sqlite3_stricmp(zTable, "sqlite_user")==0; |
| 334 | } |
| 335 | #endif |
| 336 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 337 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 338 | ** Locate the in-memory structure that describes a particular database |
| 339 | ** table given the name of that table and (optionally) the name of the |
| 340 | ** database containing the table. Return NULL if not found. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 341 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 342 | ** If zDatabase is 0, all databases are searched for the table and the |
| 343 | ** first matching table is returned. (No checking for duplicate table |
| 344 | ** names is done.) The search order is TEMP first, then MAIN, then any |
| 345 | ** auxiliary databases added using the ATTACH command. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 346 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 347 | ** See also sqlite3LocateTable(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 348 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 349 | Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 350 | Table *p = 0; |
| 351 | int i; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 352 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 353 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 354 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 355 | #if SQLITE_USER_AUTHENTICATION |
| 356 | /* Only the admin user is allowed to know that the sqlite_user table |
| 357 | ** exists */ |
drh | e933b83 | 2014-09-10 17:34:28 +0000 | [diff] [blame] | 358 | if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 359 | return 0; |
| 360 | } |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 361 | #endif |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 362 | if( zDatabase ){ |
| 363 | for(i=0; i<db->nDb; i++){ |
| 364 | if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 365 | } |
| 366 | if( i>=db->nDb ){ |
| 367 | /* No match against the official names. But always match "main" |
| 368 | ** to schema 0 as a legacy fallback. */ |
| 369 | if( sqlite3StrICmp(zDatabase,"main")==0 ){ |
| 370 | i = 0; |
| 371 | }else{ |
| 372 | return 0; |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 373 | } |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 374 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 375 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 376 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 377 | if( i==1 ){ |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 378 | if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 |
| 379 | || sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 |
| 380 | || sqlite3StrICmp(zName+7, &DFLT_SCHEMA_TABLE[7])==0 |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 381 | ){ |
| 382 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
| 383 | DFLT_TEMP_SCHEMA_TABLE); |
| 384 | } |
| 385 | }else{ |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 386 | if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 387 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, |
| 388 | DFLT_SCHEMA_TABLE); |
| 389 | } |
| 390 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 391 | } |
| 392 | }else{ |
| 393 | /* Match against TEMP first */ |
| 394 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); |
| 395 | if( p ) return p; |
| 396 | /* The main database is second */ |
| 397 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); |
| 398 | if( p ) return p; |
| 399 | /* Attached databases are in order of attachment */ |
| 400 | for(i=2; i<db->nDb; i++){ |
| 401 | assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 402 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 403 | if( p ) break; |
| 404 | } |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 405 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 406 | if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 407 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, DFLT_SCHEMA_TABLE); |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 408 | }else if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 409 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
| 410 | DFLT_TEMP_SCHEMA_TABLE); |
| 411 | } |
| 412 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 413 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 414 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 418 | ** Locate the in-memory structure that describes a particular database |
| 419 | ** table given the name of that table and (optionally) the name of the |
| 420 | ** database containing the table. Return NULL if not found. Also leave an |
| 421 | ** error message in pParse->zErrMsg. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 422 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 423 | ** The difference between this routine and sqlite3FindTable() is that this |
| 424 | ** routine leaves an error message in pParse->zErrMsg where |
| 425 | ** sqlite3FindTable() does not. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 426 | */ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 427 | Table *sqlite3LocateTable( |
| 428 | Parse *pParse, /* context in which to report errors */ |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 429 | u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 430 | const char *zName, /* Name of the table we are looking for */ |
| 431 | const char *zDbase /* Name of the database. Might be NULL */ |
| 432 | ){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 433 | Table *p; |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 434 | sqlite3 *db = pParse->db; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 435 | |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 436 | /* Read the database schema. If an error occurs, leave an error message |
| 437 | ** and code in pParse and return NULL. */ |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 438 | if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 |
| 439 | && SQLITE_OK!=sqlite3ReadSchema(pParse) |
| 440 | ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 444 | p = sqlite3FindTable(db, zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 445 | if( p==0 ){ |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 446 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 447 | /* If zName is the not the name of a table in the schema created using |
| 448 | ** CREATE, then check to see if it is the name of an virtual table that |
| 449 | ** can be an eponymous virtual table. */ |
dan | bb0eec4 | 2021-04-26 14:09:48 +0000 | [diff] [blame] | 450 | if( pParse->disableVtab==0 && db->init.busy==0 ){ |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 451 | Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 452 | if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 453 | pMod = sqlite3PragmaVtabRegister(db, zName); |
| 454 | } |
| 455 | if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
dan | bd24e8f | 2021-07-08 18:29:25 +0000 | [diff] [blame] | 456 | testcase( pMod->pEpoTab==0 ); |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 457 | return pMod->pEpoTab; |
| 458 | } |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 459 | } |
| 460 | #endif |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 461 | if( flags & LOCATE_NOERR ) return 0; |
| 462 | pParse->checkSchema = 1; |
| 463 | }else if( IsVirtual(p) && pParse->disableVtab ){ |
| 464 | p = 0; |
| 465 | } |
| 466 | |
| 467 | if( p==0 ){ |
| 468 | const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| 469 | if( zDbase ){ |
| 470 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
| 471 | }else{ |
| 472 | sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 473 | } |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 474 | }else{ |
| 475 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 476 | } |
dan | fab1d40 | 2015-11-26 15:51:55 +0000 | [diff] [blame] | 477 | |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 478 | return p; |
| 479 | } |
| 480 | |
| 481 | /* |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 482 | ** Locate the table identified by *p. |
| 483 | ** |
| 484 | ** This is a wrapper around sqlite3LocateTable(). The difference between |
| 485 | ** sqlite3LocateTable() and this function is that this function restricts |
| 486 | ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
| 487 | ** non-NULL if it is part of a view or trigger program definition. See |
| 488 | ** sqlite3FixSrcList() for details. |
| 489 | */ |
| 490 | Table *sqlite3LocateTableItem( |
| 491 | Parse *pParse, |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 492 | u32 flags, |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 493 | SrcItem *p |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 494 | ){ |
| 495 | const char *zDb; |
drh | cd1499f | 2021-05-20 00:44:04 +0000 | [diff] [blame] | 496 | assert( p->pSchema==0 || p->zDatabase==0 ); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 497 | if( p->pSchema ){ |
| 498 | int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 499 | zDb = pParse->db->aDb[iDb].zDbSName; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 500 | }else{ |
| 501 | zDb = p->zDatabase; |
| 502 | } |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 503 | return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 507 | ** Locate the in-memory structure that describes |
| 508 | ** a particular index given the name of that index |
| 509 | ** and the name of the database that contains the index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 510 | ** Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 511 | ** |
| 512 | ** If zDatabase is 0, all databases are searched for the |
| 513 | ** table and the first matching index is returned. (No checking |
| 514 | ** for duplicate index names is done.) The search order is |
| 515 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 516 | ** using the ATTACH command. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 517 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 518 | Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 519 | Index *p = 0; |
| 520 | int i; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 521 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 522 | assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 523 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 524 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 525 | Schema *pSchema = db->aDb[j].pSchema; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 526 | assert( pSchema ); |
dan | 465c2b8 | 2020-03-21 15:10:40 +0000 | [diff] [blame] | 527 | if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 528 | assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 529 | p = sqlite3HashFind(&pSchema->idxHash, zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 530 | if( p ) break; |
| 531 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 532 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 536 | ** Reclaim the memory used by an index |
| 537 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 538 | void sqlite3FreeIndex(sqlite3 *db, Index *p){ |
drh | 92aa5ea | 2009-09-11 14:05:06 +0000 | [diff] [blame] | 539 | #ifndef SQLITE_OMIT_ANALYZE |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 540 | sqlite3DeleteIndexSamples(db, p); |
drh | 92aa5ea | 2009-09-11 14:05:06 +0000 | [diff] [blame] | 541 | #endif |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 542 | sqlite3ExprDelete(db, p->pPartIdxWhere); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 543 | sqlite3ExprListDelete(db, p->aColExpr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 544 | sqlite3DbFree(db, p->zColAff); |
mistachkin | 5905f86 | 2015-12-31 19:04:42 +0000 | [diff] [blame] | 545 | if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); |
drh | 175b8f0 | 2019-08-08 15:24:17 +0000 | [diff] [blame] | 546 | #ifdef SQLITE_ENABLE_STAT4 |
drh | 75b170b | 2014-10-04 00:07:44 +0000 | [diff] [blame] | 547 | sqlite3_free(p->aiRowEst); |
| 548 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 549 | sqlite3DbFree(db, p); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 553 | ** For the index called zIdxName which is found in the database iDb, |
| 554 | ** unlike that index from its Table then remove the index from |
| 555 | ** the index hash table and free all memory structures associated |
| 556 | ** with the index. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 557 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 558 | void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 559 | Index *pIndex; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 560 | Hash *pHash; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 561 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 562 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 563 | pHash = &db->aDb[iDb].pSchema->idxHash; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 564 | pIndex = sqlite3HashInsert(pHash, zIdxName, 0); |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 565 | if( ALWAYS(pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 566 | if( pIndex->pTable->pIndex==pIndex ){ |
| 567 | pIndex->pTable->pIndex = pIndex->pNext; |
| 568 | }else{ |
| 569 | Index *p; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 570 | /* Justification of ALWAYS(); The index must be on the list of |
| 571 | ** indices. */ |
| 572 | p = pIndex->pTable->pIndex; |
| 573 | while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
| 574 | if( ALWAYS(p && p->pNext==pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 575 | p->pNext = pIndex->pNext; |
| 576 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 577 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 578 | sqlite3FreeIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 579 | } |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 580 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 584 | ** Look through the list of open database files in db->aDb[] and if |
| 585 | ** any have been closed, remove them from the list. Reallocate the |
| 586 | ** db->aDb[] structure to a smaller size, if possible. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 587 | ** |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 588 | ** Entry 0 (the "main" database) and entry 1 (the "temp" database) |
| 589 | ** are never candidates for being collapsed. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 590 | */ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 591 | void sqlite3CollapseDatabaseArray(sqlite3 *db){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 592 | int i, j; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 593 | for(i=j=2; i<db->nDb; i++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 594 | struct Db *pDb = &db->aDb[i]; |
| 595 | if( pDb->pBt==0 ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 596 | sqlite3DbFree(db, pDb->zDbSName); |
| 597 | pDb->zDbSName = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 598 | continue; |
| 599 | } |
| 600 | if( j<i ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 601 | db->aDb[j] = db->aDb[i]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 602 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 603 | j++; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 604 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 605 | db->nDb = j; |
| 606 | if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 607 | memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 608 | sqlite3DbFree(db, db->aDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 609 | db->aDb = db->aDbStatic; |
| 610 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 614 | ** Reset the schema for the database at index iDb. Also reset the |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 615 | ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. |
| 616 | ** Deferred resets may be run by calling with iDb<0. |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 617 | */ |
| 618 | void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 619 | int i; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 620 | assert( iDb<db->nDb ); |
| 621 | |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 622 | if( iDb>=0 ){ |
| 623 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 624 | DbSetProperty(db, iDb, DB_ResetWanted); |
| 625 | DbSetProperty(db, 1, DB_ResetWanted); |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 626 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 627 | } |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 628 | |
| 629 | if( db->nSchemaLock==0 ){ |
| 630 | for(i=0; i<db->nDb; i++){ |
| 631 | if( DbHasProperty(db, i, DB_ResetWanted) ){ |
| 632 | sqlite3SchemaClear(db->aDb[i].pSchema); |
| 633 | } |
| 634 | } |
| 635 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | ** Erase all schema information from all attached databases (including |
| 640 | ** "main" and "temp") for a single database connection. |
| 641 | */ |
| 642 | void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ |
| 643 | int i; |
| 644 | sqlite3BtreeEnterAll(db); |
| 645 | for(i=0; i<db->nDb; i++){ |
| 646 | Db *pDb = &db->aDb[i]; |
| 647 | if( pDb->pSchema ){ |
dan | 63e50b9 | 2018-11-27 19:47:55 +0000 | [diff] [blame] | 648 | if( db->nSchemaLock==0 ){ |
| 649 | sqlite3SchemaClear(pDb->pSchema); |
| 650 | }else{ |
| 651 | DbSetProperty(db, i, DB_ResetWanted); |
| 652 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 655 | db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 656 | sqlite3VtabUnlockList(db); |
| 657 | sqlite3BtreeLeaveAll(db); |
dan | 63e50b9 | 2018-11-27 19:47:55 +0000 | [diff] [blame] | 658 | if( db->nSchemaLock==0 ){ |
| 659 | sqlite3CollapseDatabaseArray(db); |
| 660 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 664 | ** This routine is called when a commit occurs. |
| 665 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 666 | void sqlite3CommitInternalChanges(sqlite3 *db){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 667 | db->mDbFlags &= ~DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | /* |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 671 | ** Delete memory allocated for the column names of a table or view (the |
| 672 | ** Table.aCol[] array). |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 673 | */ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 674 | void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 675 | int i; |
| 676 | Column *pCol; |
| 677 | assert( pTable!=0 ); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 678 | if( (pCol = pTable->aCol)!=0 ){ |
| 679 | for(i=0; i<pTable->nCol; i++, pCol++){ |
drh | d44390c | 2020-04-06 18:16:31 +0000 | [diff] [blame] | 680 | assert( pCol->zName==0 || pCol->hName==sqlite3StrIHash(pCol->zName) ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 681 | sqlite3DbFree(db, pCol->zName); |
| 682 | sqlite3ExprDelete(db, pCol->pDflt); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 683 | sqlite3DbFree(db, pCol->zColl); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 684 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 685 | sqlite3DbFree(db, pTable->aCol); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 686 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 690 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 691 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 692 | ** |
| 693 | ** This routine just deletes the data structure. It does not unlink |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 694 | ** the table data structure from the hash table. But it does destroy |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 695 | ** memory structures of the indices and foreign keys associated with |
| 696 | ** the table. |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 697 | ** |
| 698 | ** The db parameter is optional. It is needed if the Table object |
| 699 | ** contains lookaside memory. (Table objects in the schema do not use |
| 700 | ** lookaside memory, but some ephemeral Table objects do.) Or the |
| 701 | ** db parameter can be used with db->pnBytesFreed to measure the memory |
| 702 | ** used by the Table object. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 703 | */ |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 704 | static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 705 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 706 | |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 707 | #ifdef SQLITE_DEBUG |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 708 | /* Record the number of outstanding lookaside allocations in schema Tables |
dan | bedf84c | 2019-07-08 13:45:02 +0000 | [diff] [blame] | 709 | ** prior to doing any free() operations. Since schema Tables do not use |
| 710 | ** lookaside, this number should not change. |
| 711 | ** |
| 712 | ** If malloc has already failed, it may be that it failed while allocating |
| 713 | ** a Table object that was going to be marked ephemeral. So do not check |
| 714 | ** that no lookaside memory is used in this case either. */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 715 | int nLookaside = 0; |
dan | bedf84c | 2019-07-08 13:45:02 +0000 | [diff] [blame] | 716 | if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 717 | nLookaside = sqlite3LookasideUsed(db, 0); |
| 718 | } |
| 719 | #endif |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 720 | |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 721 | /* Delete all indices associated with this table. */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 722 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 723 | pNext = pIndex->pNext; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 724 | assert( pIndex->pSchema==pTable->pSchema |
| 725 | || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
drh | 273bfe9 | 2016-06-02 16:22:53 +0000 | [diff] [blame] | 726 | if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 727 | char *zName = pIndex->zName; |
| 728 | TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 729 | &pIndex->pSchema->idxHash, zName, 0 |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 730 | ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 731 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 732 | assert( pOld==pIndex || pOld==0 ); |
| 733 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 734 | sqlite3FreeIndex(db, pIndex); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 735 | } |
| 736 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 737 | /* Delete any foreign keys attached to this table. */ |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 738 | sqlite3FkDelete(db, pTable); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 739 | |
| 740 | /* Delete the Table structure itself. |
| 741 | */ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 742 | sqlite3DeleteColumnNames(db, pTable); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 743 | sqlite3DbFree(db, pTable->zName); |
| 744 | sqlite3DbFree(db, pTable->zColAff); |
| 745 | sqlite3SelectDelete(db, pTable->pSelect); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 746 | sqlite3ExprListDelete(db, pTable->pCheck); |
drh | 078e408 | 2010-07-28 19:17:51 +0000 | [diff] [blame] | 747 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 748 | sqlite3VtabClear(db, pTable); |
drh | 078e408 | 2010-07-28 19:17:51 +0000 | [diff] [blame] | 749 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 750 | sqlite3DbFree(db, pTable); |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 751 | |
| 752 | /* Verify that no lookaside memory was used by schema tables */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 753 | assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 754 | } |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 755 | void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 756 | /* Do not delete the table until the reference count reaches zero. */ |
| 757 | if( !pTable ) return; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 758 | if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 759 | deleteTable(db, pTable); |
| 760 | } |
| 761 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 762 | |
| 763 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 764 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 765 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 766 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 767 | void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 768 | Table *p; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 769 | Db *pDb; |
| 770 | |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 771 | assert( db!=0 ); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 772 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 972a231 | 2009-12-08 14:34:08 +0000 | [diff] [blame] | 773 | assert( zTabName ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 774 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 972a231 | 2009-12-08 14:34:08 +0000 | [diff] [blame] | 775 | testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 776 | pDb = &db->aDb[iDb]; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 777 | p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 778 | sqlite3DeleteTable(db, p); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 779 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 783 | ** Given a token, return a string that consists of the text of that |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 784 | ** token. Space to hold the returned string |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 785 | ** is obtained from sqliteMalloc() and must be freed by the calling |
| 786 | ** function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 787 | ** |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 788 | ** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
| 789 | ** surround the body of the token are removed. |
| 790 | ** |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 791 | ** Tokens are often just pointers into the original SQL text and so |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 792 | ** are not \000 terminated and are not persistent. The returned string |
| 793 | ** is \000 terminated and is persistent. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 794 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 795 | char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 796 | char *zName; |
| 797 | if( pName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 798 | zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 799 | sqlite3Dequote(zName); |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 800 | }else{ |
| 801 | zName = 0; |
| 802 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 803 | return zName; |
| 804 | } |
| 805 | |
| 806 | /* |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 807 | ** Open the sqlite_schema table stored in database number iDb for |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 808 | ** writing. The table is opened using cursor 0. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 809 | */ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 810 | void sqlite3OpenSchemaTable(Parse *p, int iDb){ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 811 | Vdbe *v = sqlite3GetVdbe(p); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 812 | sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, DFLT_SCHEMA_TABLE); |
| 813 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 814 | if( p->nTab==0 ){ |
| 815 | p->nTab = 1; |
| 816 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /* |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 820 | ** Parameter zName points to a nul-terminated buffer containing the name |
| 821 | ** of a database ("main", "temp" or the name of an attached db). This |
| 822 | ** function returns the index of the named database in db->aDb[], or |
| 823 | ** -1 if the named db cannot be found. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 824 | */ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 825 | int sqlite3FindDbName(sqlite3 *db, const char *zName){ |
| 826 | int i = -1; /* Database number */ |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 827 | if( zName ){ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 828 | Db *pDb; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 829 | for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
drh | 2951809 | 2016-12-24 19:37:16 +0000 | [diff] [blame] | 830 | if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; |
| 831 | /* "main" is always an acceptable alias for the primary database |
| 832 | ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ |
| 833 | if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 836 | return i; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 837 | } |
| 838 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 839 | /* |
| 840 | ** The token *pName contains the name of a database (either "main" or |
| 841 | ** "temp" or the name of an attached db). This routine returns the |
| 842 | ** index of the named database in db->aDb[], or -1 if the named db |
| 843 | ** does not exist. |
| 844 | */ |
| 845 | int sqlite3FindDb(sqlite3 *db, Token *pName){ |
| 846 | int i; /* Database number */ |
| 847 | char *zName; /* Name we are searching for */ |
| 848 | zName = sqlite3NameFromToken(db, pName); |
| 849 | i = sqlite3FindDbName(db, zName); |
| 850 | sqlite3DbFree(db, zName); |
| 851 | return i; |
| 852 | } |
| 853 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 854 | /* The table or view or trigger name is passed to this routine via tokens |
| 855 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 856 | ** |
| 857 | ** CREATE TABLE xxx.yyy (...); |
| 858 | ** |
| 859 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 860 | ** the table name is not fully qualified, i.e.: |
| 861 | ** |
| 862 | ** CREATE TABLE yyy(...); |
| 863 | ** |
| 864 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 865 | ** |
| 866 | ** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
| 867 | ** pName2) that stores the unqualified table name. The index of the |
| 868 | ** database "xxx" is returned. |
| 869 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 870 | int sqlite3TwoPartName( |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 871 | Parse *pParse, /* Parsing and code generating context */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 872 | Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 873 | Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
| 874 | Token **pUnqual /* Write the unqualified object name here */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 875 | ){ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 876 | int iDb; /* Database holding the object */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 877 | sqlite3 *db = pParse->db; |
| 878 | |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 879 | assert( pName2!=0 ); |
| 880 | if( pName2->n>0 ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 881 | if( db->init.busy ) { |
| 882 | sqlite3ErrorMsg(pParse, "corrupt database"); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 883 | return -1; |
| 884 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 885 | *pUnqual = pName2; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 886 | iDb = sqlite3FindDb(db, pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 887 | if( iDb<0 ){ |
| 888 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 889 | return -1; |
| 890 | } |
| 891 | }else{ |
drh | d36bcec | 2021-05-29 21:50:05 +0000 | [diff] [blame] | 892 | assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 893 | || (db->mDbFlags & DBFLAG_Vacuum)!=0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 894 | iDb = db->init.iDb; |
| 895 | *pUnqual = pName1; |
| 896 | } |
| 897 | return iDb; |
| 898 | } |
| 899 | |
| 900 | /* |
drh | 0f1c2eb | 2018-11-03 17:31:48 +0000 | [diff] [blame] | 901 | ** True if PRAGMA writable_schema is ON |
| 902 | */ |
| 903 | int sqlite3WritableSchema(sqlite3 *db){ |
| 904 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); |
| 905 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 906 | SQLITE_WriteSchema ); |
| 907 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 908 | SQLITE_Defensive ); |
| 909 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 910 | (SQLITE_WriteSchema|SQLITE_Defensive) ); |
| 911 | return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; |
| 912 | } |
| 913 | |
| 914 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 915 | ** This routine is used to check if the UTF-8 string zName is a legal |
| 916 | ** unqualified name for a new schema object (table, index, view or |
| 917 | ** trigger). All names are legal except those that begin with the string |
| 918 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
| 919 | ** is reserved for internal use. |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 920 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 921 | ** When parsing the sqlite_schema table, this routine also checks to |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 922 | ** make sure the "type", "name", and "tbl_name" columns are consistent |
| 923 | ** with the SQL. |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 924 | */ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 925 | int sqlite3CheckObjectName( |
| 926 | Parse *pParse, /* Parsing context */ |
| 927 | const char *zName, /* Name of the object to check */ |
| 928 | const char *zType, /* Type of this object */ |
| 929 | const char *zTblName /* Parent table name for triggers and indexes */ |
| 930 | ){ |
| 931 | sqlite3 *db = pParse->db; |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 932 | if( sqlite3WritableSchema(db) |
| 933 | || db->init.imposterTable |
| 934 | || !sqlite3Config.bExtraSchemaChecks |
| 935 | ){ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 936 | /* Skip these error checks for writable_schema=ON */ |
| 937 | return SQLITE_OK; |
| 938 | } |
| 939 | if( db->init.busy ){ |
| 940 | if( sqlite3_stricmp(zType, db->init.azInit[0]) |
| 941 | || sqlite3_stricmp(zName, db->init.azInit[1]) |
| 942 | || sqlite3_stricmp(zTblName, db->init.azInit[2]) |
| 943 | ){ |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 944 | sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ |
| 945 | return SQLITE_ERROR; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 946 | } |
| 947 | }else{ |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 948 | if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) |
| 949 | || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 950 | ){ |
| 951 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", |
| 952 | zName); |
| 953 | return SQLITE_ERROR; |
| 954 | } |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 955 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 956 | } |
| 957 | return SQLITE_OK; |
| 958 | } |
| 959 | |
| 960 | /* |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 961 | ** Return the PRIMARY KEY index of a table |
| 962 | */ |
| 963 | Index *sqlite3PrimaryKeyIndex(Table *pTab){ |
| 964 | Index *p; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 965 | for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 966 | return p; |
| 967 | } |
| 968 | |
| 969 | /* |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 970 | ** Convert an table column number into a index column number. That is, |
| 971 | ** for the column iCol in the table (as defined by the CREATE TABLE statement) |
| 972 | ** find the (first) offset of that column in index pIdx. Or return -1 |
| 973 | ** if column iCol is not used in index pIdx. |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 974 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 975 | i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 976 | int i; |
| 977 | for(i=0; i<pIdx->nColumn; i++){ |
| 978 | if( iCol==pIdx->aiColumn[i] ) return i; |
| 979 | } |
| 980 | return -1; |
| 981 | } |
| 982 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 983 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 984 | /* Convert a storage column number into a table column number. |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 985 | ** |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 986 | ** The storage column number (0,1,2,....) is the index of the value |
| 987 | ** as it appears in the record on disk. The true column number |
| 988 | ** is the index (0,1,2,...) of the column in the CREATE TABLE statement. |
| 989 | ** |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 990 | ** The storage column number is less than the table column number if |
| 991 | ** and only there are VIRTUAL columns to the left. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 992 | ** |
| 993 | ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 994 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 995 | i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 996 | if( pTab->tabFlags & TF_HasVirtual ){ |
| 997 | int i; |
| 998 | for(i=0; i<=iCol; i++){ |
| 999 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++; |
| 1000 | } |
| 1001 | } |
| 1002 | return iCol; |
| 1003 | } |
| 1004 | #endif |
| 1005 | |
| 1006 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1007 | /* Convert a table column number into a storage column number. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1008 | ** |
| 1009 | ** The storage column number (0,1,2,....) is the index of the value |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1010 | ** as it appears in the record on disk. Or, if the input column is |
| 1011 | ** the N-th virtual column (zero-based) then the storage number is |
| 1012 | ** the number of non-virtual columns in the table plus N. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1013 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1014 | ** The true column number is the index (0,1,2,...) of the column in |
| 1015 | ** the CREATE TABLE statement. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1016 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1017 | ** If the input column is a VIRTUAL column, then it should not appear |
| 1018 | ** in storage. But the value sometimes is cached in registers that |
| 1019 | ** follow the range of registers used to construct storage. This |
| 1020 | ** avoids computing the same VIRTUAL column multiple times, and provides |
| 1021 | ** values for use by OP_Param opcodes in triggers. Hence, if the |
| 1022 | ** input column is a VIRTUAL table, put it after all the other columns. |
| 1023 | ** |
| 1024 | ** In the following, N means "normal column", S means STORED, and |
| 1025 | ** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: |
| 1026 | ** |
| 1027 | ** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); |
| 1028 | ** -- 0 1 2 3 4 5 6 7 8 |
| 1029 | ** |
| 1030 | ** Then the mapping from this function is as follows: |
| 1031 | ** |
| 1032 | ** INPUTS: 0 1 2 3 4 5 6 7 8 |
| 1033 | ** OUTPUTS: 0 1 6 2 3 7 4 5 8 |
| 1034 | ** |
| 1035 | ** So, in other words, this routine shifts all the virtual columns to |
| 1036 | ** the end. |
| 1037 | ** |
| 1038 | ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and |
drh | 7fe2fc0 | 2019-12-07 00:22:18 +0000 | [diff] [blame] | 1039 | ** this routine is a no-op macro. If the pTab does not have any virtual |
| 1040 | ** columns, then this routine is no-op that always return iCol. If iCol |
| 1041 | ** is negative (indicating the ROWID column) then this routine return iCol. |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1042 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1043 | i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1044 | int i; |
| 1045 | i16 n; |
| 1046 | assert( iCol<pTab->nCol ); |
drh | 7fe2fc0 | 2019-12-07 00:22:18 +0000 | [diff] [blame] | 1047 | if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1048 | for(i=0, n=0; i<iCol; i++){ |
| 1049 | if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++; |
| 1050 | } |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1051 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){ |
| 1052 | /* iCol is a virtual column itself */ |
| 1053 | return pTab->nNVCol + i - n; |
| 1054 | }else{ |
| 1055 | /* iCol is a normal or stored column */ |
| 1056 | return n; |
| 1057 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1058 | } |
| 1059 | #endif |
| 1060 | |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1061 | /* |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1062 | ** Insert a single OP_JournalMode query opcode in order to force the |
| 1063 | ** prepared statement to return false for sqlite3_stmt_readonly(). This |
| 1064 | ** is used by CREATE TABLE IF NOT EXISTS and similar if the table already |
| 1065 | ** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS |
| 1066 | ** will return false for sqlite3_stmt_readonly() even if that statement |
| 1067 | ** is a read-only no-op. |
| 1068 | */ |
| 1069 | static void sqlite3ForceNotReadOnly(Parse *pParse){ |
| 1070 | int iReg = ++pParse->nMem; |
| 1071 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1072 | if( v ){ |
| 1073 | sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY); |
dan | a8f249f | 2021-05-20 11:42:51 +0000 | [diff] [blame] | 1074 | sqlite3VdbeUsesBtree(v, 0); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1079 | ** Begin constructing a new table representation in memory. This is |
| 1080 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1081 | ** to a CREATE TABLE statement. In particular, this routine is called |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1082 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1083 | ** flag is true if the table should be stored in the auxiliary database |
| 1084 | ** file instead of in the main database file. This is normally the case |
| 1085 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1086 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1087 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1088 | ** The new table record is initialized and put in pParse->pNewTable. |
| 1089 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 1090 | ** routines will be called to add more information to this record. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1091 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1092 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1093 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1094 | void sqlite3StartTable( |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1095 | Parse *pParse, /* Parser context */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1096 | Token *pName1, /* First part of the name of the table or view */ |
| 1097 | Token *pName2, /* Second part of the name of the table or view */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1098 | int isTemp, /* True if this is a TEMP table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1099 | int isView, /* True if this is a VIEW */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 1100 | int isVirtual, /* True if this is a VIRTUAL table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1101 | int noErr /* Do nothing if table already exists */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1102 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1103 | Table *pTable; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1104 | char *zName = 0; /* The name of the new table */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1105 | sqlite3 *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1106 | Vdbe *v; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1107 | int iDb; /* Database number to create the table in */ |
| 1108 | Token *pName; /* Unqualified name of the table to create */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1109 | |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1110 | if( db->init.busy && db->init.newTnum==1 ){ |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1111 | /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */ |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1112 | iDb = db->init.iDb; |
| 1113 | zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); |
| 1114 | pName = pName1; |
| 1115 | }else{ |
| 1116 | /* The common case */ |
| 1117 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 1118 | if( iDb<0 ) return; |
| 1119 | if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
| 1120 | /* If creating a temp table, the name may not be qualified. Unless |
| 1121 | ** the database name is "temp" anyway. */ |
| 1122 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 1123 | return; |
| 1124 | } |
| 1125 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 1126 | zName = sqlite3NameFromToken(db, pName); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1127 | if( IN_RENAME_OBJECT ){ |
| 1128 | sqlite3RenameTokenMap(pParse, (void*)zName, pName); |
| 1129 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1130 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1131 | pParse->sNameToken = *pName; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1132 | if( zName==0 ) return; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1133 | if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1134 | goto begin_table_error; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1135 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1136 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1137 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1138 | assert( isTemp==0 || isTemp==1 ); |
| 1139 | assert( isView==0 || isView==1 ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1140 | { |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1141 | static const u8 aCode[] = { |
| 1142 | SQLITE_CREATE_TABLE, |
| 1143 | SQLITE_CREATE_TEMP_TABLE, |
| 1144 | SQLITE_CREATE_VIEW, |
| 1145 | SQLITE_CREATE_TEMP_VIEW |
| 1146 | }; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1147 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1148 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1149 | goto begin_table_error; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1150 | } |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1151 | if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], |
| 1152 | zName, 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1153 | goto begin_table_error; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | #endif |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1157 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1158 | /* Make sure the new table name does not collide with an existing |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 1159 | ** index or table name in the same database. Issue an error message if |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1160 | ** it does. The exception is if the statement being parsed was passed |
| 1161 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 1162 | ** and types will be used, so there is no need to test for namespace |
| 1163 | ** collisions. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1164 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1165 | if( !IN_SPECIAL_PARSE ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1166 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1167 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 1168 | goto begin_table_error; |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1169 | } |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1170 | pTable = sqlite3FindTable(db, zName, zDb); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1171 | if( pTable ){ |
| 1172 | if( !noErr ){ |
| 1173 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
dan | 7687c83 | 2011-04-09 15:39:02 +0000 | [diff] [blame] | 1174 | }else{ |
drh | 33c59ec | 2015-04-19 20:39:17 +0000 | [diff] [blame] | 1175 | assert( !db->init.busy || CORRUPT_DB ); |
dan | 7687c83 | 2011-04-09 15:39:02 +0000 | [diff] [blame] | 1176 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1177 | sqlite3ForceNotReadOnly(pParse); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1178 | } |
| 1179 | goto begin_table_error; |
| 1180 | } |
drh | 8a8a0d1 | 2010-09-28 20:26:44 +0000 | [diff] [blame] | 1181 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1182 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 1183 | goto begin_table_error; |
| 1184 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1185 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1186 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 1187 | pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1188 | if( pTable==0 ){ |
drh | 4df86af | 2016-02-04 11:48:00 +0000 | [diff] [blame] | 1189 | assert( db->mallocFailed ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1190 | pParse->rc = SQLITE_NOMEM_BKPT; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1191 | pParse->nErr++; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1192 | goto begin_table_error; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1193 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1194 | pTable->zName = zName; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1195 | pTable->iPKey = -1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1196 | pTable->pSchema = db->aDb[iDb].pSchema; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 1197 | pTable->nTabRef = 1; |
drh | d1417ee | 2017-06-06 18:20:43 +0000 | [diff] [blame] | 1198 | #ifdef SQLITE_DEFAULT_ROWEST |
| 1199 | pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); |
| 1200 | #else |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1201 | pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | d1417ee | 2017-06-06 18:20:43 +0000 | [diff] [blame] | 1202 | #endif |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1203 | assert( pParse->pNewTable==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1204 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* Begin generating the code that will insert the table record into |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 1207 | ** the schema table. Note in particular that we must go ahead |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1208 | ** and allocate the record number for the table entry now. Before any |
| 1209 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 1210 | ** indices to be created and the table record must come before the |
| 1211 | ** indices. Hence, the record number for the table must be allocated |
| 1212 | ** now. |
| 1213 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1214 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1215 | int addr1; |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 1216 | int fileFormat; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1217 | int reg1, reg2, reg3; |
drh | 3c03afd | 2015-09-09 13:28:06 +0000 | [diff] [blame] | 1218 | /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ |
| 1219 | static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; |
drh | 0dd5cda | 2015-06-16 16:39:01 +0000 | [diff] [blame] | 1220 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 1221 | |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 1222 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1223 | if( isVirtual ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1224 | sqlite3VdbeAddOp0(v, OP_VBegin); |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 1225 | } |
| 1226 | #endif |
| 1227 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 1228 | /* If the file format and encoding in the database have not been set, |
| 1229 | ** set them now. |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 1230 | */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1231 | reg1 = pParse->regRowid = ++pParse->nMem; |
| 1232 | reg2 = pParse->regRoot = ++pParse->nMem; |
| 1233 | reg3 = ++pParse->nMem; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 1234 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1235 | sqlite3VdbeUsesBtree(v, iDb); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1236 | addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 1237 | fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 1238 | 1 : SQLITE_MAX_FILE_FORMAT; |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 1239 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); |
| 1240 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1241 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 1242 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1243 | /* This just creates a place-holder record in the sqlite_schema table. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1244 | ** The record created does not contain anything yet. It will be replaced |
| 1245 | ** by the real entry in code generated at sqlite3EndTable(). |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 1246 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 1247 | ** The rowid for the new entry is left in register pParse->regRowid. |
| 1248 | ** The root page number of the new table is left in reg pParse->regRoot. |
| 1249 | ** The rowid and root page number values are needed by the code that |
| 1250 | ** sqlite3EndTable will generate. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1251 | */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 1252 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 1253 | if( isView || isVirtual ){ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1254 | sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1255 | }else |
| 1256 | #endif |
| 1257 | { |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1258 | assert( !pParse->bReturning ); |
| 1259 | pParse->u1.addrCrTab = |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 1260 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1261 | } |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 1262 | sqlite3OpenSchemaTable(pParse, iDb); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1263 | sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); |
drh | 3c03afd | 2015-09-09 13:28:06 +0000 | [diff] [blame] | 1264 | sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1265 | sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); |
| 1266 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1267 | sqlite3VdbeAddOp0(v, OP_Close); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1268 | } |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* Normal (non-error) return. */ |
| 1271 | return; |
| 1272 | |
| 1273 | /* If an error occurs, we jump here */ |
| 1274 | begin_table_error: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1275 | sqlite3DbFree(db, zName); |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1276 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1279 | /* Set properties of a table column based on the (magical) |
| 1280 | ** name of the column. |
| 1281 | */ |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1282 | #if SQLITE_ENABLE_HIDDEN_COLUMNS |
drh | e611050 | 2015-12-31 15:34:03 +0000 | [diff] [blame] | 1283 | void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1284 | if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){ |
| 1285 | pCol->colFlags |= COLFLAG_HIDDEN; |
drh | 6f6e60d | 2021-02-18 15:45:34 +0000 | [diff] [blame] | 1286 | if( pTab ) pTab->tabFlags |= TF_HasHidden; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 1287 | }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ |
| 1288 | pTab->tabFlags |= TF_OOOHidden; |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1289 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1290 | } |
drh | e611050 | 2015-12-31 15:34:03 +0000 | [diff] [blame] | 1291 | #endif |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1292 | |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1293 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1294 | ** Name of the special TEMP trigger used to implement RETURNING. The |
| 1295 | ** name begins with "sqlite_" so that it is guaranteed not to collide |
| 1296 | ** with any application-generated triggers. |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1297 | */ |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1298 | #define RETURNING_TRIGGER_NAME "sqlite_returning" |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1299 | |
| 1300 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1301 | ** Clean up the data structures associated with the RETURNING clause. |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1302 | */ |
| 1303 | static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){ |
| 1304 | Hash *pHash; |
| 1305 | pHash = &(db->aDb[1].pSchema->trigHash); |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1306 | sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, 0); |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1307 | sqlite3ExprListDelete(db, pRet->pReturnEL); |
| 1308 | sqlite3DbFree(db, pRet); |
| 1309 | } |
| 1310 | |
| 1311 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1312 | ** Add the RETURNING clause to the parse currently underway. |
| 1313 | ** |
| 1314 | ** This routine creates a special TEMP trigger that will fire for each row |
| 1315 | ** of the DML statement. That TEMP trigger contains a single SELECT |
| 1316 | ** statement with a result set that is the argument of the RETURNING clause. |
| 1317 | ** The trigger has the Trigger.bReturning flag and an opcode of |
| 1318 | ** TK_RETURNING instead of TK_SELECT, so that the trigger code generator |
| 1319 | ** knows to handle it specially. The TEMP trigger is automatically |
| 1320 | ** removed at the end of the parse. |
| 1321 | ** |
| 1322 | ** When this routine is called, we do not yet know if the RETURNING clause |
| 1323 | ** is attached to a DELETE, INSERT, or UPDATE, so construct it as a |
| 1324 | ** RETURNING trigger instead. It will then be converted into the appropriate |
| 1325 | ** type on the first call to sqlite3TriggersExist(). |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1326 | */ |
| 1327 | void sqlite3AddReturning(Parse *pParse, ExprList *pList){ |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1328 | Returning *pRet; |
| 1329 | Hash *pHash; |
| 1330 | sqlite3 *db = pParse->db; |
drh | e1c9a4e | 2021-02-07 23:28:20 +0000 | [diff] [blame] | 1331 | if( pParse->pNewTrigger ){ |
| 1332 | sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger"); |
| 1333 | }else{ |
| 1334 | assert( pParse->bReturning==0 ); |
| 1335 | } |
drh | d086aa0 | 2021-01-29 21:31:59 +0000 | [diff] [blame] | 1336 | pParse->bReturning = 1; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1337 | pRet = sqlite3DbMallocZero(db, sizeof(*pRet)); |
| 1338 | if( pRet==0 ){ |
| 1339 | sqlite3ExprListDelete(db, pList); |
| 1340 | return; |
| 1341 | } |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1342 | pParse->u1.pReturning = pRet; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1343 | pRet->pParse = pParse; |
| 1344 | pRet->pReturnEL = pList; |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1345 | sqlite3ParserAddCleanup(pParse, |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1346 | (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet); |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 1347 | testcase( pParse->earlyCleanup ); |
drh | cf4108b | 2021-01-30 03:06:19 +0000 | [diff] [blame] | 1348 | if( db->mallocFailed ) return; |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1349 | pRet->retTrig.zName = RETURNING_TRIGGER_NAME; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1350 | pRet->retTrig.op = TK_RETURNING; |
| 1351 | pRet->retTrig.tr_tm = TRIGGER_AFTER; |
| 1352 | pRet->retTrig.bReturning = 1; |
| 1353 | pRet->retTrig.pSchema = db->aDb[1].pSchema; |
drh | a476768 | 2021-04-27 13:04:18 +0000 | [diff] [blame] | 1354 | pRet->retTrig.pTabSchema = db->aDb[1].pSchema; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1355 | pRet->retTrig.step_list = &pRet->retTStep; |
drh | dac9a5f | 2021-01-29 21:18:46 +0000 | [diff] [blame] | 1356 | pRet->retTStep.op = TK_RETURNING; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1357 | pRet->retTStep.pTrig = &pRet->retTrig; |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1358 | pRet->retTStep.pExprList = pList; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1359 | pHash = &(db->aDb[1].pSchema->trigHash); |
drh | e1c9a4e | 2021-02-07 23:28:20 +0000 | [diff] [blame] | 1360 | assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr ); |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1361 | if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig) |
drh | 0166df0 | 2021-01-30 12:07:32 +0000 | [diff] [blame] | 1362 | ==&pRet->retTrig ){ |
| 1363 | sqlite3OomFault(db); |
| 1364 | } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1365 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1366 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1367 | /* |
| 1368 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1369 | ** |
| 1370 | ** The parser calls this routine once for each column declaration |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1371 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1372 | ** first to get things going. Then this routine is called for each |
| 1373 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1374 | */ |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1375 | void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1376 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1377 | int i; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 1378 | char *z; |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1379 | char *zType; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1380 | Column *pCol; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1381 | sqlite3 *db = pParse->db; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1382 | u8 hName; |
| 1383 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1384 | if( (p = pParse->pNewTable)==0 ) return; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1385 | if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 1386 | sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
| 1387 | return; |
| 1388 | } |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1389 | z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1390 | if( z==0 ) return; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1391 | if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName); |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1392 | memcpy(z, pName->z, pName->n); |
| 1393 | z[pName->n] = 0; |
| 1394 | sqlite3Dequote(z); |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1395 | hName = sqlite3StrIHash(z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1396 | for(i=0; i<p->nCol; i++){ |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1397 | if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zName)==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1398 | sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1399 | sqlite3DbFree(db, z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1400 | return; |
| 1401 | } |
| 1402 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1403 | if( (p->nCol & 0x7)==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1404 | Column *aNew; |
danielk1977 | ae74e03 | 2008-12-23 11:11:51 +0000 | [diff] [blame] | 1405 | aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1406 | if( aNew==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1407 | sqlite3DbFree(db, z); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1408 | return; |
| 1409 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1410 | p->aCol = aNew; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1411 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1412 | pCol = &p->aCol[p->nCol]; |
| 1413 | memset(pCol, 0, sizeof(p->aCol[0])); |
| 1414 | pCol->zName = z; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1415 | pCol->hName = hName; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 1416 | sqlite3ColumnPropertiesFromName(p, pCol); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1417 | |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 1418 | if( pType->n==0 ){ |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1419 | /* If there is no type specified, columns have the default affinity |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1420 | ** 'BLOB' with a default size of 4 bytes. */ |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1421 | pCol->affinity = SQLITE_AFF_BLOB; |
| 1422 | pCol->szEst = 1; |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1423 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1424 | if( 4>=sqlite3GlobalConfig.szSorterRef ){ |
| 1425 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1426 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1427 | #endif |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1428 | }else{ |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 1429 | zType = z + sqlite3Strlen30(z) + 1; |
| 1430 | memcpy(zType, pType->z, pType->n); |
| 1431 | zType[pType->n] = 0; |
drh | a6dddd9 | 2016-04-18 15:46:14 +0000 | [diff] [blame] | 1432 | sqlite3Dequote(zType); |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1433 | pCol->affinity = sqlite3AffinityType(zType, pCol); |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1434 | pCol->colFlags |= COLFLAG_HASTYPE; |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1435 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1436 | p->nCol++; |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 1437 | p->nNVCol++; |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 1438 | pParse->constraintName.n = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1442 | ** This routine is called by the parser while in the middle of |
| 1443 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 1444 | ** been seen on a column. This routine sets the notNull flag on |
| 1445 | ** the column currently under construction. |
| 1446 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1447 | void sqlite3AddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1448 | Table *p; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1449 | Column *pCol; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1450 | p = pParse->pNewTable; |
| 1451 | if( p==0 || NEVER(p->nCol<1) ) return; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1452 | pCol = &p->aCol[p->nCol-1]; |
| 1453 | pCol->notNull = (u8)onError; |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1454 | p->tabFlags |= TF_HasNotNull; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1455 | |
| 1456 | /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created |
| 1457 | ** on this column. */ |
| 1458 | if( pCol->colFlags & COLFLAG_UNIQUE ){ |
| 1459 | Index *pIdx; |
| 1460 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1461 | assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); |
| 1462 | if( pIdx->aiColumn[0]==p->nCol-1 ){ |
| 1463 | pIdx->uniqNotNull = 1; |
| 1464 | } |
| 1465 | } |
| 1466 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1470 | ** Scan the column type name zType (length nType) and return the |
| 1471 | ** associated affinity type. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1472 | ** |
| 1473 | ** This routine does a case-independent search of zType for the |
| 1474 | ** substrings in the following table. If one of the substrings is |
| 1475 | ** found, the corresponding affinity is returned. If zType contains |
| 1476 | ** more than one of the substrings, entries toward the top of |
| 1477 | ** the table take priority. For example, if zType is 'BLOBINT', |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1478 | ** SQLITE_AFF_INTEGER is returned. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1479 | ** |
| 1480 | ** Substring | Affinity |
| 1481 | ** -------------------------------- |
| 1482 | ** 'INT' | SQLITE_AFF_INTEGER |
| 1483 | ** 'CHAR' | SQLITE_AFF_TEXT |
| 1484 | ** 'CLOB' | SQLITE_AFF_TEXT |
| 1485 | ** 'TEXT' | SQLITE_AFF_TEXT |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1486 | ** 'BLOB' | SQLITE_AFF_BLOB |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1487 | ** 'REAL' | SQLITE_AFF_REAL |
| 1488 | ** 'FLOA' | SQLITE_AFF_REAL |
| 1489 | ** 'DOUB' | SQLITE_AFF_REAL |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1490 | ** |
| 1491 | ** If none of the substrings in the above table are found, |
| 1492 | ** SQLITE_AFF_NUMERIC is returned. |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1493 | */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1494 | char sqlite3AffinityType(const char *zIn, Column *pCol){ |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1495 | u32 h = 0; |
| 1496 | char aff = SQLITE_AFF_NUMERIC; |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1497 | const char *zChar = 0; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1498 | |
drh | 2f1e02e | 2016-03-09 02:12:44 +0000 | [diff] [blame] | 1499 | assert( zIn!=0 ); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1500 | while( zIn[0] ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1501 | h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1502 | zIn++; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1503 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1504 | aff = SQLITE_AFF_TEXT; |
| 1505 | zChar = zIn; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1506 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| 1507 | aff = SQLITE_AFF_TEXT; |
| 1508 | }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 1509 | aff = SQLITE_AFF_TEXT; |
| 1510 | }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1511 | && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1512 | aff = SQLITE_AFF_BLOB; |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1513 | if( zIn[0]=='(' ) zChar = zIn; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1514 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1515 | }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
| 1516 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1517 | aff = SQLITE_AFF_REAL; |
| 1518 | }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
| 1519 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1520 | aff = SQLITE_AFF_REAL; |
| 1521 | }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
| 1522 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1523 | aff = SQLITE_AFF_REAL; |
| 1524 | #endif |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1525 | }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1526 | aff = SQLITE_AFF_INTEGER; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1527 | break; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1530 | |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1531 | /* If pCol is not NULL, store an estimate of the field size. The |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1532 | ** estimate is scaled so that the size of an integer is 1. */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1533 | if( pCol ){ |
| 1534 | int v = 0; /* default size is approx 4 bytes */ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 1535 | if( aff<SQLITE_AFF_NUMERIC ){ |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1536 | if( zChar ){ |
| 1537 | while( zChar[0] ){ |
| 1538 | if( sqlite3Isdigit(zChar[0]) ){ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1539 | /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1540 | sqlite3GetInt32(zChar, &v); |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1541 | break; |
| 1542 | } |
| 1543 | zChar++; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1544 | } |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1545 | }else{ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1546 | v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1547 | } |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1548 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1549 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1550 | if( v>=sqlite3GlobalConfig.szSorterRef ){ |
| 1551 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1552 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1553 | #endif |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1554 | v = v/4 + 1; |
| 1555 | if( v>255 ) v = 255; |
| 1556 | pCol->szEst = v; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1557 | } |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1558 | return aff; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1562 | ** The expression is the default value for the most recently added column |
| 1563 | ** of the table currently under construction. |
| 1564 | ** |
| 1565 | ** Default value expressions must be constant. Raise an exception if this |
| 1566 | ** is not the case. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1567 | ** |
| 1568 | ** This routine is called by the parser while in the middle of |
| 1569 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1570 | */ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1571 | void sqlite3AddDefaultValue( |
| 1572 | Parse *pParse, /* Parsing context */ |
| 1573 | Expr *pExpr, /* The parsed expression of the default value */ |
| 1574 | const char *zStart, /* Start of the default value text */ |
| 1575 | const char *zEnd /* First character past end of defaut value text */ |
| 1576 | ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1577 | Table *p; |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1578 | Column *pCol; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1579 | sqlite3 *db = pParse->db; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1580 | p = pParse->pNewTable; |
| 1581 | if( p!=0 ){ |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 1582 | int isInit = db->init.busy && db->init.iDb!=1; |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1583 | pCol = &(p->aCol[p->nCol-1]); |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 1584 | if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){ |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1585 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
| 1586 | pCol->zName); |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1587 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1588 | }else if( pCol->colFlags & COLFLAG_GENERATED ){ |
drh | ab0992f | 2019-10-23 03:53:10 +0000 | [diff] [blame] | 1589 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1590 | testcase( pCol->colFlags & COLFLAG_STORED ); |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1591 | sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column"); |
| 1592 | #endif |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1593 | }else{ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1594 | /* A copy of pExpr is used instead of the original, as pExpr contains |
drh | 424981d | 2018-03-28 15:56:55 +0000 | [diff] [blame] | 1595 | ** tokens that point to volatile memory. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1596 | */ |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1597 | Expr x; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1598 | sqlite3ExprDelete(db, pCol->pDflt); |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1599 | memset(&x, 0, sizeof(x)); |
| 1600 | x.op = TK_SPAN; |
drh | 9b2e043 | 2017-12-27 19:43:22 +0000 | [diff] [blame] | 1601 | x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1602 | x.pLeft = pExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1603 | x.flags = EP_Skip; |
| 1604 | pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
| 1605 | sqlite3DbFree(db, x.u.zToken); |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1606 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1607 | } |
dan | 8900a48 | 2018-09-05 14:36:05 +0000 | [diff] [blame] | 1608 | if( IN_RENAME_OBJECT ){ |
| 1609 | sqlite3RenameExprUnmap(pParse, pExpr); |
| 1610 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1611 | sqlite3ExprDelete(db, pExpr); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /* |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1615 | ** Backwards Compatibility Hack: |
| 1616 | ** |
| 1617 | ** Historical versions of SQLite accepted strings as column names in |
| 1618 | ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
| 1619 | ** |
| 1620 | ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) |
| 1621 | ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); |
| 1622 | ** |
| 1623 | ** This is goofy. But to preserve backwards compatibility we continue to |
| 1624 | ** accept it. This routine does the necessary conversion. It converts |
| 1625 | ** the expression given in its argument from a TK_STRING into a TK_ID |
| 1626 | ** if the expression is just a TK_STRING with an optional COLLATE clause. |
drh | 6c59136 | 2019-04-27 20:16:42 +0000 | [diff] [blame] | 1627 | ** If the expression is anything other than TK_STRING, the expression is |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1628 | ** unchanged. |
| 1629 | */ |
| 1630 | static void sqlite3StringToId(Expr *p){ |
| 1631 | if( p->op==TK_STRING ){ |
| 1632 | p->op = TK_ID; |
| 1633 | }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ |
| 1634 | p->pLeft->op = TK_ID; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | /* |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1639 | ** Tag the given column as being part of the PRIMARY KEY |
| 1640 | */ |
| 1641 | static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){ |
| 1642 | pCol->colFlags |= COLFLAG_PRIMKEY; |
| 1643 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1644 | if( pCol->colFlags & COLFLAG_GENERATED ){ |
| 1645 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1646 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 1647 | sqlite3ErrorMsg(pParse, |
| 1648 | "generated columns cannot be part of the PRIMARY KEY"); |
| 1649 | } |
| 1650 | #endif |
| 1651 | } |
| 1652 | |
| 1653 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1654 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 1655 | ** of columns that form the primary key. If pList is NULL, then the |
| 1656 | ** most recently added column of the table is the primary key. |
| 1657 | ** |
| 1658 | ** A table can have at most one primary key. If the table already has |
| 1659 | ** a primary key (and this is the second primary key) then create an |
| 1660 | ** error. |
| 1661 | ** |
| 1662 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1663 | ** 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] | 1664 | ** field of the table under construction to be the index of the |
| 1665 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 1666 | ** no INTEGER PRIMARY KEY. |
| 1667 | ** |
| 1668 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 1669 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 1670 | */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1671 | void sqlite3AddPrimaryKey( |
| 1672 | Parse *pParse, /* Parsing context */ |
| 1673 | ExprList *pList, /* List of field names to be indexed */ |
| 1674 | int onError, /* What to do with a uniqueness conflict */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 1675 | int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
| 1676 | int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1677 | ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1678 | Table *pTab = pParse->pNewTable; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1679 | Column *pCol = 0; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1680 | int iCol = -1, i; |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1681 | int nTerm; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1682 | if( pTab==0 ) goto primary_key_exit; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1683 | if( pTab->tabFlags & TF_HasPrimaryKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1684 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1685 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1686 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1687 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1688 | pTab->tabFlags |= TF_HasPrimaryKey; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1689 | if( pList==0 ){ |
| 1690 | iCol = pTab->nCol - 1; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1691 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1692 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1693 | nTerm = 1; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1694 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1695 | nTerm = pList->nExpr; |
| 1696 | for(i=0; i<nTerm; i++){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1697 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 1698 | assert( pCExpr!=0 ); |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1699 | sqlite3StringToId(pCExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 1700 | if( pCExpr->op==TK_ID ){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1701 | const char *zCName = pCExpr->u.zToken; |
| 1702 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 1703 | if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){ |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1704 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1705 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1706 | break; |
| 1707 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1708 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1709 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1710 | } |
| 1711 | } |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1712 | if( nTerm==1 |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1713 | && pCol |
| 1714 | && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0 |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1715 | && sortOrder!=SQLITE_SO_DESC |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1716 | ){ |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1717 | if( IN_RENAME_OBJECT && pList ){ |
dan | 2381f6d | 2019-03-20 16:58:21 +0000 | [diff] [blame] | 1718 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 1719 | sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1720 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1721 | pTab->iPKey = iCol; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1722 | pTab->keyConf = (u8)onError; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1723 | assert( autoInc==0 || autoInc==1 ); |
| 1724 | pTab->tabFlags |= autoInc*TF_Autoincrement; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 1725 | if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags; |
drh | 34ab941 | 2019-12-19 17:42:27 +0000 | [diff] [blame] | 1726 | (void)sqlite3HasExplicitNulls(pParse, pList); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1727 | }else if( autoInc ){ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1728 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1729 | sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1730 | "INTEGER PRIMARY KEY"); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1731 | #endif |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1732 | }else{ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1733 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
| 1734 | 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1735 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1736 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1737 | |
| 1738 | primary_key_exit: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1739 | sqlite3ExprListDelete(pParse->db, pList); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1740 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /* |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1744 | ** Add a new CHECK constraint to the table currently under construction. |
| 1745 | */ |
| 1746 | void sqlite3AddCheckConstraint( |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 1747 | Parse *pParse, /* Parsing context */ |
| 1748 | Expr *pCheckExpr, /* The check expression */ |
| 1749 | const char *zStart, /* Opening "(" */ |
| 1750 | const char *zEnd /* Closing ")" */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1751 | ){ |
| 1752 | #ifndef SQLITE_OMIT_CHECK |
| 1753 | Table *pTab = pParse->pNewTable; |
drh | c9bbb01 | 2014-05-21 08:48:18 +0000 | [diff] [blame] | 1754 | sqlite3 *db = pParse->db; |
| 1755 | if( pTab && !IN_DECLARE_VTAB |
| 1756 | && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) |
| 1757 | ){ |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1758 | pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); |
| 1759 | if( pParse->constraintName.n ){ |
| 1760 | sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 1761 | }else{ |
| 1762 | Token t; |
| 1763 | for(zStart++; sqlite3Isspace(zStart[0]); zStart++){} |
| 1764 | while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; } |
| 1765 | t.z = zStart; |
| 1766 | t.n = (int)(zEnd - t.z); |
| 1767 | sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1768 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1769 | }else |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1770 | #endif |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1771 | { |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1772 | sqlite3ExprDelete(pParse->db, pCheckExpr); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1773 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1777 | ** Set the collation function of the most recently parsed table column |
| 1778 | ** to the CollSeq given. |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1779 | */ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1780 | void sqlite3AddCollateType(Parse *pParse, Token *pToken){ |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1781 | Table *p; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1782 | int i; |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1783 | char *zColl; /* Dequoted name of collation sequence */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1784 | sqlite3 *db; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1785 | |
dan | 936a305 | 2020-10-12 15:27:50 +0000 | [diff] [blame] | 1786 | if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1787 | i = p->nCol-1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1788 | db = pParse->db; |
| 1789 | zColl = sqlite3NameFromToken(db, pToken); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1790 | if( !zColl ) return; |
| 1791 | |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1792 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1793 | Index *pIdx; |
drh | fe685c8 | 2013-06-08 19:58:27 +0000 | [diff] [blame] | 1794 | sqlite3DbFree(db, p->aCol[i].zColl); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1795 | p->aCol[i].zColl = zColl; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1796 | |
| 1797 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 1798 | ** then an index may have been created on this column before the |
| 1799 | ** collation type was added. Correct this if it is the case. |
| 1800 | */ |
| 1801 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1802 | assert( pIdx->nKeyCol==1 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1803 | if( pIdx->aiColumn[0]==i ){ |
| 1804 | pIdx->azColl[0] = p->aCol[i].zColl; |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1807 | }else{ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1808 | sqlite3DbFree(db, zColl); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1809 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1812 | /* Change the most recently parsed column to be a GENERATED ALWAYS AS |
| 1813 | ** column. |
| 1814 | */ |
| 1815 | void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){ |
| 1816 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1817 | u8 eType = COLFLAG_VIRTUAL; |
| 1818 | Table *pTab = pParse->pNewTable; |
| 1819 | Column *pCol; |
drh | f68bf5f | 2019-12-04 03:31:29 +0000 | [diff] [blame] | 1820 | if( pTab==0 ){ |
| 1821 | /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ |
| 1822 | goto generated_done; |
| 1823 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1824 | pCol = &(pTab->aCol[pTab->nCol-1]); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1825 | if( IN_DECLARE_VTAB ){ |
| 1826 | sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns"); |
| 1827 | goto generated_done; |
| 1828 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1829 | if( pCol->pDflt ) goto generated_error; |
| 1830 | if( pType ){ |
| 1831 | if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){ |
| 1832 | /* no-op */ |
| 1833 | }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){ |
| 1834 | eType = COLFLAG_STORED; |
| 1835 | }else{ |
| 1836 | goto generated_error; |
| 1837 | } |
| 1838 | } |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 1839 | if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1840 | pCol->colFlags |= eType; |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 1841 | assert( TF_HasVirtual==COLFLAG_VIRTUAL ); |
| 1842 | assert( TF_HasStored==COLFLAG_STORED ); |
| 1843 | pTab->tabFlags |= eType; |
drh | a0e16a2 | 2019-10-27 22:22:24 +0000 | [diff] [blame] | 1844 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 1845 | makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */ |
| 1846 | } |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1847 | pCol->pDflt = pExpr; |
| 1848 | pExpr = 0; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1849 | goto generated_done; |
| 1850 | |
| 1851 | generated_error: |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1852 | sqlite3ErrorMsg(pParse, "error in generated column \"%s\"", |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1853 | pCol->zName); |
| 1854 | generated_done: |
| 1855 | sqlite3ExprDelete(pParse->db, pExpr); |
| 1856 | #else |
| 1857 | /* Throw and error for the GENERATED ALWAYS AS clause if the |
| 1858 | ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */ |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1859 | sqlite3ErrorMsg(pParse, "generated columns not supported"); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1860 | sqlite3ExprDelete(pParse->db, pExpr); |
| 1861 | #endif |
| 1862 | } |
| 1863 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1864 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1865 | ** Generate code that will increment the schema cookie. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1866 | ** |
| 1867 | ** The schema cookie is used to determine when the schema for the |
| 1868 | ** database changes. After each schema change, the cookie value |
| 1869 | ** changes. When a process first reads the schema it records the |
| 1870 | ** cookie. Thereafter, whenever it goes to access the database, |
| 1871 | ** it checks the cookie to make sure the schema has not changed |
| 1872 | ** since it was last read. |
| 1873 | ** |
| 1874 | ** This plan is not completely bullet-proof. It is possible for |
| 1875 | ** the schema to change multiple times and for the cookie to be |
| 1876 | ** set back to prior value. But schema changes are infrequent |
| 1877 | ** and the probability of hitting the same cookie value is only |
| 1878 | ** 1 chance in 2^32. So we're safe enough. |
drh | 96fdcb4 | 2016-09-27 00:09:33 +0000 | [diff] [blame] | 1879 | ** |
| 1880 | ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments |
| 1881 | ** the schema-version whenever the schema changes. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1882 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1883 | void sqlite3ChangeCookie(Parse *pParse, int iDb){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1884 | sqlite3 *db = pParse->db; |
| 1885 | Vdbe *v = pParse->pVdbe; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 1886 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 1887 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, |
drh | 3517b31 | 2018-04-09 00:46:42 +0000 | [diff] [blame] | 1888 | (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1892 | ** Measure the number of characters needed to output the given |
| 1893 | ** identifier. The number returned includes any quotes used |
| 1894 | ** but does not include the null terminator. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1895 | ** |
| 1896 | ** The estimate is conservative. It might be larger that what is |
| 1897 | ** really needed. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1898 | */ |
| 1899 | static int identLength(const char *z){ |
| 1900 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1901 | for(n=0; *z; n++, z++){ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1902 | if( *z=='"' ){ n++; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1903 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1904 | return n + 2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | /* |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 1908 | ** The first parameter is a pointer to an output buffer. The second |
| 1909 | ** parameter is a pointer to an integer that contains the offset at |
| 1910 | ** which to write into the output buffer. This function copies the |
| 1911 | ** nul-terminated string pointed to by the third parameter, zSignedIdent, |
| 1912 | ** to the specified offset in the buffer and updates *pIdx to refer |
| 1913 | ** to the first byte after the last byte written before returning. |
| 1914 | ** |
| 1915 | ** If the string zSignedIdent consists entirely of alpha-numeric |
| 1916 | ** characters, does not begin with a digit and is not an SQL keyword, |
| 1917 | ** then it is copied to the output buffer exactly as it is. Otherwise, |
| 1918 | ** it is quoted using double-quotes. |
| 1919 | */ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1920 | static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1921 | unsigned char *zIdent = (unsigned char*)zSignedIdent; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1922 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1923 | i = *pIdx; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 1924 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1925 | for(j=0; zIdent[j]; j++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 1926 | if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1927 | } |
drh | c740752 | 2014-01-10 20:38:12 +0000 | [diff] [blame] | 1928 | needQuote = sqlite3Isdigit(zIdent[0]) |
| 1929 | || sqlite3KeywordCode(zIdent, j)!=TK_ID |
| 1930 | || zIdent[j]!=0 |
| 1931 | || j==0; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 1932 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1933 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1934 | for(j=0; zIdent[j]; j++){ |
| 1935 | z[i++] = zIdent[j]; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1936 | if( zIdent[j]=='"' ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1937 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1938 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1939 | z[i] = 0; |
| 1940 | *pIdx = i; |
| 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | ** Generate a CREATE TABLE statement appropriate for the given |
| 1945 | ** table. Memory to hold the text of the statement is obtained |
| 1946 | ** from sqliteMalloc() and must be freed by the calling function. |
| 1947 | */ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 1948 | static char *createTableStmt(sqlite3 *db, Table *p){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1949 | int i, k, n; |
| 1950 | char *zStmt; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1951 | char *zSep, *zSep2, *zEnd; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1952 | Column *pCol; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1953 | n = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1954 | for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1955 | n += identLength(pCol->zName) + 5; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1956 | } |
| 1957 | n += identLength(p->zName); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1958 | if( n<50 ){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1959 | zSep = ""; |
| 1960 | zSep2 = ","; |
| 1961 | zEnd = ")"; |
| 1962 | }else{ |
| 1963 | zSep = "\n "; |
| 1964 | zSep2 = ",\n "; |
| 1965 | zEnd = "\n)"; |
| 1966 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1967 | n += 35 + 6*p->nCol; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 1968 | zStmt = sqlite3DbMallocRaw(0, n); |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 1969 | if( zStmt==0 ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 1970 | sqlite3OomFault(db); |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 1971 | return 0; |
| 1972 | } |
drh | bdb339f | 2009-02-02 18:03:21 +0000 | [diff] [blame] | 1973 | sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1974 | k = sqlite3Strlen30(zStmt); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1975 | identPut(zStmt, &k, p->zName); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1976 | zStmt[k++] = '('; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 1977 | for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1978 | static const char * const azType[] = { |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1979 | /* SQLITE_AFF_BLOB */ "", |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 1980 | /* SQLITE_AFF_TEXT */ " TEXT", |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1981 | /* SQLITE_AFF_NUMERIC */ " NUM", |
| 1982 | /* SQLITE_AFF_INTEGER */ " INT", |
| 1983 | /* SQLITE_AFF_REAL */ " REAL" |
| 1984 | }; |
| 1985 | int len; |
| 1986 | const char *zType; |
| 1987 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1988 | sqlite3_snprintf(n-k, &zStmt[k], zSep); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1989 | k += sqlite3Strlen30(&zStmt[k]); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 1990 | zSep = zSep2; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1991 | identPut(zStmt, &k, pCol->zName); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1992 | assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
| 1993 | assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
| 1994 | testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 1995 | testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1996 | testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
| 1997 | testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
| 1998 | testcase( pCol->affinity==SQLITE_AFF_REAL ); |
| 1999 | |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2000 | zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2001 | len = sqlite3Strlen30(zType); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2002 | assert( pCol->affinity==SQLITE_AFF_BLOB |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2003 | || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2004 | memcpy(&zStmt[k], zType, len); |
| 2005 | k += len; |
| 2006 | assert( k<=n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2007 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2008 | sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2009 | return zStmt; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2013 | ** Resize an Index object to hold N columns total. Return SQLITE_OK |
| 2014 | ** on success and SQLITE_NOMEM on an OOM error. |
| 2015 | */ |
| 2016 | static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ |
| 2017 | char *zExtra; |
| 2018 | int nByte; |
| 2019 | if( pIdx->nColumn>=N ) return SQLITE_OK; |
| 2020 | assert( pIdx->isResized==0 ); |
dan | b5a6923 | 2020-09-15 20:48:30 +0000 | [diff] [blame] | 2021 | nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2022 | zExtra = sqlite3DbMallocZero(db, nByte); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 2023 | if( zExtra==0 ) return SQLITE_NOMEM_BKPT; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2024 | memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 2025 | pIdx->azColl = (const char**)zExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2026 | zExtra += sizeof(char*)*N; |
dan | b5a6923 | 2020-09-15 20:48:30 +0000 | [diff] [blame] | 2027 | memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1)); |
| 2028 | pIdx->aiRowLogEst = (LogEst*)zExtra; |
| 2029 | zExtra += sizeof(LogEst)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2030 | memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
| 2031 | pIdx->aiColumn = (i16*)zExtra; |
| 2032 | zExtra += sizeof(i16)*N; |
| 2033 | memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
| 2034 | pIdx->aSortOrder = (u8*)zExtra; |
| 2035 | pIdx->nColumn = N; |
| 2036 | pIdx->isResized = 1; |
| 2037 | return SQLITE_OK; |
| 2038 | } |
| 2039 | |
| 2040 | /* |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2041 | ** Estimate the total row width for a table. |
| 2042 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2043 | static void estimateTableWidth(Table *pTab){ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2044 | unsigned wTable = 0; |
| 2045 | const Column *pTabCol; |
| 2046 | int i; |
| 2047 | for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ |
| 2048 | wTable += pTabCol->szEst; |
| 2049 | } |
| 2050 | if( pTab->iPKey<0 ) wTable++; |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2051 | pTab->szTabRow = sqlite3LogEst(wTable*4); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | /* |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2055 | ** Estimate the average size of a row for an index. |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2056 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2057 | static void estimateIndexWidth(Index *pIdx){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2058 | unsigned wIndex = 0; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2059 | int i; |
| 2060 | const Column *aCol = pIdx->pTable->aCol; |
| 2061 | for(i=0; i<pIdx->nColumn; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2062 | i16 x = pIdx->aiColumn[i]; |
| 2063 | assert( x<pIdx->pTable->nCol ); |
| 2064 | wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2065 | } |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2066 | pIdx->szIdxRow = sqlite3LogEst(wIndex*4); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2069 | /* Return true if column number x is any of the first nCol entries of aiCol[]. |
| 2070 | ** This is used to determine if the column number x appears in any of the |
| 2071 | ** first nCol entries of an index. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2072 | */ |
| 2073 | static int hasColumn(const i16 *aiCol, int nCol, int x){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2074 | while( nCol-- > 0 ){ |
| 2075 | assert( aiCol[0]>=0 ); |
| 2076 | if( x==*(aiCol++) ){ |
| 2077 | return 1; |
| 2078 | } |
| 2079 | } |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | /* |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2084 | ** Return true if any of the first nKey entries of index pIdx exactly |
| 2085 | ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID |
| 2086 | ** PRIMARY KEY index. pIdx is an index on the same table. pIdx may |
| 2087 | ** or may not be the same index as pPk. |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2088 | ** |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2089 | ** The first nKey entries of pIdx are guaranteed to be ordinary columns, |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2090 | ** not a rowid or expression. |
| 2091 | ** |
| 2092 | ** This routine differs from hasColumn() in that both the column and the |
| 2093 | ** collating sequence must match for this routine, but for hasColumn() only |
| 2094 | ** the column name must match. |
| 2095 | */ |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2096 | static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2097 | int i, j; |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2098 | assert( nKey<=pIdx->nColumn ); |
| 2099 | assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) ); |
| 2100 | assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); |
| 2101 | assert( pPk->pTable->tabFlags & TF_WithoutRowid ); |
| 2102 | assert( pPk->pTable==pIdx->pTable ); |
| 2103 | testcase( pPk==pIdx ); |
| 2104 | j = pPk->aiColumn[iCol]; |
| 2105 | assert( j!=XN_ROWID && j!=XN_EXPR ); |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2106 | for(i=0; i<nKey; i++){ |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2107 | assert( pIdx->aiColumn[i]>=0 || j>=0 ); |
| 2108 | if( pIdx->aiColumn[i]==j |
| 2109 | && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2110 | ){ |
| 2111 | return 1; |
| 2112 | } |
| 2113 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2114 | return 0; |
| 2115 | } |
| 2116 | |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2117 | /* Recompute the colNotIdxed field of the Index. |
| 2118 | ** |
| 2119 | ** colNotIdxed is a bitmask that has a 0 bit representing each indexed |
| 2120 | ** columns that are within the first 63 columns of the table. The |
| 2121 | ** high-order bit of colNotIdxed is always 1. All unindexed columns |
| 2122 | ** of the table have a 1. |
| 2123 | ** |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2124 | ** 2019-10-24: For the purpose of this computation, virtual columns are |
| 2125 | ** not considered to be covered by the index, even if they are in the |
| 2126 | ** index, because we do not trust the logic in whereIndexExprTrans() to be |
| 2127 | ** able to find all instances of a reference to the indexed table column |
| 2128 | ** and convert them into references to the index. Hence we always want |
| 2129 | ** the actual table at hand in order to recompute the virtual column, if |
| 2130 | ** necessary. |
| 2131 | ** |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2132 | ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask |
| 2133 | ** to determine if the index is covering index. |
| 2134 | */ |
| 2135 | static void recomputeColumnsNotIndexed(Index *pIdx){ |
| 2136 | Bitmask m = 0; |
| 2137 | int j; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2138 | Table *pTab = pIdx->pTable; |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2139 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 2140 | int x = pIdx->aiColumn[j]; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2141 | if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2142 | testcase( x==BMS-1 ); |
| 2143 | testcase( x==BMS-2 ); |
| 2144 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 2145 | } |
| 2146 | } |
| 2147 | pIdx->colNotIdxed = ~m; |
| 2148 | assert( (pIdx->colNotIdxed>>63)==1 ); |
| 2149 | } |
| 2150 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2151 | /* |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2152 | ** This routine runs at the end of parsing a CREATE TABLE statement that |
| 2153 | ** has a WITHOUT ROWID clause. The job of this routine is to convert both |
| 2154 | ** internal schema data structures and the generated VDBE code so that they |
| 2155 | ** are appropriate for a WITHOUT ROWID table instead of a rowid table. |
| 2156 | ** Changes include: |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2157 | ** |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2158 | ** (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] | 2159 | ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY |
| 2160 | ** into BTREE_BLOBKEY. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2161 | ** (3) Bypass the creation of the sqlite_schema table entry |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2162 | ** for the PRIMARY KEY as the primary key index is now |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2163 | ** identified by the sqlite_schema table entry of the table itself. |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2164 | ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2165 | ** schema to the rootpage from the main table. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2166 | ** (5) Add all table columns to the PRIMARY KEY Index object |
| 2167 | ** so that the PRIMARY KEY is a covering index. The surplus |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2168 | ** columns are part of KeyInfo.nAllField and are not used for |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2169 | ** sorting or lookup or uniqueness checks. |
| 2170 | ** (6) Replace the rowid tail on all automatically generated UNIQUE |
| 2171 | ** indices with the PRIMARY KEY columns. |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2172 | ** |
| 2173 | ** For virtual tables, only (1) is performed. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2174 | */ |
| 2175 | static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ |
| 2176 | Index *pIdx; |
| 2177 | Index *pPk; |
| 2178 | int nPk; |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2179 | int nExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2180 | int i, j; |
| 2181 | sqlite3 *db = pParse->db; |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2182 | Vdbe *v = pParse->pVdbe; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2183 | |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2184 | /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) |
| 2185 | */ |
| 2186 | if( !db->init.imposterTable ){ |
| 2187 | for(i=0; i<pTab->nCol; i++){ |
| 2188 | if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){ |
| 2189 | pTab->aCol[i].notNull = OE_Abort; |
| 2190 | } |
| 2191 | } |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 2192 | pTab->tabFlags |= TF_HasNotNull; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 2195 | /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY |
| 2196 | ** into BTREE_BLOBKEY. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2197 | */ |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 2198 | assert( !pParse->bReturning ); |
| 2199 | if( pParse->u1.addrCrTab ){ |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2200 | assert( v ); |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 2201 | sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY); |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2204 | /* Locate the PRIMARY KEY index. Or, if this table was originally |
| 2205 | ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
| 2206 | */ |
| 2207 | if( pTab->iPKey>=0 ){ |
| 2208 | ExprList *pList; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 2209 | Token ipkToken; |
drh | 40aced5 | 2016-01-22 17:48:09 +0000 | [diff] [blame] | 2210 | sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 2211 | pList = sqlite3ExprListAppend(pParse, 0, |
| 2212 | sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 2213 | if( pList==0 ){ |
| 2214 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2215 | return; |
| 2216 | } |
dan | f9b0c45 | 2019-05-06 16:15:28 +0000 | [diff] [blame] | 2217 | if( IN_RENAME_OBJECT ){ |
| 2218 | sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); |
| 2219 | } |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2220 | pList->a[0].sortFlags = pParse->iPkSortOrder; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2221 | assert( pParse->pNewTable==pTab ); |
dan | f9b0c45 | 2019-05-06 16:15:28 +0000 | [diff] [blame] | 2222 | pTab->iPKey = -1; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2223 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 2224 | SQLITE_IDXTYPE_PRIMARYKEY); |
drh | 3bb9d75 | 2021-04-13 13:48:31 +0000 | [diff] [blame] | 2225 | if( db->mallocFailed || pParse->nErr ){ |
| 2226 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2227 | return; |
| 2228 | } |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2229 | pPk = sqlite3PrimaryKeyIndex(pTab); |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2230 | assert( pPk->nKeyCol==1 ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 2231 | }else{ |
| 2232 | pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | f0c48b1 | 2019-02-11 01:58:34 +0000 | [diff] [blame] | 2233 | assert( pPk!=0 ); |
dan | c5b7358 | 2015-05-26 11:53:14 +0000 | [diff] [blame] | 2234 | |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2235 | /* |
| 2236 | ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 2237 | ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 2238 | ** code assumes the PRIMARY KEY contains no repeated columns. |
| 2239 | */ |
| 2240 | for(i=j=1; i<pPk->nKeyCol; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2241 | if( isDupColumn(pPk, j, pPk, i) ){ |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2242 | pPk->nColumn--; |
| 2243 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2244 | testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2245 | pPk->azColl[j] = pPk->azColl[i]; |
| 2246 | pPk->aSortOrder[j] = pPk->aSortOrder[i]; |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2247 | pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 2248 | } |
| 2249 | } |
| 2250 | pPk->nKeyCol = j; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2251 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2252 | assert( pPk!=0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2253 | pPk->isCovering = 1; |
| 2254 | if( !db->init.imposterTable ) pPk->uniqNotNull = 1; |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2255 | nPk = pPk->nColumn = pPk->nKeyCol; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2256 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2257 | /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema |
drh | df94966 | 2017-07-30 18:40:52 +0000 | [diff] [blame] | 2258 | ** table entry. This is only required if currently generating VDBE |
| 2259 | ** code for a CREATE TABLE (not when parsing one as part of reading |
| 2260 | ** a database schema). */ |
| 2261 | if( v && pPk->tnum>0 ){ |
| 2262 | assert( db->init.busy==0 ); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2263 | sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto); |
drh | df94966 | 2017-07-30 18:40:52 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2266 | /* The root page of the PRIMARY KEY is the table root page */ |
| 2267 | pPk->tnum = pTab->tnum; |
| 2268 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2269 | /* Update the in-memory representation of all UNIQUE indices by converting |
| 2270 | ** the final rowid column into one or more columns of the PRIMARY KEY. |
| 2271 | */ |
| 2272 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2273 | int n; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2274 | if( IsPrimaryKeyIndex(pIdx) ) continue; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2275 | for(i=n=0; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2276 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2277 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
| 2278 | n++; |
| 2279 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2280 | } |
drh | 5a9a37b | 2013-11-05 17:30:04 +0000 | [diff] [blame] | 2281 | if( n==0 ){ |
| 2282 | /* This index is a superset of the primary key */ |
| 2283 | pIdx->nColumn = pIdx->nKeyCol; |
| 2284 | continue; |
| 2285 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2286 | if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; |
| 2287 | for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2288 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2289 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2290 | pIdx->aiColumn[j] = pPk->aiColumn[i]; |
| 2291 | pIdx->azColl[j] = pPk->azColl[i]; |
drh | bf9ff25 | 2019-05-14 00:43:13 +0000 | [diff] [blame] | 2292 | if( pPk->aSortOrder[i] ){ |
| 2293 | /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */ |
| 2294 | pIdx->bAscKeyBug = 1; |
| 2295 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2296 | j++; |
| 2297 | } |
| 2298 | } |
drh | 00012df | 2013-11-05 01:59:07 +0000 | [diff] [blame] | 2299 | assert( pIdx->nColumn>=pIdx->nKeyCol+n ); |
| 2300 | assert( pIdx->nColumn>=j ); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | /* Add all table columns to the PRIMARY KEY index |
| 2304 | */ |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2305 | nExtra = 0; |
| 2306 | for(i=0; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2307 | if( !hasColumn(pPk->aiColumn, nPk, i) |
| 2308 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2309 | } |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2310 | if( resizeIndexObject(db, pPk, nPk+nExtra) ) return; |
| 2311 | for(i=0, j=nPk; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2312 | if( !hasColumn(pPk->aiColumn, j, i) |
| 2313 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 |
| 2314 | ){ |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2315 | assert( j<pPk->nColumn ); |
| 2316 | pPk->aiColumn[j] = i; |
| 2317 | pPk->azColl[j] = sqlite3StrBINARY; |
| 2318 | j++; |
| 2319 | } |
| 2320 | } |
| 2321 | assert( pPk->nColumn==j ); |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2322 | assert( pTab->nNVCol<=j ); |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2323 | recomputeColumnsNotIndexed(pPk); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2326 | |
| 2327 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2328 | /* |
| 2329 | ** Return true if pTab is a virtual table and zName is a shadow table name |
| 2330 | ** for that virtual table. |
| 2331 | */ |
| 2332 | int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ |
| 2333 | int nName; /* Length of zName */ |
| 2334 | Module *pMod; /* Module for the virtual table */ |
| 2335 | |
| 2336 | if( !IsVirtual(pTab) ) return 0; |
| 2337 | nName = sqlite3Strlen30(pTab->zName); |
| 2338 | if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; |
| 2339 | if( zName[nName]!='_' ) return 0; |
| 2340 | pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]); |
| 2341 | if( pMod==0 ) return 0; |
| 2342 | if( pMod->pModule->iVersion<3 ) return 0; |
| 2343 | if( pMod->pModule->xShadowName==0 ) return 0; |
| 2344 | return pMod->pModule->xShadowName(zName+nName+1); |
| 2345 | } |
| 2346 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 2347 | |
dan | f6e015f | 2018-11-28 08:02:28 +0000 | [diff] [blame] | 2348 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2349 | /* |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2350 | ** Return true if zName is a shadow table name in the current database |
| 2351 | ** connection. |
| 2352 | ** |
| 2353 | ** zName is temporarily modified while this routine is running, but is |
| 2354 | ** restored to its original value prior to this routine returning. |
| 2355 | */ |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 2356 | int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2357 | char *zTail; /* Pointer to the last "_" in zName */ |
| 2358 | Table *pTab; /* Table that zName is a shadow of */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2359 | zTail = strrchr(zName, '_'); |
| 2360 | if( zTail==0 ) return 0; |
| 2361 | *zTail = 0; |
| 2362 | pTab = sqlite3FindTable(db, zName, 0); |
| 2363 | *zTail = '_'; |
| 2364 | if( pTab==0 ) return 0; |
| 2365 | if( !IsVirtual(pTab) ) return 0; |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2366 | return sqlite3IsShadowTableOf(db, pTab, zName); |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2367 | } |
dan | f6e015f | 2018-11-28 08:02:28 +0000 | [diff] [blame] | 2368 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2369 | |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2370 | |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2371 | #ifdef SQLITE_DEBUG |
| 2372 | /* |
| 2373 | ** Mark all nodes of an expression as EP_Immutable, indicating that |
| 2374 | ** they should not be changed. Expressions attached to a table or |
| 2375 | ** index definition are tagged this way to help ensure that we do |
| 2376 | ** not pass them into code generator routines by mistake. |
| 2377 | */ |
| 2378 | static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){ |
| 2379 | ExprSetVVAProperty(pExpr, EP_Immutable); |
| 2380 | return WRC_Continue; |
| 2381 | } |
| 2382 | static void markExprListImmutable(ExprList *pList){ |
| 2383 | if( pList ){ |
| 2384 | Walker w; |
| 2385 | memset(&w, 0, sizeof(w)); |
| 2386 | w.xExprCallback = markImmutableExprStep; |
| 2387 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 2388 | w.xSelectCallback2 = 0; |
| 2389 | sqlite3WalkExprList(&w, pList); |
| 2390 | } |
| 2391 | } |
| 2392 | #else |
| 2393 | #define markExprListImmutable(X) /* no-op */ |
| 2394 | #endif /* SQLITE_DEBUG */ |
| 2395 | |
| 2396 | |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2397 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2398 | ** This routine is called to report the final ")" that terminates |
| 2399 | ** a CREATE TABLE statement. |
| 2400 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2401 | ** The table structure that other action routines have been building |
| 2402 | ** is added to the internal hash tables, assuming no errors have |
| 2403 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2404 | ** |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2405 | ** 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] | 2406 | ** 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] | 2407 | ** it means we are reading the sqlite_schema table because we just |
| 2408 | ** connected to the database or because the sqlite_schema table has |
drh | ddba9e5 | 2005-03-19 01:41:21 +0000 | [diff] [blame] | 2409 | ** recently changed, so the entry for this table already exists in |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2410 | ** the sqlite_schema table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2411 | ** |
| 2412 | ** If the pSelect argument is not NULL, it means that this routine |
| 2413 | ** was called to create a table generated from a |
| 2414 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 2415 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2416 | */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2417 | void sqlite3EndTable( |
| 2418 | Parse *pParse, /* Parse context */ |
| 2419 | Token *pCons, /* The ',' token after the last column defn. */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2420 | Token *pEnd, /* The ')' before options in the CREATE TABLE */ |
| 2421 | u8 tabOpts, /* Extra table options. Usually 0. */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2422 | Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
| 2423 | ){ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2424 | Table *p; /* The new table */ |
| 2425 | sqlite3 *db = pParse->db; /* The database connection */ |
| 2426 | int iDb; /* Database in which the table lives */ |
| 2427 | Index *pIdx; /* An implied index of the table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2428 | |
drh | 027616d | 2015-08-08 22:47:47 +0000 | [diff] [blame] | 2429 | if( pEnd==0 && pSelect==0 ){ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2430 | return; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2431 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 2432 | p = pParse->pNewTable; |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2433 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2434 | |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 2435 | if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2436 | p->tabFlags |= TF_Shadow; |
| 2437 | } |
| 2438 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2439 | /* 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] | 2440 | ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2441 | ** So do not write to the disk again. Extract the root page number |
| 2442 | ** for the table from the db->init.newTnum field. (The page number |
| 2443 | ** should have been put there by the sqliteOpenCb routine.) |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2444 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2445 | ** 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] | 2446 | ** table itself. So mark it read-only. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2447 | */ |
| 2448 | if( db->init.busy ){ |
drh | 1e9c47b | 2018-03-16 20:15:58 +0000 | [diff] [blame] | 2449 | if( pSelect ){ |
| 2450 | sqlite3ErrorMsg(pParse, ""); |
| 2451 | return; |
| 2452 | } |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2453 | p->tnum = db->init.newTnum; |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2454 | if( p->tnum==1 ) p->tabFlags |= TF_Readonly; |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
drh | 3cbd2b7 | 2019-02-19 13:51:58 +0000 | [diff] [blame] | 2457 | assert( (p->tabFlags & TF_HasPrimaryKey)==0 |
| 2458 | || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 ); |
| 2459 | assert( (p->tabFlags & TF_HasPrimaryKey)!=0 |
| 2460 | || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) ); |
| 2461 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2462 | /* Special processing for WITHOUT ROWID Tables */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2463 | if( tabOpts & TF_WithoutRowid ){ |
drh | d2fe335 | 2013-11-09 18:15:35 +0000 | [diff] [blame] | 2464 | if( (p->tabFlags & TF_Autoincrement) ){ |
| 2465 | sqlite3ErrorMsg(pParse, |
| 2466 | "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
| 2467 | return; |
| 2468 | } |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2469 | if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
drh | d2fe335 | 2013-11-09 18:15:35 +0000 | [diff] [blame] | 2470 | sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2471 | return; |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 2472 | } |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2473 | p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 2474 | convertToWithoutRowidTable(pParse, p); |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 2475 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2476 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2477 | |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2478 | #ifndef SQLITE_OMIT_CHECK |
| 2479 | /* Resolve names in all CHECK constraint expressions. |
| 2480 | */ |
| 2481 | if( p->pCheck ){ |
drh | 3780be1 | 2013-07-31 19:05:22 +0000 | [diff] [blame] | 2482 | sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 +0000 | [diff] [blame] | 2483 | if( pParse->nErr ){ |
| 2484 | /* If errors are seen, delete the CHECK constraints now, else they might |
| 2485 | ** actually be used if PRAGMA writable_schema=ON is set. */ |
| 2486 | sqlite3ExprListDelete(db, p->pCheck); |
| 2487 | p->pCheck = 0; |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2488 | }else{ |
| 2489 | markExprListImmutable(p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 +0000 | [diff] [blame] | 2490 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2491 | } |
| 2492 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2493 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2494 | if( p->tabFlags & TF_HasGenerated ){ |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2495 | int ii, nNG = 0; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2496 | testcase( p->tabFlags & TF_HasVirtual ); |
| 2497 | testcase( p->tabFlags & TF_HasStored ); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2498 | for(ii=0; ii<p->nCol; ii++){ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 +0000 | [diff] [blame] | 2499 | u32 colFlags = p->aCol[ii].colFlags; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2500 | if( (colFlags & COLFLAG_GENERATED)!=0 ){ |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2501 | Expr *pX = p->aCol[ii].pDflt; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2502 | testcase( colFlags & COLFLAG_VIRTUAL ); |
| 2503 | testcase( colFlags & COLFLAG_STORED ); |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2504 | if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){ |
| 2505 | /* If there are errors in resolving the expression, change the |
| 2506 | ** expression to a NULL. This prevents code generators that operate |
| 2507 | ** on the expression from inserting extra parts into the expression |
| 2508 | ** tree that have been allocated from lookaside memory, which is |
drh | 2d58b7f | 2020-01-17 23:27:41 +0000 | [diff] [blame] | 2509 | ** illegal in a schema and will lead to errors or heap corruption |
| 2510 | ** when the database connection closes. */ |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2511 | sqlite3ExprDelete(db, pX); |
| 2512 | p->aCol[ii].pDflt = sqlite3ExprAlloc(db, TK_NULL, 0, 0); |
| 2513 | } |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2514 | }else{ |
| 2515 | nNG++; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2516 | } |
drh | 2c40a3e | 2019-10-29 01:26:24 +0000 | [diff] [blame] | 2517 | } |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2518 | if( nNG==0 ){ |
| 2519 | sqlite3ErrorMsg(pParse, "must have at least one non-generated column"); |
drh | 2c40a3e | 2019-10-29 01:26:24 +0000 | [diff] [blame] | 2520 | return; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2521 | } |
| 2522 | } |
| 2523 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2524 | |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2525 | /* Estimate the average row size for the table and for all implied indices */ |
| 2526 | estimateTableWidth(p); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2527 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2528 | estimateIndexWidth(pIdx); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2531 | /* If not initializing, then create a record for the new table |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2532 | ** in the schema table of the database. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2533 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2534 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 2535 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2536 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2537 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2538 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2539 | Vdbe *v; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2540 | char *zType; /* "view" or "table" */ |
| 2541 | char *zType2; /* "VIEW" or "TABLE" */ |
| 2542 | char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2543 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2544 | v = sqlite3GetVdbe(pParse); |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2545 | if( NEVER(v==0) ) return; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2546 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2547 | sqlite3VdbeAddOp1(v, OP_Close, 0); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 2548 | |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2549 | /* |
| 2550 | ** Initialize zType for the new view or table. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2551 | */ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2552 | if( p->pSelect==0 ){ |
| 2553 | /* A regular table */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2554 | zType = "table"; |
| 2555 | zType2 = "TABLE"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2556 | #ifndef SQLITE_OMIT_VIEW |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2557 | }else{ |
| 2558 | /* A view */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2559 | zType = "view"; |
| 2560 | zType2 = "VIEW"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2561 | #endif |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2562 | } |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2563 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2564 | /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
| 2565 | ** statement to populate the new table. The root-page number for the |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2566 | ** new table is in register pParse->regRoot. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2567 | ** |
| 2568 | ** Once the SELECT has been coded by sqlite3Select(), it is in a |
| 2569 | ** suitable state to query for the column names and types to be used |
| 2570 | ** by the new table. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 2571 | ** |
| 2572 | ** A shared-cache write-lock is not required to write to the new table, |
| 2573 | ** as a schema-lock must have already been obtained to create it. Since |
| 2574 | ** a schema-lock excludes all other database users, the write-lock would |
| 2575 | ** be redundant. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2576 | */ |
| 2577 | if( pSelect ){ |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2578 | SelectDest dest; /* Where the SELECT should store results */ |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2579 | int regYield; /* Register holding co-routine entry-point */ |
| 2580 | int addrTop; /* Top of the co-routine */ |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2581 | int regRec; /* A record to be insert into the new table */ |
| 2582 | int regRowid; /* Rowid of the next row to insert */ |
| 2583 | int addrInsLoop; /* Top of the loop for inserting rows */ |
| 2584 | Table *pSelTab; /* A table that describes the SELECT results */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2585 | |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2586 | regYield = ++pParse->nMem; |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2587 | regRec = ++pParse->nMem; |
| 2588 | regRowid = ++pParse->nMem; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 2589 | assert(pParse->nTab==1); |
drh | 0dd5cda | 2015-06-16 16:39:01 +0000 | [diff] [blame] | 2590 | sqlite3MayAbort(pParse); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2591 | sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 2592 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2593 | pParse->nTab = 2; |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2594 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 2595 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
drh | 512795d | 2017-12-24 18:56:28 +0000 | [diff] [blame] | 2596 | if( pParse->nErr ) return; |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 2597 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2598 | if( pSelTab==0 ) return; |
| 2599 | assert( p->aCol==0 ); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 2600 | p->nCol = p->nNVCol = pSelTab->nCol; |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2601 | p->aCol = pSelTab->aCol; |
| 2602 | pSelTab->nCol = 0; |
| 2603 | pSelTab->aCol = 0; |
| 2604 | sqlite3DeleteTable(db, pSelTab); |
drh | 755b0fd | 2017-12-23 12:33:40 +0000 | [diff] [blame] | 2605 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 2606 | sqlite3Select(pParse, pSelect, &dest); |
drh | 5060a67 | 2017-12-25 13:43:54 +0000 | [diff] [blame] | 2607 | if( pParse->nErr ) return; |
drh | 755b0fd | 2017-12-23 12:33:40 +0000 | [diff] [blame] | 2608 | sqlite3VdbeEndCoroutine(v, regYield); |
| 2609 | sqlite3VdbeJumpHere(v, addrTop - 1); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2610 | addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
| 2611 | VdbeCoverage(v); |
| 2612 | sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
| 2613 | sqlite3TableAffinity(v, p, 0); |
| 2614 | sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); |
| 2615 | sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2616 | sqlite3VdbeGoto(v, addrInsLoop); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2617 | sqlite3VdbeJumpHere(v, addrInsLoop); |
| 2618 | sqlite3VdbeAddOp1(v, OP_Close, 1); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2619 | } |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2620 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2621 | /* Compute the complete text of the CREATE statement */ |
| 2622 | if( pSelect ){ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 2623 | zStmt = createTableStmt(db, p); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2624 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 2625 | Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; |
| 2626 | n = (int)(pEnd2->z - pParse->sNameToken.z); |
| 2627 | if( pEnd2->z[0]!=';' ) n += pEnd2->n; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2628 | zStmt = sqlite3MPrintf(db, |
| 2629 | "CREATE %s %.*s", zType2, n, pParse->sNameToken.z |
| 2630 | ); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | /* A slot for the record has already been allocated in the |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2634 | ** schema table. We just need to update that slot with all |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2635 | ** the information we've collected. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2636 | */ |
| 2637 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2638 | "UPDATE %Q." DFLT_SCHEMA_TABLE |
| 2639 | " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q" |
| 2640 | " WHERE rowid=#%d", |
| 2641 | db->aDb[iDb].zDbSName, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2642 | zType, |
| 2643 | p->zName, |
| 2644 | p->zName, |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2645 | pParse->regRoot, |
| 2646 | zStmt, |
| 2647 | pParse->regRowid |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2648 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2649 | sqlite3DbFree(db, zStmt); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2650 | sqlite3ChangeCookie(pParse, iDb); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2651 | |
| 2652 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2653 | /* Check to see if we need to create an sqlite_sequence table for |
| 2654 | ** keeping track of autoincrement keys. |
| 2655 | */ |
dan | 755ed41 | 2021-04-07 12:02:30 +0000 | [diff] [blame] | 2656 | if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2657 | Db *pDb = &db->aDb[iDb]; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2658 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2659 | if( pDb->pSchema->pSeqTab==0 ){ |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2660 | sqlite3NestedParse(pParse, |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 2661 | "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 2662 | pDb->zDbSName |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2663 | ); |
| 2664 | } |
| 2665 | } |
| 2666 | #endif |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2667 | |
| 2668 | /* Reparse everything to update our internal data structures */ |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 2669 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 2670 | sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2671 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2672 | |
| 2673 | /* Add the table to the in-memory representation of the database. |
| 2674 | */ |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 2675 | if( db->init.busy ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2676 | Table *pOld; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2677 | Schema *pSchema = p->pSchema; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2678 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 2679 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 2680 | pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2681 | if( pOld ){ |
| 2682 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2683 | sqlite3OomFault(db); |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2684 | return; |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2685 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2686 | pParse->pNewTable = 0; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2687 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | d4b6469 | 2021-04-06 16:16:15 +0000 | [diff] [blame] | 2688 | |
| 2689 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 2690 | ** then record a pointer to this table in the main database structure |
| 2691 | ** so that INSERT can find the table easily. */ |
| 2692 | assert( !pParse->nested ); |
| 2693 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2694 | if( strcmp(p->zName, "sqlite_sequence")==0 ){ |
| 2695 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2696 | p->pSchema->pSeqTab = p; |
| 2697 | } |
| 2698 | #endif |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2699 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2700 | |
| 2701 | #ifndef SQLITE_OMIT_ALTERTABLE |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2702 | if( !pSelect && !p->pSelect ){ |
| 2703 | assert( pCons && pEnd ); |
| 2704 | if( pCons->z==0 ){ |
| 2705 | pCons = pEnd; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2706 | } |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2707 | p->addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2708 | } |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2709 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2712 | #ifndef SQLITE_OMIT_VIEW |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2713 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2714 | ** The parser calls this routine in order to create a new VIEW |
| 2715 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2716 | void sqlite3CreateView( |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2717 | Parse *pParse, /* The parsing context */ |
| 2718 | Token *pBegin, /* The CREATE token that begins the statement */ |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 2719 | Token *pName1, /* The token that holds the name of the view */ |
| 2720 | Token *pName2, /* The token that holds the name of the view */ |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2721 | ExprList *pCNames, /* Optional list of view column names */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 2722 | Select *pSelect, /* A SELECT statement that will become the new view */ |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 2723 | int isTemp, /* TRUE for a TEMPORARY view */ |
| 2724 | int noErr /* Suppress error messages if VIEW already exists */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2725 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2726 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2727 | int n; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 2728 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2729 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 2730 | DbFixer sFix; |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 2731 | Token *pName = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2732 | int iDb; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2733 | sqlite3 *db = pParse->db; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2734 | |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 2735 | if( pParse->nVar>0 ){ |
| 2736 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
drh | 32498f1 | 2015-09-26 11:15:44 +0000 | [diff] [blame] | 2737 | goto create_view_fail; |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 2738 | } |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 2739 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2740 | p = pParse->pNewTable; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2741 | if( p==0 || pParse->nErr ) goto create_view_fail; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 2742 | |
| 2743 | /* Legacy versions of SQLite allowed the use of the magic "rowid" column |
| 2744 | ** on a view, even though views do not have rowids. The following flag |
| 2745 | ** setting fixes this problem. But the fix can be disabled by compiling |
| 2746 | ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that |
| 2747 | ** depend upon the old buggy behavior. */ |
| 2748 | #ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
drh | a6c54de | 2021-04-06 19:13:44 +0000 | [diff] [blame] | 2749 | p->tabFlags |= TF_NoVisibleRowid; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 2750 | #endif |
| 2751 | |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2752 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2753 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
drh | d100f69 | 2013-10-03 15:39:44 +0000 | [diff] [blame] | 2754 | sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2755 | if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 2756 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2757 | /* Make a copy of the entire SELECT statement that defines the view. |
| 2758 | ** This will force all the Expr.token.z values to be dynamically |
| 2759 | ** allocated rather than point to the input string - which means that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 2760 | ** they will persist after the current sqlite3_exec() call returns. |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2761 | */ |
dan | 3809696 | 2019-12-09 08:13:43 +0000 | [diff] [blame] | 2762 | pSelect->selFlags |= SF_View; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 2763 | if( IN_RENAME_OBJECT ){ |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 2764 | p->pSelect = pSelect; |
| 2765 | pSelect = 0; |
| 2766 | }else{ |
| 2767 | p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
| 2768 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2769 | p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); |
| 2770 | if( db->mallocFailed ) goto create_view_fail; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2771 | |
| 2772 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 2773 | ** the end. |
| 2774 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2775 | sEnd = pParse->sLastToken; |
drh | 6116ee4 | 2018-01-10 00:40:06 +0000 | [diff] [blame] | 2776 | assert( sEnd.z[0]!=0 || sEnd.n==0 ); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2777 | if( sEnd.z[0]!=';' ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2778 | sEnd.z += sEnd.n; |
| 2779 | } |
| 2780 | sEnd.n = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 2781 | n = (int)(sEnd.z - pBegin->z); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2782 | assert( n>0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 2783 | z = pBegin->z; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2784 | while( sqlite3Isspace(z[n-1]) ){ n--; } |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2785 | sEnd.z = &z[n-1]; |
| 2786 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2787 | |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2788 | /* Use sqlite3EndTable() to add the view to the schema table */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2789 | sqlite3EndTable(pParse, 0, &sEnd, 0, 0); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2790 | |
| 2791 | create_view_fail: |
| 2792 | sqlite3SelectDelete(db, pSelect); |
dan | e8ab40d | 2018-09-12 08:51:48 +0000 | [diff] [blame] | 2793 | if( IN_RENAME_OBJECT ){ |
| 2794 | sqlite3RenameExprlistUnmap(pParse, pCNames); |
| 2795 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2796 | sqlite3ExprListDelete(db, pCNames); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2797 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2798 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2799 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2800 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2801 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2802 | /* |
| 2803 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 2804 | ** the columns of the view in the pTable structure. Return the number |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 2805 | ** 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] | 2806 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2807 | int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 2808 | Table *pSelTab; /* A fake table from which we get the result set */ |
| 2809 | Select *pSel; /* Copy of the SELECT that implements the view */ |
| 2810 | int nErr = 0; /* Number of errors encountered */ |
| 2811 | int n; /* Temporarily holds the number of cursors assigned */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2812 | sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
drh | 424981d | 2018-03-28 15:56:55 +0000 | [diff] [blame] | 2813 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 2814 | int rc; |
| 2815 | #endif |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2816 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 32c6a48 | 2014-09-11 13:44:52 +0000 | [diff] [blame] | 2817 | sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2818 | #endif |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2819 | |
| 2820 | assert( pTable ); |
| 2821 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2822 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 2823 | db->nSchemaLock++; |
| 2824 | rc = sqlite3VtabCallConnect(pParse, pTable); |
| 2825 | db->nSchemaLock--; |
| 2826 | if( rc ){ |
drh | efaffb6 | 2017-08-17 18:23:46 +0000 | [diff] [blame] | 2827 | return 1; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2828 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 2829 | if( IsVirtual(pTable) ) return 0; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2830 | #endif |
| 2831 | |
| 2832 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2833 | /* A positive nCol means the columns names for this view are |
| 2834 | ** already known. |
| 2835 | */ |
| 2836 | if( pTable->nCol>0 ) return 0; |
| 2837 | |
| 2838 | /* A negative nCol is a special marker meaning that we are currently |
| 2839 | ** trying to compute the column names. If we enter this routine with |
| 2840 | ** a negative nCol, it means two or more views form a loop, like this: |
| 2841 | ** |
| 2842 | ** CREATE VIEW one AS SELECT * FROM two; |
| 2843 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 2844 | ** |
drh | 768578e | 2009-05-12 00:40:12 +0000 | [diff] [blame] | 2845 | ** Actually, the error above is now caught prior to reaching this point. |
| 2846 | ** But the following test is still important as it does come up |
| 2847 | ** in the following: |
| 2848 | ** |
| 2849 | ** CREATE TABLE main.ex1(a); |
| 2850 | ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; |
| 2851 | ** SELECT * FROM temp.ex1; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2852 | */ |
| 2853 | if( pTable->nCol<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2854 | sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2855 | return 1; |
| 2856 | } |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 2857 | assert( pTable->nCol>=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2858 | |
| 2859 | /* 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] | 2860 | ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
| 2861 | ** "*" elements in the results set of the view and will assign cursors |
| 2862 | ** to the elements of the FROM clause. But we do not want these changes |
| 2863 | ** to be permanent. So the computation is done on a copy of the SELECT |
| 2864 | ** statement that defines the view. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2865 | */ |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 2866 | assert( pTable->pSelect ); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2867 | pSel = sqlite3SelectDup(db, pTable->pSelect, 0); |
| 2868 | if( pSel ){ |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 2869 | u8 eParseMode = pParse->eParseMode; |
| 2870 | pParse->eParseMode = PARSE_MODE_NORMAL; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2871 | n = pParse->nTab; |
| 2872 | sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 2873 | pTable->nCol = -1; |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 2874 | DisableLookaside; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 2875 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2876 | xAuth = db->xAuth; |
| 2877 | db->xAuth = 0; |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 2878 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2879 | db->xAuth = xAuth; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 2880 | #else |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 2881 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 2882 | #endif |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2883 | pParse->nTab = n; |
dan | 5d59102 | 2019-12-28 08:26:47 +0000 | [diff] [blame] | 2884 | if( pSelTab==0 ){ |
| 2885 | pTable->nCol = 0; |
| 2886 | nErr++; |
| 2887 | }else if( pTable->pCheck ){ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2888 | /* CREATE VIEW name(arglist) AS ... |
| 2889 | ** The names of the columns in the table are taken from |
| 2890 | ** arglist which is stored in pTable->pCheck. The pCheck field |
| 2891 | ** normally holds CHECK constraints on an ordinary table, but for |
| 2892 | ** a VIEW it holds the list of column names. |
| 2893 | */ |
| 2894 | sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
| 2895 | &pTable->nCol, &pTable->aCol); |
| 2896 | if( db->mallocFailed==0 |
drh | 4c983b2 | 2020-01-03 14:34:04 +0000 | [diff] [blame] | 2897 | && pParse->nErr==0 |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2898 | && pTable->nCol==pSel->pEList->nExpr |
| 2899 | ){ |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 2900 | sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel, |
| 2901 | SQLITE_AFF_NONE); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2902 | } |
dan | 5d59102 | 2019-12-28 08:26:47 +0000 | [diff] [blame] | 2903 | }else{ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2904 | /* CREATE VIEW name AS... without an argument list. Construct |
| 2905 | ** the column names from the SELECT statement that defines the view. |
| 2906 | */ |
| 2907 | assert( pTable->aCol==0 ); |
drh | 0383661 | 2019-11-02 17:59:10 +0000 | [diff] [blame] | 2908 | pTable->nCol = pSelTab->nCol; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2909 | pTable->aCol = pSelTab->aCol; |
dan | 3dc864b | 2021-02-25 16:55:47 +0000 | [diff] [blame] | 2910 | pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2911 | pSelTab->nCol = 0; |
| 2912 | pSelTab->aCol = 0; |
| 2913 | assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2914 | } |
drh | 0383661 | 2019-11-02 17:59:10 +0000 | [diff] [blame] | 2915 | pTable->nNVCol = pTable->nCol; |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 2916 | sqlite3DeleteTable(db, pSelTab); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2917 | sqlite3SelectDelete(db, pSel); |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 2918 | EnableLookaside; |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 2919 | pParse->eParseMode = eParseMode; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2920 | } else { |
| 2921 | nErr++; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2922 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2923 | pTable->pSchema->schemaFlags |= DB_UnresetViews; |
dan | 77f3f40 | 2018-07-09 18:55:44 +0000 | [diff] [blame] | 2924 | if( db->mallocFailed ){ |
| 2925 | sqlite3DeleteColumnNames(db, pTable); |
| 2926 | pTable->aCol = 0; |
| 2927 | pTable->nCol = 0; |
| 2928 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2929 | #endif /* SQLITE_OMIT_VIEW */ |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 2930 | return nErr; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2931 | } |
| 2932 | #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2933 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2934 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2935 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2936 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2937 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2938 | static void sqliteViewResetAll(sqlite3 *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2939 | HashElem *i; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2940 | assert( sqlite3SchemaMutexHeld(db, idx, 0) ); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2941 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2942 | for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2943 | Table *pTab = sqliteHashData(i); |
| 2944 | if( pTab->pSelect ){ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 2945 | sqlite3DeleteColumnNames(db, pTab); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 2946 | pTab->aCol = 0; |
| 2947 | pTab->nCol = 0; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2948 | } |
| 2949 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 2950 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2951 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2952 | #else |
| 2953 | # define sqliteViewResetAll(A,B) |
| 2954 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2955 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2956 | /* |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2957 | ** This function is called by the VDBE to adjust the internal schema |
| 2958 | ** used by SQLite when the btree layer moves a table root page. The |
| 2959 | ** root-page of a table or index in database iDb has changed from iFrom |
| 2960 | ** to iTo. |
drh | 6205d4a | 2006-03-24 03:36:26 +0000 | [diff] [blame] | 2961 | ** |
| 2962 | ** Ticket #1728: The symbol table might still contain information |
| 2963 | ** on tables and/or indices that are the process of being deleted. |
| 2964 | ** If you are unlucky, one of those deleted indices or tables might |
| 2965 | ** have the same rootpage number as the real table or index that is |
| 2966 | ** being moved. So we cannot stop searching after the first match |
| 2967 | ** because the first match might be for one of the deleted indices |
| 2968 | ** or tables and not the table/index that is actually being moved. |
| 2969 | ** We must continue looping until all tables and indices with |
| 2970 | ** rootpage==iFrom have been converted to have a rootpage of iTo |
| 2971 | ** in order to be certain that we got the right one. |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2972 | */ |
| 2973 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2974 | void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2975 | HashElem *pElem; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2976 | Hash *pHash; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 2977 | Db *pDb; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2978 | |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 2979 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2980 | pDb = &db->aDb[iDb]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2981 | pHash = &pDb->pSchema->tblHash; |
| 2982 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2983 | Table *pTab = sqliteHashData(pElem); |
| 2984 | if( pTab->tnum==iFrom ){ |
| 2985 | pTab->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2986 | } |
| 2987 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2988 | pHash = &pDb->pSchema->idxHash; |
| 2989 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2990 | Index *pIdx = sqliteHashData(pElem); |
| 2991 | if( pIdx->tnum==iFrom ){ |
| 2992 | pIdx->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2993 | } |
| 2994 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2995 | } |
| 2996 | #endif |
| 2997 | |
| 2998 | /* |
| 2999 | ** Write code to erase the table with root-page iTable from database iDb. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3000 | ** Also write code to modify the sqlite_schema table and internal schema |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3001 | ** if a root-page of another table is moved by the btree-layer whilst |
| 3002 | ** erasing iTable (this can happen with an auto-vacuum database). |
| 3003 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3004 | static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 3005 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3006 | int r1 = sqlite3GetTempReg(pParse); |
drh | 1991888 | 2020-07-30 23:47:00 +0000 | [diff] [blame] | 3007 | if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3008 | sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3009 | sqlite3MayAbort(pParse); |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 3010 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3011 | /* OP_Destroy stores an in integer r1. If this integer |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3012 | ** 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] | 3013 | ** location iTable. The following code modifies the sqlite_schema table to |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3014 | ** reflect this. |
| 3015 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 3016 | ** The "#NNN" in the SQL is a special constant that means whatever value |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 3017 | ** is in register NNN. See grammar rules associated with the TK_REGISTER |
| 3018 | ** token for additional information. |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3019 | */ |
danielk1977 | 63e3e9f | 2004-11-05 09:19:27 +0000 | [diff] [blame] | 3020 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3021 | "UPDATE %Q." DFLT_SCHEMA_TABLE |
| 3022 | " SET rootpage=%d WHERE #%d AND rootpage=#%d", |
| 3023 | pParse->db->aDb[iDb].zDbSName, iTable, r1, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3024 | #endif |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3025 | sqlite3ReleaseTempReg(pParse, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | /* |
| 3029 | ** Write VDBE code to erase table pTab and all associated indices on disk. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3030 | ** Code to update the sqlite_schema tables and internal schema definitions |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3031 | ** in case a root-page belonging to another table is moved by the btree layer |
| 3032 | ** is also added (this can happen with an auto-vacuum database). |
| 3033 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3034 | static void destroyTable(Parse *pParse, Table *pTab){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3035 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
| 3036 | ** is not defined), then it is important to call OP_Destroy on the |
| 3037 | ** table and index root-pages in order, starting with the numerically |
| 3038 | ** largest root-page number. This guarantees that none of the root-pages |
| 3039 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
| 3040 | ** following were coded: |
| 3041 | ** |
| 3042 | ** OP_Destroy 4 0 |
| 3043 | ** ... |
| 3044 | ** OP_Destroy 5 0 |
| 3045 | ** |
| 3046 | ** and root page 5 happened to be the largest root-page number in the |
| 3047 | ** database, then root page 5 would be moved to page 4 by the |
| 3048 | ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
| 3049 | ** a free-list page. |
| 3050 | */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3051 | Pgno iTab = pTab->tnum; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 3052 | Pgno iDestroyed = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3053 | |
| 3054 | while( 1 ){ |
| 3055 | Index *pIdx; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 3056 | Pgno iLargest = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3057 | |
| 3058 | if( iDestroyed==0 || iTab<iDestroyed ){ |
| 3059 | iLargest = iTab; |
| 3060 | } |
| 3061 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3062 | Pgno iIdx = pIdx->tnum; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3063 | assert( pIdx->pSchema==pTab->pSchema ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3064 | if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
| 3065 | iLargest = iIdx; |
| 3066 | } |
| 3067 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3068 | if( iLargest==0 ){ |
| 3069 | return; |
| 3070 | }else{ |
| 3071 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 3072 | assert( iDb>=0 && iDb<pParse->db->nDb ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3073 | destroyRootPage(pParse, iLargest, iDb); |
| 3074 | iDestroyed = iLargest; |
| 3075 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3076 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /* |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3080 | ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3081 | ** after a DROP INDEX or DROP TABLE command. |
| 3082 | */ |
| 3083 | static void sqlite3ClearStatTables( |
| 3084 | Parse *pParse, /* The parsing context */ |
| 3085 | int iDb, /* The database number */ |
| 3086 | const char *zType, /* "idx" or "tbl" */ |
| 3087 | const char *zName /* Name of index or table */ |
| 3088 | ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3089 | int i; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3090 | const char *zDbName = pParse->db->aDb[iDb].zDbSName; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 3091 | for(i=1; i<=4; i++){ |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3092 | char zTab[24]; |
| 3093 | sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); |
| 3094 | if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3095 | sqlite3NestedParse(pParse, |
| 3096 | "DELETE FROM %Q.%s WHERE %s=%Q", |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3097 | zDbName, zTab, zType, zName |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3098 | ); |
| 3099 | } |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3104 | ** Generate code to drop a table. |
| 3105 | */ |
| 3106 | void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 3107 | Vdbe *v; |
| 3108 | sqlite3 *db = pParse->db; |
| 3109 | Trigger *pTrigger; |
| 3110 | Db *pDb = &db->aDb[iDb]; |
| 3111 | |
| 3112 | v = sqlite3GetVdbe(pParse); |
| 3113 | assert( v!=0 ); |
| 3114 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 3115 | |
| 3116 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3117 | if( IsVirtual(pTab) ){ |
| 3118 | sqlite3VdbeAddOp0(v, OP_VBegin); |
| 3119 | } |
| 3120 | #endif |
| 3121 | |
| 3122 | /* Drop all triggers associated with the table being dropped. Code |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3123 | ** is generated to remove entries from sqlite_schema and/or |
| 3124 | ** sqlite_temp_schema if required. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3125 | */ |
| 3126 | pTrigger = sqlite3TriggerList(pParse, pTab); |
| 3127 | while( pTrigger ){ |
| 3128 | assert( pTrigger->pSchema==pTab->pSchema || |
| 3129 | pTrigger->pSchema==db->aDb[1].pSchema ); |
| 3130 | sqlite3DropTriggerPtr(pParse, pTrigger); |
| 3131 | pTrigger = pTrigger->pNext; |
| 3132 | } |
| 3133 | |
| 3134 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 3135 | /* Remove any entries of the sqlite_sequence table associated with |
| 3136 | ** the table being dropped. This is done before the table is dropped |
| 3137 | ** at the btree level, in case the sqlite_sequence table needs to |
| 3138 | ** move as a result of the drop (can happen in auto-vacuum mode). |
| 3139 | */ |
| 3140 | if( pTab->tabFlags & TF_Autoincrement ){ |
| 3141 | sqlite3NestedParse(pParse, |
| 3142 | "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3143 | pDb->zDbSName, pTab->zName |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3144 | ); |
| 3145 | } |
| 3146 | #endif |
| 3147 | |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3148 | /* Drop all entries in the schema table that refer to the |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3149 | ** table. The program name loops through the schema table and deletes |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3150 | ** 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] | 3151 | ** dropped. Triggers are handled separately because a trigger can be |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3152 | ** created in the temp database that refers to a table in another |
| 3153 | ** database. |
| 3154 | */ |
| 3155 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3156 | "DELETE FROM %Q." DFLT_SCHEMA_TABLE |
| 3157 | " WHERE tbl_name=%Q and type!='trigger'", |
| 3158 | pDb->zDbSName, pTab->zName); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3159 | if( !isView && !IsVirtual(pTab) ){ |
| 3160 | destroyTable(pParse, pTab); |
| 3161 | } |
| 3162 | |
| 3163 | /* Remove the table entry from SQLite's internal schema and modify |
| 3164 | ** the schema cookie. |
| 3165 | */ |
| 3166 | if( IsVirtual(pTab) ){ |
| 3167 | sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
dan | 1d4b164 | 2018-12-28 17:45:08 +0000 | [diff] [blame] | 3168 | sqlite3MayAbort(pParse); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3169 | } |
| 3170 | sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 3171 | sqlite3ChangeCookie(pParse, iDb); |
| 3172 | sqliteViewResetAll(db, iDb); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | /* |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3176 | ** Return TRUE if shadow tables should be read-only in the current |
| 3177 | ** context. |
| 3178 | */ |
| 3179 | int sqlite3ReadOnlyShadowTables(sqlite3 *db){ |
| 3180 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3181 | if( (db->flags & SQLITE_Defensive)!=0 |
| 3182 | && db->pVtabCtx==0 |
| 3183 | && db->nVdbeExec==0 |
dan | 7398365 | 2021-07-19 14:00:29 +0000 | [diff] [blame] | 3184 | && !sqlite3VtabInSync(db) |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3185 | ){ |
| 3186 | return 1; |
| 3187 | } |
| 3188 | #endif |
| 3189 | return 0; |
| 3190 | } |
| 3191 | |
| 3192 | /* |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3193 | ** Return true if it is not allowed to drop the given table |
| 3194 | */ |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3195 | static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){ |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3196 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 3197 | if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0; |
| 3198 | if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; |
| 3199 | return 1; |
| 3200 | } |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3201 | if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ |
| 3202 | return 1; |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3203 | } |
| 3204 | return 0; |
| 3205 | } |
| 3206 | |
| 3207 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3208 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3209 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3210 | */ |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3211 | void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3212 | Table *pTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3213 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3214 | sqlite3 *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 3215 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3216 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3217 | if( db->mallocFailed ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 3218 | goto exit_drop_table; |
| 3219 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3220 | assert( pParse->nErr==0 ); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3221 | assert( pName->nSrc==1 ); |
drh | 7520996 | 2015-04-19 22:31:45 +0000 | [diff] [blame] | 3222 | if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 3223 | if( noErr ) db->suppressErr++; |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 3224 | assert( isView==0 || isView==LOCATE_VIEW ); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 3225 | pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 3226 | if( noErr ) db->suppressErr--; |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3227 | |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3228 | if( pTab==0 ){ |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 3229 | if( noErr ){ |
| 3230 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 3231 | sqlite3ForceNotReadOnly(pParse); |
| 3232 | } |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3233 | goto exit_drop_table; |
| 3234 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3235 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3236 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | b5258c3 | 2007-10-04 18:11:15 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
| 3239 | ** it is initialized. |
| 3240 | */ |
| 3241 | if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 3242 | goto exit_drop_table; |
| 3243 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3244 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3245 | { |
| 3246 | int code; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3247 | const char *zTab = SCHEMA_TABLE(iDb); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3248 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3249 | const char *zArg2 = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3250 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3251 | goto exit_drop_table; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3252 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3253 | if( isView ){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3254 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3255 | code = SQLITE_DROP_TEMP_VIEW; |
| 3256 | }else{ |
| 3257 | code = SQLITE_DROP_VIEW; |
| 3258 | } |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3259 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3260 | }else if( IsVirtual(pTab) ){ |
| 3261 | code = SQLITE_DROP_VTABLE; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 3262 | zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3263 | #endif |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3264 | }else{ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3265 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3266 | code = SQLITE_DROP_TEMP_TABLE; |
| 3267 | }else{ |
| 3268 | code = SQLITE_DROP_TABLE; |
| 3269 | } |
| 3270 | } |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3271 | if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3272 | goto exit_drop_table; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3273 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3274 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 3275 | goto exit_drop_table; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 3276 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3277 | } |
| 3278 | #endif |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3279 | if( tableMayNotBeDropped(db, pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3280 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3281 | goto exit_drop_table; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3282 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3283 | |
| 3284 | #ifndef SQLITE_OMIT_VIEW |
| 3285 | /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
| 3286 | ** on a table. |
| 3287 | */ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3288 | if( isView && pTab->pSelect==0 ){ |
| 3289 | sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
| 3290 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3291 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3292 | if( !isView && pTab->pSelect ){ |
| 3293 | sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
| 3294 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3295 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3296 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3297 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3298 | /* Generate code to remove the table from the schema table |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 3299 | ** on disk. |
| 3300 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3301 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3302 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 3303 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
mistachkin | 0fc2da3 | 2018-07-20 20:56:22 +0000 | [diff] [blame] | 3304 | if( !isView ){ |
| 3305 | sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 3306 | sqlite3FkDropTable(pParse, pName, pTab); |
| 3307 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3308 | sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3309 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3310 | |
| 3311 | exit_drop_table: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3312 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3316 | ** This routine is called to create a new foreign key on the table |
| 3317 | ** currently under construction. pFromCol determines which columns |
| 3318 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 3319 | ** connect the key to the last column inserted. pTo is the name of |
drh | bd50a92 | 2013-11-03 02:27:58 +0000 | [diff] [blame] | 3320 | ** the table referred to (a.k.a the "parent" table). pToCol is a list |
| 3321 | ** of tables in the parent pTo table. flags contains all |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3322 | ** information about the conflict resolution algorithms specified |
| 3323 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 3324 | ** |
| 3325 | ** An FKey structure is created and added to the table currently |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3326 | ** under construction in the pParse->pNewTable field. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3327 | ** |
| 3328 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3329 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3330 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3331 | void sqlite3CreateForeignKey( |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3332 | Parse *pParse, /* Parsing context */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3333 | ExprList *pFromCol, /* Columns in this table that point to other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3334 | Token *pTo, /* Name of the other table */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3335 | ExprList *pToCol, /* Columns in the other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3336 | int flags /* Conflict resolution algorithms. */ |
| 3337 | ){ |
danielk1977 | 1857693 | 2008-08-06 13:47:40 +0000 | [diff] [blame] | 3338 | sqlite3 *db = pParse->db; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3339 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 3340 | FKey *pFKey = 0; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3341 | FKey *pNextTo; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3342 | Table *p = pParse->pNewTable; |
| 3343 | int nByte; |
| 3344 | int i; |
| 3345 | int nCol; |
| 3346 | char *z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3347 | |
| 3348 | assert( pTo!=0 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3349 | if( p==0 || IN_DECLARE_VTAB ) goto fk_end; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3350 | if( pFromCol==0 ){ |
| 3351 | int iCol = p->nCol-1; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3352 | if( NEVER(iCol<0) ) goto fk_end; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3353 | if( pToCol && pToCol->nExpr!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3354 | sqlite3ErrorMsg(pParse, "foreign key on %s" |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3355 | " should reference only one column of table %T", |
| 3356 | p->aCol[iCol].zName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3357 | goto fk_end; |
| 3358 | } |
| 3359 | nCol = 1; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3360 | }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3361 | sqlite3ErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3362 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3363 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3364 | goto fk_end; |
| 3365 | }else{ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3366 | nCol = pFromCol->nExpr; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3367 | } |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3368 | nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3369 | if( pToCol ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3370 | for(i=0; i<pToCol->nExpr; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3371 | nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3372 | } |
| 3373 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3374 | pFKey = sqlite3DbMallocZero(db, nByte ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3375 | if( pFKey==0 ){ |
| 3376 | goto fk_end; |
| 3377 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3378 | pFKey->pFrom = p; |
| 3379 | pFKey->pNextFrom = p->pFKey; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3380 | z = (char*)&pFKey->aCol[nCol]; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 3381 | pFKey->zTo = z; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3382 | if( IN_RENAME_OBJECT ){ |
| 3383 | sqlite3RenameTokenMap(pParse, (void*)z, pTo); |
| 3384 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3385 | memcpy(z, pTo->z, pTo->n); |
| 3386 | z[pTo->n] = 0; |
danielk1977 | 70d9e9c | 2009-04-24 18:06:09 +0000 | [diff] [blame] | 3387 | sqlite3Dequote(z); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3388 | z += pTo->n+1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3389 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3390 | if( pFromCol==0 ){ |
| 3391 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 3392 | }else{ |
| 3393 | for(i=0; i<nCol; i++){ |
| 3394 | int j; |
| 3395 | for(j=0; j<p->nCol; j++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3396 | if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zEName)==0 ){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3397 | pFKey->aCol[i].iFrom = j; |
| 3398 | break; |
| 3399 | } |
| 3400 | } |
| 3401 | if( j>=p->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3402 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3403 | "unknown column \"%s\" in foreign key definition", |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3404 | pFromCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3405 | goto fk_end; |
| 3406 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3407 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3408 | sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3409 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | if( pToCol ){ |
| 3413 | for(i=0; i<nCol; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3414 | int n = sqlite3Strlen30(pToCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3415 | pFKey->aCol[i].zCol = z; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3416 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3417 | sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName); |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 3418 | } |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3419 | memcpy(z, pToCol->a[i].zEName, n); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3420 | z[n] = 0; |
| 3421 | z += n+1; |
| 3422 | } |
| 3423 | } |
| 3424 | pFKey->isDeferred = 0; |
dan | 8099ce6 | 2009-09-23 08:43:35 +0000 | [diff] [blame] | 3425 | pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
| 3426 | pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3427 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3428 | assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3429 | pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 3430 | pFKey->zTo, (void *)pFKey |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3431 | ); |
dan | f59c5ca | 2009-09-22 16:55:38 +0000 | [diff] [blame] | 3432 | if( pNextTo==pFKey ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 3433 | sqlite3OomFault(db); |
dan | f59c5ca | 2009-09-22 16:55:38 +0000 | [diff] [blame] | 3434 | goto fk_end; |
| 3435 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3436 | if( pNextTo ){ |
| 3437 | assert( pNextTo->pPrevTo==0 ); |
| 3438 | pFKey->pNextTo = pNextTo; |
| 3439 | pNextTo->pPrevTo = pFKey; |
| 3440 | } |
| 3441 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3442 | /* Link the foreign key to the table as the last step. |
| 3443 | */ |
| 3444 | p->pFKey = pFKey; |
| 3445 | pFKey = 0; |
| 3446 | |
| 3447 | fk_end: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3448 | sqlite3DbFree(db, pFKey); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3449 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3450 | sqlite3ExprListDelete(db, pFromCol); |
| 3451 | sqlite3ExprListDelete(db, pToCol); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | /* |
| 3455 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 3456 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 3457 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 3458 | ** The behavior of the most recently created foreign key is adjusted |
| 3459 | ** accordingly. |
| 3460 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3461 | void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3462 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3463 | Table *pTab; |
| 3464 | FKey *pFKey; |
| 3465 | if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
drh | 4c42983 | 2009-10-12 22:30:49 +0000 | [diff] [blame] | 3466 | assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3467 | pFKey->isDeferred = (u8)isDeferred; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3468 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | /* |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3472 | ** Generate code that will erase and refill index *pIdx. This is |
| 3473 | ** used to initialize a newly created index or to recompute the |
| 3474 | ** content of an index in response to a REINDEX command. |
| 3475 | ** |
| 3476 | ** if memRootPage is not negative, it means that the index is newly |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3477 | ** created. The register specified by memRootPage contains the |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3478 | ** root page number of the index. If memRootPage is negative, then |
| 3479 | ** the index already exists and must be cleared before being refilled and |
| 3480 | ** the root page number of the index is taken from pIndex->tnum. |
| 3481 | */ |
| 3482 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 3483 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 3484 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 3485 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3486 | int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3487 | int addr1; /* Address of top of loop */ |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3488 | int addr2; /* Address to jump to for next iteration */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3489 | Pgno tnum; /* Root page of index */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3490 | int iPartIdxLabel; /* Jump to this label to skip a row */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3491 | Vdbe *v; /* Generate code into this virtual machine */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3492 | KeyInfo *pKey; /* KeyInfo for index */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3493 | int regRecord; /* Register holding assembled index record */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3494 | sqlite3 *db = pParse->db; /* The database connection */ |
| 3495 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3496 | |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 3497 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3498 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3499 | db->aDb[iDb].zDbSName ) ){ |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 3500 | return; |
| 3501 | } |
| 3502 | #endif |
| 3503 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 3504 | /* Require a write-lock on the table to perform this operation */ |
| 3505 | sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 3506 | |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3507 | v = sqlite3GetVdbe(pParse); |
| 3508 | if( v==0 ) return; |
| 3509 | if( memRootPage>=0 ){ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3510 | tnum = (Pgno)memRootPage; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3511 | }else{ |
| 3512 | tnum = pIndex->tnum; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3513 | } |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3514 | pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
drh | 60de73e | 2016-04-05 15:59:23 +0000 | [diff] [blame] | 3515 | assert( pKey!=0 || db->mallocFailed || pParse->nErr ); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3516 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3517 | /* Open the sorter cursor if we are to use one. */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3518 | iSorter = pParse->nTab++; |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3519 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3520 | sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3521 | |
| 3522 | /* Open the table. Loop through all rows of the table, inserting index |
| 3523 | ** records into the sorter. */ |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 3524 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3525 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 3526 | regRecord = sqlite3GetTempReg(pParse); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3527 | sqlite3MultiWrite(pParse); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3528 | |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 3529 | sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3530 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
drh | 8774451 | 2014-04-13 19:15:49 +0000 | [diff] [blame] | 3531 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3532 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3533 | sqlite3VdbeJumpHere(v, addr1); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3534 | if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3535 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3536 | (char *)pKey, P4_KEYINFO); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3537 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
| 3538 | |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3539 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
drh | 60de73e | 2016-04-05 15:59:23 +0000 | [diff] [blame] | 3540 | if( IsUniqueIndex(pIndex) ){ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3541 | int j2 = sqlite3VdbeGoto(v, 1); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3542 | addr2 = sqlite3VdbeCurrentAddr(v); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3543 | sqlite3VdbeVerifyAbortable(v, OE_Abort); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 3544 | sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 3545 | pIndex->nKeyCol); VdbeCoverage(v); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 3546 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3547 | sqlite3VdbeJumpHere(v, j2); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3548 | }else{ |
dan | 7ed6c06 | 2019-05-21 16:32:41 +0000 | [diff] [blame] | 3549 | /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not |
| 3550 | ** abort. The exception is if one of the indexed expressions contains a |
| 3551 | ** user function that throws an exception when it is evaluated. But the |
| 3552 | ** overhead of adding a statement journal to a CREATE INDEX statement is |
| 3553 | ** very small (since most of the pages written do not contain content that |
| 3554 | ** needs to be restored if the statement aborts), so we call |
| 3555 | ** sqlite3MayAbort() for all CREATE INDEX statements. */ |
dan | ef14abb | 2019-05-21 14:42:24 +0000 | [diff] [blame] | 3556 | sqlite3MayAbort(pParse); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3557 | addr2 = sqlite3VdbeCurrentAddr(v); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3558 | } |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 3559 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
drh | bf9ff25 | 2019-05-14 00:43:13 +0000 | [diff] [blame] | 3560 | if( !pIndex->bAscKeyBug ){ |
| 3561 | /* This OP_SeekEnd opcode makes index insert for a REINDEX go much |
| 3562 | ** faster by avoiding unnecessary seeks. But the optimization does |
| 3563 | ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables |
| 3564 | ** with DESC primary keys, since those indexes have there keys in |
| 3565 | ** a different order from the main table. |
| 3566 | ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf |
| 3567 | */ |
| 3568 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); |
| 3569 | } |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 3570 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3571 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 3572 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3573 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 3574 | sqlite3VdbeJumpHere(v, addr1); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3575 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3576 | sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 3577 | sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3578 | sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | /* |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3582 | ** Allocate heap space to hold an Index object with nCol columns. |
| 3583 | ** |
| 3584 | ** Increase the allocation size to provide an extra nExtra bytes |
| 3585 | ** of 8-byte aligned space after the Index object and return a |
| 3586 | ** pointer to this extra space in *ppExtra. |
| 3587 | */ |
| 3588 | Index *sqlite3AllocateIndexObject( |
| 3589 | sqlite3 *db, /* Database connection */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3590 | i16 nCol, /* Total number of columns in the index */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3591 | int nExtra, /* Number of bytes of extra space to alloc */ |
| 3592 | char **ppExtra /* Pointer to the "extra" space */ |
| 3593 | ){ |
| 3594 | Index *p; /* Allocated index object */ |
| 3595 | int nByte; /* Bytes of space for Index object + arrays */ |
| 3596 | |
| 3597 | nByte = ROUND8(sizeof(Index)) + /* Index structure */ |
| 3598 | ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3599 | ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3600 | sizeof(i16)*nCol + /* Index.aiColumn */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3601 | sizeof(u8)*nCol); /* Index.aSortOrder */ |
| 3602 | p = sqlite3DbMallocZero(db, nByte + nExtra); |
| 3603 | if( p ){ |
| 3604 | char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 3605 | p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3606 | p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); |
| 3607 | p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3608 | p->aSortOrder = (u8*)pExtra; |
| 3609 | p->nColumn = nCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3610 | p->nKeyCol = nCol - 1; |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3611 | *ppExtra = ((char*)p) + nByte; |
| 3612 | } |
| 3613 | return p; |
| 3614 | } |
| 3615 | |
| 3616 | /* |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3617 | ** If expression list pList contains an expression that was parsed with |
| 3618 | ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in |
| 3619 | ** pParse and return non-zero. Otherwise, return zero. |
| 3620 | */ |
| 3621 | int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ |
| 3622 | if( pList ){ |
| 3623 | int i; |
| 3624 | for(i=0; i<pList->nExpr; i++){ |
| 3625 | if( pList->a[i].bNulls ){ |
| 3626 | u8 sf = pList->a[i].sortFlags; |
| 3627 | sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", |
| 3628 | (sf==0 || sf==3) ? "FIRST" : "LAST" |
| 3629 | ); |
| 3630 | return 1; |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
| 3637 | /* |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3638 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 3639 | ** 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] | 3640 | ** be NULL for a primary key or an index that is created to satisfy a |
| 3641 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 3642 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 3643 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3644 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 3645 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 3646 | ** is a primary key or unique-constraint on the most recent column added |
| 3647 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3648 | */ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3649 | void sqlite3CreateIndex( |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3650 | Parse *pParse, /* All information about this parse */ |
| 3651 | Token *pName1, /* First part of index name. May be NULL */ |
| 3652 | Token *pName2, /* Second part of index name. May be NULL */ |
| 3653 | SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3654 | ExprList *pList, /* A list of columns to be indexed */ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3655 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 1c55ba0 | 2007-07-02 19:31:27 +0000 | [diff] [blame] | 3656 | Token *pStart, /* The CREATE token that begins this statement */ |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 3657 | Expr *pPIWhere, /* WHERE clause for partial indices */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 3658 | int sortOrder, /* Sort order of primary key when pList==NULL */ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3659 | int ifNotExist, /* Omit error if index already exists */ |
| 3660 | u8 idxType /* The index type */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3661 | ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3662 | Table *pTab = 0; /* Table to be indexed */ |
| 3663 | Index *pIndex = 0; /* The index to be created */ |
| 3664 | char *zName = 0; /* Name of the index */ |
| 3665 | int nName; /* Number of characters in zName */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 3666 | int i, j; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3667 | DbFixer sFix; /* For assigning database names to pTable */ |
| 3668 | int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3669 | sqlite3 *db = pParse->db; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3670 | Db *pDb; /* The specific table containing the indexed database */ |
| 3671 | int iDb; /* Index of the database that is being written */ |
| 3672 | Token *pName = 0; /* Unqualified name of the index to create */ |
| 3673 | struct ExprList_item *pListItem; /* For looping over pList */ |
drh | c28c4e5 | 2013-10-03 19:21:41 +0000 | [diff] [blame] | 3674 | int nExtra = 0; /* Space allocated for zExtra[] */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3675 | int nExtraCol; /* Number of extra columns needed */ |
drh | 47b927d | 2013-12-03 00:11:40 +0000 | [diff] [blame] | 3676 | char *zExtra = 0; /* Extra space after the Index object */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3677 | Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3678 | |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3679 | if( db->mallocFailed || pParse->nErr>0 ){ |
| 3680 | goto exit_create_index; |
| 3681 | } |
| 3682 | if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3683 | goto exit_create_index; |
| 3684 | } |
| 3685 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 3686 | goto exit_create_index; |
| 3687 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3688 | if( sqlite3HasExplicitNulls(pParse, pList) ){ |
| 3689 | goto exit_create_index; |
| 3690 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3691 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3692 | /* |
| 3693 | ** Find the table that is to be indexed. Return early if not found. |
| 3694 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3695 | if( pTblName!=0 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3696 | |
| 3697 | /* Use the two-part index name to determine the database |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3698 | ** to search for the table. 'Fix' the table name to this db |
| 3699 | ** before looking up the table. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3700 | */ |
| 3701 | assert( pName1 && pName2 ); |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3702 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3703 | if( iDb<0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3704 | assert( pName && pName->z ); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3705 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3706 | #ifndef SQLITE_OMIT_TEMPDB |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 3707 | /* If the index name was unqualified, check if the table |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3708 | ** is a temp table. If so, set the database to 1. Do not do this |
| 3709 | ** if initialising a database schema. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3710 | */ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3711 | if( !db->init.busy ){ |
| 3712 | pTab = sqlite3SrcListLookup(pParse, pTblName); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3713 | if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3714 | iDb = 1; |
| 3715 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3716 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3717 | #endif |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3718 | |
drh | d100f69 | 2013-10-03 15:39:44 +0000 | [diff] [blame] | 3719 | sqlite3FixInit(&sFix, pParse, iDb, "index", pName); |
| 3720 | if( sqlite3FixSrcList(&sFix, pTblName) ){ |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 3721 | /* Because the parser constructs pTblName from a single identifier, |
| 3722 | ** sqlite3FixSrcList can never fail. */ |
| 3723 | assert(0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3724 | } |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 3725 | pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); |
drh | c31c7c1 | 2012-10-08 23:25:07 +0000 | [diff] [blame] | 3726 | assert( db->mallocFailed==0 || pTab==0 ); |
| 3727 | if( pTab==0 ) goto exit_create_index; |
drh | 989b116 | 2013-08-01 22:27:26 +0000 | [diff] [blame] | 3728 | if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ |
| 3729 | sqlite3ErrorMsg(pParse, |
| 3730 | "cannot create a TEMP index on non-TEMP table \"%s\"", |
| 3731 | pTab->zName); |
| 3732 | goto exit_create_index; |
| 3733 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3734 | if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3735 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 3736 | assert( pName==0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3737 | assert( pStart==0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3738 | pTab = pParse->pNewTable; |
drh | a6370df | 2006-01-04 21:40:06 +0000 | [diff] [blame] | 3739 | if( !pTab ) goto exit_create_index; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3740 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3741 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3742 | pDb = &db->aDb[iDb]; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3743 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3744 | assert( pTab!=0 ); |
| 3745 | assert( pParse->nErr==0 ); |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 3746 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
drh | 3a3a03f | 2014-09-11 16:36:43 +0000 | [diff] [blame] | 3747 | && db->init.busy==0 |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3748 | && pTblName!=0 |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 3749 | #if SQLITE_USER_AUTHENTICATION |
| 3750 | && sqlite3UserAuthTable(pTab->zName)==0 |
| 3751 | #endif |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3752 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3753 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 3754 | goto exit_create_index; |
| 3755 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3756 | #ifndef SQLITE_OMIT_VIEW |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3757 | if( pTab->pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3758 | sqlite3ErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3759 | goto exit_create_index; |
| 3760 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3761 | #endif |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 +0000 | [diff] [blame] | 3762 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3763 | if( IsVirtual(pTab) ){ |
| 3764 | sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); |
| 3765 | goto exit_create_index; |
| 3766 | } |
| 3767 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3768 | |
| 3769 | /* |
| 3770 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3771 | ** index or table with the same name. |
| 3772 | ** |
| 3773 | ** Exception: If we are reading the names of permanent indices from the |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3774 | ** sqlite_schema table (because some other process changed the schema) and |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3775 | ** 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] | 3776 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3777 | ** |
| 3778 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3779 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 3780 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3781 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3782 | if( pName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3783 | zName = sqlite3NameFromToken(db, pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 3784 | if( zName==0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3785 | assert( pName->z!=0 ); |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 3786 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 3787 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 3788 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3789 | if( !IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3790 | if( !db->init.busy ){ |
| 3791 | if( sqlite3FindTable(db, zName, 0)!=0 ){ |
| 3792 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 3793 | goto exit_create_index; |
| 3794 | } |
| 3795 | } |
| 3796 | if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| 3797 | if( !ifNotExist ){ |
| 3798 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 3799 | }else{ |
| 3800 | assert( !db->init.busy ); |
| 3801 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 3802 | sqlite3ForceNotReadOnly(pParse); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3803 | } |
danielk1977 | d45a031 | 2007-03-13 16:32:25 +0000 | [diff] [blame] | 3804 | goto exit_create_index; |
| 3805 | } |
| 3806 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 3807 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3808 | int n; |
| 3809 | Index *pLoop; |
| 3810 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3811 | zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3812 | if( zName==0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3813 | goto exit_create_index; |
| 3814 | } |
drh | 0aafa9c | 2016-08-05 14:35:47 +0000 | [diff] [blame] | 3815 | |
| 3816 | /* Automatic index names generated from within sqlite3_declare_vtab() |
| 3817 | ** must have names that are distinct from normal automatic index names. |
| 3818 | ** The following statement converts "sqlite3_autoindex..." into |
| 3819 | ** "sqlite3_butoindex..." in order to make the names distinct. |
| 3820 | ** The "vtab_err.test" test demonstrates the need of this statement. */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3821 | if( IN_SPECIAL_PARSE ) zName[7]++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3824 | /* Check for authorization to create an index. |
| 3825 | */ |
| 3826 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3827 | if( !IN_RENAME_OBJECT ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3828 | const char *zDb = pDb->zDbSName; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3829 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3830 | goto exit_create_index; |
| 3831 | } |
| 3832 | i = SQLITE_CREATE_INDEX; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3833 | if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3834 | if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3835 | goto exit_create_index; |
| 3836 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3837 | } |
| 3838 | #endif |
| 3839 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3840 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 3841 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3842 | ** So create a fake list to simulate this. |
| 3843 | */ |
| 3844 | if( pList==0 ){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 3845 | Token prevCol; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 3846 | Column *pCol = &pTab->aCol[pTab->nCol-1]; |
| 3847 | pCol->colFlags |= COLFLAG_UNIQUE; |
| 3848 | sqlite3TokenInit(&prevCol, pCol->zName); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 3849 | pList = sqlite3ExprListAppend(pParse, 0, |
| 3850 | sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3851 | if( pList==0 ) goto exit_create_index; |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 3852 | assert( pList->nExpr==1 ); |
dan | 5b32bdf | 2019-08-17 15:47:32 +0000 | [diff] [blame] | 3853 | sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 3854 | }else{ |
| 3855 | sqlite3ExprListCheckLength(pParse, pList, "index"); |
drh | 8fe25c6 | 2019-03-31 21:09:33 +0000 | [diff] [blame] | 3856 | if( pParse->nErr ) goto exit_create_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3859 | /* Figure out how many bytes of space are required to store explicitly |
| 3860 | ** specified collation sequence names. |
| 3861 | */ |
| 3862 | for(i=0; i<pList->nExpr; i++){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3863 | Expr *pExpr = pList->a[i].pExpr; |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 3864 | assert( pExpr!=0 ); |
| 3865 | if( pExpr->op==TK_COLLATE ){ |
dan | 911ce41 | 2013-05-15 15:16:50 +0000 | [diff] [blame] | 3866 | nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3870 | /* |
| 3871 | ** Allocate the index structure. |
| 3872 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3873 | nName = sqlite3Strlen30(zName); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3874 | nExtraCol = pPk ? pPk->nKeyCol : 1; |
drh | 8fe25c6 | 2019-03-31 21:09:33 +0000 | [diff] [blame] | 3875 | assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3876 | pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3877 | nName + nExtra + 1, &zExtra); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3878 | if( db->mallocFailed ){ |
| 3879 | goto exit_create_index; |
| 3880 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3881 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); |
drh | e09b84c | 2011-11-14 02:53:54 +0000 | [diff] [blame] | 3882 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3883 | pIndex->zName = zExtra; |
| 3884 | zExtra += nName + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3885 | memcpy(pIndex->zName, zName, nName+1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3886 | pIndex->pTable = pTab; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3887 | pIndex->onError = (u8)onError; |
drh | 9eade08 | 2013-10-24 14:16:10 +0000 | [diff] [blame] | 3888 | pIndex->uniqNotNull = onError!=OE_None; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3889 | pIndex->idxType = idxType; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3890 | pIndex->pSchema = db->aDb[iDb].pSchema; |
drh | 72ffd09 | 2013-10-30 15:52:32 +0000 | [diff] [blame] | 3891 | pIndex->nKeyCol = pList->nExpr; |
drh | 3780be1 | 2013-07-31 19:05:22 +0000 | [diff] [blame] | 3892 | if( pPIWhere ){ |
| 3893 | sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); |
| 3894 | pIndex->pPartIdxWhere = pPIWhere; |
| 3895 | pPIWhere = 0; |
| 3896 | } |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3897 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3898 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3899 | /* Check to see if we should honor DESC requests on index columns |
| 3900 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3901 | if( pDb->pSchema->file_format>=4 ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3902 | sortOrderMask = -1; /* Honor DESC */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3903 | }else{ |
| 3904 | sortOrderMask = 0; /* Ignore DESC */ |
| 3905 | } |
| 3906 | |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3907 | /* Analyze the list of expressions that form the terms of the index and |
| 3908 | ** report any errors. In the common case where the expression is exactly |
| 3909 | ** a table column, store that column in aiColumn[]. For general expressions, |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 3910 | ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3911 | ** |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3912 | ** TODO: Issue a warning if two or more columns of the index are identical. |
| 3913 | ** TODO: Issue a warning if the table primary key is used as part of the |
| 3914 | ** index key. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3915 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3916 | pListItem = pList->a; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3917 | if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3918 | pIndex->aColExpr = pList; |
| 3919 | pList = 0; |
| 3920 | } |
| 3921 | for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3922 | Expr *pCExpr; /* The i-th index expression */ |
| 3923 | int requestedSortOrder; /* ASC or DESC on the i-th expression */ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 3924 | const char *zColl; /* Collation sequence name */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3925 | |
drh | edb04ed | 2015-09-04 12:54:01 +0000 | [diff] [blame] | 3926 | sqlite3StringToId(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 3927 | sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); |
| 3928 | if( pParse->nErr ) goto exit_create_index; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 3929 | pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 3930 | if( pCExpr->op!=TK_COLUMN ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3931 | if( pTab==pParse->pNewTable ){ |
| 3932 | sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " |
| 3933 | "UNIQUE constraints"); |
| 3934 | goto exit_create_index; |
| 3935 | } |
| 3936 | if( pIndex->aColExpr==0 ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3937 | pIndex->aColExpr = pList; |
| 3938 | pList = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3939 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 3940 | j = XN_EXPR; |
| 3941 | pIndex->aiColumn[i] = XN_EXPR; |
drh | 8492653 | 2015-08-31 19:38:42 +0000 | [diff] [blame] | 3942 | pIndex->uniqNotNull = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3943 | }else{ |
| 3944 | j = pCExpr->iColumn; |
| 3945 | assert( j<=0x7fff ); |
| 3946 | if( j<0 ){ |
| 3947 | j = pTab->iPKey; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 3948 | }else{ |
| 3949 | if( pTab->aCol[j].notNull==0 ){ |
| 3950 | pIndex->uniqNotNull = 0; |
| 3951 | } |
| 3952 | if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ |
| 3953 | pIndex->bHasVCol = 1; |
| 3954 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3955 | } |
| 3956 | pIndex->aiColumn[i] = (i16)j; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 3957 | } |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 3958 | zColl = 0; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 3959 | if( pListItem->pExpr->op==TK_COLLATE ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3960 | int nColl; |
dan | 911ce41 | 2013-05-15 15:16:50 +0000 | [diff] [blame] | 3961 | zColl = pListItem->pExpr->u.zToken; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3962 | nColl = sqlite3Strlen30(zColl) + 1; |
| 3963 | assert( nExtra>=nColl ); |
| 3964 | memcpy(zExtra, zColl, nColl); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3965 | zColl = zExtra; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3966 | zExtra += nColl; |
| 3967 | nExtra -= nColl; |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 3968 | }else if( j>=0 ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3969 | zColl = pTab->aCol[j].zColl; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3970 | } |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 3971 | if( !zColl ) zColl = sqlite3StrBINARY; |
drh | b7f24de | 2009-05-13 17:35:23 +0000 | [diff] [blame] | 3972 | if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 3973 | goto exit_create_index; |
| 3974 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3975 | pIndex->azColl[i] = zColl; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 3976 | requestedSortOrder = pListItem->sortFlags & sortOrderMask; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3977 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3978 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3979 | |
| 3980 | /* Append the table key to the end of the index. For WITHOUT ROWID |
| 3981 | ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For |
| 3982 | ** normal tables (when pPk==0) this will be the rowid. |
| 3983 | */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3984 | if( pPk ){ |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 3985 | for(j=0; j<pPk->nKeyCol; j++){ |
| 3986 | int x = pPk->aiColumn[j]; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3987 | assert( x>=0 ); |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 3988 | if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 3989 | pIndex->nColumn--; |
| 3990 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 3991 | testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 3992 | pIndex->aiColumn[i] = x; |
| 3993 | pIndex->azColl[i] = pPk->azColl[j]; |
| 3994 | pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
| 3995 | i++; |
| 3996 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3997 | } |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 3998 | assert( i==pIndex->nColumn ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3999 | }else{ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4000 | pIndex->aiColumn[i] = XN_ROWID; |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4001 | pIndex->azColl[i] = sqlite3StrBINARY; |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4002 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4003 | sqlite3DefaultRowEst(pIndex); |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 4004 | if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4005 | |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4006 | /* If this index contains every column of its table, then mark |
| 4007 | ** it as a covering index */ |
| 4008 | assert( HasRowid(pTab) |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 4009 | || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 4010 | recomputeColumnsNotIndexed(pIndex); |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4011 | if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ |
| 4012 | pIndex->isCovering = 1; |
| 4013 | for(j=0; j<pTab->nCol; j++){ |
| 4014 | if( j==pTab->iPKey ) continue; |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 4015 | if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4016 | pIndex->isCovering = 0; |
| 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4021 | if( pTab==pParse->pNewTable ){ |
| 4022 | /* This routine has been called to create an automatic index as a |
| 4023 | ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
| 4024 | ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
| 4025 | ** i.e. one of: |
| 4026 | ** |
| 4027 | ** CREATE TABLE t(x PRIMARY KEY, y); |
| 4028 | ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
| 4029 | ** |
| 4030 | ** Either way, check to see if the table already has such an index. If |
| 4031 | ** so, don't bother creating this one. This only applies to |
| 4032 | ** automatically created indices. Users can do as they wish with |
| 4033 | ** explicit indices. |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4034 | ** |
| 4035 | ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent |
| 4036 | ** (and thus suppressing the second one) even if they have different |
| 4037 | ** sort orders. |
| 4038 | ** |
| 4039 | ** If there are different collating sequences or if the columns of |
| 4040 | ** the constraint occur in different orders, then the constraints are |
| 4041 | ** considered distinct and both result in separate indices. |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4042 | */ |
| 4043 | Index *pIdx; |
| 4044 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 4045 | int k; |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4046 | assert( IsUniqueIndex(pIdx) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4047 | assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4048 | assert( IsUniqueIndex(pIndex) ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4049 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4050 | if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
| 4051 | for(k=0; k<pIdx->nKeyCol; k++){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4052 | const char *z1; |
| 4053 | const char *z2; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4054 | assert( pIdx->aiColumn[k]>=0 ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4055 | if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4056 | z1 = pIdx->azColl[k]; |
| 4057 | z2 = pIndex->azColl[k]; |
drh | c41c132 | 2016-02-11 13:30:36 +0000 | [diff] [blame] | 4058 | if( sqlite3StrICmp(z1, z2) ) break; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4059 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4060 | if( k==pIdx->nKeyCol ){ |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 4061 | if( pIdx->onError!=pIndex->onError ){ |
| 4062 | /* This constraint creates the same index as a previous |
| 4063 | ** constraint specified somewhere in the CREATE TABLE statement. |
| 4064 | ** However the ON CONFLICT clauses are different. If both this |
| 4065 | ** constraint and the previous equivalent constraint have explicit |
| 4066 | ** ON CONFLICT clauses this is an error. Otherwise, use the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 4067 | ** explicitly specified behavior for the index. |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 4068 | */ |
| 4069 | if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
| 4070 | sqlite3ErrorMsg(pParse, |
| 4071 | "conflicting ON CONFLICT clauses specified", 0); |
| 4072 | } |
| 4073 | if( pIdx->onError==OE_Default ){ |
| 4074 | pIdx->onError = pIndex->onError; |
| 4075 | } |
| 4076 | } |
drh | 273bfe9 | 2016-06-02 16:22:53 +0000 | [diff] [blame] | 4077 | if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; |
drh | 885eeb6 | 2019-01-09 02:02:24 +0000 | [diff] [blame] | 4078 | if( IN_RENAME_OBJECT ){ |
| 4079 | pIndex->pNext = pParse->pNewIndex; |
| 4080 | pParse->pNewIndex = pIndex; |
| 4081 | pIndex = 0; |
| 4082 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4083 | goto exit_create_index; |
| 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4088 | if( !IN_RENAME_OBJECT ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 4089 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4090 | /* Link the new Index structure to its table and to the other |
| 4091 | ** in-memory database structures. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4092 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4093 | assert( pParse->nErr==0 ); |
| 4094 | if( db->init.busy ){ |
| 4095 | Index *p; |
| 4096 | assert( !IN_SPECIAL_PARSE ); |
| 4097 | assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4098 | if( pTblName!=0 ){ |
| 4099 | pIndex->tnum = db->init.newTnum; |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 4100 | if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ |
drh | 8bf4126 | 2019-01-30 19:50:07 +0000 | [diff] [blame] | 4101 | sqlite3ErrorMsg(pParse, "invalid rootpage"); |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 4102 | pParse->rc = SQLITE_CORRUPT_BKPT; |
| 4103 | goto exit_create_index; |
| 4104 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4105 | } |
dan | da7a4c0 | 2019-01-30 19:12:13 +0000 | [diff] [blame] | 4106 | p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
| 4107 | pIndex->zName, pIndex); |
| 4108 | if( p ){ |
| 4109 | assert( p==pIndex ); /* Malloc must have failed */ |
| 4110 | sqlite3OomFault(db); |
| 4111 | goto exit_create_index; |
| 4112 | } |
| 4113 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4114 | } |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4115 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4116 | /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
| 4117 | ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
| 4118 | ** 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] | 4119 | ** the index in the sqlite_schema table and populate the index with |
| 4120 | ** 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] | 4121 | ** table to parse the schema, or if this index is the PRIMARY KEY index |
| 4122 | ** of a WITHOUT ROWID table. |
| 4123 | ** |
| 4124 | ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY |
| 4125 | ** or UNIQUE index in a CREATE TABLE statement. Since the table |
| 4126 | ** has just been created, it contains no data and the index initialization |
| 4127 | ** step can be skipped. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4128 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4129 | else if( HasRowid(pTab) || pTblName!=0 ){ |
| 4130 | Vdbe *v; |
| 4131 | char *zStmt; |
| 4132 | int iMem = ++pParse->nMem; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4133 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4134 | v = sqlite3GetVdbe(pParse); |
| 4135 | if( v==0 ) goto exit_create_index; |
| 4136 | |
| 4137 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 4138 | |
| 4139 | /* Create the rootpage for the index using CreateIndex. But before |
| 4140 | ** doing so, code a Noop instruction and store its address in |
| 4141 | ** Index.tnum. This is required in case this index is actually a |
| 4142 | ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In |
| 4143 | ** that case the convertToWithoutRowidTable() routine will replace |
| 4144 | ** the Noop with a Goto to jump over the VDBE code generated below. */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4145 | pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4146 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); |
| 4147 | |
| 4148 | /* Gather the complete text of the CREATE INDEX statement into |
| 4149 | ** the zStmt variable |
| 4150 | */ |
drh | 55f66b3 | 2019-07-16 19:44:32 +0000 | [diff] [blame] | 4151 | assert( pName!=0 || pStart==0 ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4152 | if( pStart ){ |
| 4153 | int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; |
| 4154 | if( pName->z[n-1]==';' ) n--; |
| 4155 | /* A named index with an explicit CREATE INDEX statement */ |
| 4156 | zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", |
| 4157 | onError==OE_None ? "" : " UNIQUE", n, pName->z); |
| 4158 | }else{ |
| 4159 | /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
| 4160 | /* zStmt = sqlite3MPrintf(""); */ |
| 4161 | zStmt = 0; |
| 4162 | } |
| 4163 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 4164 | /* Add an entry in sqlite_schema for this index |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4165 | */ |
| 4166 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 4167 | "INSERT INTO %Q." DFLT_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", |
| 4168 | db->aDb[iDb].zDbSName, |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4169 | pIndex->zName, |
| 4170 | pTab->zName, |
| 4171 | iMem, |
| 4172 | zStmt |
| 4173 | ); |
| 4174 | sqlite3DbFree(db, zStmt); |
| 4175 | |
| 4176 | /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 4177 | ** to invalidate all pre-compiled statements. |
| 4178 | */ |
| 4179 | if( pTblName ){ |
| 4180 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 4181 | sqlite3ChangeCookie(pParse, iDb); |
| 4182 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 4183 | sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4184 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); |
| 4185 | } |
| 4186 | |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4187 | sqlite3VdbeJumpHere(v, (int)pIndex->tnum); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4188 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4189 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4190 | if( db->init.busy || pTblName==0 ){ |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4191 | pIndex->pNext = pTab->pIndex; |
| 4192 | pTab->pIndex = pIndex; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4193 | pIndex = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4194 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4195 | else if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4196 | assert( pParse->pNewIndex==0 ); |
| 4197 | pParse->pNewIndex = pIndex; |
| 4198 | pIndex = 0; |
| 4199 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4200 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4201 | /* Clean up before exiting */ |
| 4202 | exit_create_index: |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4203 | if( pIndex ) sqlite3FreeIndex(db, pIndex); |
drh | 97060e5 | 2021-03-21 17:52:47 +0000 | [diff] [blame] | 4204 | if( pTab ){ |
| 4205 | /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list. |
| 4206 | ** The list was already ordered when this routine was entered, so at this |
| 4207 | ** point at most a single index (the newly added index) will be out of |
| 4208 | ** order. So we have to reorder at most one index. */ |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4209 | Index **ppFrom = &pTab->pIndex; |
| 4210 | Index *pThis; |
| 4211 | for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ |
| 4212 | Index *pNext; |
| 4213 | if( pThis->onError!=OE_Replace ) continue; |
| 4214 | while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ |
| 4215 | *ppFrom = pNext; |
| 4216 | pThis->pNext = pNext->pNext; |
| 4217 | pNext->pNext = pThis; |
| 4218 | ppFrom = &pNext->pNext; |
| 4219 | } |
| 4220 | break; |
| 4221 | } |
drh | 97060e5 | 2021-03-21 17:52:47 +0000 | [diff] [blame] | 4222 | #ifdef SQLITE_DEBUG |
| 4223 | /* Verify that all REPLACE indexes really are now at the end |
| 4224 | ** of the index list. In other words, no other index type ever |
| 4225 | ** comes after a REPLACE index on the list. */ |
| 4226 | for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){ |
| 4227 | assert( pThis->onError!=OE_Replace |
| 4228 | || pThis->pNext==0 |
| 4229 | || pThis->pNext->onError==OE_Replace ); |
| 4230 | } |
| 4231 | #endif |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4232 | } |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 4233 | sqlite3ExprDelete(db, pPIWhere); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4234 | sqlite3ExprListDelete(db, pList); |
| 4235 | sqlite3SrcListDelete(db, pTblName); |
| 4236 | sqlite3DbFree(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4240 | ** Fill the Index.aiRowEst[] array with default information - information |
drh | 91124b3 | 2005-08-18 18:15:05 +0000 | [diff] [blame] | 4241 | ** to be used when we have not run the ANALYZE command. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4242 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4243 | ** aiRowEst[0] is supposed to contain the number of elements in the index. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4244 | ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 4245 | ** number of rows in the table that match any particular value of the |
| 4246 | ** first column of the index. aiRowEst[2] is an estimate of the number |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4247 | ** of rows that match any particular combination of the first 2 columns |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4248 | ** of the index. And so forth. It must always be the case that |
| 4249 | * |
| 4250 | ** aiRowEst[N]<=aiRowEst[N-1] |
| 4251 | ** aiRowEst[N]>=1 |
| 4252 | ** |
| 4253 | ** Apart from that, we have little to go on besides intuition as to |
| 4254 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 4255 | ** are based on typical values found in actual indices. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4256 | */ |
| 4257 | void sqlite3DefaultRowEst(Index *pIdx){ |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4258 | /* 10, 9, 8, 7, 6 */ |
| 4259 | static const LogEst aVal[] = { 33, 32, 30, 28, 26 }; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4260 | LogEst *a = pIdx->aiRowLogEst; |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4261 | LogEst x; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4262 | int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4263 | int i; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4264 | |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 4265 | /* Indexes with default row estimates should not have stat1 data */ |
| 4266 | assert( !pIdx->hasStat1 ); |
| 4267 | |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4268 | /* Set the first entry (number of rows in the index) to the estimated |
drh | 8dc570b | 2016-06-08 18:07:21 +0000 | [diff] [blame] | 4269 | ** 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] | 4270 | ** for a partial index. |
| 4271 | ** |
| 4272 | ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 |
| 4273 | ** table but other parts we are having to guess at, then do not let the |
| 4274 | ** estimated number of rows in the table be less than 1000 (LogEst 99). |
| 4275 | ** 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] | 4276 | ** stat1 data to be ignored by the query planner. |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4277 | */ |
| 4278 | x = pIdx->pTable->nRowLogEst; |
| 4279 | assert( 99==sqlite3LogEst(1000) ); |
| 4280 | if( x<99 ){ |
| 4281 | pIdx->pTable->nRowLogEst = x = 99; |
| 4282 | } |
drh | 5f086dd | 2021-04-29 13:37:36 +0000 | [diff] [blame] | 4283 | if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4284 | a[0] = x; |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4285 | |
| 4286 | /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
| 4287 | ** 6 and each subsequent value (if any) is 5. */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4288 | memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4289 | for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ |
| 4290 | a[i] = 23; assert( 23==sqlite3LogEst(5) ); |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4291 | } |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4292 | |
| 4293 | assert( 0==sqlite3LogEst(1) ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4294 | if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 4298 | ** This routine will drop an existing named index. This routine |
| 4299 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4300 | */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4301 | void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4302 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4303 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 4304 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4305 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4306 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4307 | assert( pParse->nErr==0 ); /* Never called with prior errors */ |
| 4308 | if( db->mallocFailed ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 4309 | goto exit_drop_index; |
| 4310 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4311 | assert( pName->nSrc==1 ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 4312 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 4313 | goto exit_drop_index; |
| 4314 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4315 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4316 | if( pIndex==0 ){ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4317 | if( !ifExists ){ |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 4318 | sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 4319 | }else{ |
| 4320 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 4321 | sqlite3ForceNotReadOnly(pParse); |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4322 | } |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 4323 | pParse->checkSchema = 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4324 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4325 | } |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4326 | if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4327 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 4328 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4329 | goto exit_drop_index; |
| 4330 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4331 | iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4332 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 4333 | { |
| 4334 | int code = SQLITE_DROP_INDEX; |
| 4335 | Table *pTab = pIndex->pTable; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4336 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4337 | const char *zTab = SCHEMA_TABLE(iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4338 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4339 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4340 | } |
drh | 93fd542 | 2021-06-14 20:41:20 +0000 | [diff] [blame] | 4341 | if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4342 | if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4343 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4344 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 4345 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4346 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4347 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4348 | /* Generate code to remove the index and from the schema table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4349 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4350 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4351 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4352 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 4353 | "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
| 4354 | db->aDb[iDb].zDbSName, pIndex->zName |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4355 | ); |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 4356 | sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4357 | sqlite3ChangeCookie(pParse, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4358 | destroyRootPage(pParse, pIndex->tnum, iDb); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4359 | sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4362 | exit_drop_index: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4363 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | /* |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4367 | ** pArray is a pointer to an array of objects. Each object in the |
| 4368 | ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() |
| 4369 | ** 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] | 4370 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4371 | ** When this function is called, *pnEntry contains the current size of |
| 4372 | ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes |
| 4373 | ** in total). |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4374 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4375 | ** If the realloc() is successful (i.e. if no OOM condition occurs), the |
| 4376 | ** space allocated for the new object is zeroed, *pnEntry updated to |
| 4377 | ** reflect the new size of the array and a pointer to the new allocation |
| 4378 | ** 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] | 4379 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4380 | ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains |
| 4381 | ** unchanged and a copy of pArray returned. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4382 | */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4383 | void *sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4384 | sqlite3 *db, /* Connection to notify of malloc failures */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4385 | void *pArray, /* Array of objects. Might be reallocated */ |
| 4386 | int szEntry, /* Size of each object in the array */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4387 | int *pnEntry, /* Number of objects currently in use */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4388 | int *pIdx /* Write the index of a new slot here */ |
| 4389 | ){ |
| 4390 | char *z; |
drh | f6ad201 | 2019-04-13 14:07:57 +0000 | [diff] [blame] | 4391 | sqlite3_int64 n = *pIdx = *pnEntry; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4392 | if( (n & (n-1))==0 ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 4393 | sqlite3_int64 sz = (n==0) ? 1 : 2*n; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4394 | void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4395 | if( pNew==0 ){ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4396 | *pIdx = -1; |
| 4397 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4398 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4399 | pArray = pNew; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4400 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4401 | z = (char*)pArray; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4402 | memset(&z[n * szEntry], 0, szEntry); |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4403 | ++*pnEntry; |
| 4404 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4408 | ** Append a new element to the given IdList. Create a new IdList if |
| 4409 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4410 | ** |
| 4411 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4412 | */ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 4413 | IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ |
| 4414 | sqlite3 *db = pParse->db; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4415 | int i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4416 | if( pList==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4417 | pList = sqlite3DbMallocZero(db, sizeof(IdList) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4418 | if( pList==0 ) return 0; |
| 4419 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4420 | pList->a = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4421 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4422 | pList->a, |
| 4423 | sizeof(pList->a[0]), |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4424 | &pList->nId, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4425 | &i |
| 4426 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4427 | if( i<0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4428 | sqlite3IdListDelete(db, pList); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4429 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4430 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4431 | pList->a[i].zName = sqlite3NameFromToken(db, pToken); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4432 | if( IN_RENAME_OBJECT && pList->a[i].zName ){ |
dan | 07e9523 | 2018-08-21 16:32:53 +0000 | [diff] [blame] | 4433 | sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 4434 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4435 | return pList; |
| 4436 | } |
| 4437 | |
| 4438 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4439 | ** Delete an IdList. |
| 4440 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4441 | void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4442 | int i; |
| 4443 | if( pList==0 ) return; |
| 4444 | for(i=0; i<pList->nId; i++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4445 | sqlite3DbFree(db, pList->a[i].zName); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4446 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4447 | sqlite3DbFree(db, pList->a); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4448 | sqlite3DbFreeNN(db, pList); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | /* |
| 4452 | ** Return the index in pList of the identifier named zId. Return -1 |
| 4453 | ** if not found. |
| 4454 | */ |
| 4455 | int sqlite3IdListIndex(IdList *pList, const char *zName){ |
| 4456 | int i; |
| 4457 | if( pList==0 ) return -1; |
| 4458 | for(i=0; i<pList->nId; i++){ |
| 4459 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 4460 | } |
| 4461 | return -1; |
| 4462 | } |
| 4463 | |
| 4464 | /* |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4465 | ** Maximum size of a SrcList object. |
| 4466 | ** The SrcList object is used to represent the FROM clause of a |
| 4467 | ** SELECT statement, and the query planner cannot deal with more |
| 4468 | ** than 64 tables in a join. So any value larger than 64 here |
| 4469 | ** is sufficient for most uses. Smaller values, like say 10, are |
| 4470 | ** appropriate for small and memory-limited applications. |
| 4471 | */ |
| 4472 | #ifndef SQLITE_MAX_SRCLIST |
| 4473 | # define SQLITE_MAX_SRCLIST 200 |
| 4474 | #endif |
| 4475 | |
| 4476 | /* |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4477 | ** Expand the space allocated for the given SrcList object by |
| 4478 | ** creating nExtra new slots beginning at iStart. iStart is zero based. |
| 4479 | ** New slots are zeroed. |
| 4480 | ** |
| 4481 | ** For example, suppose a SrcList initially contains two entries: A,B. |
| 4482 | ** To append 3 new entries onto the end, do this: |
| 4483 | ** |
| 4484 | ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); |
| 4485 | ** |
| 4486 | ** After the call above it would contain: A, B, nil, nil, nil. |
| 4487 | ** If the iStart argument had been 1 instead of 2, then the result |
| 4488 | ** would have been: A, nil, nil, nil, B. To prepend the new slots, |
| 4489 | ** the iStart value would be 0. The result then would |
| 4490 | ** be: nil, nil, nil, A, B. |
| 4491 | ** |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4492 | ** If a memory allocation fails or the SrcList becomes too large, leave |
| 4493 | ** the original SrcList unchanged, return NULL, and leave an error message |
| 4494 | ** in pParse. |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4495 | */ |
| 4496 | SrcList *sqlite3SrcListEnlarge( |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4497 | Parse *pParse, /* Parsing context into which errors are reported */ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4498 | SrcList *pSrc, /* The SrcList to be enlarged */ |
| 4499 | int nExtra, /* Number of new slots to add to pSrc->a[] */ |
| 4500 | int iStart /* Index in pSrc->a[] of first new slot */ |
| 4501 | ){ |
| 4502 | int i; |
| 4503 | |
| 4504 | /* Sanity checking on calling parameters */ |
| 4505 | assert( iStart>=0 ); |
| 4506 | assert( nExtra>=1 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4507 | assert( pSrc!=0 ); |
| 4508 | assert( iStart<=pSrc->nSrc ); |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4509 | |
| 4510 | /* Allocate additional space if needed */ |
drh | fc5717c | 2014-03-05 19:04:46 +0000 | [diff] [blame] | 4511 | if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4512 | SrcList *pNew; |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 4513 | sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4514 | sqlite3 *db = pParse->db; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4515 | |
| 4516 | if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4517 | sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", |
| 4518 | SQLITE_MAX_SRCLIST); |
| 4519 | return 0; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4520 | } |
| 4521 | if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4522 | pNew = sqlite3DbRealloc(db, pSrc, |
| 4523 | sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); |
| 4524 | if( pNew==0 ){ |
| 4525 | assert( db->mallocFailed ); |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4526 | return 0; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4527 | } |
| 4528 | pSrc = pNew; |
drh | d0ee3a1 | 2019-02-06 01:18:36 +0000 | [diff] [blame] | 4529 | pSrc->nAlloc = nAlloc; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | /* Move existing slots that come after the newly inserted slots |
| 4533 | ** out of the way */ |
| 4534 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| 4535 | pSrc->a[i+nExtra] = pSrc->a[i]; |
| 4536 | } |
drh | 6d1626e | 2014-03-05 15:52:43 +0000 | [diff] [blame] | 4537 | pSrc->nSrc += nExtra; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4538 | |
| 4539 | /* Zero the newly allocated slots */ |
| 4540 | memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
| 4541 | for(i=iStart; i<iStart+nExtra; i++){ |
| 4542 | pSrc->a[i].iCursor = -1; |
| 4543 | } |
| 4544 | |
| 4545 | /* Return a pointer to the enlarged SrcList */ |
| 4546 | return pSrc; |
| 4547 | } |
| 4548 | |
| 4549 | |
| 4550 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4551 | ** 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] | 4552 | ** 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] | 4553 | ** |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4554 | ** A SrcList is returned, or NULL if there is an OOM error or if the |
| 4555 | ** SrcList grows to large. The returned |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4556 | ** SrcList might be the same as the SrcList that was input or it might be |
| 4557 | ** a new one. If an OOM error does occurs, then the prior value of pList |
| 4558 | ** that is input to this routine is automatically freed. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4559 | ** |
| 4560 | ** If pDatabase is not null, it means that the table has an optional |
| 4561 | ** database name prefix. Like this: "database.table". The pDatabase |
| 4562 | ** points to the table name and the pTable points to the database name. |
| 4563 | ** The SrcList.a[].zName field is filled with the table name which might |
| 4564 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 4565 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 4566 | ** or with NULL if no database is specified. |
| 4567 | ** |
| 4568 | ** In other words, if call like this: |
| 4569 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4570 | ** sqlite3SrcListAppend(D,A,B,0); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4571 | ** |
| 4572 | ** Then B is a table name and the database name is unspecified. If called |
| 4573 | ** like this: |
| 4574 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4575 | ** sqlite3SrcListAppend(D,A,B,C); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4576 | ** |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4577 | ** Then C is the table name and B is the database name. If C is defined |
| 4578 | ** then so is B. In other words, we never have a case where: |
| 4579 | ** |
| 4580 | ** sqlite3SrcListAppend(D,A,0,C); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 4581 | ** |
| 4582 | ** Both pTable and pDatabase are assumed to be quoted. They are dequoted |
| 4583 | ** before being added to the SrcList. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4584 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4585 | SrcList *sqlite3SrcListAppend( |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4586 | Parse *pParse, /* Parsing context, in which errors are reported */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4587 | SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ |
| 4588 | Token *pTable, /* Table to append */ |
| 4589 | Token *pDatabase /* Database of the table */ |
| 4590 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4591 | SrcItem *pItem; |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4592 | sqlite3 *db; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4593 | assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4594 | assert( pParse!=0 ); |
| 4595 | assert( pParse->db!=0 ); |
| 4596 | db = pParse->db; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4597 | if( pList==0 ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4598 | pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4599 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 4600 | pList->nAlloc = 1; |
drh | ac178b3 | 2016-12-14 11:14:13 +0000 | [diff] [blame] | 4601 | pList->nSrc = 1; |
| 4602 | memset(&pList->a[0], 0, sizeof(pList->a[0])); |
| 4603 | pList->a[0].iCursor = -1; |
| 4604 | }else{ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4605 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); |
| 4606 | if( pNew==0 ){ |
| 4607 | sqlite3SrcListDelete(db, pList); |
| 4608 | return 0; |
| 4609 | }else{ |
| 4610 | pList = pNew; |
| 4611 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4612 | } |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4613 | pItem = &pList->a[pList->nSrc-1]; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4614 | if( pDatabase && pDatabase->z==0 ){ |
| 4615 | pDatabase = 0; |
| 4616 | } |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4617 | if( pDatabase ){ |
drh | 169a689 | 2017-07-06 01:02:09 +0000 | [diff] [blame] | 4618 | pItem->zName = sqlite3NameFromToken(db, pDatabase); |
| 4619 | pItem->zDatabase = sqlite3NameFromToken(db, pTable); |
| 4620 | }else{ |
| 4621 | pItem->zName = sqlite3NameFromToken(db, pTable); |
| 4622 | pItem->zDatabase = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4623 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4624 | return pList; |
| 4625 | } |
| 4626 | |
| 4627 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4628 | ** Assign VdbeCursor index numbers to all tables in a SrcList |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4629 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4630 | void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4631 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4632 | SrcItem *pItem; |
drh | 9da977f | 2021-04-20 12:14:12 +0000 | [diff] [blame] | 4633 | assert( pList || pParse->db->mallocFailed ); |
| 4634 | if( ALWAYS(pList) ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4635 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 4636 | if( pItem->iCursor>=0 ) continue; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4637 | pItem->iCursor = pParse->nTab++; |
| 4638 | if( pItem->pSelect ){ |
| 4639 | sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); |
| 4640 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4641 | } |
| 4642 | } |
| 4643 | } |
| 4644 | |
| 4645 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4646 | ** Delete an entire SrcList including all its substructure. |
| 4647 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4648 | void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4649 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4650 | SrcItem *pItem; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4651 | if( pList==0 ) return; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 4652 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4653 | if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4654 | sqlite3DbFree(db, pItem->zName); |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4655 | if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias); |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4656 | if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
| 4657 | if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 4658 | sqlite3DeleteTable(db, pItem->pTab); |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4659 | if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect); |
| 4660 | if( pItem->pOn ) sqlite3ExprDelete(db, pItem->pOn); |
| 4661 | if( pItem->pUsing ) sqlite3IdListDelete(db, pItem->pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4662 | } |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4663 | sqlite3DbFreeNN(db, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 4666 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4667 | ** This routine is called by the parser to add a new term to the |
| 4668 | ** end of a growing FROM clause. The "p" parameter is the part of |
| 4669 | ** the FROM clause that has already been constructed. "p" is NULL |
| 4670 | ** if this is the first term of the FROM clause. pTable and pDatabase |
| 4671 | ** are the name of the table and database named in the FROM clause term. |
| 4672 | ** pDatabase is NULL if the database name qualifier is missing - the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4673 | ** usual case. If the term has an alias, then pAlias points to the |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4674 | ** alias token. If the term is a subquery, then pSubquery is the |
| 4675 | ** SELECT statement that the subquery encodes. The pTable and |
| 4676 | ** pDatabase parameters are NULL for subqueries. The pOn and pUsing |
| 4677 | ** parameters are the content of the ON and USING clauses. |
| 4678 | ** |
| 4679 | ** Return a new SrcList which encodes is the FROM with the new |
| 4680 | ** term added. |
| 4681 | */ |
| 4682 | SrcList *sqlite3SrcListAppendFromTerm( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4683 | Parse *pParse, /* Parsing context */ |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4684 | SrcList *p, /* The left part of the FROM clause already seen */ |
| 4685 | Token *pTable, /* Name of the table to add to the FROM clause */ |
| 4686 | Token *pDatabase, /* Name of the database containing pTable */ |
| 4687 | Token *pAlias, /* The right-hand side of the AS subexpression */ |
| 4688 | Select *pSubquery, /* A subquery used in place of a table name */ |
| 4689 | Expr *pOn, /* The ON clause of a join */ |
| 4690 | IdList *pUsing /* The USING clause of a join */ |
| 4691 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4692 | SrcItem *pItem; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4693 | sqlite3 *db = pParse->db; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4694 | if( !p && (pOn || pUsing) ){ |
| 4695 | sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", |
| 4696 | (pOn ? "ON" : "USING") |
| 4697 | ); |
| 4698 | goto append_from_error; |
| 4699 | } |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4700 | p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 4701 | if( p==0 ){ |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4702 | goto append_from_error; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4703 | } |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 4704 | assert( p->nSrc>0 ); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4705 | pItem = &p->a[p->nSrc-1]; |
drh | a488ec9 | 2018-09-07 18:52:25 +0000 | [diff] [blame] | 4706 | assert( (pTable==0)==(pDatabase==0) ); |
| 4707 | assert( pItem->zName==0 || pDatabase!=0 ); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4708 | if( IN_RENAME_OBJECT && pItem->zName ){ |
drh | a488ec9 | 2018-09-07 18:52:25 +0000 | [diff] [blame] | 4709 | Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4710 | sqlite3RenameTokenMap(pParse, pItem->zName, pToken); |
| 4711 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4712 | assert( pAlias!=0 ); |
| 4713 | if( pAlias->n ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4714 | pItem->zAlias = sqlite3NameFromToken(db, pAlias); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4715 | } |
| 4716 | pItem->pSelect = pSubquery; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4717 | pItem->pOn = pOn; |
| 4718 | pItem->pUsing = pUsing; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4719 | return p; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4720 | |
| 4721 | append_from_error: |
| 4722 | assert( p==0 ); |
| 4723 | sqlite3ExprDelete(db, pOn); |
| 4724 | sqlite3IdListDelete(db, pUsing); |
| 4725 | sqlite3SelectDelete(db, pSubquery); |
| 4726 | return 0; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | /* |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4730 | ** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
| 4731 | ** element of the source-list passed as the second argument. |
| 4732 | */ |
| 4733 | void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4734 | assert( pIndexedBy!=0 ); |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 4735 | if( p && pIndexedBy->n>0 ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4736 | SrcItem *pItem; |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 4737 | assert( p->nSrc>0 ); |
| 4738 | pItem = &p->a[p->nSrc-1]; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4739 | assert( pItem->fg.notIndexed==0 ); |
| 4740 | assert( pItem->fg.isIndexedBy==0 ); |
| 4741 | assert( pItem->fg.isTabFunc==0 ); |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4742 | if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
| 4743 | /* A "NOT INDEXED" clause was supplied. See parse.y |
| 4744 | ** construct "indexed_opt" for details. */ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4745 | pItem->fg.notIndexed = 1; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4746 | }else{ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4747 | pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 4748 | pItem->fg.isIndexedBy = 1; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4749 | } |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | /* |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 4754 | ** Append the contents of SrcList p2 to SrcList p1 and return the resulting |
| 4755 | ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 |
| 4756 | ** are deleted by this function. |
| 4757 | */ |
| 4758 | SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){ |
dan | 8b023cf | 2020-04-30 18:28:40 +0000 | [diff] [blame] | 4759 | assert( p1 && p1->nSrc==1 ); |
| 4760 | if( p2 ){ |
| 4761 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1); |
| 4762 | if( pNew==0 ){ |
| 4763 | sqlite3SrcListDelete(pParse->db, p2); |
| 4764 | }else{ |
| 4765 | p1 = pNew; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4766 | memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem)); |
drh | 525326e | 2020-07-15 21:53:53 +0000 | [diff] [blame] | 4767 | sqlite3DbFree(pParse->db, p2); |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 4768 | } |
| 4769 | } |
| 4770 | return p1; |
| 4771 | } |
| 4772 | |
| 4773 | /* |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 4774 | ** Add the list of function arguments to the SrcList entry for a |
| 4775 | ** table-valued-function. |
| 4776 | */ |
| 4777 | void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 4778 | if( p ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4779 | SrcItem *pItem = &p->a[p->nSrc-1]; |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 4780 | assert( pItem->fg.notIndexed==0 ); |
| 4781 | assert( pItem->fg.isIndexedBy==0 ); |
| 4782 | assert( pItem->fg.isTabFunc==0 ); |
| 4783 | pItem->u1.pFuncArg = pList; |
| 4784 | pItem->fg.isTabFunc = 1; |
drh | d8b1bfc | 2015-08-20 23:21:34 +0000 | [diff] [blame] | 4785 | }else{ |
| 4786 | sqlite3ExprListDelete(pParse->db, pList); |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4791 | ** When building up a FROM clause in the parser, the join operator |
| 4792 | ** is initially attached to the left operand. But the code generator |
| 4793 | ** expects the join operator to be on the right operand. This routine |
| 4794 | ** Shifts all join operators from left to right for an entire FROM |
| 4795 | ** clause. |
| 4796 | ** |
| 4797 | ** Example: Suppose the join is like this: |
| 4798 | ** |
| 4799 | ** A natural cross join B |
| 4800 | ** |
| 4801 | ** The operator is "natural cross join". The A and B operands are stored |
| 4802 | ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
| 4803 | ** operator with A. This routine shifts that operator over to B. |
| 4804 | */ |
| 4805 | void sqlite3SrcListShiftJoinType(SrcList *p){ |
drh | d017ab9 | 2011-08-23 00:01:58 +0000 | [diff] [blame] | 4806 | if( p ){ |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4807 | int i; |
| 4808 | for(i=p->nSrc-1; i>0; i--){ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4809 | p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4810 | } |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4811 | p->a[0].fg.jointype = 0; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | |
| 4815 | /* |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 4816 | ** Generate VDBE code for a BEGIN statement. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 4817 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4818 | void sqlite3BeginTransaction(Parse *pParse, int type){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 4819 | sqlite3 *db; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4820 | Vdbe *v; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4821 | int i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4822 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4823 | assert( pParse!=0 ); |
| 4824 | db = pParse->db; |
| 4825 | assert( db!=0 ); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4826 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ |
| 4827 | return; |
| 4828 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4829 | v = sqlite3GetVdbe(pParse); |
| 4830 | if( !v ) return; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4831 | if( type!=TK_DEFERRED ){ |
| 4832 | for(i=0; i<db->nDb; i++){ |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 4833 | int eTxnType; |
| 4834 | Btree *pBt = db->aDb[i].pBt; |
| 4835 | if( pBt && sqlite3BtreeIsReadonly(pBt) ){ |
| 4836 | eTxnType = 0; /* Read txn */ |
| 4837 | }else if( type==TK_EXCLUSIVE ){ |
| 4838 | eTxnType = 2; /* Exclusive txn */ |
| 4839 | }else{ |
| 4840 | eTxnType = 1; /* Write txn */ |
| 4841 | } |
| 4842 | sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4843 | sqlite3VdbeUsesBtree(v, i); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4844 | } |
| 4845 | } |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 4846 | sqlite3VdbeAddOp0(v, OP_AutoCommit); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | /* |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 4850 | ** Generate VDBE code for a COMMIT or ROLLBACK statement. |
| 4851 | ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise |
| 4852 | ** code is generated for a COMMIT. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 4853 | */ |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 4854 | void sqlite3EndTransaction(Parse *pParse, int eType){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4855 | Vdbe *v; |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 4856 | int isRollback; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4857 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4858 | assert( pParse!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4859 | assert( pParse->db!=0 ); |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 4860 | assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); |
| 4861 | isRollback = eType==TK_ROLLBACK; |
| 4862 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, |
| 4863 | isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4864 | return; |
| 4865 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4866 | v = sqlite3GetVdbe(pParse); |
| 4867 | if( v ){ |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 4868 | sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 4869 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 4870 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 4871 | |
| 4872 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4873 | ** This function is called by the parser when it parses a command to create, |
| 4874 | ** release or rollback an SQL savepoint. |
| 4875 | */ |
| 4876 | void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 4877 | char *zName = sqlite3NameFromToken(pParse->db, pName); |
| 4878 | if( zName ){ |
| 4879 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 4880 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | 558814f | 2010-06-02 05:53:53 +0000 | [diff] [blame] | 4881 | static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 4882 | assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
| 4883 | #endif |
| 4884 | if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ |
| 4885 | sqlite3DbFree(pParse->db, zName); |
| 4886 | return; |
| 4887 | } |
| 4888 | sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | /* |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4893 | ** Make sure the TEMP database is open and available for use. Return |
| 4894 | ** the number of errors. Leave any error messages in the pParse structure. |
| 4895 | */ |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 4896 | int sqlite3OpenTempDatabase(Parse *pParse){ |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4897 | sqlite3 *db = pParse->db; |
| 4898 | if( db->aDb[1].pBt==0 && !pParse->explain ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4899 | int rc; |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 4900 | Btree *pBt; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4901 | static const int flags = |
| 4902 | SQLITE_OPEN_READWRITE | |
| 4903 | SQLITE_OPEN_CREATE | |
| 4904 | SQLITE_OPEN_EXCLUSIVE | |
| 4905 | SQLITE_OPEN_DELETEONCLOSE | |
| 4906 | SQLITE_OPEN_TEMP_DB; |
| 4907 | |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 4908 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4909 | if( rc!=SQLITE_OK ){ |
| 4910 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 4911 | "file for storing temporary tables"); |
| 4912 | pParse->rc = rc; |
| 4913 | return 1; |
| 4914 | } |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 4915 | db->aDb[1].pBt = pBt; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 4916 | assert( db->aDb[1].pSchema ); |
drh | e937df8 | 2020-05-07 01:56:57 +0000 | [diff] [blame] | 4917 | if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 4918 | sqlite3OomFault(db); |
drh | 7c9c986 | 2010-01-31 14:18:21 +0000 | [diff] [blame] | 4919 | return 1; |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 4920 | } |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 4921 | } |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
| 4925 | /* |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 4926 | ** Record the fact that the schema cookie will need to be verified |
| 4927 | ** for database iDb. The code to actually verify the schema cookie |
| 4928 | ** will occur at the end of the top-level VDBE and will be generated |
| 4929 | ** later, by sqlite3FinishCoding(). |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4930 | */ |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 4931 | static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){ |
| 4932 | assert( iDb>=0 && iDb<pToplevel->db->nDb ); |
| 4933 | assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 ); |
drh | 099b385 | 2021-03-10 16:35:37 +0000 | [diff] [blame] | 4934 | assert( iDb<SQLITE_MAX_DB ); |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 4935 | assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 4936 | if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ |
| 4937 | DbMaskSet(pToplevel->cookieMask, iDb); |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 4938 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 4939 | sqlite3OpenTempDatabase(pToplevel); |
| 4940 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4941 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4942 | } |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 4943 | void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
| 4944 | sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb); |
| 4945 | } |
| 4946 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4947 | |
| 4948 | /* |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 4949 | ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
| 4950 | ** attached database. Otherwise, invoke it for the database named zDb only. |
| 4951 | */ |
| 4952 | void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ |
| 4953 | sqlite3 *db = pParse->db; |
| 4954 | int i; |
| 4955 | for(i=0; i<db->nDb; i++){ |
| 4956 | Db *pDb = &db->aDb[i]; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4957 | if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 4958 | sqlite3CodeVerifySchema(pParse, i); |
| 4959 | } |
| 4960 | } |
| 4961 | } |
| 4962 | |
| 4963 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 4964 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 4965 | ** might change the database. |
| 4966 | ** |
| 4967 | ** This routine starts a new transaction if we are not already within |
| 4968 | ** a transaction. If we are already within a transaction, then a checkpoint |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 4969 | ** is set if the setStatement parameter is true. A checkpoint should |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 4970 | ** be set for operations that might fail (due to a constraint) part of |
| 4971 | ** the way through and which will need to undo some writes without having to |
| 4972 | ** rollback the whole transaction. For operations where all constraints |
| 4973 | ** can be checked before any changes are made to the database, it is never |
| 4974 | ** necessary to undo a write and the checkpoint should not be set. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 4975 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 4976 | void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4977 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 4978 | sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 4979 | DbMaskSet(pToplevel->writeMask, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4980 | pToplevel->isMultiWrite |= setStatement; |
| 4981 | } |
| 4982 | |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 4983 | /* |
| 4984 | ** Indicate that the statement currently under construction might write |
| 4985 | ** more than one entry (example: deleting one row then inserting another, |
| 4986 | ** inserting multiple rows in a table, or inserting a row and index entries.) |
| 4987 | ** If an abort occurs after some of these writes have completed, then it will |
| 4988 | ** be necessary to undo the completed writes. |
| 4989 | */ |
| 4990 | void sqlite3MultiWrite(Parse *pParse){ |
| 4991 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 4992 | pToplevel->isMultiWrite = 1; |
| 4993 | } |
| 4994 | |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4995 | /* |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 4996 | ** The code generator calls this routine if is discovers that it is |
| 4997 | ** possible to abort a statement prior to completion. In order to |
| 4998 | ** perform this abort without corrupting the database, we need to make |
| 4999 | ** sure that the statement is protected by a statement transaction. |
| 5000 | ** |
| 5001 | ** Technically, we only need to set the mayAbort flag if the |
| 5002 | ** isMultiWrite flag was previously set. There is a time dependency |
| 5003 | ** such that the abort must occur after the multiwrite. This makes |
| 5004 | ** some statements involving the REPLACE conflict resolution algorithm |
| 5005 | ** go a little faster. But taking advantage of this time dependency |
| 5006 | ** makes it more difficult to prove that the code is correct (in |
| 5007 | ** particular, it prevents us from writing an effective |
| 5008 | ** implementation of sqlite3AssertMayAbort()) and so we have chosen |
| 5009 | ** to take the safe route and skip the optimization. |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5010 | */ |
| 5011 | void sqlite3MayAbort(Parse *pParse){ |
| 5012 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5013 | pToplevel->mayAbort = 1; |
| 5014 | } |
| 5015 | |
| 5016 | /* |
| 5017 | ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
| 5018 | ** error. The onError parameter determines which (if any) of the statement |
| 5019 | ** and/or current transaction is rolled back. |
| 5020 | */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5021 | void sqlite3HaltConstraint( |
| 5022 | Parse *pParse, /* Parsing context */ |
| 5023 | int errCode, /* extended error code */ |
| 5024 | int onError, /* Constraint type */ |
| 5025 | char *p4, /* Error message */ |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5026 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 5027 | u8 p5Errmsg /* P5_ErrMsg type */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5028 | ){ |
drh | 289a0c8 | 2020-08-15 22:23:00 +0000 | [diff] [blame] | 5029 | Vdbe *v; |
| 5030 | assert( pParse->pVdbe!=0 ); |
| 5031 | v = sqlite3GetVdbe(pParse); |
drh | 9e5fdc4 | 2020-05-08 19:02:21 +0000 | [diff] [blame] | 5032 | assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5033 | if( onError==OE_Abort ){ |
| 5034 | sqlite3MayAbort(pParse); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5035 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5036 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 5037 | sqlite3VdbeChangeP5(v, p5Errmsg); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | /* |
| 5041 | ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. |
| 5042 | */ |
| 5043 | void sqlite3UniqueConstraint( |
| 5044 | Parse *pParse, /* Parsing context */ |
| 5045 | int onError, /* Constraint type */ |
| 5046 | Index *pIdx /* The index that triggers the constraint */ |
| 5047 | ){ |
| 5048 | char *zErr; |
| 5049 | int j; |
| 5050 | StrAccum errMsg; |
| 5051 | Table *pTab = pIdx->pTable; |
| 5052 | |
drh | 86ec1ed | 2019-04-10 00:58:07 +0000 | [diff] [blame] | 5053 | sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, |
| 5054 | pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5055 | if( pIdx->aColExpr ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 5056 | sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5057 | }else{ |
| 5058 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 5059 | char *zCol; |
| 5060 | assert( pIdx->aiColumn[j]>=0 ); |
| 5061 | zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 5062 | if( j ) sqlite3_str_append(&errMsg, ", ", 2); |
| 5063 | sqlite3_str_appendall(&errMsg, pTab->zName); |
| 5064 | sqlite3_str_append(&errMsg, ".", 1); |
| 5065 | sqlite3_str_appendall(&errMsg, zCol); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5066 | } |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5067 | } |
| 5068 | zErr = sqlite3StrAccumFinish(&errMsg); |
| 5069 | sqlite3HaltConstraint(pParse, |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 5070 | IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
| 5071 | : SQLITE_CONSTRAINT_UNIQUE, |
dan | 93889d9 | 2013-11-06 16:28:59 +0000 | [diff] [blame] | 5072 | onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | |
| 5076 | /* |
| 5077 | ** Code an OP_Halt due to non-unique rowid. |
| 5078 | */ |
| 5079 | void sqlite3RowidConstraint( |
| 5080 | Parse *pParse, /* Parsing context */ |
| 5081 | int onError, /* Conflict resolution algorithm */ |
| 5082 | Table *pTab /* The table with the non-unique rowid */ |
| 5083 | ){ |
| 5084 | char *zMsg; |
| 5085 | int rc; |
| 5086 | if( pTab->iPKey>=0 ){ |
| 5087 | zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, |
| 5088 | pTab->aCol[pTab->iPKey].zName); |
| 5089 | rc = SQLITE_CONSTRAINT_PRIMARYKEY; |
| 5090 | }else{ |
| 5091 | zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); |
| 5092 | rc = SQLITE_CONSTRAINT_ROWID; |
| 5093 | } |
| 5094 | sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, |
| 5095 | P5_ConstraintUnique); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5098 | /* |
| 5099 | ** Check to see if pIndex uses the collating sequence pColl. Return |
| 5100 | ** true if it does and false if it does not. |
| 5101 | */ |
| 5102 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5103 | static int collationMatch(const char *zColl, Index *pIndex){ |
| 5104 | int i; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 5105 | assert( zColl!=0 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5106 | for(i=0; i<pIndex->nColumn; i++){ |
| 5107 | const char *z = pIndex->azColl[i]; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5108 | assert( z!=0 || pIndex->aiColumn[i]<0 ); |
| 5109 | if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5110 | return 1; |
| 5111 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5112 | } |
| 5113 | return 0; |
| 5114 | } |
| 5115 | #endif |
| 5116 | |
| 5117 | /* |
| 5118 | ** Recompute all indices of pTab that use the collating sequence pColl. |
| 5119 | ** If pColl==0 then recompute all indices of pTab. |
| 5120 | */ |
| 5121 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5122 | static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
dan | e6370e9 | 2019-01-11 17:41:23 +0000 | [diff] [blame] | 5123 | if( !IsVirtual(pTab) ){ |
| 5124 | Index *pIndex; /* An index associated with pTab */ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5125 | |
dan | e6370e9 | 2019-01-11 17:41:23 +0000 | [diff] [blame] | 5126 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 5127 | if( zColl==0 || collationMatch(zColl, pIndex) ){ |
| 5128 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 5129 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5130 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5131 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5132 | } |
| 5133 | } |
| 5134 | } |
| 5135 | #endif |
| 5136 | |
| 5137 | /* |
| 5138 | ** Recompute all indices of all tables in all databases where the |
| 5139 | ** indices use the collating sequence pColl. If pColl==0 then recompute |
| 5140 | ** all indices everywhere. |
| 5141 | */ |
| 5142 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5143 | static void reindexDatabases(Parse *pParse, char const *zColl){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5144 | Db *pDb; /* A single database */ |
| 5145 | int iDb; /* The database index number */ |
| 5146 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5147 | HashElem *k; /* For looping over tables in pDb */ |
| 5148 | Table *pTab; /* A table in the database */ |
| 5149 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 5150 | assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5151 | for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 5152 | assert( pDb!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5153 | for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5154 | pTab = (Table*)sqliteHashData(k); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5155 | reindexTable(pParse, pTab, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5156 | } |
| 5157 | } |
| 5158 | } |
| 5159 | #endif |
| 5160 | |
| 5161 | /* |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 5162 | ** Generate code for the REINDEX command. |
| 5163 | ** |
| 5164 | ** REINDEX -- 1 |
| 5165 | ** REINDEX <collation> -- 2 |
| 5166 | ** REINDEX ?<database>.?<tablename> -- 3 |
| 5167 | ** REINDEX ?<database>.?<indexname> -- 4 |
| 5168 | ** |
| 5169 | ** Form 1 causes all indices in all attached databases to be rebuilt. |
| 5170 | ** Form 2 rebuilds all indices in all databases that use the named |
| 5171 | ** collating function. Forms 3 and 4 rebuild the named index or all |
| 5172 | ** indices associated with the named table. |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5173 | */ |
| 5174 | #ifndef SQLITE_OMIT_REINDEX |
| 5175 | void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
| 5176 | CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
| 5177 | char *z; /* Name of a table or index */ |
| 5178 | const char *zDb; /* Name of the database */ |
| 5179 | Table *pTab; /* A table in the database */ |
| 5180 | Index *pIndex; /* An index associated with pTab */ |
| 5181 | int iDb; /* The database index number */ |
| 5182 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5183 | Token *pObjName; /* Name of the table or index to be reindexed */ |
| 5184 | |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 5185 | /* Read the database schema. If an error occurs, leave an error message |
| 5186 | ** and code in pParse and return NULL. */ |
| 5187 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e63739a | 2005-01-27 00:33:37 +0000 | [diff] [blame] | 5188 | return; |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 5191 | if( pName1==0 ){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5192 | reindexDatabases(pParse, 0); |
| 5193 | return; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5194 | }else if( NEVER(pName2==0) || pName2->z==0 ){ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 5195 | char *zColl; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5196 | assert( pName1->z ); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 5197 | zColl = sqlite3NameFromToken(pParse->db, pName1); |
| 5198 | if( !zColl ) return; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 5199 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5200 | if( pColl ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5201 | reindexDatabases(pParse, zColl); |
| 5202 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5203 | return; |
| 5204 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5205 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5206 | } |
| 5207 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 5208 | if( iDb<0 ) return; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5209 | z = sqlite3NameFromToken(db, pObjName); |
drh | 84f3112 | 2007-05-12 15:00:14 +0000 | [diff] [blame] | 5210 | if( z==0 ) return; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5211 | zDb = db->aDb[iDb].zDbSName; |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5212 | pTab = sqlite3FindTable(db, z, zDb); |
| 5213 | if( pTab ){ |
| 5214 | reindexTable(pParse, pTab, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5215 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5216 | return; |
| 5217 | } |
| 5218 | pIndex = sqlite3FindIndex(db, z, zDb); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5219 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5220 | if( pIndex ){ |
| 5221 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5222 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5223 | return; |
| 5224 | } |
| 5225 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| 5226 | } |
| 5227 | #endif |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5228 | |
| 5229 | /* |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5230 | ** Return a KeyInfo structure that is appropriate for the given Index. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5231 | ** |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5232 | ** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
| 5233 | ** when it has finished using it. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5234 | */ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5235 | KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5236 | int i; |
| 5237 | int nCol = pIdx->nColumn; |
| 5238 | int nKey = pIdx->nKeyCol; |
| 5239 | KeyInfo *pKey; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5240 | if( pParse->nErr ) return 0; |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5241 | if( pIdx->uniqNotNull ){ |
| 5242 | pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); |
| 5243 | }else{ |
| 5244 | pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 5245 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5246 | if( pKey ){ |
| 5247 | assert( sqlite3KeyInfoIsWriteable(pKey) ); |
| 5248 | for(i=0; i<nCol; i++){ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 5249 | const char *zColl = pIdx->azColl[i]; |
| 5250 | pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5251 | sqlite3LocateCollSeq(pParse, zColl); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 5252 | pKey->aSortFlags[i] = pIdx->aSortOrder[i]; |
| 5253 | assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5254 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5255 | if( pParse->nErr ){ |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 5256 | assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); |
| 5257 | if( pIdx->bNoQuery==0 ){ |
| 5258 | /* Deactivate the index because it contains an unknown collating |
| 5259 | ** sequence. The only way to reactive the index is to reload the |
| 5260 | ** schema. Adding the missing collating sequence later does not |
| 5261 | ** reactive the index. The application had the chance to register |
| 5262 | ** the missing index using the collation-needed callback. For |
| 5263 | ** simplicity, SQLite will not give the application a second chance. |
| 5264 | */ |
| 5265 | pIdx->bNoQuery = 1; |
| 5266 | pParse->rc = SQLITE_ERROR_RETRY; |
| 5267 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5268 | sqlite3KeyInfoUnref(pKey); |
| 5269 | pKey = 0; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5270 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5271 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5272 | return pKey; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5273 | } |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5274 | |
| 5275 | #ifndef SQLITE_OMIT_CTE |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5276 | /* |
| 5277 | ** Create a new CTE object |
| 5278 | */ |
| 5279 | Cte *sqlite3CteNew( |
| 5280 | Parse *pParse, /* Parsing context */ |
| 5281 | Token *pName, /* Name of the common-table */ |
| 5282 | ExprList *pArglist, /* Optional column name list for the table */ |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5283 | Select *pQuery, /* Query used to initialize the table */ |
| 5284 | u8 eM10d /* The MATERIALIZED flag */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5285 | ){ |
| 5286 | Cte *pNew; |
| 5287 | sqlite3 *db = pParse->db; |
| 5288 | |
| 5289 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew)); |
| 5290 | assert( pNew!=0 || db->mallocFailed ); |
| 5291 | |
| 5292 | if( db->mallocFailed ){ |
| 5293 | sqlite3ExprListDelete(db, pArglist); |
| 5294 | sqlite3SelectDelete(db, pQuery); |
| 5295 | }else{ |
| 5296 | pNew->pSelect = pQuery; |
| 5297 | pNew->pCols = pArglist; |
| 5298 | pNew->zName = sqlite3NameFromToken(pParse->db, pName); |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5299 | pNew->eM10d = eM10d; |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5300 | } |
| 5301 | return pNew; |
| 5302 | } |
| 5303 | |
| 5304 | /* |
| 5305 | ** Clear information from a Cte object, but do not deallocate storage |
| 5306 | ** for the object itself. |
| 5307 | */ |
| 5308 | static void cteClear(sqlite3 *db, Cte *pCte){ |
| 5309 | assert( pCte!=0 ); |
| 5310 | sqlite3ExprListDelete(db, pCte->pCols); |
| 5311 | sqlite3SelectDelete(db, pCte->pSelect); |
| 5312 | sqlite3DbFree(db, pCte->zName); |
| 5313 | } |
| 5314 | |
| 5315 | /* |
| 5316 | ** Free the contents of the CTE object passed as the second argument. |
| 5317 | */ |
| 5318 | void sqlite3CteDelete(sqlite3 *db, Cte *pCte){ |
| 5319 | assert( pCte!=0 ); |
| 5320 | cteClear(db, pCte); |
| 5321 | sqlite3DbFree(db, pCte); |
| 5322 | } |
| 5323 | |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5324 | /* |
| 5325 | ** This routine is invoked once per CTE by the parser while parsing a |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5326 | ** WITH clause. The CTE described by teh third argument is added to |
| 5327 | ** the WITH clause of the second argument. If the second argument is |
| 5328 | ** NULL, then a new WITH argument is created. |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5329 | */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5330 | With *sqlite3WithAdd( |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5331 | Parse *pParse, /* Parsing context */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5332 | With *pWith, /* Existing WITH clause, or NULL */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5333 | Cte *pCte /* CTE to add to the WITH clause */ |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5334 | ){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5335 | sqlite3 *db = pParse->db; |
| 5336 | With *pNew; |
| 5337 | char *zName; |
| 5338 | |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5339 | if( pCte==0 ){ |
| 5340 | return pWith; |
| 5341 | } |
| 5342 | |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5343 | /* Check that the CTE name is unique within this WITH clause. If |
| 5344 | ** not, store an error in the Parse structure. */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5345 | zName = pCte->zName; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5346 | if( zName && pWith ){ |
| 5347 | int i; |
| 5348 | for(i=0; i<pWith->nCte; i++){ |
| 5349 | if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ |
drh | 727a99f | 2014-01-16 21:59:51 +0000 | [diff] [blame] | 5350 | sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5351 | } |
| 5352 | } |
| 5353 | } |
| 5354 | |
| 5355 | if( pWith ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 5356 | sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5357 | pNew = sqlite3DbRealloc(db, pWith, nByte); |
| 5358 | }else{ |
| 5359 | pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); |
| 5360 | } |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 5361 | assert( (pNew!=0 && zName!=0) || db->mallocFailed ); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5362 | |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 5363 | if( db->mallocFailed ){ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5364 | sqlite3CteDelete(db, pCte); |
dan | a9f5c13 | 2014-01-13 16:36:40 +0000 | [diff] [blame] | 5365 | pNew = pWith; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5366 | }else{ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5367 | pNew->a[pNew->nCte++] = *pCte; |
| 5368 | sqlite3DbFree(db, pCte); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | return pNew; |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5374 | /* |
| 5375 | ** Free the contents of the With object passed as the second argument. |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5376 | */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5377 | void sqlite3WithDelete(sqlite3 *db, With *pWith){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5378 | if( pWith ){ |
| 5379 | int i; |
| 5380 | for(i=0; i<pWith->nCte; i++){ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5381 | cteClear(db, &pWith->a[i]); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5382 | } |
| 5383 | sqlite3DbFree(db, pWith); |
| 5384 | } |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5385 | } |
| 5386 | #endif /* !defined(SQLITE_OMIT_CTE) */ |