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 |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 288 | ** currently under construction. Notes: |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 289 | ** |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 290 | ** * The final OP_Halt is not appended and other initialization |
| 291 | ** and finalization steps are omitted because those are handling by the |
| 292 | ** outermost parser. |
| 293 | ** |
| 294 | ** * Built-in SQL functions always take precedence over application-defined |
| 295 | ** SQL functions. In other words, it is not possible to override a |
| 296 | ** built-in function. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 297 | */ |
| 298 | void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 299 | va_list ap; |
| 300 | char *zSql; |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 301 | char *zErrMsg = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 302 | sqlite3 *db = pParse->db; |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 303 | u32 savedDbFlags = db->mDbFlags; |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 304 | char saveBuf[PARSE_TAIL_SZ]; |
drh | f197484 | 2004-11-05 03:56:00 +0000 | [diff] [blame] | 305 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 306 | if( pParse->nErr ) return; |
| 307 | assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
| 308 | va_start(ap, zFormat); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 309 | zSql = sqlite3VMPrintf(db, zFormat, ap); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 310 | va_end(ap); |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 311 | if( zSql==0 ){ |
drh | 480c572 | 2019-02-22 16:18:12 +0000 | [diff] [blame] | 312 | /* This can result either from an OOM or because the formatted string |
| 313 | ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set |
| 314 | ** an error */ |
| 315 | if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG; |
drh | a2b6806 | 2019-03-28 04:03:17 +0000 | [diff] [blame] | 316 | pParse->nErr++; |
drh | 480c572 | 2019-02-22 16:18:12 +0000 | [diff] [blame] | 317 | return; |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 318 | } |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 319 | pParse->nested++; |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 320 | memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 321 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 322 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 323 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
drh | 3edc927 | 2021-08-04 13:42:12 +0000 | [diff] [blame] | 324 | db->mDbFlags = savedDbFlags; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 325 | sqlite3DbFree(db, zErrMsg); |
| 326 | sqlite3DbFree(db, zSql); |
drh | cd9af60 | 2016-09-30 22:24:29 +0000 | [diff] [blame] | 327 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 328 | pParse->nested--; |
| 329 | } |
| 330 | |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 331 | #if SQLITE_USER_AUTHENTICATION |
| 332 | /* |
| 333 | ** Return TRUE if zTable is the name of the system table that stores the |
| 334 | ** list of users and their access credentials. |
| 335 | */ |
| 336 | int sqlite3UserAuthTable(const char *zTable){ |
| 337 | return sqlite3_stricmp(zTable, "sqlite_user")==0; |
| 338 | } |
| 339 | #endif |
| 340 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 341 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 342 | ** Locate the in-memory structure that describes a particular database |
| 343 | ** table given the name of that table and (optionally) the name of the |
| 344 | ** database containing the table. Return NULL if not found. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 345 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 346 | ** If zDatabase is 0, all databases are searched for the table and the |
| 347 | ** first matching table is returned. (No checking for duplicate table |
| 348 | ** names is done.) The search order is TEMP first, then MAIN, then any |
| 349 | ** auxiliary databases added using the ATTACH command. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 350 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 351 | ** See also sqlite3LocateTable(). |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 352 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 353 | Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 354 | Table *p = 0; |
| 355 | int i; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 356 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 357 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 358 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 359 | #if SQLITE_USER_AUTHENTICATION |
| 360 | /* Only the admin user is allowed to know that the sqlite_user table |
| 361 | ** exists */ |
drh | e933b83 | 2014-09-10 17:34:28 +0000 | [diff] [blame] | 362 | if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){ |
| 363 | return 0; |
| 364 | } |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 365 | #endif |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 366 | if( zDatabase ){ |
| 367 | for(i=0; i<db->nDb; i++){ |
| 368 | if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 369 | } |
| 370 | if( i>=db->nDb ){ |
| 371 | /* No match against the official names. But always match "main" |
| 372 | ** to schema 0 as a legacy fallback. */ |
| 373 | if( sqlite3StrICmp(zDatabase,"main")==0 ){ |
| 374 | i = 0; |
| 375 | }else{ |
| 376 | return 0; |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 377 | } |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 378 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 379 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 380 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 381 | if( i==1 ){ |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 382 | if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 |
| 383 | || sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 |
| 384 | || sqlite3StrICmp(zName+7, &DFLT_SCHEMA_TABLE[7])==0 |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 385 | ){ |
| 386 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
| 387 | DFLT_TEMP_SCHEMA_TABLE); |
| 388 | } |
| 389 | }else{ |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 390 | if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 391 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, |
| 392 | DFLT_SCHEMA_TABLE); |
| 393 | } |
| 394 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 395 | } |
| 396 | }else{ |
| 397 | /* Match against TEMP first */ |
| 398 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); |
| 399 | if( p ) return p; |
| 400 | /* The main database is second */ |
| 401 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); |
| 402 | if( p ) return p; |
| 403 | /* Attached databases are in order of attachment */ |
| 404 | for(i=2; i<db->nDb; i++){ |
| 405 | assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 406 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 407 | if( p ) break; |
| 408 | } |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 409 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 410 | if( sqlite3StrICmp(zName+7, &ALT_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 411 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, DFLT_SCHEMA_TABLE); |
drh | a764709 | 2020-06-20 03:43:46 +0000 | [diff] [blame] | 412 | }else if( sqlite3StrICmp(zName+7, &ALT_TEMP_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 413 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
| 414 | DFLT_TEMP_SCHEMA_TABLE); |
| 415 | } |
| 416 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 417 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 +0000 | [diff] [blame] | 418 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 422 | ** Locate the in-memory structure that describes a particular database |
| 423 | ** table given the name of that table and (optionally) the name of the |
| 424 | ** database containing the table. Return NULL if not found. Also leave an |
| 425 | ** error message in pParse->zErrMsg. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 426 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 427 | ** The difference between this routine and sqlite3FindTable() is that this |
| 428 | ** routine leaves an error message in pParse->zErrMsg where |
| 429 | ** sqlite3FindTable() does not. |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 430 | */ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 431 | Table *sqlite3LocateTable( |
| 432 | Parse *pParse, /* context in which to report errors */ |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 433 | u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ |
drh | ca42411 | 2008-01-25 15:04:48 +0000 | [diff] [blame] | 434 | const char *zName, /* Name of the table we are looking for */ |
| 435 | const char *zDbase /* Name of the database. Might be NULL */ |
| 436 | ){ |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 437 | Table *p; |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 438 | sqlite3 *db = pParse->db; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 439 | |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 440 | /* Read the database schema. If an error occurs, leave an error message |
| 441 | ** and code in pParse and return NULL. */ |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 442 | if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 |
| 443 | && SQLITE_OK!=sqlite3ReadSchema(pParse) |
| 444 | ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 448 | p = sqlite3FindTable(db, zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 449 | if( p==0 ){ |
drh | d297592 | 2015-08-29 17:22:33 +0000 | [diff] [blame] | 450 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9196c81 | 2018-11-05 16:38:10 +0000 | [diff] [blame] | 451 | /* If zName is the not the name of a table in the schema created using |
| 452 | ** CREATE, then check to see if it is the name of an virtual table that |
| 453 | ** can be an eponymous virtual table. */ |
dan | bb0eec4 | 2021-04-26 14:09:48 +0000 | [diff] [blame] | 454 | if( pParse->disableVtab==0 && db->init.busy==0 ){ |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 455 | Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 456 | if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 457 | pMod = sqlite3PragmaVtabRegister(db, zName); |
| 458 | } |
| 459 | if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
dan | bd24e8f | 2021-07-08 18:29:25 +0000 | [diff] [blame] | 460 | testcase( pMod->pEpoTab==0 ); |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 461 | return pMod->pEpoTab; |
| 462 | } |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 463 | } |
| 464 | #endif |
dan | 1ea0443 | 2018-12-21 19:29:11 +0000 | [diff] [blame] | 465 | if( flags & LOCATE_NOERR ) return 0; |
| 466 | pParse->checkSchema = 1; |
| 467 | }else if( IsVirtual(p) && pParse->disableVtab ){ |
| 468 | p = 0; |
| 469 | } |
| 470 | |
| 471 | if( p==0 ){ |
| 472 | const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| 473 | if( zDbase ){ |
| 474 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
| 475 | }else{ |
| 476 | sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 477 | } |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 478 | }else{ |
| 479 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 480 | } |
dan | fab1d40 | 2015-11-26 15:51:55 +0000 | [diff] [blame] | 481 | |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 482 | return p; |
| 483 | } |
| 484 | |
| 485 | /* |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 486 | ** Locate the table identified by *p. |
| 487 | ** |
| 488 | ** This is a wrapper around sqlite3LocateTable(). The difference between |
| 489 | ** sqlite3LocateTable() and this function is that this function restricts |
| 490 | ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
| 491 | ** non-NULL if it is part of a view or trigger program definition. See |
| 492 | ** sqlite3FixSrcList() for details. |
| 493 | */ |
| 494 | Table *sqlite3LocateTableItem( |
| 495 | Parse *pParse, |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 496 | u32 flags, |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 497 | SrcItem *p |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 498 | ){ |
| 499 | const char *zDb; |
drh | cd1499f | 2021-05-20 00:44:04 +0000 | [diff] [blame] | 500 | assert( p->pSchema==0 || p->zDatabase==0 ); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 501 | if( p->pSchema ){ |
| 502 | int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 503 | zDb = pParse->db->aDb[iDb].zDbSName; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 504 | }else{ |
| 505 | zDb = p->zDatabase; |
| 506 | } |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 507 | return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 511 | ** Locate the in-memory structure that describes |
| 512 | ** a particular index given the name of that index |
| 513 | ** and the name of the database that contains the index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 514 | ** Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 515 | ** |
| 516 | ** If zDatabase is 0, all databases are searched for the |
| 517 | ** table and the first matching index is returned. (No checking |
| 518 | ** for duplicate index names is done.) The search order is |
| 519 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 520 | ** using the ATTACH command. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 521 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 522 | Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 523 | Index *p = 0; |
| 524 | int i; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 525 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 526 | assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 527 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 +0000 | [diff] [blame] | 528 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 529 | Schema *pSchema = db->aDb[j].pSchema; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 530 | assert( pSchema ); |
dan | 465c2b8 | 2020-03-21 15:10:40 +0000 | [diff] [blame] | 531 | if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 532 | assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 533 | p = sqlite3HashFind(&pSchema->idxHash, zName); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 534 | if( p ) break; |
| 535 | } |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 536 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 540 | ** Reclaim the memory used by an index |
| 541 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 542 | void sqlite3FreeIndex(sqlite3 *db, Index *p){ |
drh | 92aa5ea | 2009-09-11 14:05:06 +0000 | [diff] [blame] | 543 | #ifndef SQLITE_OMIT_ANALYZE |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 544 | sqlite3DeleteIndexSamples(db, p); |
drh | 92aa5ea | 2009-09-11 14:05:06 +0000 | [diff] [blame] | 545 | #endif |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 546 | sqlite3ExprDelete(db, p->pPartIdxWhere); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 547 | sqlite3ExprListDelete(db, p->aColExpr); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 548 | sqlite3DbFree(db, p->zColAff); |
mistachkin | 5905f86 | 2015-12-31 19:04:42 +0000 | [diff] [blame] | 549 | if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); |
drh | 175b8f0 | 2019-08-08 15:24:17 +0000 | [diff] [blame] | 550 | #ifdef SQLITE_ENABLE_STAT4 |
drh | 75b170b | 2014-10-04 00:07:44 +0000 | [diff] [blame] | 551 | sqlite3_free(p->aiRowEst); |
| 552 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 553 | sqlite3DbFree(db, p); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 557 | ** For the index called zIdxName which is found in the database iDb, |
| 558 | ** unlike that index from its Table then remove the index from |
| 559 | ** the index hash table and free all memory structures associated |
| 560 | ** with the index. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 561 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 562 | void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 563 | Index *pIndex; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 564 | Hash *pHash; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 565 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 566 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 567 | pHash = &db->aDb[iDb].pSchema->idxHash; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 568 | pIndex = sqlite3HashInsert(pHash, zIdxName, 0); |
drh | 2264584 | 2011-03-24 01:34:03 +0000 | [diff] [blame] | 569 | if( ALWAYS(pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 570 | if( pIndex->pTable->pIndex==pIndex ){ |
| 571 | pIndex->pTable->pIndex = pIndex->pNext; |
| 572 | }else{ |
| 573 | Index *p; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 574 | /* Justification of ALWAYS(); The index must be on the list of |
| 575 | ** indices. */ |
| 576 | p = pIndex->pTable->pIndex; |
| 577 | while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
| 578 | if( ALWAYS(p && p->pNext==pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 579 | p->pNext = pIndex->pNext; |
| 580 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 581 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 582 | sqlite3FreeIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 583 | } |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 584 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 588 | ** Look through the list of open database files in db->aDb[] and if |
| 589 | ** any have been closed, remove them from the list. Reallocate the |
| 590 | ** db->aDb[] structure to a smaller size, if possible. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 591 | ** |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 592 | ** Entry 0 (the "main" database) and entry 1 (the "temp" database) |
| 593 | ** are never candidates for being collapsed. |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 594 | */ |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 595 | void sqlite3CollapseDatabaseArray(sqlite3 *db){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 596 | int i, j; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 597 | for(i=j=2; i<db->nDb; i++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 598 | struct Db *pDb = &db->aDb[i]; |
| 599 | if( pDb->pBt==0 ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 600 | sqlite3DbFree(db, pDb->zDbSName); |
| 601 | pDb->zDbSName = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 602 | continue; |
| 603 | } |
| 604 | if( j<i ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 605 | db->aDb[j] = db->aDb[i]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 606 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 607 | j++; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 608 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 609 | db->nDb = j; |
| 610 | if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 611 | memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 612 | sqlite3DbFree(db, db->aDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 613 | db->aDb = db->aDbStatic; |
| 614 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 618 | ** Reset the schema for the database at index iDb. Also reset the |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 619 | ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. |
| 620 | ** Deferred resets may be run by calling with iDb<0. |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 621 | */ |
| 622 | void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 623 | int i; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 624 | assert( iDb<db->nDb ); |
| 625 | |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 626 | if( iDb>=0 ){ |
| 627 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 628 | DbSetProperty(db, iDb, DB_ResetWanted); |
| 629 | DbSetProperty(db, 1, DB_ResetWanted); |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 630 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 631 | } |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 632 | |
| 633 | if( db->nSchemaLock==0 ){ |
| 634 | for(i=0; i<db->nDb; i++){ |
| 635 | if( DbHasProperty(db, i, DB_ResetWanted) ){ |
| 636 | sqlite3SchemaClear(db->aDb[i].pSchema); |
| 637 | } |
| 638 | } |
| 639 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* |
| 643 | ** Erase all schema information from all attached databases (including |
| 644 | ** "main" and "temp") for a single database connection. |
| 645 | */ |
| 646 | void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ |
| 647 | int i; |
| 648 | sqlite3BtreeEnterAll(db); |
| 649 | for(i=0; i<db->nDb; i++){ |
| 650 | Db *pDb = &db->aDb[i]; |
| 651 | if( pDb->pSchema ){ |
dan | 63e50b9 | 2018-11-27 19:47:55 +0000 | [diff] [blame] | 652 | if( db->nSchemaLock==0 ){ |
| 653 | sqlite3SchemaClear(pDb->pSchema); |
| 654 | }else{ |
| 655 | DbSetProperty(db, i, DB_ResetWanted); |
| 656 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 659 | db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 660 | sqlite3VtabUnlockList(db); |
| 661 | sqlite3BtreeLeaveAll(db); |
dan | 63e50b9 | 2018-11-27 19:47:55 +0000 | [diff] [blame] | 662 | if( db->nSchemaLock==0 ){ |
| 663 | sqlite3CollapseDatabaseArray(db); |
| 664 | } |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 668 | ** This routine is called when a commit occurs. |
| 669 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 670 | void sqlite3CommitInternalChanges(sqlite3 *db){ |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 671 | db->mDbFlags &= ~DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 675 | ** Set the expression associated with a column. This is usually |
| 676 | ** the DEFAULT value, but might also be the expression that computes |
| 677 | ** the value for a generated column. |
| 678 | */ |
| 679 | void sqlite3ColumnSetExpr( |
| 680 | Parse *pParse, /* Parsing context */ |
| 681 | Table *pTab, /* The table containing the column */ |
| 682 | Column *pCol, /* The column to receive the new DEFAULT expression */ |
| 683 | Expr *pExpr /* The new default expression */ |
| 684 | ){ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 685 | ExprList *pList; |
| 686 | assert( !IsVirtual(pTab) ); |
| 687 | pList = pTab->u.tab.pDfltList; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 688 | if( pCol->iDflt==0 |
drh | 324f91a | 2021-08-04 14:50:23 +0000 | [diff] [blame] | 689 | || NEVER(pList==0) |
| 690 | || NEVER(pList->nExpr<pCol->iDflt) |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 691 | ){ |
| 692 | pCol->iDflt = pList==0 ? 1 : pList->nExpr+1; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 693 | pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr); |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 694 | }else{ |
| 695 | sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr); |
| 696 | pList->a[pCol->iDflt-1].pExpr = pExpr; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | ** Return the expression associated with a column. The expression might be |
| 702 | ** the DEFAULT clause or the AS clause of a generated column. |
| 703 | ** Return NULL if the column has no associated expression. |
| 704 | */ |
| 705 | Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){ |
| 706 | if( pCol->iDflt==0 ) return 0; |
drh | 324f91a | 2021-08-04 14:50:23 +0000 | [diff] [blame] | 707 | if( NEVER(IsVirtual(pTab)) ) return 0; |
| 708 | if( NEVER(pTab->u.tab.pDfltList==0) ) return 0; |
| 709 | if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 710 | return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 714 | ** Set the collating sequence name for a column. |
| 715 | */ |
| 716 | void sqlite3ColumnSetColl( |
| 717 | sqlite3 *db, |
| 718 | Column *pCol, |
| 719 | const char *zColl |
| 720 | ){ |
| 721 | int nColl; |
| 722 | int n; |
| 723 | char *zNew; |
| 724 | assert( zColl!=0 ); |
| 725 | n = sqlite3Strlen30(pCol->zCnName) + 1; |
| 726 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 727 | n += sqlite3Strlen30(pCol->zCnName+n) + 1; |
| 728 | } |
| 729 | nColl = sqlite3Strlen30(zColl) + 1; |
| 730 | zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n); |
| 731 | if( zNew ){ |
| 732 | pCol->zCnName = zNew; |
| 733 | memcpy(pCol->zCnName + n, zColl, nColl); |
| 734 | pCol->colFlags |= COLFLAG_HASCOLL; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | ** Return the collating squence name for a column |
| 740 | */ |
| 741 | const char *sqlite3ColumnColl(Column *pCol){ |
| 742 | const char *z; |
| 743 | if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0; |
| 744 | z = pCol->zCnName; |
| 745 | while( *z ){ z++; } |
| 746 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 747 | do{ z++; }while( *z ); |
| 748 | } |
| 749 | return z+1; |
| 750 | } |
| 751 | |
| 752 | /* |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 753 | ** Delete memory allocated for the column names of a table or view (the |
| 754 | ** Table.aCol[] array). |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 755 | */ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 756 | void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 757 | int i; |
| 758 | Column *pCol; |
| 759 | assert( pTable!=0 ); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 760 | if( (pCol = pTable->aCol)!=0 ){ |
| 761 | for(i=0; i<pTable->nCol; i++, pCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 762 | assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) ); |
| 763 | sqlite3DbFree(db, pCol->zCnName); |
drh | dd5b2fa | 2005-03-28 03:39:55 +0000 | [diff] [blame] | 764 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 765 | sqlite3DbFree(db, pTable->aCol); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 766 | if( !IsVirtual(pTable) ){ |
| 767 | sqlite3ExprListDelete(db, pTable->u.tab.pDfltList); |
| 768 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 769 | if( db==0 || db->pnBytesFreed==0 ){ |
| 770 | pTable->aCol = 0; |
| 771 | pTable->nCol = 0; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 772 | if( !IsVirtual(pTable) ){ |
| 773 | pTable->u.tab.pDfltList = 0; |
| 774 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 775 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 776 | } |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 780 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 781 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 782 | ** |
| 783 | ** This routine just deletes the data structure. It does not unlink |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 784 | ** the table data structure from the hash table. But it does destroy |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 785 | ** memory structures of the indices and foreign keys associated with |
| 786 | ** the table. |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 787 | ** |
| 788 | ** The db parameter is optional. It is needed if the Table object |
| 789 | ** contains lookaside memory. (Table objects in the schema do not use |
| 790 | ** lookaside memory, but some ephemeral Table objects do.) Or the |
| 791 | ** db parameter can be used with db->pnBytesFreed to measure the memory |
| 792 | ** used by the Table object. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 793 | */ |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 794 | static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 796 | |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 797 | #ifdef SQLITE_DEBUG |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 798 | /* Record the number of outstanding lookaside allocations in schema Tables |
dan | bedf84c | 2019-07-08 13:45:02 +0000 | [diff] [blame] | 799 | ** prior to doing any free() operations. Since schema Tables do not use |
| 800 | ** lookaside, this number should not change. |
| 801 | ** |
| 802 | ** If malloc has already failed, it may be that it failed while allocating |
| 803 | ** a Table object that was going to be marked ephemeral. So do not check |
| 804 | ** that no lookaside memory is used in this case either. */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 805 | int nLookaside = 0; |
dan | bedf84c | 2019-07-08 13:45:02 +0000 | [diff] [blame] | 806 | if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 807 | nLookaside = sqlite3LookasideUsed(db, 0); |
| 808 | } |
| 809 | #endif |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 810 | |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 811 | /* Delete all indices associated with this table. */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 812 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 813 | pNext = pIndex->pNext; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 814 | assert( pIndex->pSchema==pTable->pSchema |
| 815 | || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
drh | 273bfe9 | 2016-06-02 16:22:53 +0000 | [diff] [blame] | 816 | if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 817 | char *zName = pIndex->zName; |
| 818 | TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 819 | &pIndex->pSchema->idxHash, zName, 0 |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 820 | ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 821 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 822 | assert( pOld==pIndex || pOld==0 ); |
| 823 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 824 | sqlite3FreeIndex(db, pIndex); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 825 | } |
| 826 | |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 827 | if( IsOrdinaryTable(pTable) ){ |
| 828 | sqlite3FkDelete(db, pTable); |
| 829 | } |
| 830 | #ifndef SQLITE_OMIT_VIRTUAL_TABLE |
| 831 | else if( IsVirtual(pTable) ){ |
| 832 | sqlite3VtabClear(db, pTable); |
| 833 | } |
| 834 | #endif |
| 835 | else{ |
| 836 | assert( IsView(pTable) ); |
| 837 | sqlite3SelectDelete(db, pTable->u.view.pSelect); |
| 838 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 839 | |
| 840 | /* Delete the Table structure itself. |
| 841 | */ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 842 | sqlite3DeleteColumnNames(db, pTable); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 843 | sqlite3DbFree(db, pTable->zName); |
| 844 | sqlite3DbFree(db, pTable->zColAff); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 845 | sqlite3ExprListDelete(db, pTable->pCheck); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 846 | sqlite3DbFree(db, pTable); |
drh | 29ddd3a | 2012-05-15 12:49:32 +0000 | [diff] [blame] | 847 | |
| 848 | /* Verify that no lookaside memory was used by schema tables */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 +0000 | [diff] [blame] | 849 | assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 850 | } |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 851 | void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 852 | /* Do not delete the table until the reference count reaches zero. */ |
| 853 | if( !pTable ) return; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 854 | if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return; |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 855 | deleteTable(db, pTable); |
| 856 | } |
| 857 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 858 | |
| 859 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 860 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 861 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 862 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 863 | void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 864 | Table *p; |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 865 | Db *pDb; |
| 866 | |
drh | d229ca9 | 2002-01-09 13:30:41 +0000 | [diff] [blame] | 867 | assert( db!=0 ); |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 868 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 972a231 | 2009-12-08 14:34:08 +0000 | [diff] [blame] | 869 | assert( zTabName ); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 870 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 972a231 | 2009-12-08 14:34:08 +0000 | [diff] [blame] | 871 | testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
drh | 956bc92 | 2004-07-24 17:38:29 +0000 | [diff] [blame] | 872 | pDb = &db->aDb[iDb]; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 873 | p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 874 | sqlite3DeleteTable(db, p); |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 875 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 879 | ** Given a token, return a string that consists of the text of that |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 880 | ** token. Space to hold the returned string |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 881 | ** is obtained from sqliteMalloc() and must be freed by the calling |
| 882 | ** function. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 883 | ** |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 884 | ** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
| 885 | ** surround the body of the token are removed. |
| 886 | ** |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 887 | ** Tokens are often just pointers into the original SQL text and so |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 888 | ** are not \000 terminated and are not persistent. The returned string |
| 889 | ** is \000 terminated and is persistent. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 890 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 891 | char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 892 | char *zName; |
| 893 | if( pName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 894 | zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 895 | sqlite3Dequote(zName); |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 896 | }else{ |
| 897 | zName = 0; |
| 898 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 899 | return zName; |
| 900 | } |
| 901 | |
| 902 | /* |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 903 | ** Open the sqlite_schema table stored in database number iDb for |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 904 | ** writing. The table is opened using cursor 0. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 905 | */ |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 906 | void sqlite3OpenSchemaTable(Parse *p, int iDb){ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 907 | Vdbe *v = sqlite3GetVdbe(p); |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 908 | sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, DFLT_SCHEMA_TABLE); |
| 909 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 910 | if( p->nTab==0 ){ |
| 911 | p->nTab = 1; |
| 912 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 916 | ** Parameter zName points to a nul-terminated buffer containing the name |
| 917 | ** of a database ("main", "temp" or the name of an attached db). This |
| 918 | ** function returns the index of the named database in db->aDb[], or |
| 919 | ** -1 if the named db cannot be found. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 920 | */ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 921 | int sqlite3FindDbName(sqlite3 *db, const char *zName){ |
| 922 | int i = -1; /* Database number */ |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 923 | if( zName ){ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 924 | Db *pDb; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 925 | for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
drh | 2951809 | 2016-12-24 19:37:16 +0000 | [diff] [blame] | 926 | if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; |
| 927 | /* "main" is always an acceptable alias for the primary database |
| 928 | ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ |
| 929 | if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 932 | return i; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 933 | } |
| 934 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 935 | /* |
| 936 | ** The token *pName contains the name of a database (either "main" or |
| 937 | ** "temp" or the name of an attached db). This routine returns the |
| 938 | ** index of the named database in db->aDb[], or -1 if the named db |
| 939 | ** does not exist. |
| 940 | */ |
| 941 | int sqlite3FindDb(sqlite3 *db, Token *pName){ |
| 942 | int i; /* Database number */ |
| 943 | char *zName; /* Name we are searching for */ |
| 944 | zName = sqlite3NameFromToken(db, pName); |
| 945 | i = sqlite3FindDbName(db, zName); |
| 946 | sqlite3DbFree(db, zName); |
| 947 | return i; |
| 948 | } |
| 949 | |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 950 | /* The table or view or trigger name is passed to this routine via tokens |
| 951 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 952 | ** |
| 953 | ** CREATE TABLE xxx.yyy (...); |
| 954 | ** |
| 955 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 956 | ** the table name is not fully qualified, i.e.: |
| 957 | ** |
| 958 | ** CREATE TABLE yyy(...); |
| 959 | ** |
| 960 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 961 | ** |
| 962 | ** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
| 963 | ** pName2) that stores the unqualified table name. The index of the |
| 964 | ** database "xxx" is returned. |
| 965 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 966 | int sqlite3TwoPartName( |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 967 | Parse *pParse, /* Parsing and code generating context */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 968 | Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 969 | Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
| 970 | Token **pUnqual /* Write the unqualified object name here */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 971 | ){ |
drh | 0e3d747 | 2004-06-19 17:33:07 +0000 | [diff] [blame] | 972 | int iDb; /* Database holding the object */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 973 | sqlite3 *db = pParse->db; |
| 974 | |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 975 | assert( pName2!=0 ); |
| 976 | if( pName2->n>0 ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 977 | if( db->init.busy ) { |
| 978 | sqlite3ErrorMsg(pParse, "corrupt database"); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 979 | return -1; |
| 980 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 981 | *pUnqual = pName2; |
drh | ff2d5ea | 2005-07-23 00:41:48 +0000 | [diff] [blame] | 982 | iDb = sqlite3FindDb(db, pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 983 | if( iDb<0 ){ |
| 984 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 985 | return -1; |
| 986 | } |
| 987 | }else{ |
drh | d36bcec | 2021-05-29 21:50:05 +0000 | [diff] [blame] | 988 | assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 989 | || (db->mDbFlags & DBFLAG_Vacuum)!=0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 990 | iDb = db->init.iDb; |
| 991 | *pUnqual = pName1; |
| 992 | } |
| 993 | return iDb; |
| 994 | } |
| 995 | |
| 996 | /* |
drh | 0f1c2eb | 2018-11-03 17:31:48 +0000 | [diff] [blame] | 997 | ** True if PRAGMA writable_schema is ON |
| 998 | */ |
| 999 | int sqlite3WritableSchema(sqlite3 *db){ |
| 1000 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); |
| 1001 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1002 | SQLITE_WriteSchema ); |
| 1003 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1004 | SQLITE_Defensive ); |
| 1005 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1006 | (SQLITE_WriteSchema|SQLITE_Defensive) ); |
| 1007 | return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1011 | ** This routine is used to check if the UTF-8 string zName is a legal |
| 1012 | ** unqualified name for a new schema object (table, index, view or |
| 1013 | ** trigger). All names are legal except those that begin with the string |
| 1014 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
| 1015 | ** is reserved for internal use. |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1016 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1017 | ** When parsing the sqlite_schema table, this routine also checks to |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1018 | ** make sure the "type", "name", and "tbl_name" columns are consistent |
| 1019 | ** with the SQL. |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1020 | */ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1021 | int sqlite3CheckObjectName( |
| 1022 | Parse *pParse, /* Parsing context */ |
| 1023 | const char *zName, /* Name of the object to check */ |
| 1024 | const char *zType, /* Type of this object */ |
| 1025 | const char *zTblName /* Parent table name for triggers and indexes */ |
| 1026 | ){ |
| 1027 | sqlite3 *db = pParse->db; |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 1028 | if( sqlite3WritableSchema(db) |
| 1029 | || db->init.imposterTable |
| 1030 | || !sqlite3Config.bExtraSchemaChecks |
| 1031 | ){ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1032 | /* Skip these error checks for writable_schema=ON */ |
| 1033 | return SQLITE_OK; |
| 1034 | } |
| 1035 | if( db->init.busy ){ |
| 1036 | if( sqlite3_stricmp(zType, db->init.azInit[0]) |
| 1037 | || sqlite3_stricmp(zName, db->init.azInit[1]) |
| 1038 | || sqlite3_stricmp(zTblName, db->init.azInit[2]) |
| 1039 | ){ |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 1040 | sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ |
| 1041 | return SQLITE_ERROR; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1042 | } |
| 1043 | }else{ |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 1044 | if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) |
| 1045 | || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1046 | ){ |
| 1047 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", |
| 1048 | zName); |
| 1049 | return SQLITE_ERROR; |
| 1050 | } |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 1051 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1052 | } |
| 1053 | return SQLITE_OK; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1057 | ** Return the PRIMARY KEY index of a table |
| 1058 | */ |
| 1059 | Index *sqlite3PrimaryKeyIndex(Table *pTab){ |
| 1060 | Index *p; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 1061 | for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1062 | return p; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1066 | ** Convert an table column number into a index column number. That is, |
| 1067 | ** for the column iCol in the table (as defined by the CREATE TABLE statement) |
| 1068 | ** find the (first) offset of that column in index pIdx. Or return -1 |
| 1069 | ** if column iCol is not used in index pIdx. |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1070 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1071 | i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1072 | int i; |
| 1073 | for(i=0; i<pIdx->nColumn; i++){ |
| 1074 | if( iCol==pIdx->aiColumn[i] ) return i; |
| 1075 | } |
| 1076 | return -1; |
| 1077 | } |
| 1078 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1079 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1080 | /* Convert a storage column number into a table column number. |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1081 | ** |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1082 | ** The storage column number (0,1,2,....) is the index of the value |
| 1083 | ** as it appears in the record on disk. The true column number |
| 1084 | ** is the index (0,1,2,...) of the column in the CREATE TABLE statement. |
| 1085 | ** |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1086 | ** The storage column number is less than the table column number if |
| 1087 | ** and only there are VIRTUAL columns to the left. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1088 | ** |
| 1089 | ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1090 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1091 | i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1092 | if( pTab->tabFlags & TF_HasVirtual ){ |
| 1093 | int i; |
| 1094 | for(i=0; i<=iCol; i++){ |
| 1095 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++; |
| 1096 | } |
| 1097 | } |
| 1098 | return iCol; |
| 1099 | } |
| 1100 | #endif |
| 1101 | |
| 1102 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1103 | /* Convert a table column number into a storage column number. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1104 | ** |
| 1105 | ** The storage column number (0,1,2,....) is the index of the value |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1106 | ** as it appears in the record on disk. Or, if the input column is |
| 1107 | ** the N-th virtual column (zero-based) then the storage number is |
| 1108 | ** the number of non-virtual columns in the table plus N. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1109 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1110 | ** The true column number is the index (0,1,2,...) of the column in |
| 1111 | ** the CREATE TABLE statement. |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 1112 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1113 | ** If the input column is a VIRTUAL column, then it should not appear |
| 1114 | ** in storage. But the value sometimes is cached in registers that |
| 1115 | ** follow the range of registers used to construct storage. This |
| 1116 | ** avoids computing the same VIRTUAL column multiple times, and provides |
| 1117 | ** values for use by OP_Param opcodes in triggers. Hence, if the |
| 1118 | ** input column is a VIRTUAL table, put it after all the other columns. |
| 1119 | ** |
| 1120 | ** In the following, N means "normal column", S means STORED, and |
| 1121 | ** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: |
| 1122 | ** |
| 1123 | ** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); |
| 1124 | ** -- 0 1 2 3 4 5 6 7 8 |
| 1125 | ** |
| 1126 | ** Then the mapping from this function is as follows: |
| 1127 | ** |
| 1128 | ** INPUTS: 0 1 2 3 4 5 6 7 8 |
| 1129 | ** OUTPUTS: 0 1 6 2 3 7 4 5 8 |
| 1130 | ** |
| 1131 | ** So, in other words, this routine shifts all the virtual columns to |
| 1132 | ** the end. |
| 1133 | ** |
| 1134 | ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and |
drh | 7fe2fc0 | 2019-12-07 00:22:18 +0000 | [diff] [blame] | 1135 | ** this routine is a no-op macro. If the pTab does not have any virtual |
| 1136 | ** columns, then this routine is no-op that always return iCol. If iCol |
| 1137 | ** is negative (indicating the ROWID column) then this routine return iCol. |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1138 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1139 | i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1140 | int i; |
| 1141 | i16 n; |
| 1142 | assert( iCol<pTab->nCol ); |
drh | 7fe2fc0 | 2019-12-07 00:22:18 +0000 | [diff] [blame] | 1143 | if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1144 | for(i=0, n=0; i<iCol; i++){ |
| 1145 | if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++; |
| 1146 | } |
drh | dd6cc9b | 2019-10-19 18:47:27 +0000 | [diff] [blame] | 1147 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){ |
| 1148 | /* iCol is a virtual column itself */ |
| 1149 | return pTab->nNVCol + i - n; |
| 1150 | }else{ |
| 1151 | /* iCol is a normal or stored column */ |
| 1152 | return n; |
| 1153 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1154 | } |
| 1155 | #endif |
| 1156 | |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1157 | /* |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1158 | ** Insert a single OP_JournalMode query opcode in order to force the |
| 1159 | ** prepared statement to return false for sqlite3_stmt_readonly(). This |
| 1160 | ** is used by CREATE TABLE IF NOT EXISTS and similar if the table already |
| 1161 | ** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS |
| 1162 | ** will return false for sqlite3_stmt_readonly() even if that statement |
| 1163 | ** is a read-only no-op. |
| 1164 | */ |
| 1165 | static void sqlite3ForceNotReadOnly(Parse *pParse){ |
| 1166 | int iReg = ++pParse->nMem; |
| 1167 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1168 | if( v ){ |
| 1169 | sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY); |
dan | a8f249f | 2021-05-20 11:42:51 +0000 | [diff] [blame] | 1170 | sqlite3VdbeUsesBtree(v, 0); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1175 | ** Begin constructing a new table representation in memory. This is |
| 1176 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1177 | ** to a CREATE TABLE statement. In particular, this routine is called |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1178 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 1179 | ** flag is true if the table should be stored in the auxiliary database |
| 1180 | ** file instead of in the main database file. This is normally the case |
| 1181 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1182 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1183 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1184 | ** The new table record is initialized and put in pParse->pNewTable. |
| 1185 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 1186 | ** routines will be called to add more information to this record. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1187 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1188 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1189 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1190 | void sqlite3StartTable( |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1191 | Parse *pParse, /* Parser context */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1192 | Token *pName1, /* First part of the name of the table or view */ |
| 1193 | Token *pName2, /* Second part of the name of the table or view */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1194 | int isTemp, /* True if this is a TEMP table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1195 | int isView, /* True if this is a VIEW */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 1196 | int isVirtual, /* True if this is a VIRTUAL table */ |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1197 | int noErr /* Do nothing if table already exists */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1198 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1199 | Table *pTable; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1200 | char *zName = 0; /* The name of the new table */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1201 | sqlite3 *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1202 | Vdbe *v; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1203 | int iDb; /* Database number to create the table in */ |
| 1204 | Token *pName; /* Unqualified name of the table to create */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1205 | |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1206 | if( db->init.busy && db->init.newTnum==1 ){ |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1207 | /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */ |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1208 | iDb = db->init.iDb; |
| 1209 | zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); |
| 1210 | pName = pName1; |
| 1211 | }else{ |
| 1212 | /* The common case */ |
| 1213 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 1214 | if( iDb<0 ) return; |
| 1215 | if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
| 1216 | /* If creating a temp table, the name may not be qualified. Unless |
| 1217 | ** the database name is "temp" anyway. */ |
| 1218 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 1219 | return; |
| 1220 | } |
| 1221 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 1222 | zName = sqlite3NameFromToken(db, pName); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1223 | if( IN_RENAME_OBJECT ){ |
| 1224 | sqlite3RenameTokenMap(pParse, (void*)zName, pName); |
| 1225 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1226 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 1227 | pParse->sNameToken = *pName; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1228 | if( zName==0 ) return; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 1229 | if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1230 | goto begin_table_error; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1231 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 1232 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1233 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1234 | assert( isTemp==0 || isTemp==1 ); |
| 1235 | assert( isView==0 || isView==1 ); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1236 | { |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1237 | static const u8 aCode[] = { |
| 1238 | SQLITE_CREATE_TABLE, |
| 1239 | SQLITE_CREATE_TEMP_TABLE, |
| 1240 | SQLITE_CREATE_VIEW, |
| 1241 | SQLITE_CREATE_TEMP_VIEW |
| 1242 | }; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1243 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1244 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1245 | goto begin_table_error; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 1246 | } |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 1247 | if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], |
| 1248 | zName, 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1249 | goto begin_table_error; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | #endif |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1253 | |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1254 | /* Make sure the new table name does not collide with an existing |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 1255 | ** index or table name in the same database. Issue an error message if |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1256 | ** it does. The exception is if the statement being parsed was passed |
| 1257 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 1258 | ** and types will be used, so there is no need to test for namespace |
| 1259 | ** collisions. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 1260 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 1261 | if( !IN_SPECIAL_PARSE ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1262 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1263 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 1264 | goto begin_table_error; |
drh | faa5955 | 2005-12-29 23:33:54 +0000 | [diff] [blame] | 1265 | } |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1266 | pTable = sqlite3FindTable(db, zName, zDb); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1267 | if( pTable ){ |
| 1268 | if( !noErr ){ |
| 1269 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
dan | 7687c83 | 2011-04-09 15:39:02 +0000 | [diff] [blame] | 1270 | }else{ |
drh | 33c59ec | 2015-04-19 20:39:17 +0000 | [diff] [blame] | 1271 | assert( !db->init.busy || CORRUPT_DB ); |
dan | 7687c83 | 2011-04-09 15:39:02 +0000 | [diff] [blame] | 1272 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 1273 | sqlite3ForceNotReadOnly(pParse); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1274 | } |
| 1275 | goto begin_table_error; |
| 1276 | } |
drh | 8a8a0d1 | 2010-09-28 20:26:44 +0000 | [diff] [blame] | 1277 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1278 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 1279 | goto begin_table_error; |
| 1280 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1281 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 1282 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 1283 | pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1284 | if( pTable==0 ){ |
drh | 4df86af | 2016-02-04 11:48:00 +0000 | [diff] [blame] | 1285 | assert( db->mallocFailed ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1286 | pParse->rc = SQLITE_NOMEM_BKPT; |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1287 | pParse->nErr++; |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1288 | goto begin_table_error; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1289 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1290 | pTable->zName = zName; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1291 | pTable->iPKey = -1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1292 | pTable->pSchema = db->aDb[iDb].pSchema; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 1293 | pTable->nTabRef = 1; |
drh | d1417ee | 2017-06-06 18:20:43 +0000 | [diff] [blame] | 1294 | #ifdef SQLITE_DEFAULT_ROWEST |
| 1295 | pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); |
| 1296 | #else |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 1297 | pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | d1417ee | 2017-06-06 18:20:43 +0000 | [diff] [blame] | 1298 | #endif |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1299 | assert( pParse->pNewTable==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1300 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1301 | |
| 1302 | /* Begin generating the code that will insert the table record into |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 1303 | ** the schema table. Note in particular that we must go ahead |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1304 | ** and allocate the record number for the table entry now. Before any |
| 1305 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
| 1306 | ** indices to be created and the table record must come before the |
| 1307 | ** indices. Hence, the record number for the table must be allocated |
| 1308 | ** now. |
| 1309 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1310 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1311 | int addr1; |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 1312 | int fileFormat; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1313 | int reg1, reg2, reg3; |
drh | 3c03afd | 2015-09-09 13:28:06 +0000 | [diff] [blame] | 1314 | /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ |
| 1315 | static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; |
drh | 0dd5cda | 2015-06-16 16:39:01 +0000 | [diff] [blame] | 1316 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 1317 | |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 1318 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1319 | if( isVirtual ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1320 | sqlite3VdbeAddOp0(v, OP_VBegin); |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 +0000 | [diff] [blame] | 1321 | } |
| 1322 | #endif |
| 1323 | |
danielk1977 | 36963fd | 2005-02-19 08:18:05 +0000 | [diff] [blame] | 1324 | /* If the file format and encoding in the database have not been set, |
| 1325 | ** set them now. |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 1326 | */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1327 | reg1 = pParse->regRowid = ++pParse->nMem; |
| 1328 | reg2 = pParse->regRoot = ++pParse->nMem; |
| 1329 | reg3 = ++pParse->nMem; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 1330 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1331 | sqlite3VdbeUsesBtree(v, iDb); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1332 | addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 1333 | fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 1334 | 1 : SQLITE_MAX_FILE_FORMAT; |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 1335 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); |
| 1336 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 1337 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 1338 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 1339 | /* This just creates a place-holder record in the sqlite_schema table. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1340 | ** The record created does not contain anything yet. It will be replaced |
| 1341 | ** by the real entry in code generated at sqlite3EndTable(). |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 1342 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 1343 | ** The rowid for the new entry is left in register pParse->regRowid. |
| 1344 | ** The root page number of the new table is left in reg pParse->regRoot. |
| 1345 | ** The rowid and root page number values are needed by the code that |
| 1346 | ** sqlite3EndTable will generate. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1347 | */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 1348 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 1349 | if( isView || isVirtual ){ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1350 | sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1351 | }else |
| 1352 | #endif |
| 1353 | { |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1354 | assert( !pParse->bReturning ); |
| 1355 | pParse->u1.addrCrTab = |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 1356 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1357 | } |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 1358 | sqlite3OpenSchemaTable(pParse, iDb); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1359 | sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); |
drh | 3c03afd | 2015-09-09 13:28:06 +0000 | [diff] [blame] | 1360 | sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1361 | sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); |
| 1362 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1363 | sqlite3VdbeAddOp0(v, OP_Close); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1364 | } |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Normal (non-error) return. */ |
| 1367 | return; |
| 1368 | |
| 1369 | /* If an error occurs, we jump here */ |
| 1370 | begin_table_error: |
drh | c0495e8 | 2021-07-22 21:11:06 +0000 | [diff] [blame] | 1371 | pParse->checkSchema = 1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1372 | sqlite3DbFree(db, zName); |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1373 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1376 | /* Set properties of a table column based on the (magical) |
| 1377 | ** name of the column. |
| 1378 | */ |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1379 | #if SQLITE_ENABLE_HIDDEN_COLUMNS |
drh | e611050 | 2015-12-31 15:34:03 +0000 | [diff] [blame] | 1380 | void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1381 | if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){ |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1382 | pCol->colFlags |= COLFLAG_HIDDEN; |
drh | 6f6e60d | 2021-02-18 15:45:34 +0000 | [diff] [blame] | 1383 | if( pTab ) pTab->tabFlags |= TF_HasHidden; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 1384 | }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ |
| 1385 | pTab->tabFlags |= TF_OOOHidden; |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1386 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1387 | } |
drh | e611050 | 2015-12-31 15:34:03 +0000 | [diff] [blame] | 1388 | #endif |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1389 | |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1390 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1391 | ** Name of the special TEMP trigger used to implement RETURNING. The |
| 1392 | ** name begins with "sqlite_" so that it is guaranteed not to collide |
| 1393 | ** with any application-generated triggers. |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1394 | */ |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1395 | #define RETURNING_TRIGGER_NAME "sqlite_returning" |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1396 | |
| 1397 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1398 | ** Clean up the data structures associated with the RETURNING clause. |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1399 | */ |
| 1400 | static void sqlite3DeleteReturning(sqlite3 *db, Returning *pRet){ |
| 1401 | Hash *pHash; |
| 1402 | pHash = &(db->aDb[1].pSchema->trigHash); |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1403 | sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, 0); |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1404 | sqlite3ExprListDelete(db, pRet->pReturnEL); |
| 1405 | sqlite3DbFree(db, pRet); |
| 1406 | } |
| 1407 | |
| 1408 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1409 | ** Add the RETURNING clause to the parse currently underway. |
| 1410 | ** |
| 1411 | ** This routine creates a special TEMP trigger that will fire for each row |
| 1412 | ** of the DML statement. That TEMP trigger contains a single SELECT |
| 1413 | ** statement with a result set that is the argument of the RETURNING clause. |
| 1414 | ** The trigger has the Trigger.bReturning flag and an opcode of |
| 1415 | ** TK_RETURNING instead of TK_SELECT, so that the trigger code generator |
| 1416 | ** knows to handle it specially. The TEMP trigger is automatically |
| 1417 | ** removed at the end of the parse. |
| 1418 | ** |
| 1419 | ** When this routine is called, we do not yet know if the RETURNING clause |
| 1420 | ** is attached to a DELETE, INSERT, or UPDATE, so construct it as a |
| 1421 | ** RETURNING trigger instead. It will then be converted into the appropriate |
| 1422 | ** type on the first call to sqlite3TriggersExist(). |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1423 | */ |
| 1424 | void sqlite3AddReturning(Parse *pParse, ExprList *pList){ |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1425 | Returning *pRet; |
| 1426 | Hash *pHash; |
| 1427 | sqlite3 *db = pParse->db; |
drh | e1c9a4e | 2021-02-07 23:28:20 +0000 | [diff] [blame] | 1428 | if( pParse->pNewTrigger ){ |
| 1429 | sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger"); |
| 1430 | }else{ |
| 1431 | assert( pParse->bReturning==0 ); |
| 1432 | } |
drh | d086aa0 | 2021-01-29 21:31:59 +0000 | [diff] [blame] | 1433 | pParse->bReturning = 1; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1434 | pRet = sqlite3DbMallocZero(db, sizeof(*pRet)); |
| 1435 | if( pRet==0 ){ |
| 1436 | sqlite3ExprListDelete(db, pList); |
| 1437 | return; |
| 1438 | } |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1439 | pParse->u1.pReturning = pRet; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1440 | pRet->pParse = pParse; |
| 1441 | pRet->pReturnEL = pList; |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1442 | sqlite3ParserAddCleanup(pParse, |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1443 | (void(*)(sqlite3*,void*))sqlite3DeleteReturning, pRet); |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 1444 | testcase( pParse->earlyCleanup ); |
drh | cf4108b | 2021-01-30 03:06:19 +0000 | [diff] [blame] | 1445 | if( db->mallocFailed ) return; |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1446 | pRet->retTrig.zName = RETURNING_TRIGGER_NAME; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1447 | pRet->retTrig.op = TK_RETURNING; |
| 1448 | pRet->retTrig.tr_tm = TRIGGER_AFTER; |
| 1449 | pRet->retTrig.bReturning = 1; |
| 1450 | pRet->retTrig.pSchema = db->aDb[1].pSchema; |
drh | a476768 | 2021-04-27 13:04:18 +0000 | [diff] [blame] | 1451 | pRet->retTrig.pTabSchema = db->aDb[1].pSchema; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1452 | pRet->retTrig.step_list = &pRet->retTStep; |
drh | dac9a5f | 2021-01-29 21:18:46 +0000 | [diff] [blame] | 1453 | pRet->retTStep.op = TK_RETURNING; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1454 | pRet->retTStep.pTrig = &pRet->retTrig; |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 1455 | pRet->retTStep.pExprList = pList; |
drh | b835247 | 2021-01-29 19:32:17 +0000 | [diff] [blame] | 1456 | pHash = &(db->aDb[1].pSchema->trigHash); |
drh | e1c9a4e | 2021-02-07 23:28:20 +0000 | [diff] [blame] | 1457 | assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr ); |
drh | 28828c5 | 2021-01-30 21:55:38 +0000 | [diff] [blame] | 1458 | if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig) |
drh | 0166df0 | 2021-01-30 12:07:32 +0000 | [diff] [blame] | 1459 | ==&pRet->retTrig ){ |
| 1460 | sqlite3OomFault(db); |
| 1461 | } |
drh | 2053f31 | 2021-01-12 20:16:31 +0000 | [diff] [blame] | 1462 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 +0000 | [diff] [blame] | 1463 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1464 | /* |
| 1465 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1466 | ** |
| 1467 | ** The parser calls this routine once for each column declaration |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1468 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1469 | ** first to get things going. Then this routine is called for each |
| 1470 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1471 | */ |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1472 | void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1473 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1474 | int i; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 1475 | char *z; |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1476 | char *zType; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1477 | Column *pCol; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1478 | sqlite3 *db = pParse->db; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1479 | u8 hName; |
drh | 7b3c514 | 2021-07-30 12:47:35 +0000 | [diff] [blame] | 1480 | Column *aNew; |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1481 | u8 eType = COLTYPE_CUSTOM; |
| 1482 | u8 szEst = 1; |
| 1483 | char affinity = SQLITE_AFF_BLOB; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1484 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1485 | if( (p = pParse->pNewTable)==0 ) return; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1486 | if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 1487 | sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
| 1488 | return; |
| 1489 | } |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1490 | if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(&sName); |
drh | e48f261 | 2021-07-30 20:09:08 +0000 | [diff] [blame] | 1491 | |
| 1492 | /* Because keywords GENERATE ALWAYS can be converted into indentifiers |
| 1493 | ** by the parser, we can sometimes end up with a typename that ends |
| 1494 | ** with "generated always". Check for this case and omit the surplus |
| 1495 | ** text. */ |
| 1496 | if( sType.n>=16 |
| 1497 | && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0 |
| 1498 | ){ |
| 1499 | sType.n -= 6; |
| 1500 | while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 1501 | if( sType.n>=9 |
| 1502 | && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0 |
| 1503 | ){ |
| 1504 | sType.n -= 9; |
| 1505 | while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 1506 | } |
| 1507 | } |
| 1508 | |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1509 | /* Check for standard typenames. For standard typenames we will |
| 1510 | ** set the Column.eType field rather than storing the typename after |
| 1511 | ** the column name, in order to save space. */ |
| 1512 | if( sType.n>=3 ){ |
| 1513 | sqlite3DequoteToken(&sType); |
| 1514 | for(i=0; i<SQLITE_N_STDTYPE; i++){ |
| 1515 | if( sType.n==sqlite3StdTypeLen[i] |
| 1516 | && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0 |
| 1517 | ){ |
| 1518 | sType.n = 0; |
| 1519 | eType = i+1; |
| 1520 | affinity = sqlite3StdTypeAffinity[i]; |
| 1521 | if( affinity<=SQLITE_AFF_TEXT ) szEst = 5; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | z = sqlite3DbMallocRaw(db, sName.n + 1 + sType.n + (sType.n>0) ); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1528 | if( z==0 ) return; |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1529 | if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName); |
| 1530 | memcpy(z, sName.z, sName.n); |
| 1531 | z[sName.n] = 0; |
drh | 94eaafa | 2016-02-29 15:53:11 +0000 | [diff] [blame] | 1532 | sqlite3Dequote(z); |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1533 | hName = sqlite3StrIHash(z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1534 | for(i=0; i<p->nCol; i++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1535 | if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1536 | sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1537 | sqlite3DbFree(db, z); |
drh | 97fc3d0 | 2002-05-22 21:27:03 +0000 | [diff] [blame] | 1538 | return; |
| 1539 | } |
| 1540 | } |
drh | 7b3c514 | 2021-07-30 12:47:35 +0000 | [diff] [blame] | 1541 | aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+1)*sizeof(p->aCol[0])); |
| 1542 | if( aNew==0 ){ |
| 1543 | sqlite3DbFree(db, z); |
| 1544 | return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1545 | } |
drh | 7b3c514 | 2021-07-30 12:47:35 +0000 | [diff] [blame] | 1546 | p->aCol = aNew; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1547 | pCol = &p->aCol[p->nCol]; |
| 1548 | memset(pCol, 0, sizeof(p->aCol[0])); |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1549 | pCol->zCnName = z; |
drh | 3e992d1 | 2021-01-01 19:17:01 +0000 | [diff] [blame] | 1550 | pCol->hName = hName; |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 1551 | sqlite3ColumnPropertiesFromName(p, pCol); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1552 | |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1553 | if( sType.n==0 ){ |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1554 | /* If there is no type specified, columns have the default affinity |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1555 | ** 'BLOB' with a default size of 4 bytes. */ |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1556 | pCol->affinity = affinity; |
drh | b70f2ea | 2021-08-18 12:05:22 +0000 | [diff] [blame] | 1557 | pCol->eCType = eType; |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1558 | pCol->szEst = szEst; |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1559 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | c2df4d6 | 2021-07-30 23:30:30 +0000 | [diff] [blame] | 1560 | if( affinity==SQLITE_AFF_BLOB ){ |
| 1561 | if( 4>=sqlite3GlobalConfig.szSorterRef ){ |
| 1562 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1563 | } |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1564 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1565 | #endif |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1566 | }else{ |
drh | ddb2b4a | 2016-03-25 12:10:32 +0000 | [diff] [blame] | 1567 | zType = z + sqlite3Strlen30(z) + 1; |
drh | 77441fa | 2021-07-30 18:39:59 +0000 | [diff] [blame] | 1568 | memcpy(zType, sType.z, sType.n); |
| 1569 | zType[sType.n] = 0; |
drh | a6dddd9 | 2016-04-18 15:46:14 +0000 | [diff] [blame] | 1570 | sqlite3Dequote(zType); |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1571 | pCol->affinity = sqlite3AffinityType(zType, pCol); |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1572 | pCol->colFlags |= COLFLAG_HASTYPE; |
drh | 2881ab6 | 2016-02-27 23:25:36 +0000 | [diff] [blame] | 1573 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1574 | p->nCol++; |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 1575 | p->nNVCol++; |
drh | 986dde7 | 2016-02-29 13:37:21 +0000 | [diff] [blame] | 1576 | pParse->constraintName.n = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1580 | ** This routine is called by the parser while in the middle of |
| 1581 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 1582 | ** been seen on a column. This routine sets the notNull flag on |
| 1583 | ** the column currently under construction. |
| 1584 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1585 | void sqlite3AddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1586 | Table *p; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1587 | Column *pCol; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1588 | p = pParse->pNewTable; |
| 1589 | if( p==0 || NEVER(p->nCol<1) ) return; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1590 | pCol = &p->aCol[p->nCol-1]; |
| 1591 | pCol->notNull = (u8)onError; |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1592 | p->tabFlags |= TF_HasNotNull; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 1593 | |
| 1594 | /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created |
| 1595 | ** on this column. */ |
| 1596 | if( pCol->colFlags & COLFLAG_UNIQUE ){ |
| 1597 | Index *pIdx; |
| 1598 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1599 | assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); |
| 1600 | if( pIdx->aiColumn[0]==p->nCol-1 ){ |
| 1601 | pIdx->uniqNotNull = 1; |
| 1602 | } |
| 1603 | } |
| 1604 | } |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1608 | ** Scan the column type name zType (length nType) and return the |
| 1609 | ** associated affinity type. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1610 | ** |
| 1611 | ** This routine does a case-independent search of zType for the |
| 1612 | ** substrings in the following table. If one of the substrings is |
| 1613 | ** found, the corresponding affinity is returned. If zType contains |
| 1614 | ** more than one of the substrings, entries toward the top of |
| 1615 | ** the table take priority. For example, if zType is 'BLOBINT', |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1616 | ** SQLITE_AFF_INTEGER is returned. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1617 | ** |
| 1618 | ** Substring | Affinity |
| 1619 | ** -------------------------------- |
| 1620 | ** 'INT' | SQLITE_AFF_INTEGER |
| 1621 | ** 'CHAR' | SQLITE_AFF_TEXT |
| 1622 | ** 'CLOB' | SQLITE_AFF_TEXT |
| 1623 | ** 'TEXT' | SQLITE_AFF_TEXT |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1624 | ** 'BLOB' | SQLITE_AFF_BLOB |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1625 | ** 'REAL' | SQLITE_AFF_REAL |
| 1626 | ** 'FLOA' | SQLITE_AFF_REAL |
| 1627 | ** 'DOUB' | SQLITE_AFF_REAL |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1628 | ** |
| 1629 | ** If none of the substrings in the above table are found, |
| 1630 | ** SQLITE_AFF_NUMERIC is returned. |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1631 | */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1632 | char sqlite3AffinityType(const char *zIn, Column *pCol){ |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1633 | u32 h = 0; |
| 1634 | char aff = SQLITE_AFF_NUMERIC; |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1635 | const char *zChar = 0; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1636 | |
drh | 2f1e02e | 2016-03-09 02:12:44 +0000 | [diff] [blame] | 1637 | assert( zIn!=0 ); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1638 | while( zIn[0] ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1639 | h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1640 | zIn++; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1641 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1642 | aff = SQLITE_AFF_TEXT; |
| 1643 | zChar = zIn; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1644 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| 1645 | aff = SQLITE_AFF_TEXT; |
| 1646 | }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 1647 | aff = SQLITE_AFF_TEXT; |
| 1648 | }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1649 | && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 1650 | aff = SQLITE_AFF_BLOB; |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1651 | if( zIn[0]=='(' ) zChar = zIn; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1652 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1653 | }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
| 1654 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1655 | aff = SQLITE_AFF_REAL; |
| 1656 | }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
| 1657 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1658 | aff = SQLITE_AFF_REAL; |
| 1659 | }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
| 1660 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1661 | aff = SQLITE_AFF_REAL; |
| 1662 | #endif |
danielk1977 | 201f716 | 2005-02-01 02:13:29 +0000 | [diff] [blame] | 1663 | }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1664 | aff = SQLITE_AFF_INTEGER; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1665 | break; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1666 | } |
| 1667 | } |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1668 | |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1669 | /* If pCol is not NULL, store an estimate of the field size. The |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1670 | ** estimate is scaled so that the size of an integer is 1. */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1671 | if( pCol ){ |
| 1672 | int v = 0; /* default size is approx 4 bytes */ |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 1673 | if( aff<SQLITE_AFF_NUMERIC ){ |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1674 | if( zChar ){ |
| 1675 | while( zChar[0] ){ |
| 1676 | if( sqlite3Isdigit(zChar[0]) ){ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1677 | /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1678 | sqlite3GetInt32(zChar, &v); |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | } |
| 1681 | zChar++; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1682 | } |
drh | d3037a4 | 2013-10-04 18:29:25 +0000 | [diff] [blame] | 1683 | }else{ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1684 | v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1685 | } |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1686 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1687 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1688 | if( v>=sqlite3GlobalConfig.szSorterRef ){ |
| 1689 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1690 | } |
drh | bbade8d | 2018-04-18 14:48:08 +0000 | [diff] [blame] | 1691 | #endif |
dan | 2e3a5a8 | 2018-04-16 21:12:42 +0000 | [diff] [blame] | 1692 | v = v/4 + 1; |
| 1693 | if( v>255 ) v = 255; |
| 1694 | pCol->szEst = v; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 1695 | } |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 +0000 | [diff] [blame] | 1696 | return aff; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /* |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1700 | ** The expression is the default value for the most recently added column |
| 1701 | ** of the table currently under construction. |
| 1702 | ** |
| 1703 | ** Default value expressions must be constant. Raise an exception if this |
| 1704 | ** is not the case. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1705 | ** |
| 1706 | ** This routine is called by the parser while in the middle of |
| 1707 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1708 | */ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1709 | void sqlite3AddDefaultValue( |
| 1710 | Parse *pParse, /* Parsing context */ |
| 1711 | Expr *pExpr, /* The parsed expression of the default value */ |
| 1712 | const char *zStart, /* Start of the default value text */ |
| 1713 | const char *zEnd /* First character past end of defaut value text */ |
| 1714 | ){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1715 | Table *p; |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1716 | Column *pCol; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1717 | sqlite3 *db = pParse->db; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1718 | p = pParse->pNewTable; |
| 1719 | if( p!=0 ){ |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 1720 | int isInit = db->init.busy && db->init.iDb!=1; |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1721 | pCol = &(p->aCol[p->nCol-1]); |
drh | 014fff2 | 2020-01-08 22:22:36 +0000 | [diff] [blame] | 1722 | if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){ |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1723 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1724 | pCol->zCnName); |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1725 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1726 | }else if( pCol->colFlags & COLFLAG_GENERATED ){ |
drh | ab0992f | 2019-10-23 03:53:10 +0000 | [diff] [blame] | 1727 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1728 | testcase( pCol->colFlags & COLFLAG_STORED ); |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1729 | sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column"); |
| 1730 | #endif |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1731 | }else{ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1732 | /* A copy of pExpr is used instead of the original, as pExpr contains |
drh | 424981d | 2018-03-28 15:56:55 +0000 | [diff] [blame] | 1733 | ** tokens that point to volatile memory. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1734 | */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1735 | Expr x, *pDfltExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1736 | memset(&x, 0, sizeof(x)); |
| 1737 | x.op = TK_SPAN; |
drh | 9b2e043 | 2017-12-27 19:43:22 +0000 | [diff] [blame] | 1738 | x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1739 | x.pLeft = pExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1740 | x.flags = EP_Skip; |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1741 | pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 1742 | sqlite3DbFree(db, x.u.zToken); |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1743 | sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr); |
drh | 42b9d7c | 2005-08-13 00:56:27 +0000 | [diff] [blame] | 1744 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 1745 | } |
dan | 8900a48 | 2018-09-05 14:36:05 +0000 | [diff] [blame] | 1746 | if( IN_RENAME_OBJECT ){ |
| 1747 | sqlite3RenameExprUnmap(pParse, pExpr); |
| 1748 | } |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1749 | sqlite3ExprDelete(db, pExpr); |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | /* |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1753 | ** Backwards Compatibility Hack: |
| 1754 | ** |
| 1755 | ** Historical versions of SQLite accepted strings as column names in |
| 1756 | ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
| 1757 | ** |
| 1758 | ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) |
| 1759 | ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); |
| 1760 | ** |
| 1761 | ** This is goofy. But to preserve backwards compatibility we continue to |
| 1762 | ** accept it. This routine does the necessary conversion. It converts |
| 1763 | ** the expression given in its argument from a TK_STRING into a TK_ID |
| 1764 | ** if the expression is just a TK_STRING with an optional COLLATE clause. |
drh | 6c59136 | 2019-04-27 20:16:42 +0000 | [diff] [blame] | 1765 | ** If the expression is anything other than TK_STRING, the expression is |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1766 | ** unchanged. |
| 1767 | */ |
| 1768 | static void sqlite3StringToId(Expr *p){ |
| 1769 | if( p->op==TK_STRING ){ |
| 1770 | p->op = TK_ID; |
| 1771 | }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ |
| 1772 | p->pLeft->op = TK_ID; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /* |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1777 | ** Tag the given column as being part of the PRIMARY KEY |
| 1778 | */ |
| 1779 | static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){ |
| 1780 | pCol->colFlags |= COLFLAG_PRIMKEY; |
| 1781 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1782 | if( pCol->colFlags & COLFLAG_GENERATED ){ |
| 1783 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1784 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 1785 | sqlite3ErrorMsg(pParse, |
| 1786 | "generated columns cannot be part of the PRIMARY KEY"); |
| 1787 | } |
| 1788 | #endif |
| 1789 | } |
| 1790 | |
| 1791 | /* |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1792 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
| 1793 | ** of columns that form the primary key. If pList is NULL, then the |
| 1794 | ** most recently added column of the table is the primary key. |
| 1795 | ** |
| 1796 | ** A table can have at most one primary key. If the table already has |
| 1797 | ** a primary key (and this is the second primary key) then create an |
| 1798 | ** error. |
| 1799 | ** |
| 1800 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 1801 | ** 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] | 1802 | ** field of the table under construction to be the index of the |
| 1803 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 1804 | ** no INTEGER PRIMARY KEY. |
| 1805 | ** |
| 1806 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 1807 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 1808 | */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1809 | void sqlite3AddPrimaryKey( |
| 1810 | Parse *pParse, /* Parsing context */ |
| 1811 | ExprList *pList, /* List of field names to be indexed */ |
| 1812 | int onError, /* What to do with a uniqueness conflict */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 1813 | int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
| 1814 | int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1815 | ){ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1816 | Table *pTab = pParse->pNewTable; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1817 | Column *pCol = 0; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1818 | int iCol = -1, i; |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1819 | int nTerm; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1820 | if( pTab==0 ) goto primary_key_exit; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1821 | if( pTab->tabFlags & TF_HasPrimaryKey ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1822 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 1823 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1824 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1825 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1826 | pTab->tabFlags |= TF_HasPrimaryKey; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1827 | if( pList==0 ){ |
| 1828 | iCol = pTab->nCol - 1; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1829 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1830 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1831 | nTerm = 1; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1832 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1833 | nTerm = pList->nExpr; |
| 1834 | for(i=0; i<nTerm; i++){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1835 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 1836 | assert( pCExpr!=0 ); |
drh | 153110a | 2015-11-01 21:19:13 +0000 | [diff] [blame] | 1837 | sqlite3StringToId(pCExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 1838 | if( pCExpr->op==TK_ID ){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1839 | const char *zCName = pCExpr->u.zToken; |
| 1840 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1841 | if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){ |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1842 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 +0000 | [diff] [blame] | 1843 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1846 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1847 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1848 | } |
| 1849 | } |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1850 | if( nTerm==1 |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1851 | && pCol |
drh | b70f2ea | 2021-08-18 12:05:22 +0000 | [diff] [blame] | 1852 | && pCol->eCType==COLTYPE_INTEGER |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1853 | && sortOrder!=SQLITE_SO_DESC |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 1854 | ){ |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1855 | if( IN_RENAME_OBJECT && pList ){ |
dan | 2381f6d | 2019-03-20 16:58:21 +0000 | [diff] [blame] | 1856 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 1857 | sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 1858 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1859 | pTab->iPKey = iCol; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1860 | pTab->keyConf = (u8)onError; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1861 | assert( autoInc==0 || autoInc==1 ); |
| 1862 | pTab->tabFlags |= autoInc*TF_Autoincrement; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 1863 | if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags; |
drh | 34ab941 | 2019-12-19 17:42:27 +0000 | [diff] [blame] | 1864 | (void)sqlite3HasExplicitNulls(pParse, pList); |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1865 | }else if( autoInc ){ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1866 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 1867 | sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1868 | "INTEGER PRIMARY KEY"); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 1869 | #endif |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1870 | }else{ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 1871 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
| 1872 | 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1873 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1874 | } |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1875 | |
| 1876 | primary_key_exit: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1877 | sqlite3ExprListDelete(pParse->db, pList); |
drh | e0194f2 | 2003-02-26 13:52:51 +0000 | [diff] [blame] | 1878 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | /* |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1882 | ** Add a new CHECK constraint to the table currently under construction. |
| 1883 | */ |
| 1884 | void sqlite3AddCheckConstraint( |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 1885 | Parse *pParse, /* Parsing context */ |
| 1886 | Expr *pCheckExpr, /* The check expression */ |
| 1887 | const char *zStart, /* Opening "(" */ |
| 1888 | const char *zEnd /* Closing ")" */ |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1889 | ){ |
| 1890 | #ifndef SQLITE_OMIT_CHECK |
| 1891 | Table *pTab = pParse->pNewTable; |
drh | c9bbb01 | 2014-05-21 08:48:18 +0000 | [diff] [blame] | 1892 | sqlite3 *db = pParse->db; |
| 1893 | if( pTab && !IN_DECLARE_VTAB |
| 1894 | && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) |
| 1895 | ){ |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1896 | pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); |
| 1897 | if( pParse->constraintName.n ){ |
| 1898 | sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1); |
drh | 92e21ef | 2020-08-27 18:36:30 +0000 | [diff] [blame] | 1899 | }else{ |
| 1900 | Token t; |
| 1901 | for(zStart++; sqlite3Isspace(zStart[0]); zStart++){} |
| 1902 | while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; } |
| 1903 | t.z = zStart; |
| 1904 | t.n = (int)(zEnd - t.z); |
| 1905 | sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1); |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1906 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1907 | }else |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1908 | #endif |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1909 | { |
drh | 2938f92 | 2012-03-07 19:13:29 +0000 | [diff] [blame] | 1910 | sqlite3ExprDelete(pParse->db, pCheckExpr); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1911 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1915 | ** Set the collation function of the most recently parsed table column |
| 1916 | ** to the CollSeq given. |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1917 | */ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1918 | void sqlite3AddCollateType(Parse *pParse, Token *pToken){ |
drh | 8e2ca02 | 2002-06-17 17:07:19 +0000 | [diff] [blame] | 1919 | Table *p; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1920 | int i; |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1921 | char *zColl; /* Dequoted name of collation sequence */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1922 | sqlite3 *db; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1923 | |
dan | 936a305 | 2020-10-12 15:27:50 +0000 | [diff] [blame] | 1924 | if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1925 | i = p->nCol-1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1926 | db = pParse->db; |
| 1927 | zColl = sqlite3NameFromToken(db, pToken); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 1928 | if( !zColl ) return; |
| 1929 | |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1930 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1931 | Index *pIdx; |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 1932 | sqlite3ColumnSetColl(db, &p->aCol[i], zColl); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 1935 | ** then an index may have been created on this column before the |
| 1936 | ** collation type was added. Correct this if it is the case. |
| 1937 | */ |
| 1938 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1939 | assert( pIdx->nKeyCol==1 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 1940 | if( pIdx->aiColumn[0]==i ){ |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 1941 | pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | } |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 1945 | sqlite3DbFree(db, zColl); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1948 | /* Change the most recently parsed column to be a GENERATED ALWAYS AS |
| 1949 | ** column. |
| 1950 | */ |
| 1951 | void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){ |
| 1952 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1953 | u8 eType = COLFLAG_VIRTUAL; |
| 1954 | Table *pTab = pParse->pNewTable; |
| 1955 | Column *pCol; |
drh | f68bf5f | 2019-12-04 03:31:29 +0000 | [diff] [blame] | 1956 | if( pTab==0 ){ |
| 1957 | /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ |
| 1958 | goto generated_done; |
| 1959 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1960 | pCol = &(pTab->aCol[pTab->nCol-1]); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1961 | if( IN_DECLARE_VTAB ){ |
| 1962 | sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns"); |
| 1963 | goto generated_done; |
| 1964 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1965 | if( pCol->iDflt>0 ) goto generated_error; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1966 | if( pType ){ |
| 1967 | if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){ |
| 1968 | /* no-op */ |
| 1969 | }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){ |
| 1970 | eType = COLFLAG_STORED; |
| 1971 | }else{ |
| 1972 | goto generated_error; |
| 1973 | } |
| 1974 | } |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 1975 | if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1976 | pCol->colFlags |= eType; |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 1977 | assert( TF_HasVirtual==COLFLAG_VIRTUAL ); |
| 1978 | assert( TF_HasStored==COLFLAG_STORED ); |
| 1979 | pTab->tabFlags |= eType; |
drh | a0e16a2 | 2019-10-27 22:22:24 +0000 | [diff] [blame] | 1980 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 1981 | makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */ |
| 1982 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1983 | sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr); |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 1984 | pExpr = 0; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1985 | goto generated_done; |
| 1986 | |
| 1987 | generated_error: |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1988 | sqlite3ErrorMsg(pParse, "error in generated column \"%s\"", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1989 | pCol->zCnName); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1990 | generated_done: |
| 1991 | sqlite3ExprDelete(pParse->db, pExpr); |
| 1992 | #else |
| 1993 | /* Throw and error for the GENERATED ALWAYS AS clause if the |
| 1994 | ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */ |
drh | 7e7fd73 | 2019-10-22 13:59:23 +0000 | [diff] [blame] | 1995 | sqlite3ErrorMsg(pParse, "generated columns not supported"); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 1996 | sqlite3ExprDelete(pParse->db, pExpr); |
| 1997 | #endif |
| 1998 | } |
| 1999 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 2000 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 2001 | ** Generate code that will increment the schema cookie. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2002 | ** |
| 2003 | ** The schema cookie is used to determine when the schema for the |
| 2004 | ** database changes. After each schema change, the cookie value |
| 2005 | ** changes. When a process first reads the schema it records the |
| 2006 | ** cookie. Thereafter, whenever it goes to access the database, |
| 2007 | ** it checks the cookie to make sure the schema has not changed |
| 2008 | ** since it was last read. |
| 2009 | ** |
| 2010 | ** This plan is not completely bullet-proof. It is possible for |
| 2011 | ** the schema to change multiple times and for the cookie to be |
| 2012 | ** set back to prior value. But schema changes are infrequent |
| 2013 | ** and the probability of hitting the same cookie value is only |
| 2014 | ** 1 chance in 2^32. So we're safe enough. |
drh | 96fdcb4 | 2016-09-27 00:09:33 +0000 | [diff] [blame] | 2015 | ** |
| 2016 | ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments |
| 2017 | ** the schema-version whenever the schema changes. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2018 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2019 | void sqlite3ChangeCookie(Parse *pParse, int iDb){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2020 | sqlite3 *db = pParse->db; |
| 2021 | Vdbe *v = pParse->pVdbe; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2022 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 2023 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, |
drh | 3517b31 | 2018-04-09 00:46:42 +0000 | [diff] [blame] | 2024 | (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2028 | ** Measure the number of characters needed to output the given |
| 2029 | ** identifier. The number returned includes any quotes used |
| 2030 | ** but does not include the null terminator. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2031 | ** |
| 2032 | ** The estimate is conservative. It might be larger that what is |
| 2033 | ** really needed. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2034 | */ |
| 2035 | static int identLength(const char *z){ |
| 2036 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2037 | for(n=0; *z; n++, z++){ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2038 | if( *z=='"' ){ n++; } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2039 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2040 | return n + 2; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /* |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 2044 | ** The first parameter is a pointer to an output buffer. The second |
| 2045 | ** parameter is a pointer to an integer that contains the offset at |
| 2046 | ** which to write into the output buffer. This function copies the |
| 2047 | ** nul-terminated string pointed to by the third parameter, zSignedIdent, |
| 2048 | ** to the specified offset in the buffer and updates *pIdx to refer |
| 2049 | ** to the first byte after the last byte written before returning. |
| 2050 | ** |
| 2051 | ** If the string zSignedIdent consists entirely of alpha-numeric |
| 2052 | ** characters, does not begin with a digit and is not an SQL keyword, |
| 2053 | ** then it is copied to the output buffer exactly as it is. Otherwise, |
| 2054 | ** it is quoted using double-quotes. |
| 2055 | */ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2056 | static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2057 | unsigned char *zIdent = (unsigned char*)zSignedIdent; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2058 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2059 | i = *pIdx; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 2060 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2061 | for(j=0; zIdent[j]; j++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 2062 | if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2063 | } |
drh | c740752 | 2014-01-10 20:38:12 +0000 | [diff] [blame] | 2064 | needQuote = sqlite3Isdigit(zIdent[0]) |
| 2065 | || sqlite3KeywordCode(zIdent, j)!=TK_ID |
| 2066 | || zIdent[j]!=0 |
| 2067 | || j==0; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 +0000 | [diff] [blame] | 2068 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2069 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2070 | for(j=0; zIdent[j]; j++){ |
| 2071 | z[i++] = zIdent[j]; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2072 | if( zIdent[j]=='"' ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2073 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2074 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2075 | z[i] = 0; |
| 2076 | *pIdx = i; |
| 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | ** Generate a CREATE TABLE statement appropriate for the given |
| 2081 | ** table. Memory to hold the text of the statement is obtained |
| 2082 | ** from sqliteMalloc() and must be freed by the calling function. |
| 2083 | */ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 2084 | static char *createTableStmt(sqlite3 *db, Table *p){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2085 | int i, k, n; |
| 2086 | char *zStmt; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2087 | char *zSep, *zSep2, *zEnd; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2088 | Column *pCol; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2089 | n = 0; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2090 | for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2091 | n += identLength(pCol->zCnName) + 5; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2092 | } |
| 2093 | n += identLength(p->zName); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2094 | if( n<50 ){ |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2095 | zSep = ""; |
| 2096 | zSep2 = ","; |
| 2097 | zEnd = ")"; |
| 2098 | }else{ |
| 2099 | zSep = "\n "; |
| 2100 | zSep2 = ",\n "; |
| 2101 | zEnd = "\n)"; |
| 2102 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2103 | n += 35 + 6*p->nCol; |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2104 | zStmt = sqlite3DbMallocRaw(0, n); |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 2105 | if( zStmt==0 ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2106 | sqlite3OomFault(db); |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 2107 | return 0; |
| 2108 | } |
drh | bdb339f | 2009-02-02 18:03:21 +0000 | [diff] [blame] | 2109 | sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2110 | k = sqlite3Strlen30(zStmt); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2111 | identPut(zStmt, &k, p->zName); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2112 | zStmt[k++] = '('; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 2113 | for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2114 | static const char * const azType[] = { |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2115 | /* SQLITE_AFF_BLOB */ "", |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 2116 | /* SQLITE_AFF_TEXT */ " TEXT", |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2117 | /* SQLITE_AFF_NUMERIC */ " NUM", |
| 2118 | /* SQLITE_AFF_INTEGER */ " INT", |
| 2119 | /* SQLITE_AFF_REAL */ " REAL" |
| 2120 | }; |
| 2121 | int len; |
| 2122 | const char *zType; |
| 2123 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2124 | sqlite3_snprintf(n-k, &zStmt[k], zSep); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 2125 | k += sqlite3Strlen30(&zStmt[k]); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2126 | zSep = zSep2; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2127 | identPut(zStmt, &k, pCol->zCnName); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2128 | assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
| 2129 | assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
| 2130 | testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
drh | 7ea31cc | 2014-09-18 14:36:00 +0000 | [diff] [blame] | 2131 | testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2132 | testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
| 2133 | testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
| 2134 | testcase( pCol->affinity==SQLITE_AFF_REAL ); |
| 2135 | |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2136 | zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2137 | len = sqlite3Strlen30(zType); |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2138 | assert( pCol->affinity==SQLITE_AFF_BLOB |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2139 | || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2140 | memcpy(&zStmt[k], zType, len); |
| 2141 | k += len; |
| 2142 | assert( k<=n ); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2143 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2144 | sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2145 | return zStmt; |
| 2146 | } |
| 2147 | |
| 2148 | /* |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2149 | ** Resize an Index object to hold N columns total. Return SQLITE_OK |
| 2150 | ** on success and SQLITE_NOMEM on an OOM error. |
| 2151 | */ |
| 2152 | static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){ |
| 2153 | char *zExtra; |
| 2154 | int nByte; |
| 2155 | if( pIdx->nColumn>=N ) return SQLITE_OK; |
| 2156 | assert( pIdx->isResized==0 ); |
dan | b5a6923 | 2020-09-15 20:48:30 +0000 | [diff] [blame] | 2157 | nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2158 | zExtra = sqlite3DbMallocZero(db, nByte); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 2159 | if( zExtra==0 ) return SQLITE_NOMEM_BKPT; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2160 | memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 2161 | pIdx->azColl = (const char**)zExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2162 | zExtra += sizeof(char*)*N; |
dan | b5a6923 | 2020-09-15 20:48:30 +0000 | [diff] [blame] | 2163 | memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1)); |
| 2164 | pIdx->aiRowLogEst = (LogEst*)zExtra; |
| 2165 | zExtra += sizeof(LogEst)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2166 | memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
| 2167 | pIdx->aiColumn = (i16*)zExtra; |
| 2168 | zExtra += sizeof(i16)*N; |
| 2169 | memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
| 2170 | pIdx->aSortOrder = (u8*)zExtra; |
| 2171 | pIdx->nColumn = N; |
| 2172 | pIdx->isResized = 1; |
| 2173 | return SQLITE_OK; |
| 2174 | } |
| 2175 | |
| 2176 | /* |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2177 | ** Estimate the total row width for a table. |
| 2178 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2179 | static void estimateTableWidth(Table *pTab){ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2180 | unsigned wTable = 0; |
| 2181 | const Column *pTabCol; |
| 2182 | int i; |
| 2183 | for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ |
| 2184 | wTable += pTabCol->szEst; |
| 2185 | } |
| 2186 | if( pTab->iPKey<0 ) wTable++; |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2187 | pTab->szTabRow = sqlite3LogEst(wTable*4); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /* |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2191 | ** Estimate the average size of a row for an index. |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2192 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2193 | static void estimateIndexWidth(Index *pIdx){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2194 | unsigned wIndex = 0; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2195 | int i; |
| 2196 | const Column *aCol = pIdx->pTable->aCol; |
| 2197 | for(i=0; i<pIdx->nColumn; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 2198 | i16 x = pIdx->aiColumn[i]; |
| 2199 | assert( x<pIdx->pTable->nCol ); |
| 2200 | wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2201 | } |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2202 | pIdx->szIdxRow = sqlite3LogEst(wIndex*4); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2205 | /* Return true if column number x is any of the first nCol entries of aiCol[]. |
| 2206 | ** This is used to determine if the column number x appears in any of the |
| 2207 | ** first nCol entries of an index. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2208 | */ |
| 2209 | static int hasColumn(const i16 *aiCol, int nCol, int x){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2210 | while( nCol-- > 0 ){ |
| 2211 | assert( aiCol[0]>=0 ); |
| 2212 | if( x==*(aiCol++) ){ |
| 2213 | return 1; |
| 2214 | } |
| 2215 | } |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | /* |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2220 | ** Return true if any of the first nKey entries of index pIdx exactly |
| 2221 | ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID |
| 2222 | ** PRIMARY KEY index. pIdx is an index on the same table. pIdx may |
| 2223 | ** or may not be the same index as pPk. |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2224 | ** |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2225 | ** The first nKey entries of pIdx are guaranteed to be ordinary columns, |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2226 | ** not a rowid or expression. |
| 2227 | ** |
| 2228 | ** This routine differs from hasColumn() in that both the column and the |
| 2229 | ** collating sequence must match for this routine, but for hasColumn() only |
| 2230 | ** the column name must match. |
| 2231 | */ |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2232 | static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2233 | int i, j; |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2234 | assert( nKey<=pIdx->nColumn ); |
| 2235 | assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) ); |
| 2236 | assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); |
| 2237 | assert( pPk->pTable->tabFlags & TF_WithoutRowid ); |
| 2238 | assert( pPk->pTable==pIdx->pTable ); |
| 2239 | testcase( pPk==pIdx ); |
| 2240 | j = pPk->aiColumn[iCol]; |
| 2241 | assert( j!=XN_ROWID && j!=XN_EXPR ); |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2242 | for(i=0; i<nKey; i++){ |
drh | c19b63c | 2019-04-29 13:30:16 +0000 | [diff] [blame] | 2243 | assert( pIdx->aiColumn[i]>=0 || j>=0 ); |
| 2244 | if( pIdx->aiColumn[i]==j |
| 2245 | && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2246 | ){ |
| 2247 | return 1; |
| 2248 | } |
| 2249 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2250 | return 0; |
| 2251 | } |
| 2252 | |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2253 | /* Recompute the colNotIdxed field of the Index. |
| 2254 | ** |
| 2255 | ** colNotIdxed is a bitmask that has a 0 bit representing each indexed |
| 2256 | ** columns that are within the first 63 columns of the table. The |
| 2257 | ** high-order bit of colNotIdxed is always 1. All unindexed columns |
| 2258 | ** of the table have a 1. |
| 2259 | ** |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2260 | ** 2019-10-24: For the purpose of this computation, virtual columns are |
| 2261 | ** not considered to be covered by the index, even if they are in the |
| 2262 | ** index, because we do not trust the logic in whereIndexExprTrans() to be |
| 2263 | ** able to find all instances of a reference to the indexed table column |
| 2264 | ** and convert them into references to the index. Hence we always want |
| 2265 | ** the actual table at hand in order to recompute the virtual column, if |
| 2266 | ** necessary. |
| 2267 | ** |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2268 | ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask |
| 2269 | ** to determine if the index is covering index. |
| 2270 | */ |
| 2271 | static void recomputeColumnsNotIndexed(Index *pIdx){ |
| 2272 | Bitmask m = 0; |
| 2273 | int j; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2274 | Table *pTab = pIdx->pTable; |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2275 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 2276 | int x = pIdx->aiColumn[j]; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 2277 | if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2278 | testcase( x==BMS-1 ); |
| 2279 | testcase( x==BMS-2 ); |
| 2280 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 2281 | } |
| 2282 | } |
| 2283 | pIdx->colNotIdxed = ~m; |
| 2284 | assert( (pIdx->colNotIdxed>>63)==1 ); |
| 2285 | } |
| 2286 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2287 | /* |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2288 | ** This routine runs at the end of parsing a CREATE TABLE statement that |
| 2289 | ** has a WITHOUT ROWID clause. The job of this routine is to convert both |
| 2290 | ** internal schema data structures and the generated VDBE code so that they |
| 2291 | ** are appropriate for a WITHOUT ROWID table instead of a rowid table. |
| 2292 | ** Changes include: |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2293 | ** |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2294 | ** (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] | 2295 | ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY |
| 2296 | ** into BTREE_BLOBKEY. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2297 | ** (3) Bypass the creation of the sqlite_schema table entry |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2298 | ** for the PRIMARY KEY as the primary key index is now |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2299 | ** identified by the sqlite_schema table entry of the table itself. |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2300 | ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2301 | ** schema to the rootpage from the main table. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2302 | ** (5) Add all table columns to the PRIMARY KEY Index object |
| 2303 | ** so that the PRIMARY KEY is a covering index. The surplus |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 2304 | ** columns are part of KeyInfo.nAllField and are not used for |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2305 | ** sorting or lookup or uniqueness checks. |
| 2306 | ** (6) Replace the rowid tail on all automatically generated UNIQUE |
| 2307 | ** indices with the PRIMARY KEY columns. |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2308 | ** |
| 2309 | ** For virtual tables, only (1) is performed. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2310 | */ |
| 2311 | static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ |
| 2312 | Index *pIdx; |
| 2313 | Index *pPk; |
| 2314 | int nPk; |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2315 | int nExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2316 | int i, j; |
| 2317 | sqlite3 *db = pParse->db; |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2318 | Vdbe *v = pParse->pVdbe; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2319 | |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2320 | /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) |
| 2321 | */ |
| 2322 | if( !db->init.imposterTable ){ |
| 2323 | for(i=0; i<pTab->nCol; i++){ |
drh | fd46ec6 | 2021-08-18 22:26:51 +0000 | [diff] [blame] | 2324 | if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 |
| 2325 | && (pTab->aCol[i].notNull==OE_None) |
| 2326 | ){ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2327 | pTab->aCol[i].notNull = OE_Abort; |
| 2328 | } |
| 2329 | } |
drh | cbda9c7 | 2019-10-26 17:08:06 +0000 | [diff] [blame] | 2330 | pTab->tabFlags |= TF_HasNotNull; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
drh | 0f3f766 | 2017-08-18 14:34:28 +0000 | [diff] [blame] | 2333 | /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY |
| 2334 | ** into BTREE_BLOBKEY. |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2335 | */ |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 2336 | assert( !pParse->bReturning ); |
| 2337 | if( pParse->u1.addrCrTab ){ |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2338 | assert( v ); |
drh | 381bdac | 2021-02-04 17:29:04 +0000 | [diff] [blame] | 2339 | sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY); |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2342 | /* Locate the PRIMARY KEY index. Or, if this table was originally |
| 2343 | ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
| 2344 | */ |
| 2345 | if( pTab->iPKey>=0 ){ |
| 2346 | ExprList *pList; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 2347 | Token ipkToken; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2348 | sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 2349 | pList = sqlite3ExprListAppend(pParse, 0, |
| 2350 | sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 2351 | if( pList==0 ){ |
| 2352 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2353 | return; |
| 2354 | } |
dan | f9b0c45 | 2019-05-06 16:15:28 +0000 | [diff] [blame] | 2355 | if( IN_RENAME_OBJECT ){ |
| 2356 | sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); |
| 2357 | } |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 2358 | pList->a[0].sortFlags = pParse->iPkSortOrder; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2359 | assert( pParse->pNewTable==pTab ); |
dan | f9b0c45 | 2019-05-06 16:15:28 +0000 | [diff] [blame] | 2360 | pTab->iPKey = -1; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2361 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 2362 | SQLITE_IDXTYPE_PRIMARYKEY); |
drh | 3bb9d75 | 2021-04-13 13:48:31 +0000 | [diff] [blame] | 2363 | if( db->mallocFailed || pParse->nErr ){ |
| 2364 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2365 | return; |
| 2366 | } |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2367 | pPk = sqlite3PrimaryKeyIndex(pTab); |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2368 | assert( pPk->nKeyCol==1 ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 2369 | }else{ |
| 2370 | pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | f0c48b1 | 2019-02-11 01:58:34 +0000 | [diff] [blame] | 2371 | assert( pPk!=0 ); |
dan | c5b7358 | 2015-05-26 11:53:14 +0000 | [diff] [blame] | 2372 | |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2373 | /* |
| 2374 | ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 2375 | ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 2376 | ** code assumes the PRIMARY KEY contains no repeated columns. |
| 2377 | */ |
| 2378 | for(i=j=1; i<pPk->nKeyCol; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2379 | if( isDupColumn(pPk, j, pPk, i) ){ |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2380 | pPk->nColumn--; |
| 2381 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2382 | testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2383 | pPk->azColl[j] = pPk->azColl[i]; |
| 2384 | pPk->aSortOrder[j] = pPk->aSortOrder[i]; |
drh | e385d88 | 2014-12-28 22:10:51 +0000 | [diff] [blame] | 2385 | pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 2386 | } |
| 2387 | } |
| 2388 | pPk->nKeyCol = j; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2389 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2390 | assert( pPk!=0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 2391 | pPk->isCovering = 1; |
| 2392 | if( !db->init.imposterTable ) pPk->uniqNotNull = 1; |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2393 | nPk = pPk->nColumn = pPk->nKeyCol; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2394 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2395 | /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema |
drh | df94966 | 2017-07-30 18:40:52 +0000 | [diff] [blame] | 2396 | ** table entry. This is only required if currently generating VDBE |
| 2397 | ** code for a CREATE TABLE (not when parsing one as part of reading |
| 2398 | ** a database schema). */ |
| 2399 | if( v && pPk->tnum>0 ){ |
| 2400 | assert( db->init.busy==0 ); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 2401 | sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto); |
drh | df94966 | 2017-07-30 18:40:52 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2404 | /* The root page of the PRIMARY KEY is the table root page */ |
| 2405 | pPk->tnum = pTab->tnum; |
| 2406 | |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2407 | /* Update the in-memory representation of all UNIQUE indices by converting |
| 2408 | ** the final rowid column into one or more columns of the PRIMARY KEY. |
| 2409 | */ |
| 2410 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2411 | int n; |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 2412 | if( IsPrimaryKeyIndex(pIdx) ) continue; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2413 | for(i=n=0; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2414 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2415 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
| 2416 | n++; |
| 2417 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2418 | } |
drh | 5a9a37b | 2013-11-05 17:30:04 +0000 | [diff] [blame] | 2419 | if( n==0 ){ |
| 2420 | /* This index is a superset of the primary key */ |
| 2421 | pIdx->nColumn = pIdx->nKeyCol; |
| 2422 | continue; |
| 2423 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2424 | if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; |
| 2425 | for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 2426 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2427 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2428 | pIdx->aiColumn[j] = pPk->aiColumn[i]; |
| 2429 | pIdx->azColl[j] = pPk->azColl[i]; |
drh | bf9ff25 | 2019-05-14 00:43:13 +0000 | [diff] [blame] | 2430 | if( pPk->aSortOrder[i] ){ |
| 2431 | /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */ |
| 2432 | pIdx->bAscKeyBug = 1; |
| 2433 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2434 | j++; |
| 2435 | } |
| 2436 | } |
drh | 00012df | 2013-11-05 01:59:07 +0000 | [diff] [blame] | 2437 | assert( pIdx->nColumn>=pIdx->nKeyCol+n ); |
| 2438 | assert( pIdx->nColumn>=j ); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /* Add all table columns to the PRIMARY KEY index |
| 2442 | */ |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2443 | nExtra = 0; |
| 2444 | for(i=0; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2445 | if( !hasColumn(pPk->aiColumn, nPk, i) |
| 2446 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++; |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2447 | } |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2448 | if( resizeIndexObject(db, pPk, nPk+nExtra) ) return; |
| 2449 | for(i=0, j=nPk; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2450 | if( !hasColumn(pPk->aiColumn, j, i) |
| 2451 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 |
| 2452 | ){ |
dan | 1ff9407 | 2019-07-17 09:18:06 +0000 | [diff] [blame] | 2453 | assert( j<pPk->nColumn ); |
| 2454 | pPk->aiColumn[j] = i; |
| 2455 | pPk->azColl[j] = sqlite3StrBINARY; |
| 2456 | j++; |
| 2457 | } |
| 2458 | } |
| 2459 | assert( pPk->nColumn==j ); |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2460 | assert( pTab->nNVCol<=j ); |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 2461 | recomputeColumnsNotIndexed(pPk); |
drh | 7f9c5db | 2013-10-23 00:32:58 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2464 | |
| 2465 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2466 | /* |
| 2467 | ** Return true if pTab is a virtual table and zName is a shadow table name |
| 2468 | ** for that virtual table. |
| 2469 | */ |
| 2470 | int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ |
| 2471 | int nName; /* Length of zName */ |
| 2472 | Module *pMod; /* Module for the virtual table */ |
| 2473 | |
| 2474 | if( !IsVirtual(pTab) ) return 0; |
| 2475 | nName = sqlite3Strlen30(pTab->zName); |
| 2476 | if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; |
| 2477 | if( zName[nName]!='_' ) return 0; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2478 | pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2479 | if( pMod==0 ) return 0; |
| 2480 | if( pMod->pModule->iVersion<3 ) return 0; |
| 2481 | if( pMod->pModule->xShadowName==0 ) return 0; |
| 2482 | return pMod->pModule->xShadowName(zName+nName+1); |
| 2483 | } |
| 2484 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 2485 | |
dan | f6e015f | 2018-11-28 08:02:28 +0000 | [diff] [blame] | 2486 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2487 | /* |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2488 | ** Return true if zName is a shadow table name in the current database |
| 2489 | ** connection. |
| 2490 | ** |
| 2491 | ** zName is temporarily modified while this routine is running, but is |
| 2492 | ** restored to its original value prior to this routine returning. |
| 2493 | */ |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 2494 | int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2495 | char *zTail; /* Pointer to the last "_" in zName */ |
| 2496 | Table *pTab; /* Table that zName is a shadow of */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2497 | zTail = strrchr(zName, '_'); |
| 2498 | if( zTail==0 ) return 0; |
| 2499 | *zTail = 0; |
| 2500 | pTab = sqlite3FindTable(db, zName, 0); |
| 2501 | *zTail = '_'; |
| 2502 | if( pTab==0 ) return 0; |
| 2503 | if( !IsVirtual(pTab) ) return 0; |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2504 | return sqlite3IsShadowTableOf(db, pTab, zName); |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2505 | } |
dan | f6e015f | 2018-11-28 08:02:28 +0000 | [diff] [blame] | 2506 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2507 | |
drh | 3d863b5 | 2020-05-14 21:16:52 +0000 | [diff] [blame] | 2508 | |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2509 | #ifdef SQLITE_DEBUG |
| 2510 | /* |
| 2511 | ** Mark all nodes of an expression as EP_Immutable, indicating that |
| 2512 | ** they should not be changed. Expressions attached to a table or |
| 2513 | ** index definition are tagged this way to help ensure that we do |
| 2514 | ** not pass them into code generator routines by mistake. |
| 2515 | */ |
| 2516 | static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){ |
| 2517 | ExprSetVVAProperty(pExpr, EP_Immutable); |
| 2518 | return WRC_Continue; |
| 2519 | } |
| 2520 | static void markExprListImmutable(ExprList *pList){ |
| 2521 | if( pList ){ |
| 2522 | Walker w; |
| 2523 | memset(&w, 0, sizeof(w)); |
| 2524 | w.xExprCallback = markImmutableExprStep; |
| 2525 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 2526 | w.xSelectCallback2 = 0; |
| 2527 | sqlite3WalkExprList(&w, pList); |
| 2528 | } |
| 2529 | } |
| 2530 | #else |
| 2531 | #define markExprListImmutable(X) /* no-op */ |
| 2532 | #endif /* SQLITE_DEBUG */ |
| 2533 | |
| 2534 | |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2535 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2536 | ** This routine is called to report the final ")" that terminates |
| 2537 | ** a CREATE TABLE statement. |
| 2538 | ** |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2539 | ** The table structure that other action routines have been building |
| 2540 | ** is added to the internal hash tables, assuming no errors have |
| 2541 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2542 | ** |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 2543 | ** 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] | 2544 | ** 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] | 2545 | ** it means we are reading the sqlite_schema table because we just |
| 2546 | ** connected to the database or because the sqlite_schema table has |
drh | ddba9e5 | 2005-03-19 01:41:21 +0000 | [diff] [blame] | 2547 | ** recently changed, so the entry for this table already exists in |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2548 | ** the sqlite_schema table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 2549 | ** |
| 2550 | ** If the pSelect argument is not NULL, it means that this routine |
| 2551 | ** was called to create a table generated from a |
| 2552 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 2553 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2554 | */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2555 | void sqlite3EndTable( |
| 2556 | Parse *pParse, /* Parse context */ |
| 2557 | Token *pCons, /* The ',' token after the last column defn. */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2558 | Token *pEnd, /* The ')' before options in the CREATE TABLE */ |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2559 | u32 tabOpts, /* Extra table options. Usually 0. */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2560 | Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
| 2561 | ){ |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2562 | Table *p; /* The new table */ |
| 2563 | sqlite3 *db = pParse->db; /* The database connection */ |
| 2564 | int iDb; /* Database in which the table lives */ |
| 2565 | Index *pIdx; /* An implied index of the table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2566 | |
drh | 027616d | 2015-08-08 22:47:47 +0000 | [diff] [blame] | 2567 | if( pEnd==0 && pSelect==0 ){ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2568 | return; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2569 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 2570 | p = pParse->pNewTable; |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2571 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2572 | |
drh | 527cbd4 | 2019-11-16 14:15:19 +0000 | [diff] [blame] | 2573 | if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2574 | p->tabFlags |= TF_Shadow; |
| 2575 | } |
| 2576 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2577 | /* 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] | 2578 | ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2579 | ** So do not write to the disk again. Extract the root page number |
| 2580 | ** for the table from the db->init.newTnum field. (The page number |
| 2581 | ** should have been put there by the sqliteOpenCb routine.) |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2582 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 2583 | ** 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] | 2584 | ** table itself. So mark it read-only. |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2585 | */ |
| 2586 | if( db->init.busy ){ |
drh | 1e9c47b | 2018-03-16 20:15:58 +0000 | [diff] [blame] | 2587 | if( pSelect ){ |
| 2588 | sqlite3ErrorMsg(pParse, ""); |
| 2589 | return; |
| 2590 | } |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2591 | p->tnum = db->init.newTnum; |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 2592 | if( p->tnum==1 ) p->tabFlags |= TF_Readonly; |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
drh | 71c770f | 2021-08-19 16:29:33 +0000 | [diff] [blame] | 2595 | /* Special processing for tables that include the STRICT keyword: |
| 2596 | ** |
| 2597 | ** * Do not allow custom column datatypes. Every column must have |
| 2598 | ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB. |
| 2599 | ** |
| 2600 | ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY, |
| 2601 | ** then all columns of the PRIMARY KEY must have a NOT NULL |
| 2602 | ** constraint. |
| 2603 | */ |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2604 | if( tabOpts & TF_Strict ){ |
| 2605 | int ii; |
| 2606 | p->tabFlags |= TF_Strict; |
| 2607 | for(ii=0; ii<p->nCol; ii++){ |
drh | ab16578 | 2021-08-19 00:24:43 +0000 | [diff] [blame] | 2608 | Column *pCol = &p->aCol[ii]; |
drh | b9fd010 | 2021-08-23 10:28:02 +0000 | [diff] [blame^] | 2609 | if( pCol->eCType==COLTYPE_CUSTOM ){ |
| 2610 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 2611 | sqlite3ErrorMsg(pParse, |
| 2612 | "unknown datatype for %s.%s: \"%s\"", |
| 2613 | p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "") |
| 2614 | ); |
| 2615 | }else{ |
| 2616 | sqlite3ErrorMsg(pParse, "missing datatype for %s.%s", |
| 2617 | p->zName, pCol->zCnName); |
| 2618 | } |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2619 | return; |
drh | b9fd010 | 2021-08-23 10:28:02 +0000 | [diff] [blame^] | 2620 | }else if( pCol->eCType==COLTYPE_ANY ){ |
| 2621 | pCol->affinity = SQLITE_AFF_BLOB; |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2622 | } |
drh | ab16578 | 2021-08-19 00:24:43 +0000 | [diff] [blame] | 2623 | if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0 |
| 2624 | && p->iPKey!=ii |
| 2625 | && pCol->notNull == OE_None |
| 2626 | ){ |
drh | ab16578 | 2021-08-19 00:24:43 +0000 | [diff] [blame] | 2627 | pCol->notNull = OE_Abort; |
| 2628 | p->tabFlags |= TF_HasNotNull; |
| 2629 | } |
drh | 44183f8 | 2021-08-18 13:13:58 +0000 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | |
drh | 3cbd2b7 | 2019-02-19 13:51:58 +0000 | [diff] [blame] | 2633 | assert( (p->tabFlags & TF_HasPrimaryKey)==0 |
| 2634 | || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 ); |
| 2635 | assert( (p->tabFlags & TF_HasPrimaryKey)!=0 |
| 2636 | || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) ); |
| 2637 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 +0000 | [diff] [blame] | 2638 | /* Special processing for WITHOUT ROWID Tables */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2639 | if( tabOpts & TF_WithoutRowid ){ |
drh | d2fe335 | 2013-11-09 18:15:35 +0000 | [diff] [blame] | 2640 | if( (p->tabFlags & TF_Autoincrement) ){ |
| 2641 | sqlite3ErrorMsg(pParse, |
| 2642 | "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
| 2643 | return; |
| 2644 | } |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2645 | if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
drh | d2fe335 | 2013-11-09 18:15:35 +0000 | [diff] [blame] | 2646 | sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2647 | return; |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 2648 | } |
drh | 8e10d74 | 2019-10-18 17:42:47 +0000 | [diff] [blame] | 2649 | p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
drh | f95909c | 2019-10-18 18:33:25 +0000 | [diff] [blame] | 2650 | convertToWithoutRowidTable(pParse, p); |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 2651 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2652 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2653 | |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2654 | #ifndef SQLITE_OMIT_CHECK |
| 2655 | /* Resolve names in all CHECK constraint expressions. |
| 2656 | */ |
| 2657 | if( p->pCheck ){ |
drh | 3780be1 | 2013-07-31 19:05:22 +0000 | [diff] [blame] | 2658 | sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 +0000 | [diff] [blame] | 2659 | if( pParse->nErr ){ |
| 2660 | /* If errors are seen, delete the CHECK constraints now, else they might |
| 2661 | ** actually be used if PRAGMA writable_schema=ON is set. */ |
| 2662 | sqlite3ExprListDelete(db, p->pCheck); |
| 2663 | p->pCheck = 0; |
drh | e7375bf | 2020-03-10 19:24:38 +0000 | [diff] [blame] | 2664 | }else{ |
| 2665 | markExprListImmutable(p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 +0000 | [diff] [blame] | 2666 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2667 | } |
| 2668 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2669 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2670 | if( p->tabFlags & TF_HasGenerated ){ |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2671 | int ii, nNG = 0; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2672 | testcase( p->tabFlags & TF_HasVirtual ); |
| 2673 | testcase( p->tabFlags & TF_HasStored ); |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2674 | for(ii=0; ii<p->nCol; ii++){ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 +0000 | [diff] [blame] | 2675 | u32 colFlags = p->aCol[ii].colFlags; |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2676 | if( (colFlags & COLFLAG_GENERATED)!=0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2677 | Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]); |
drh | 427b96a | 2019-10-22 13:01:24 +0000 | [diff] [blame] | 2678 | testcase( colFlags & COLFLAG_VIRTUAL ); |
| 2679 | testcase( colFlags & COLFLAG_STORED ); |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2680 | if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){ |
| 2681 | /* If there are errors in resolving the expression, change the |
| 2682 | ** expression to a NULL. This prevents code generators that operate |
| 2683 | ** on the expression from inserting extra parts into the expression |
| 2684 | ** tree that have been allocated from lookaside memory, which is |
drh | 2d58b7f | 2020-01-17 23:27:41 +0000 | [diff] [blame] | 2685 | ** illegal in a schema and will lead to errors or heap corruption |
| 2686 | ** when the database connection closes. */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 2687 | sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii], |
| 2688 | sqlite3ExprAlloc(db, TK_NULL, 0, 0)); |
drh | 7e3f135 | 2019-12-14 19:55:31 +0000 | [diff] [blame] | 2689 | } |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2690 | }else{ |
| 2691 | nNG++; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2692 | } |
drh | 2c40a3e | 2019-10-29 01:26:24 +0000 | [diff] [blame] | 2693 | } |
drh | f4658b6 | 2019-10-29 03:39:17 +0000 | [diff] [blame] | 2694 | if( nNG==0 ){ |
| 2695 | sqlite3ErrorMsg(pParse, "must have at least one non-generated column"); |
drh | 2c40a3e | 2019-10-29 01:26:24 +0000 | [diff] [blame] | 2696 | return; |
drh | 81f7b37 | 2019-10-16 12:18:59 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } |
| 2699 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2700 | |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2701 | /* Estimate the average row size for the table and for all implied indices */ |
| 2702 | estimateTableWidth(p); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2703 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 2704 | estimateIndexWidth(pIdx); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 2707 | /* If not initializing, then create a record for the new table |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2708 | ** in the schema table of the database. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 2709 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2710 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 2711 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2712 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 2713 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2714 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2715 | Vdbe *v; |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2716 | char *zType; /* "view" or "table" */ |
| 2717 | char *zType2; /* "VIEW" or "TABLE" */ |
| 2718 | char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2719 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2720 | v = sqlite3GetVdbe(pParse); |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2721 | if( NEVER(v==0) ) return; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2722 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2723 | sqlite3VdbeAddOp1(v, OP_Close, 0); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 2724 | |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2725 | /* |
| 2726 | ** Initialize zType for the new view or table. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2727 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2728 | if( IsOrdinaryTable(p) ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2729 | /* A regular table */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2730 | zType = "table"; |
| 2731 | zType2 = "TABLE"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2732 | #ifndef SQLITE_OMIT_VIEW |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2733 | }else{ |
| 2734 | /* A view */ |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2735 | zType = "view"; |
| 2736 | zType2 = "VIEW"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2737 | #endif |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2738 | } |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2739 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2740 | /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
| 2741 | ** statement to populate the new table. The root-page number for the |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2742 | ** new table is in register pParse->regRoot. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2743 | ** |
| 2744 | ** Once the SELECT has been coded by sqlite3Select(), it is in a |
| 2745 | ** suitable state to query for the column names and types to be used |
| 2746 | ** by the new table. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 2747 | ** |
| 2748 | ** A shared-cache write-lock is not required to write to the new table, |
| 2749 | ** as a schema-lock must have already been obtained to create it. Since |
| 2750 | ** a schema-lock excludes all other database users, the write-lock would |
| 2751 | ** be redundant. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2752 | */ |
| 2753 | if( pSelect ){ |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2754 | SelectDest dest; /* Where the SELECT should store results */ |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2755 | int regYield; /* Register holding co-routine entry-point */ |
| 2756 | int addrTop; /* Top of the co-routine */ |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2757 | int regRec; /* A record to be insert into the new table */ |
| 2758 | int regRowid; /* Rowid of the next row to insert */ |
| 2759 | int addrInsLoop; /* Top of the loop for inserting rows */ |
| 2760 | Table *pSelTab; /* A table that describes the SELECT results */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2761 | |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2762 | regYield = ++pParse->nMem; |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2763 | regRec = ++pParse->nMem; |
| 2764 | regRowid = ++pParse->nMem; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 2765 | assert(pParse->nTab==1); |
drh | 0dd5cda | 2015-06-16 16:39:01 +0000 | [diff] [blame] | 2766 | sqlite3MayAbort(pParse); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2767 | sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 2768 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2769 | pParse->nTab = 2; |
drh | 9df25c4 | 2015-05-20 15:51:09 +0000 | [diff] [blame] | 2770 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 2771 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
drh | 512795d | 2017-12-24 18:56:28 +0000 | [diff] [blame] | 2772 | if( pParse->nErr ) return; |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 2773 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2774 | if( pSelTab==0 ) return; |
| 2775 | assert( p->aCol==0 ); |
drh | f5f1915 | 2019-10-21 01:04:11 +0000 | [diff] [blame] | 2776 | p->nCol = p->nNVCol = pSelTab->nCol; |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2777 | p->aCol = pSelTab->aCol; |
| 2778 | pSelTab->nCol = 0; |
| 2779 | pSelTab->aCol = 0; |
| 2780 | sqlite3DeleteTable(db, pSelTab); |
drh | 755b0fd | 2017-12-23 12:33:40 +0000 | [diff] [blame] | 2781 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 2782 | sqlite3Select(pParse, pSelect, &dest); |
drh | 5060a67 | 2017-12-25 13:43:54 +0000 | [diff] [blame] | 2783 | if( pParse->nErr ) return; |
drh | 755b0fd | 2017-12-23 12:33:40 +0000 | [diff] [blame] | 2784 | sqlite3VdbeEndCoroutine(v, regYield); |
| 2785 | sqlite3VdbeJumpHere(v, addrTop - 1); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2786 | addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
| 2787 | VdbeCoverage(v); |
| 2788 | sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
| 2789 | sqlite3TableAffinity(v, p, 0); |
| 2790 | sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid); |
| 2791 | sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2792 | sqlite3VdbeGoto(v, addrInsLoop); |
drh | 9263220 | 2015-05-20 17:18:29 +0000 | [diff] [blame] | 2793 | sqlite3VdbeJumpHere(v, addrInsLoop); |
| 2794 | sqlite3VdbeAddOp1(v, OP_Close, 1); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2795 | } |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2796 | |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2797 | /* Compute the complete text of the CREATE statement */ |
| 2798 | if( pSelect ){ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 2799 | zStmt = createTableStmt(db, p); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2800 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 +0000 | [diff] [blame] | 2801 | Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; |
| 2802 | n = (int)(pEnd2->z - pParse->sNameToken.z); |
| 2803 | if( pEnd2->z[0]!=';' ) n += pEnd2->n; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2804 | zStmt = sqlite3MPrintf(db, |
| 2805 | "CREATE %s %.*s", zType2, n, pParse->sNameToken.z |
| 2806 | ); |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | /* A slot for the record has already been allocated in the |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2810 | ** schema table. We just need to update that slot with all |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 2811 | ** the information we've collected. |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2812 | */ |
| 2813 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2814 | "UPDATE %Q." DFLT_SCHEMA_TABLE |
| 2815 | " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q" |
| 2816 | " WHERE rowid=#%d", |
| 2817 | db->aDb[iDb].zDbSName, |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2818 | zType, |
| 2819 | p->zName, |
| 2820 | p->zName, |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2821 | pParse->regRoot, |
| 2822 | zStmt, |
| 2823 | pParse->regRowid |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2824 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2825 | sqlite3DbFree(db, zStmt); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2826 | sqlite3ChangeCookie(pParse, iDb); |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2827 | |
| 2828 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2829 | /* Check to see if we need to create an sqlite_sequence table for |
| 2830 | ** keeping track of autoincrement keys. |
| 2831 | */ |
dan | 755ed41 | 2021-04-07 12:02:30 +0000 | [diff] [blame] | 2832 | if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2833 | Db *pDb = &db->aDb[iDb]; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2834 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2835 | if( pDb->pSchema->pSeqTab==0 ){ |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2836 | sqlite3NestedParse(pParse, |
drh | f338814 | 2004-11-13 03:48:06 +0000 | [diff] [blame] | 2837 | "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 2838 | pDb->zDbSName |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 2839 | ); |
| 2840 | } |
| 2841 | } |
| 2842 | #endif |
drh | 4794f73 | 2004-11-05 17:17:50 +0000 | [diff] [blame] | 2843 | |
| 2844 | /* Reparse everything to update our internal data structures */ |
drh | 5d9c9da | 2011-06-03 20:11:17 +0000 | [diff] [blame] | 2845 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 2846 | sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2847 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2848 | |
| 2849 | /* Add the table to the in-memory representation of the database. |
| 2850 | */ |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 2851 | if( db->init.busy ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2852 | Table *pOld; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2853 | Schema *pSchema = p->pSchema; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 2854 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1bb89e9 | 2021-04-19 18:03:52 +0000 | [diff] [blame] | 2855 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 2856 | pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2857 | if( pOld ){ |
| 2858 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2859 | sqlite3OomFault(db); |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2860 | return; |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2861 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2862 | pParse->pNewTable = 0; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 2863 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | d4b6469 | 2021-04-06 16:16:15 +0000 | [diff] [blame] | 2864 | |
| 2865 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 2866 | ** then record a pointer to this table in the main database structure |
| 2867 | ** so that INSERT can find the table easily. */ |
| 2868 | assert( !pParse->nested ); |
| 2869 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2870 | if( strcmp(p->zName, "sqlite_sequence")==0 ){ |
| 2871 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2872 | p->pSchema->pSeqTab = p; |
| 2873 | } |
| 2874 | #endif |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2875 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2876 | |
| 2877 | #ifndef SQLITE_OMIT_ALTERTABLE |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2878 | if( !pSelect && IsOrdinaryTable(p) ){ |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2879 | assert( pCons && pEnd ); |
| 2880 | if( pCons->z==0 ){ |
| 2881 | pCons = pEnd; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 2882 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2883 | p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z); |
drh | 17e9e29 | 2003-02-01 13:53:28 +0000 | [diff] [blame] | 2884 | } |
dan | 578277c | 2021-02-19 18:39:32 +0000 | [diff] [blame] | 2885 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2888 | #ifndef SQLITE_OMIT_VIEW |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2889 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2890 | ** The parser calls this routine in order to create a new VIEW |
| 2891 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2892 | void sqlite3CreateView( |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2893 | Parse *pParse, /* The parsing context */ |
| 2894 | Token *pBegin, /* The CREATE token that begins the statement */ |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 2895 | Token *pName1, /* The token that holds the name of the view */ |
| 2896 | Token *pName2, /* The token that holds the name of the view */ |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2897 | ExprList *pCNames, /* Optional list of view column names */ |
drh | 6276c1c | 2002-07-08 22:03:32 +0000 | [diff] [blame] | 2898 | Select *pSelect, /* A SELECT statement that will become the new view */ |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 2899 | int isTemp, /* TRUE for a TEMPORARY view */ |
| 2900 | int noErr /* Suppress error messages if VIEW already exists */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2901 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2902 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2903 | int n; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 2904 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2905 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 2906 | DbFixer sFix; |
drh | 88caeac | 2011-08-24 15:12:08 +0000 | [diff] [blame] | 2907 | Token *pName = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2908 | int iDb; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2909 | sqlite3 *db = pParse->db; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2910 | |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 2911 | if( pParse->nVar>0 ){ |
| 2912 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
drh | 32498f1 | 2015-09-26 11:15:44 +0000 | [diff] [blame] | 2913 | goto create_view_fail; |
drh | 7c3d64f | 2005-06-06 15:32:08 +0000 | [diff] [blame] | 2914 | } |
drh | fdd48a7 | 2006-09-11 23:45:48 +0000 | [diff] [blame] | 2915 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2916 | p = pParse->pNewTable; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2917 | if( p==0 || pParse->nErr ) goto create_view_fail; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 2918 | |
| 2919 | /* Legacy versions of SQLite allowed the use of the magic "rowid" column |
| 2920 | ** on a view, even though views do not have rowids. The following flag |
| 2921 | ** setting fixes this problem. But the fix can be disabled by compiling |
| 2922 | ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that |
| 2923 | ** depend upon the old buggy behavior. */ |
| 2924 | #ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
drh | a6c54de | 2021-04-06 19:13:44 +0000 | [diff] [blame] | 2925 | p->tabFlags |= TF_NoVisibleRowid; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 2926 | #endif |
| 2927 | |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 2928 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2929 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
drh | d100f69 | 2013-10-03 15:39:44 +0000 | [diff] [blame] | 2930 | sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2931 | if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 2932 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2933 | /* Make a copy of the entire SELECT statement that defines the view. |
| 2934 | ** This will force all the Expr.token.z values to be dynamically |
| 2935 | ** allocated rather than point to the input string - which means that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 2936 | ** they will persist after the current sqlite3_exec() call returns. |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2937 | */ |
dan | 3809696 | 2019-12-09 08:13:43 +0000 | [diff] [blame] | 2938 | pSelect->selFlags |= SF_View; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 2939 | if( IN_RENAME_OBJECT ){ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2940 | p->u.view.pSelect = pSelect; |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 2941 | pSelect = 0; |
| 2942 | }else{ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2943 | p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
dan | 987db76 | 2018-08-14 20:18:50 +0000 | [diff] [blame] | 2944 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2945 | p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 2946 | p->eTabType = TABTYP_VIEW; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2947 | if( db->mallocFailed ) goto create_view_fail; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2948 | |
| 2949 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 2950 | ** the end. |
| 2951 | */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2952 | sEnd = pParse->sLastToken; |
drh | 6116ee4 | 2018-01-10 00:40:06 +0000 | [diff] [blame] | 2953 | assert( sEnd.z[0]!=0 || sEnd.n==0 ); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2954 | if( sEnd.z[0]!=';' ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2955 | sEnd.z += sEnd.n; |
| 2956 | } |
| 2957 | sEnd.n = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 2958 | n = (int)(sEnd.z - pBegin->z); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2959 | assert( n>0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 2960 | z = pBegin->z; |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2961 | while( sqlite3Isspace(z[n-1]) ){ n--; } |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 2962 | sEnd.z = &z[n-1]; |
| 2963 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2964 | |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 2965 | /* Use sqlite3EndTable() to add the view to the schema table */ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 2966 | sqlite3EndTable(pParse, 0, &sEnd, 0, 0); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2967 | |
| 2968 | create_view_fail: |
| 2969 | sqlite3SelectDelete(db, pSelect); |
dan | e8ab40d | 2018-09-12 08:51:48 +0000 | [diff] [blame] | 2970 | if( IN_RENAME_OBJECT ){ |
| 2971 | sqlite3RenameExprlistUnmap(pParse, pCNames); |
| 2972 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2973 | sqlite3ExprListDelete(db, pCNames); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2974 | return; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2975 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2976 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2977 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2978 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2979 | /* |
| 2980 | ** The Table structure pTable is really a VIEW. Fill in the names of |
| 2981 | ** the columns of the view in the pTable structure. Return the number |
jplyon | cfa5684 | 2004-01-19 04:55:56 +0000 | [diff] [blame] | 2982 | ** 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] | 2983 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2984 | int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
drh | 9b3187e | 2005-01-18 14:45:47 +0000 | [diff] [blame] | 2985 | Table *pSelTab; /* A fake table from which we get the result set */ |
| 2986 | Select *pSel; /* Copy of the SELECT that implements the view */ |
| 2987 | int nErr = 0; /* Number of errors encountered */ |
| 2988 | int n; /* Temporarily holds the number of cursors assigned */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2989 | sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
drh | 424981d | 2018-03-28 15:56:55 +0000 | [diff] [blame] | 2990 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 2991 | int rc; |
| 2992 | #endif |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2993 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 32c6a48 | 2014-09-11 13:44:52 +0000 | [diff] [blame] | 2994 | sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
drh | a0daa75 | 2016-09-16 11:53:10 +0000 | [diff] [blame] | 2995 | #endif |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 2996 | |
| 2997 | assert( pTable ); |
| 2998 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 2999 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | dc6b41e | 2017-08-17 02:26:35 +0000 | [diff] [blame] | 3000 | db->nSchemaLock++; |
| 3001 | rc = sqlite3VtabCallConnect(pParse, pTable); |
| 3002 | db->nSchemaLock--; |
| 3003 | if( rc ){ |
drh | efaffb6 | 2017-08-17 18:23:46 +0000 | [diff] [blame] | 3004 | return 1; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3005 | } |
drh | 4cbdda9 | 2006-06-14 19:00:20 +0000 | [diff] [blame] | 3006 | if( IsVirtual(pTable) ) return 0; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3007 | #endif |
| 3008 | |
| 3009 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3010 | /* A positive nCol means the columns names for this view are |
| 3011 | ** already known. |
| 3012 | */ |
| 3013 | if( pTable->nCol>0 ) return 0; |
| 3014 | |
| 3015 | /* A negative nCol is a special marker meaning that we are currently |
| 3016 | ** trying to compute the column names. If we enter this routine with |
| 3017 | ** a negative nCol, it means two or more views form a loop, like this: |
| 3018 | ** |
| 3019 | ** CREATE VIEW one AS SELECT * FROM two; |
| 3020 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 3021 | ** |
drh | 768578e | 2009-05-12 00:40:12 +0000 | [diff] [blame] | 3022 | ** Actually, the error above is now caught prior to reaching this point. |
| 3023 | ** But the following test is still important as it does come up |
| 3024 | ** in the following: |
| 3025 | ** |
| 3026 | ** CREATE TABLE main.ex1(a); |
| 3027 | ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; |
| 3028 | ** SELECT * FROM temp.ex1; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3029 | */ |
| 3030 | if( pTable->nCol<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3031 | sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3032 | return 1; |
| 3033 | } |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 3034 | assert( pTable->nCol>=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3035 | |
| 3036 | /* 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] | 3037 | ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
| 3038 | ** "*" elements in the results set of the view and will assign cursors |
| 3039 | ** to the elements of the FROM clause. But we do not want these changes |
| 3040 | ** to be permanent. So the computation is done on a copy of the SELECT |
| 3041 | ** statement that defines the view. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3042 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3043 | assert( IsView(pTable) ); |
| 3044 | pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3045 | if( pSel ){ |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 3046 | u8 eParseMode = pParse->eParseMode; |
| 3047 | pParse->eParseMode = PARSE_MODE_NORMAL; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3048 | n = pParse->nTab; |
| 3049 | sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 3050 | pTable->nCol = -1; |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 3051 | DisableLookaside; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 3052 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3053 | xAuth = db->xAuth; |
| 3054 | db->xAuth = 0; |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3055 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3056 | db->xAuth = xAuth; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 3057 | #else |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3058 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
danielk1977 | db2d286 | 2007-10-15 07:08:44 +0000 | [diff] [blame] | 3059 | #endif |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3060 | pParse->nTab = n; |
dan | 5d59102 | 2019-12-28 08:26:47 +0000 | [diff] [blame] | 3061 | if( pSelTab==0 ){ |
| 3062 | pTable->nCol = 0; |
| 3063 | nErr++; |
| 3064 | }else if( pTable->pCheck ){ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3065 | /* CREATE VIEW name(arglist) AS ... |
| 3066 | ** The names of the columns in the table are taken from |
| 3067 | ** arglist which is stored in pTable->pCheck. The pCheck field |
| 3068 | ** normally holds CHECK constraints on an ordinary table, but for |
| 3069 | ** a VIEW it holds the list of column names. |
| 3070 | */ |
| 3071 | sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
| 3072 | &pTable->nCol, &pTable->aCol); |
| 3073 | if( db->mallocFailed==0 |
drh | 4c983b2 | 2020-01-03 14:34:04 +0000 | [diff] [blame] | 3074 | && pParse->nErr==0 |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3075 | && pTable->nCol==pSel->pEList->nExpr |
| 3076 | ){ |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3077 | sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel, |
| 3078 | SQLITE_AFF_NONE); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3079 | } |
dan | 5d59102 | 2019-12-28 08:26:47 +0000 | [diff] [blame] | 3080 | }else{ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3081 | /* CREATE VIEW name AS... without an argument list. Construct |
| 3082 | ** the column names from the SELECT statement that defines the view. |
| 3083 | */ |
| 3084 | assert( pTable->aCol==0 ); |
drh | 0383661 | 2019-11-02 17:59:10 +0000 | [diff] [blame] | 3085 | pTable->nCol = pSelTab->nCol; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3086 | pTable->aCol = pSelTab->aCol; |
dan | 3dc864b | 2021-02-25 16:55:47 +0000 | [diff] [blame] | 3087 | pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3088 | pSelTab->nCol = 0; |
| 3089 | pSelTab->aCol = 0; |
| 3090 | assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 3091 | } |
drh | 0383661 | 2019-11-02 17:59:10 +0000 | [diff] [blame] | 3092 | pTable->nNVCol = pTable->nCol; |
drh | e8da01c | 2016-05-07 12:15:34 +0000 | [diff] [blame] | 3093 | sqlite3DeleteTable(db, pSelTab); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3094 | sqlite3SelectDelete(db, pSel); |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 3095 | EnableLookaside; |
dan | 0208337 | 2018-09-17 08:27:23 +0000 | [diff] [blame] | 3096 | pParse->eParseMode = eParseMode; |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 3097 | } else { |
| 3098 | nErr++; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3099 | } |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 3100 | pTable->pSchema->schemaFlags |= DB_UnresetViews; |
dan | 77f3f40 | 2018-07-09 18:55:44 +0000 | [diff] [blame] | 3101 | if( db->mallocFailed ){ |
| 3102 | sqlite3DeleteColumnNames(db, pTable); |
dan | 77f3f40 | 2018-07-09 18:55:44 +0000 | [diff] [blame] | 3103 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3104 | #endif /* SQLITE_OMIT_VIEW */ |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3105 | return nErr; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 +0000 | [diff] [blame] | 3106 | } |
| 3107 | #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3108 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3109 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3110 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3111 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3112 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3113 | static void sqliteViewResetAll(sqlite3 *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3114 | HashElem *i; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3115 | assert( sqlite3SchemaMutexHeld(db, idx, 0) ); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3116 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3117 | for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3118 | Table *pTab = sqliteHashData(i); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3119 | if( IsView(pTab) ){ |
drh | 51be387 | 2015-08-19 02:32:25 +0000 | [diff] [blame] | 3120 | sqlite3DeleteColumnNames(db, pTab); |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 3123 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3124 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3125 | #else |
| 3126 | # define sqliteViewResetAll(A,B) |
| 3127 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3128 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3129 | /* |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3130 | ** This function is called by the VDBE to adjust the internal schema |
| 3131 | ** used by SQLite when the btree layer moves a table root page. The |
| 3132 | ** root-page of a table or index in database iDb has changed from iFrom |
| 3133 | ** to iTo. |
drh | 6205d4a | 2006-03-24 03:36:26 +0000 | [diff] [blame] | 3134 | ** |
| 3135 | ** Ticket #1728: The symbol table might still contain information |
| 3136 | ** on tables and/or indices that are the process of being deleted. |
| 3137 | ** If you are unlucky, one of those deleted indices or tables might |
| 3138 | ** have the same rootpage number as the real table or index that is |
| 3139 | ** being moved. So we cannot stop searching after the first match |
| 3140 | ** because the first match might be for one of the deleted indices |
| 3141 | ** or tables and not the table/index that is actually being moved. |
| 3142 | ** We must continue looping until all tables and indices with |
| 3143 | ** rootpage==iFrom have been converted to have a rootpage of iTo |
| 3144 | ** in order to be certain that we got the right one. |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3145 | */ |
| 3146 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3147 | void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3148 | HashElem *pElem; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3149 | Hash *pHash; |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 3150 | Db *pDb; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3151 | |
drh | cdf011d | 2011-04-04 21:25:28 +0000 | [diff] [blame] | 3152 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 3153 | pDb = &db->aDb[iDb]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3154 | pHash = &pDb->pSchema->tblHash; |
| 3155 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3156 | Table *pTab = sqliteHashData(pElem); |
| 3157 | if( pTab->tnum==iFrom ){ |
| 3158 | pTab->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3161 | pHash = &pDb->pSchema->idxHash; |
| 3162 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3163 | Index *pIdx = sqliteHashData(pElem); |
| 3164 | if( pIdx->tnum==iFrom ){ |
| 3165 | pIdx->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3166 | } |
| 3167 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3168 | } |
| 3169 | #endif |
| 3170 | |
| 3171 | /* |
| 3172 | ** Write code to erase the table with root-page iTable from database iDb. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3173 | ** Also write code to modify the sqlite_schema table and internal schema |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3174 | ** if a root-page of another table is moved by the btree-layer whilst |
| 3175 | ** erasing iTable (this can happen with an auto-vacuum database). |
| 3176 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3177 | static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 3178 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3179 | int r1 = sqlite3GetTempReg(pParse); |
drh | 1991888 | 2020-07-30 23:47:00 +0000 | [diff] [blame] | 3180 | if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3181 | sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 3182 | sqlite3MayAbort(pParse); |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 3183 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3184 | /* OP_Destroy stores an in integer r1. If this integer |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3185 | ** 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] | 3186 | ** location iTable. The following code modifies the sqlite_schema table to |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3187 | ** reflect this. |
| 3188 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 +0000 | [diff] [blame] | 3189 | ** The "#NNN" in the SQL is a special constant that means whatever value |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 3190 | ** is in register NNN. See grammar rules associated with the TK_REGISTER |
| 3191 | ** token for additional information. |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3192 | */ |
danielk1977 | 63e3e9f | 2004-11-05 09:19:27 +0000 | [diff] [blame] | 3193 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3194 | "UPDATE %Q." DFLT_SCHEMA_TABLE |
| 3195 | " SET rootpage=%d WHERE #%d AND rootpage=#%d", |
| 3196 | pParse->db->aDb[iDb].zDbSName, iTable, r1, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3197 | #endif |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 3198 | sqlite3ReleaseTempReg(pParse, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | /* |
| 3202 | ** Write VDBE code to erase table pTab and all associated indices on disk. |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3203 | ** Code to update the sqlite_schema tables and internal schema definitions |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3204 | ** in case a root-page belonging to another table is moved by the btree layer |
| 3205 | ** is also added (this can happen with an auto-vacuum database). |
| 3206 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3207 | static void destroyTable(Parse *pParse, Table *pTab){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3208 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
| 3209 | ** is not defined), then it is important to call OP_Destroy on the |
| 3210 | ** table and index root-pages in order, starting with the numerically |
| 3211 | ** largest root-page number. This guarantees that none of the root-pages |
| 3212 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
| 3213 | ** following were coded: |
| 3214 | ** |
| 3215 | ** OP_Destroy 4 0 |
| 3216 | ** ... |
| 3217 | ** OP_Destroy 5 0 |
| 3218 | ** |
| 3219 | ** and root page 5 happened to be the largest root-page number in the |
| 3220 | ** database, then root page 5 would be moved to page 4 by the |
| 3221 | ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
| 3222 | ** a free-list page. |
| 3223 | */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3224 | Pgno iTab = pTab->tnum; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 3225 | Pgno iDestroyed = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3226 | |
| 3227 | while( 1 ){ |
| 3228 | Index *pIdx; |
drh | 8deae5a | 2020-07-29 12:23:20 +0000 | [diff] [blame] | 3229 | Pgno iLargest = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3230 | |
| 3231 | if( iDestroyed==0 || iTab<iDestroyed ){ |
| 3232 | iLargest = iTab; |
| 3233 | } |
| 3234 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3235 | Pgno iIdx = pIdx->tnum; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3236 | assert( pIdx->pSchema==pTab->pSchema ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3237 | if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
| 3238 | iLargest = iIdx; |
| 3239 | } |
| 3240 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3241 | if( iLargest==0 ){ |
| 3242 | return; |
| 3243 | }else{ |
| 3244 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 3245 | assert( iDb>=0 && iDb<pParse->db->nDb ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3246 | destroyRootPage(pParse, iLargest, iDb); |
| 3247 | iDestroyed = iLargest; |
| 3248 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3249 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | /* |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3253 | ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3254 | ** after a DROP INDEX or DROP TABLE command. |
| 3255 | */ |
| 3256 | static void sqlite3ClearStatTables( |
| 3257 | Parse *pParse, /* The parsing context */ |
| 3258 | int iDb, /* The database number */ |
| 3259 | const char *zType, /* "idx" or "tbl" */ |
| 3260 | const char *zName /* Name of index or table */ |
| 3261 | ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3262 | int i; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3263 | const char *zDbName = pParse->db->aDb[iDb].zDbSName; |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 3264 | for(i=1; i<=4; i++){ |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3265 | char zTab[24]; |
| 3266 | sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); |
| 3267 | if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3268 | sqlite3NestedParse(pParse, |
| 3269 | "DELETE FROM %Q.%s WHERE %s=%Q", |
drh | 74e7c8f | 2011-10-21 19:06:32 +0000 | [diff] [blame] | 3270 | zDbName, zTab, zType, zName |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 3271 | ); |
| 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3277 | ** Generate code to drop a table. |
| 3278 | */ |
| 3279 | void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 3280 | Vdbe *v; |
| 3281 | sqlite3 *db = pParse->db; |
| 3282 | Trigger *pTrigger; |
| 3283 | Db *pDb = &db->aDb[iDb]; |
| 3284 | |
| 3285 | v = sqlite3GetVdbe(pParse); |
| 3286 | assert( v!=0 ); |
| 3287 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 3288 | |
| 3289 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3290 | if( IsVirtual(pTab) ){ |
| 3291 | sqlite3VdbeAddOp0(v, OP_VBegin); |
| 3292 | } |
| 3293 | #endif |
| 3294 | |
| 3295 | /* Drop all triggers associated with the table being dropped. Code |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3296 | ** is generated to remove entries from sqlite_schema and/or |
| 3297 | ** sqlite_temp_schema if required. |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3298 | */ |
| 3299 | pTrigger = sqlite3TriggerList(pParse, pTab); |
| 3300 | while( pTrigger ){ |
| 3301 | assert( pTrigger->pSchema==pTab->pSchema || |
| 3302 | pTrigger->pSchema==db->aDb[1].pSchema ); |
| 3303 | sqlite3DropTriggerPtr(pParse, pTrigger); |
| 3304 | pTrigger = pTrigger->pNext; |
| 3305 | } |
| 3306 | |
| 3307 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 3308 | /* Remove any entries of the sqlite_sequence table associated with |
| 3309 | ** the table being dropped. This is done before the table is dropped |
| 3310 | ** at the btree level, in case the sqlite_sequence table needs to |
| 3311 | ** move as a result of the drop (can happen in auto-vacuum mode). |
| 3312 | */ |
| 3313 | if( pTab->tabFlags & TF_Autoincrement ){ |
| 3314 | sqlite3NestedParse(pParse, |
| 3315 | "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3316 | pDb->zDbSName, pTab->zName |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3317 | ); |
| 3318 | } |
| 3319 | #endif |
| 3320 | |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3321 | /* Drop all entries in the schema table that refer to the |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3322 | ** table. The program name loops through the schema table and deletes |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3323 | ** 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] | 3324 | ** dropped. Triggers are handled separately because a trigger can be |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3325 | ** created in the temp database that refers to a table in another |
| 3326 | ** database. |
| 3327 | */ |
| 3328 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 3329 | "DELETE FROM %Q." DFLT_SCHEMA_TABLE |
| 3330 | " WHERE tbl_name=%Q and type!='trigger'", |
| 3331 | pDb->zDbSName, pTab->zName); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3332 | if( !isView && !IsVirtual(pTab) ){ |
| 3333 | destroyTable(pParse, pTab); |
| 3334 | } |
| 3335 | |
| 3336 | /* Remove the table entry from SQLite's internal schema and modify |
| 3337 | ** the schema cookie. |
| 3338 | */ |
| 3339 | if( IsVirtual(pTab) ){ |
| 3340 | sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
dan | 1d4b164 | 2018-12-28 17:45:08 +0000 | [diff] [blame] | 3341 | sqlite3MayAbort(pParse); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3342 | } |
| 3343 | sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 3344 | sqlite3ChangeCookie(pParse, iDb); |
| 3345 | sqliteViewResetAll(db, iDb); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | /* |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3349 | ** Return TRUE if shadow tables should be read-only in the current |
| 3350 | ** context. |
| 3351 | */ |
| 3352 | int sqlite3ReadOnlyShadowTables(sqlite3 *db){ |
| 3353 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3354 | if( (db->flags & SQLITE_Defensive)!=0 |
| 3355 | && db->pVtabCtx==0 |
| 3356 | && db->nVdbeExec==0 |
dan | 7398365 | 2021-07-19 14:00:29 +0000 | [diff] [blame] | 3357 | && !sqlite3VtabInSync(db) |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3358 | ){ |
| 3359 | return 1; |
| 3360 | } |
| 3361 | #endif |
| 3362 | return 0; |
| 3363 | } |
| 3364 | |
| 3365 | /* |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3366 | ** Return true if it is not allowed to drop the given table |
| 3367 | */ |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3368 | static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){ |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3369 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 3370 | if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0; |
| 3371 | if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; |
| 3372 | return 1; |
| 3373 | } |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3374 | if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ |
| 3375 | return 1; |
drh | d0c51d1 | 2019-11-16 12:04:38 +0000 | [diff] [blame] | 3376 | } |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
| 3380 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3381 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 3382 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3383 | */ |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3384 | void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3385 | Table *pTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3386 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3387 | sqlite3 *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 3388 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3389 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3390 | if( db->mallocFailed ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 3391 | goto exit_drop_table; |
| 3392 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3393 | assert( pParse->nErr==0 ); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3394 | assert( pName->nSrc==1 ); |
drh | 7520996 | 2015-04-19 22:31:45 +0000 | [diff] [blame] | 3395 | if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 3396 | if( noErr ) db->suppressErr++; |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 3397 | assert( isView==0 || isView==LOCATE_VIEW ); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 3398 | pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
drh | a756466 | 2010-02-22 19:32:31 +0000 | [diff] [blame] | 3399 | if( noErr ) db->suppressErr--; |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3400 | |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3401 | if( pTab==0 ){ |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 3402 | if( noErr ){ |
| 3403 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
| 3404 | sqlite3ForceNotReadOnly(pParse); |
| 3405 | } |
drh | a073384 | 2005-12-29 01:11:36 +0000 | [diff] [blame] | 3406 | goto exit_drop_table; |
| 3407 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3408 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3409 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | b5258c3 | 2007-10-04 18:11:15 +0000 | [diff] [blame] | 3410 | |
| 3411 | /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
| 3412 | ** it is initialized. |
| 3413 | */ |
| 3414 | if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 3415 | goto exit_drop_table; |
| 3416 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3417 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3418 | { |
| 3419 | int code; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3420 | const char *zTab = SCHEMA_TABLE(iDb); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3421 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3422 | const char *zArg2 = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3423 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3424 | goto exit_drop_table; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 3425 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3426 | if( isView ){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3427 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3428 | code = SQLITE_DROP_TEMP_VIEW; |
| 3429 | }else{ |
| 3430 | code = SQLITE_DROP_VIEW; |
| 3431 | } |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3432 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3433 | }else if( IsVirtual(pTab) ){ |
| 3434 | code = SQLITE_DROP_VTABLE; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 3435 | zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 3436 | #endif |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3437 | }else{ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3438 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3439 | code = SQLITE_DROP_TEMP_TABLE; |
| 3440 | }else{ |
| 3441 | code = SQLITE_DROP_TABLE; |
| 3442 | } |
| 3443 | } |
danielk1977 | f1a381e | 2006-06-16 08:01:02 +0000 | [diff] [blame] | 3444 | if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3445 | goto exit_drop_table; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3446 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3447 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 3448 | goto exit_drop_table; |
drh | 77ad4e4 | 2003-01-14 02:49:27 +0000 | [diff] [blame] | 3449 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 3450 | } |
| 3451 | #endif |
drh | 070ae3b | 2019-11-16 13:51:31 +0000 | [diff] [blame] | 3452 | if( tableMayNotBeDropped(db, pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3453 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3454 | goto exit_drop_table; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3455 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3456 | |
| 3457 | #ifndef SQLITE_OMIT_VIEW |
| 3458 | /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
| 3459 | ** on a table. |
| 3460 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3461 | if( isView && !IsView(pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3462 | sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
| 3463 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3464 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3465 | if( !isView && IsView(pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3466 | sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
| 3467 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3468 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3469 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3470 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3471 | /* Generate code to remove the table from the schema table |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 3472 | ** on disk. |
| 3473 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3474 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3475 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 3476 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
mistachkin | 0fc2da3 | 2018-07-20 20:56:22 +0000 | [diff] [blame] | 3477 | if( !isView ){ |
| 3478 | sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 3479 | sqlite3FkDropTable(pParse, pName, pTab); |
| 3480 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 3481 | sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3482 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 +0000 | [diff] [blame] | 3483 | |
| 3484 | exit_drop_table: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3485 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3489 | ** This routine is called to create a new foreign key on the table |
| 3490 | ** currently under construction. pFromCol determines which columns |
| 3491 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 3492 | ** connect the key to the last column inserted. pTo is the name of |
drh | bd50a92 | 2013-11-03 02:27:58 +0000 | [diff] [blame] | 3493 | ** the table referred to (a.k.a the "parent" table). pToCol is a list |
| 3494 | ** of tables in the parent pTo table. flags contains all |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3495 | ** information about the conflict resolution algorithms specified |
| 3496 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 3497 | ** |
| 3498 | ** An FKey structure is created and added to the table currently |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3499 | ** under construction in the pParse->pNewTable field. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3500 | ** |
| 3501 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3502 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3503 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3504 | void sqlite3CreateForeignKey( |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3505 | Parse *pParse, /* Parsing context */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3506 | ExprList *pFromCol, /* Columns in this table that point to other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3507 | Token *pTo, /* Name of the other table */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3508 | ExprList *pToCol, /* Columns in the other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3509 | int flags /* Conflict resolution algorithms. */ |
| 3510 | ){ |
danielk1977 | 1857693 | 2008-08-06 13:47:40 +0000 | [diff] [blame] | 3511 | sqlite3 *db = pParse->db; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3512 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 3513 | FKey *pFKey = 0; |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3514 | FKey *pNextTo; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3515 | Table *p = pParse->pNewTable; |
| 3516 | int nByte; |
| 3517 | int i; |
| 3518 | int nCol; |
| 3519 | char *z; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3520 | |
| 3521 | assert( pTo!=0 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 3522 | if( p==0 || IN_DECLARE_VTAB ) goto fk_end; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3523 | if( pFromCol==0 ){ |
| 3524 | int iCol = p->nCol-1; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3525 | if( NEVER(iCol<0) ) goto fk_end; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3526 | if( pToCol && pToCol->nExpr!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3527 | sqlite3ErrorMsg(pParse, "foreign key on %s" |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3528 | " should reference only one column of table %T", |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 3529 | p->aCol[iCol].zCnName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3530 | goto fk_end; |
| 3531 | } |
| 3532 | nCol = 1; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3533 | }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3534 | sqlite3ErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3535 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3536 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3537 | goto fk_end; |
| 3538 | }else{ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3539 | nCol = pFromCol->nExpr; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3540 | } |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3541 | nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3542 | if( pToCol ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3543 | for(i=0; i<pToCol->nExpr; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3544 | nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3545 | } |
| 3546 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3547 | pFKey = sqlite3DbMallocZero(db, nByte ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3548 | if( pFKey==0 ){ |
| 3549 | goto fk_end; |
| 3550 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3551 | pFKey->pFrom = p; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3552 | pFKey->pNextFrom = p->u.tab.pFKey; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 3553 | z = (char*)&pFKey->aCol[nCol]; |
drh | df68f6b | 2002-09-21 15:57:57 +0000 | [diff] [blame] | 3554 | pFKey->zTo = z; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3555 | if( IN_RENAME_OBJECT ){ |
| 3556 | sqlite3RenameTokenMap(pParse, (void*)z, pTo); |
| 3557 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3558 | memcpy(z, pTo->z, pTo->n); |
| 3559 | z[pTo->n] = 0; |
danielk1977 | 70d9e9c | 2009-04-24 18:06:09 +0000 | [diff] [blame] | 3560 | sqlite3Dequote(z); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3561 | z += pTo->n+1; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3562 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3563 | if( pFromCol==0 ){ |
| 3564 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 3565 | }else{ |
| 3566 | for(i=0; i<nCol; i++){ |
| 3567 | int j; |
| 3568 | for(j=0; j<p->nCol; j++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 3569 | if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3570 | pFKey->aCol[i].iFrom = j; |
| 3571 | break; |
| 3572 | } |
| 3573 | } |
| 3574 | if( j>=p->nCol ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3575 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 3576 | "unknown column \"%s\" in foreign key definition", |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3577 | pFromCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3578 | goto fk_end; |
| 3579 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3580 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3581 | sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3582 | } |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3583 | } |
| 3584 | } |
| 3585 | if( pToCol ){ |
| 3586 | for(i=0; i<nCol; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3587 | int n = sqlite3Strlen30(pToCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3588 | pFKey->aCol[i].zCol = z; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3589 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3590 | sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName); |
dan | 6fe7f23 | 2018-08-10 19:19:33 +0000 | [diff] [blame] | 3591 | } |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 3592 | memcpy(z, pToCol->a[i].zEName, n); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3593 | z[n] = 0; |
| 3594 | z += n+1; |
| 3595 | } |
| 3596 | } |
| 3597 | pFKey->isDeferred = 0; |
dan | 8099ce6 | 2009-09-23 08:43:35 +0000 | [diff] [blame] | 3598 | pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
| 3599 | pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3600 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 3601 | assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3602 | pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 3603 | pFKey->zTo, (void *)pFKey |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3604 | ); |
dan | f59c5ca | 2009-09-22 16:55:38 +0000 | [diff] [blame] | 3605 | if( pNextTo==pFKey ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 3606 | sqlite3OomFault(db); |
dan | f59c5ca | 2009-09-22 16:55:38 +0000 | [diff] [blame] | 3607 | goto fk_end; |
| 3608 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 3609 | if( pNextTo ){ |
| 3610 | assert( pNextTo->pPrevTo==0 ); |
| 3611 | pFKey->pNextTo = pNextTo; |
| 3612 | pNextTo->pPrevTo = pFKey; |
| 3613 | } |
| 3614 | |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3615 | /* Link the foreign key to the table as the last step. |
| 3616 | */ |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3617 | assert( !IsVirtual(p) ); |
| 3618 | p->u.tab.pFKey = pFKey; |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3619 | pFKey = 0; |
| 3620 | |
| 3621 | fk_end: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3622 | sqlite3DbFree(db, pFKey); |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3623 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3624 | sqlite3ExprListDelete(db, pFromCol); |
| 3625 | sqlite3ExprListDelete(db, pToCol); |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | /* |
| 3629 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 3630 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 3631 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 3632 | ** The behavior of the most recently created foreign key is adjusted |
| 3633 | ** accordingly. |
| 3634 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3635 | void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3636 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3637 | Table *pTab; |
| 3638 | FKey *pFKey; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3639 | if( (pTab = pParse->pNewTable)==0 ) return; |
drh | 324f91a | 2021-08-04 14:50:23 +0000 | [diff] [blame] | 3640 | if( NEVER(IsVirtual(pTab)) ) return; |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3641 | if( (pFKey = pTab->u.tab.pFKey)==0 ) return; |
drh | 4c42983 | 2009-10-12 22:30:49 +0000 | [diff] [blame] | 3642 | assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3643 | pFKey->isDeferred = (u8)isDeferred; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3644 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | /* |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3648 | ** Generate code that will erase and refill index *pIdx. This is |
| 3649 | ** used to initialize a newly created index or to recompute the |
| 3650 | ** content of an index in response to a REINDEX command. |
| 3651 | ** |
| 3652 | ** if memRootPage is not negative, it means that the index is newly |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3653 | ** created. The register specified by memRootPage contains the |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3654 | ** root page number of the index. If memRootPage is negative, then |
| 3655 | ** the index already exists and must be cleared before being refilled and |
| 3656 | ** the root page number of the index is taken from pIndex->tnum. |
| 3657 | */ |
| 3658 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 3659 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 3660 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 3661 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3662 | int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3663 | int addr1; /* Address of top of loop */ |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3664 | int addr2; /* Address to jump to for next iteration */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3665 | Pgno tnum; /* Root page of index */ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3666 | int iPartIdxLabel; /* Jump to this label to skip a row */ |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3667 | Vdbe *v; /* Generate code into this virtual machine */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 3668 | KeyInfo *pKey; /* KeyInfo for index */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3669 | int regRecord; /* Register holding assembled index record */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3670 | sqlite3 *db = pParse->db; /* The database connection */ |
| 3671 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3672 | |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 3673 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3674 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 3675 | db->aDb[iDb].zDbSName ) ){ |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 +0000 | [diff] [blame] | 3676 | return; |
| 3677 | } |
| 3678 | #endif |
| 3679 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 3680 | /* Require a write-lock on the table to perform this operation */ |
| 3681 | sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 3682 | |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3683 | v = sqlite3GetVdbe(pParse); |
| 3684 | if( v==0 ) return; |
| 3685 | if( memRootPage>=0 ){ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3686 | tnum = (Pgno)memRootPage; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3687 | }else{ |
| 3688 | tnum = pIndex->tnum; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3689 | } |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3690 | pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
drh | 60de73e | 2016-04-05 15:59:23 +0000 | [diff] [blame] | 3691 | assert( pKey!=0 || db->mallocFailed || pParse->nErr ); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3692 | |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3693 | /* Open the sorter cursor if we are to use one. */ |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3694 | iSorter = pParse->nTab++; |
dan | fad9f9a | 2014-04-01 18:41:51 +0000 | [diff] [blame] | 3695 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3696 | sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3697 | |
| 3698 | /* Open the table. Loop through all rows of the table, inserting index |
| 3699 | ** records into the sorter. */ |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 3700 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3701 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 3702 | regRecord = sqlite3GetTempReg(pParse); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3703 | sqlite3MultiWrite(pParse); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3704 | |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 3705 | sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3706 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
drh | 8774451 | 2014-04-13 19:15:49 +0000 | [diff] [blame] | 3707 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3708 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3709 | sqlite3VdbeJumpHere(v, addr1); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3710 | if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3711 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3712 | (char *)pKey, P4_KEYINFO); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3713 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
| 3714 | |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3715 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
drh | 60de73e | 2016-04-05 15:59:23 +0000 | [diff] [blame] | 3716 | if( IsUniqueIndex(pIndex) ){ |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3717 | int j2 = sqlite3VdbeGoto(v, 1); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3718 | addr2 = sqlite3VdbeCurrentAddr(v); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3719 | sqlite3VdbeVerifyAbortable(v, OE_Abort); |
drh | 1153c7b | 2013-11-01 22:02:56 +0000 | [diff] [blame] | 3720 | sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
drh | ac50232 | 2014-07-30 13:56:48 +0000 | [diff] [blame] | 3721 | pIndex->nKeyCol); VdbeCoverage(v); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 3722 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
drh | 4031baf | 2018-05-28 17:31:20 +0000 | [diff] [blame] | 3723 | sqlite3VdbeJumpHere(v, j2); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3724 | }else{ |
dan | 7ed6c06 | 2019-05-21 16:32:41 +0000 | [diff] [blame] | 3725 | /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not |
| 3726 | ** abort. The exception is if one of the indexed expressions contains a |
| 3727 | ** user function that throws an exception when it is evaluated. But the |
| 3728 | ** overhead of adding a statement journal to a CREATE INDEX statement is |
| 3729 | ** very small (since most of the pages written do not contain content that |
| 3730 | ** needs to be restored if the statement aborts), so we call |
| 3731 | ** sqlite3MayAbort() for all CREATE INDEX statements. */ |
dan | ef14abb | 2019-05-21 14:42:24 +0000 | [diff] [blame] | 3732 | sqlite3MayAbort(pParse); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3733 | addr2 = sqlite3VdbeCurrentAddr(v); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3734 | } |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 3735 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
drh | bf9ff25 | 2019-05-14 00:43:13 +0000 | [diff] [blame] | 3736 | if( !pIndex->bAscKeyBug ){ |
| 3737 | /* This OP_SeekEnd opcode makes index insert for a REINDEX go much |
| 3738 | ** faster by avoiding unnecessary seeks. But the optimization does |
| 3739 | ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables |
| 3740 | ** with DESC primary keys, since those indexes have there keys in |
| 3741 | ** a different order from the main table. |
| 3742 | ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf |
| 3743 | */ |
| 3744 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); |
| 3745 | } |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 3746 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
drh | ca892a7 | 2011-09-03 00:17:51 +0000 | [diff] [blame] | 3747 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 3748 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3749 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 3750 | sqlite3VdbeJumpHere(v, addr1); |
dan | a20fde6 | 2011-07-12 14:28:05 +0000 | [diff] [blame] | 3751 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3752 | sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 3753 | sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
dan | 689ab89 | 2011-08-12 15:02:00 +0000 | [diff] [blame] | 3754 | sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | /* |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3758 | ** Allocate heap space to hold an Index object with nCol columns. |
| 3759 | ** |
| 3760 | ** Increase the allocation size to provide an extra nExtra bytes |
| 3761 | ** of 8-byte aligned space after the Index object and return a |
| 3762 | ** pointer to this extra space in *ppExtra. |
| 3763 | */ |
| 3764 | Index *sqlite3AllocateIndexObject( |
| 3765 | sqlite3 *db, /* Database connection */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3766 | i16 nCol, /* Total number of columns in the index */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3767 | int nExtra, /* Number of bytes of extra space to alloc */ |
| 3768 | char **ppExtra /* Pointer to the "extra" space */ |
| 3769 | ){ |
| 3770 | Index *p; /* Allocated index object */ |
| 3771 | int nByte; /* Bytes of space for Index object + arrays */ |
| 3772 | |
| 3773 | nByte = ROUND8(sizeof(Index)) + /* Index structure */ |
| 3774 | ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3775 | ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3776 | sizeof(i16)*nCol + /* Index.aiColumn */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3777 | sizeof(u8)*nCol); /* Index.aSortOrder */ |
| 3778 | p = sqlite3DbMallocZero(db, nByte + nExtra); |
| 3779 | if( p ){ |
| 3780 | char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 3781 | p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 3782 | p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); |
| 3783 | p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3784 | p->aSortOrder = (u8*)pExtra; |
| 3785 | p->nColumn = nCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3786 | p->nKeyCol = nCol - 1; |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 3787 | *ppExtra = ((char*)p) + nByte; |
| 3788 | } |
| 3789 | return p; |
| 3790 | } |
| 3791 | |
| 3792 | /* |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3793 | ** If expression list pList contains an expression that was parsed with |
| 3794 | ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in |
| 3795 | ** pParse and return non-zero. Otherwise, return zero. |
| 3796 | */ |
| 3797 | int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ |
| 3798 | if( pList ){ |
| 3799 | int i; |
| 3800 | for(i=0; i<pList->nExpr; i++){ |
| 3801 | if( pList->a[i].bNulls ){ |
| 3802 | u8 sf = pList->a[i].sortFlags; |
| 3803 | sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", |
| 3804 | (sf==0 || sf==3) ? "FIRST" : "LAST" |
| 3805 | ); |
| 3806 | return 1; |
| 3807 | } |
| 3808 | } |
| 3809 | } |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | /* |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3814 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 3815 | ** 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] | 3816 | ** be NULL for a primary key or an index that is created to satisfy a |
| 3817 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 3818 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 3819 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3820 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 3821 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 3822 | ** is a primary key or unique-constraint on the most recent column added |
| 3823 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3824 | */ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3825 | void sqlite3CreateIndex( |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3826 | Parse *pParse, /* All information about this parse */ |
| 3827 | Token *pName1, /* First part of index name. May be NULL */ |
| 3828 | Token *pName2, /* Second part of index name. May be NULL */ |
| 3829 | SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 3830 | ExprList *pList, /* A list of columns to be indexed */ |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 3831 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 1c55ba0 | 2007-07-02 19:31:27 +0000 | [diff] [blame] | 3832 | Token *pStart, /* The CREATE token that begins this statement */ |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 3833 | Expr *pPIWhere, /* WHERE clause for partial indices */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 3834 | int sortOrder, /* Sort order of primary key when pList==NULL */ |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3835 | int ifNotExist, /* Omit error if index already exists */ |
| 3836 | u8 idxType /* The index type */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3837 | ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3838 | Table *pTab = 0; /* Table to be indexed */ |
| 3839 | Index *pIndex = 0; /* The index to be created */ |
| 3840 | char *zName = 0; /* Name of the index */ |
| 3841 | int nName; /* Number of characters in zName */ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 3842 | int i, j; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3843 | DbFixer sFix; /* For assigning database names to pTable */ |
| 3844 | int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 3845 | sqlite3 *db = pParse->db; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3846 | Db *pDb; /* The specific table containing the indexed database */ |
| 3847 | int iDb; /* Index of the database that is being written */ |
| 3848 | Token *pName = 0; /* Unqualified name of the index to create */ |
| 3849 | struct ExprList_item *pListItem; /* For looping over pList */ |
drh | c28c4e5 | 2013-10-03 19:21:41 +0000 | [diff] [blame] | 3850 | int nExtra = 0; /* Space allocated for zExtra[] */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3851 | int nExtraCol; /* Number of extra columns needed */ |
drh | 47b927d | 2013-12-03 00:11:40 +0000 | [diff] [blame] | 3852 | char *zExtra = 0; /* Extra space after the Index object */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3853 | Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3854 | |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 3855 | if( db->mallocFailed || pParse->nErr>0 ){ |
| 3856 | goto exit_create_index; |
| 3857 | } |
| 3858 | if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3859 | goto exit_create_index; |
| 3860 | } |
| 3861 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 3862 | goto exit_create_index; |
| 3863 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 3864 | if( sqlite3HasExplicitNulls(pParse, pList) ){ |
| 3865 | goto exit_create_index; |
| 3866 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3867 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3868 | /* |
| 3869 | ** Find the table that is to be indexed. Return early if not found. |
| 3870 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3871 | if( pTblName!=0 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3872 | |
| 3873 | /* Use the two-part index name to determine the database |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3874 | ** to search for the table. 'Fix' the table name to this db |
| 3875 | ** before looking up the table. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3876 | */ |
| 3877 | assert( pName1 && pName2 ); |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3878 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3879 | if( iDb<0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3880 | assert( pName && pName->z ); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3881 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3882 | #ifndef SQLITE_OMIT_TEMPDB |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 3883 | /* If the index name was unqualified, check if the table |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3884 | ** is a temp table. If so, set the database to 1. Do not do this |
| 3885 | ** if initialising a database schema. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3886 | */ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3887 | if( !db->init.busy ){ |
| 3888 | pTab = sqlite3SrcListLookup(pParse, pTblName); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3889 | if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 +0000 | [diff] [blame] | 3890 | iDb = 1; |
| 3891 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3892 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 3893 | #endif |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 3894 | |
drh | d100f69 | 2013-10-03 15:39:44 +0000 | [diff] [blame] | 3895 | sqlite3FixInit(&sFix, pParse, iDb, "index", pName); |
| 3896 | if( sqlite3FixSrcList(&sFix, pTblName) ){ |
drh | 85c23c6 | 2005-08-20 03:03:04 +0000 | [diff] [blame] | 3897 | /* Because the parser constructs pTblName from a single identifier, |
| 3898 | ** sqlite3FixSrcList can never fail. */ |
| 3899 | assert(0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3900 | } |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 3901 | pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); |
drh | c31c7c1 | 2012-10-08 23:25:07 +0000 | [diff] [blame] | 3902 | assert( db->mallocFailed==0 || pTab==0 ); |
| 3903 | if( pTab==0 ) goto exit_create_index; |
drh | 989b116 | 2013-08-01 22:27:26 +0000 | [diff] [blame] | 3904 | if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ |
| 3905 | sqlite3ErrorMsg(pParse, |
| 3906 | "cannot create a TEMP index on non-TEMP table \"%s\"", |
| 3907 | pTab->zName); |
| 3908 | goto exit_create_index; |
| 3909 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 3910 | if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3911 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 3912 | assert( pName==0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3913 | assert( pStart==0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3914 | pTab = pParse->pNewTable; |
drh | a6370df | 2006-01-04 21:40:06 +0000 | [diff] [blame] | 3915 | if( !pTab ) goto exit_create_index; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3916 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3917 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 3918 | pDb = &db->aDb[iDb]; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 +0000 | [diff] [blame] | 3919 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 3920 | assert( pTab!=0 ); |
| 3921 | assert( pParse->nErr==0 ); |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 3922 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
drh | 3a3a03f | 2014-09-11 16:36:43 +0000 | [diff] [blame] | 3923 | && db->init.busy==0 |
drh | 346f4e2 | 2019-03-25 21:35:41 +0000 | [diff] [blame] | 3924 | && pTblName!=0 |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 3925 | #if SQLITE_USER_AUTHENTICATION |
| 3926 | && sqlite3UserAuthTable(pTab->zName)==0 |
| 3927 | #endif |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3928 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3929 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 3930 | goto exit_create_index; |
| 3931 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3932 | #ifndef SQLITE_OMIT_VIEW |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 3933 | if( IsView(pTab) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3934 | sqlite3ErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 3935 | goto exit_create_index; |
| 3936 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 3937 | #endif |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 +0000 | [diff] [blame] | 3938 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3939 | if( IsVirtual(pTab) ){ |
| 3940 | sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); |
| 3941 | goto exit_create_index; |
| 3942 | } |
| 3943 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3944 | |
| 3945 | /* |
| 3946 | ** Find the name of the index. Make sure there is not already another |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3947 | ** index or table with the same name. |
| 3948 | ** |
| 3949 | ** Exception: If we are reading the names of permanent indices from the |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 3950 | ** sqlite_schema table (because some other process changed the schema) and |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3951 | ** 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] | 3952 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 3953 | ** |
| 3954 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3955 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 3956 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3957 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 3958 | if( pName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3959 | zName = sqlite3NameFromToken(db, pName); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 3960 | if( zName==0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 3961 | assert( pName->z!=0 ); |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 3962 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 3963 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 3964 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 3965 | if( !IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3966 | if( !db->init.busy ){ |
| 3967 | if( sqlite3FindTable(db, zName, 0)!=0 ){ |
| 3968 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 3969 | goto exit_create_index; |
| 3970 | } |
| 3971 | } |
| 3972 | if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| 3973 | if( !ifNotExist ){ |
| 3974 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 3975 | }else{ |
| 3976 | assert( !db->init.busy ); |
| 3977 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 3978 | sqlite3ForceNotReadOnly(pParse); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3979 | } |
danielk1977 | d45a031 | 2007-03-13 16:32:25 +0000 | [diff] [blame] | 3980 | goto exit_create_index; |
| 3981 | } |
| 3982 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 3983 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 3984 | int n; |
| 3985 | Index *pLoop; |
| 3986 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 3987 | zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3988 | if( zName==0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 3989 | goto exit_create_index; |
| 3990 | } |
drh | 0aafa9c | 2016-08-05 14:35:47 +0000 | [diff] [blame] | 3991 | |
| 3992 | /* Automatic index names generated from within sqlite3_declare_vtab() |
| 3993 | ** must have names that are distinct from normal automatic index names. |
| 3994 | ** The following statement converts "sqlite3_autoindex..." into |
| 3995 | ** "sqlite3_butoindex..." in order to make the names distinct. |
| 3996 | ** The "vtab_err.test" test demonstrates the need of this statement. */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 3997 | if( IN_SPECIAL_PARSE ) zName[7]++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4000 | /* Check for authorization to create an index. |
| 4001 | */ |
| 4002 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4003 | if( !IN_RENAME_OBJECT ){ |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4004 | const char *zDb = pDb->zDbSName; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4005 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 4006 | goto exit_create_index; |
| 4007 | } |
| 4008 | i = SQLITE_CREATE_INDEX; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 4009 | if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4010 | if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame] | 4011 | goto exit_create_index; |
| 4012 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4013 | } |
| 4014 | #endif |
| 4015 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4016 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 4017 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4018 | ** So create a fake list to simulate this. |
| 4019 | */ |
| 4020 | if( pList==0 ){ |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4021 | Token prevCol; |
dan | 26e731c | 2018-01-29 16:22:39 +0000 | [diff] [blame] | 4022 | Column *pCol = &pTab->aCol[pTab->nCol-1]; |
| 4023 | pCol->colFlags |= COLFLAG_UNIQUE; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 4024 | sqlite3TokenInit(&prevCol, pCol->zCnName); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4025 | pList = sqlite3ExprListAppend(pParse, 0, |
| 4026 | sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4027 | if( pList==0 ) goto exit_create_index; |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 4028 | assert( pList->nExpr==1 ); |
dan | 5b32bdf | 2019-08-17 15:47:32 +0000 | [diff] [blame] | 4029 | sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4030 | }else{ |
| 4031 | sqlite3ExprListCheckLength(pParse, pList, "index"); |
drh | 8fe25c6 | 2019-03-31 21:09:33 +0000 | [diff] [blame] | 4032 | if( pParse->nErr ) goto exit_create_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4035 | /* Figure out how many bytes of space are required to store explicitly |
| 4036 | ** specified collation sequence names. |
| 4037 | */ |
| 4038 | for(i=0; i<pList->nExpr; i++){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4039 | Expr *pExpr = pList->a[i].pExpr; |
drh | 7d3d9da | 2015-09-01 00:42:52 +0000 | [diff] [blame] | 4040 | assert( pExpr!=0 ); |
| 4041 | if( pExpr->op==TK_COLLATE ){ |
dan | 911ce41 | 2013-05-15 15:16:50 +0000 | [diff] [blame] | 4042 | nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4046 | /* |
| 4047 | ** Allocate the index structure. |
| 4048 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4049 | nName = sqlite3Strlen30(zName); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4050 | nExtraCol = pPk ? pPk->nKeyCol : 1; |
drh | 8fe25c6 | 2019-03-31 21:09:33 +0000 | [diff] [blame] | 4051 | assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4052 | pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 4053 | nName + nExtra + 1, &zExtra); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4054 | if( db->mallocFailed ){ |
| 4055 | goto exit_create_index; |
| 4056 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4057 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); |
drh | e09b84c | 2011-11-14 02:53:54 +0000 | [diff] [blame] | 4058 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 4059 | pIndex->zName = zExtra; |
| 4060 | zExtra += nName + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 4061 | memcpy(pIndex->zName, zName, nName+1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4062 | pIndex->pTable = pTab; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4063 | pIndex->onError = (u8)onError; |
drh | 9eade08 | 2013-10-24 14:16:10 +0000 | [diff] [blame] | 4064 | pIndex->uniqNotNull = onError!=OE_None; |
drh | 62340f8 | 2016-05-31 21:18:15 +0000 | [diff] [blame] | 4065 | pIndex->idxType = idxType; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4066 | pIndex->pSchema = db->aDb[iDb].pSchema; |
drh | 72ffd09 | 2013-10-30 15:52:32 +0000 | [diff] [blame] | 4067 | pIndex->nKeyCol = pList->nExpr; |
drh | 3780be1 | 2013-07-31 19:05:22 +0000 | [diff] [blame] | 4068 | if( pPIWhere ){ |
| 4069 | sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); |
| 4070 | pIndex->pPartIdxWhere = pPIWhere; |
| 4071 | pPIWhere = 0; |
| 4072 | } |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 4073 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4074 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4075 | /* Check to see if we should honor DESC requests on index columns |
| 4076 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4077 | if( pDb->pSchema->file_format>=4 ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4078 | sortOrderMask = -1; /* Honor DESC */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 4079 | }else{ |
| 4080 | sortOrderMask = 0; /* Ignore DESC */ |
| 4081 | } |
| 4082 | |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4083 | /* Analyze the list of expressions that form the terms of the index and |
| 4084 | ** report any errors. In the common case where the expression is exactly |
| 4085 | ** a table column, store that column in aiColumn[]. For general expressions, |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4086 | ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4087 | ** |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4088 | ** TODO: Issue a warning if two or more columns of the index are identical. |
| 4089 | ** TODO: Issue a warning if the table primary key is used as part of the |
| 4090 | ** index key. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4091 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4092 | pListItem = pList->a; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4093 | if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4094 | pIndex->aColExpr = pList; |
| 4095 | pList = 0; |
| 4096 | } |
| 4097 | for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4098 | Expr *pCExpr; /* The i-th index expression */ |
| 4099 | int requestedSortOrder; /* ASC or DESC on the i-th expression */ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4100 | const char *zColl; /* Collation sequence name */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4101 | |
drh | edb04ed | 2015-09-04 12:54:01 +0000 | [diff] [blame] | 4102 | sqlite3StringToId(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4103 | sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); |
| 4104 | if( pParse->nErr ) goto exit_create_index; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4105 | pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4106 | if( pCExpr->op!=TK_COLUMN ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4107 | if( pTab==pParse->pNewTable ){ |
| 4108 | sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " |
| 4109 | "UNIQUE constraints"); |
| 4110 | goto exit_create_index; |
| 4111 | } |
| 4112 | if( pIndex->aColExpr==0 ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4113 | pIndex->aColExpr = pList; |
| 4114 | pList = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4115 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4116 | j = XN_EXPR; |
| 4117 | pIndex->aiColumn[i] = XN_EXPR; |
drh | 8492653 | 2015-08-31 19:38:42 +0000 | [diff] [blame] | 4118 | pIndex->uniqNotNull = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4119 | }else{ |
| 4120 | j = pCExpr->iColumn; |
| 4121 | assert( j<=0x7fff ); |
| 4122 | if( j<0 ){ |
| 4123 | j = pTab->iPKey; |
drh | c747673 | 2019-10-24 20:29:25 +0000 | [diff] [blame] | 4124 | }else{ |
| 4125 | if( pTab->aCol[j].notNull==0 ){ |
| 4126 | pIndex->uniqNotNull = 0; |
| 4127 | } |
| 4128 | if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ |
| 4129 | pIndex->bHasVCol = 1; |
| 4130 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4131 | } |
| 4132 | pIndex->aiColumn[i] = (i16)j; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4133 | } |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4134 | zColl = 0; |
drh | 108aa00 | 2015-08-24 20:21:20 +0000 | [diff] [blame] | 4135 | if( pListItem->pExpr->op==TK_COLLATE ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4136 | int nColl; |
dan | 911ce41 | 2013-05-15 15:16:50 +0000 | [diff] [blame] | 4137 | zColl = pListItem->pExpr->u.zToken; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4138 | nColl = sqlite3Strlen30(zColl) + 1; |
| 4139 | assert( nExtra>=nColl ); |
| 4140 | memcpy(zExtra, zColl, nColl); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4141 | zColl = zExtra; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4142 | zExtra += nColl; |
| 4143 | nExtra -= nColl; |
drh | a514b8e | 2015-08-25 00:27:06 +0000 | [diff] [blame] | 4144 | }else if( j>=0 ){ |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 4145 | zColl = sqlite3ColumnColl(&pTab->aCol[j]); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 4146 | } |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4147 | if( !zColl ) zColl = sqlite3StrBINARY; |
drh | b7f24de | 2009-05-13 17:35:23 +0000 | [diff] [blame] | 4148 | if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 4149 | goto exit_create_index; |
| 4150 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 4151 | pIndex->azColl[i] = zColl; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 4152 | requestedSortOrder = pListItem->sortFlags & sortOrderMask; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4153 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4154 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4155 | |
| 4156 | /* Append the table key to the end of the index. For WITHOUT ROWID |
| 4157 | ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For |
| 4158 | ** normal tables (when pPk==0) this will be the rowid. |
| 4159 | */ |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4160 | if( pPk ){ |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4161 | for(j=0; j<pPk->nKeyCol; j++){ |
| 4162 | int x = pPk->aiColumn[j]; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4163 | assert( x>=0 ); |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 4164 | if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4165 | pIndex->nColumn--; |
| 4166 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 +0000 | [diff] [blame] | 4167 | testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4168 | pIndex->aiColumn[i] = x; |
| 4169 | pIndex->azColl[i] = pPk->azColl[j]; |
| 4170 | pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
| 4171 | i++; |
| 4172 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4173 | } |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 4174 | assert( i==pIndex->nColumn ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4175 | }else{ |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 4176 | pIndex->aiColumn[i] = XN_ROWID; |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 4177 | pIndex->azColl[i] = sqlite3StrBINARY; |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 4178 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4179 | sqlite3DefaultRowEst(pIndex); |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 4180 | if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4181 | |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4182 | /* If this index contains every column of its table, then mark |
| 4183 | ** it as a covering index */ |
| 4184 | assert( HasRowid(pTab) |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 4185 | || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); |
drh | 1fe3ac7 | 2018-06-09 01:12:08 +0000 | [diff] [blame] | 4186 | recomputeColumnsNotIndexed(pIndex); |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4187 | if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ |
| 4188 | pIndex->isCovering = 1; |
| 4189 | for(j=0; j<pTab->nCol; j++){ |
| 4190 | if( j==pTab->iPKey ) continue; |
drh | b9bcf7c | 2019-10-19 13:29:10 +0000 | [diff] [blame] | 4191 | if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; |
dan | f769cd6 | 2016-02-24 20:16:28 +0000 | [diff] [blame] | 4192 | pIndex->isCovering = 0; |
| 4193 | break; |
| 4194 | } |
| 4195 | } |
| 4196 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4197 | if( pTab==pParse->pNewTable ){ |
| 4198 | /* This routine has been called to create an automatic index as a |
| 4199 | ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
| 4200 | ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
| 4201 | ** i.e. one of: |
| 4202 | ** |
| 4203 | ** CREATE TABLE t(x PRIMARY KEY, y); |
| 4204 | ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
| 4205 | ** |
| 4206 | ** Either way, check to see if the table already has such an index. If |
| 4207 | ** so, don't bother creating this one. This only applies to |
| 4208 | ** automatically created indices. Users can do as they wish with |
| 4209 | ** explicit indices. |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4210 | ** |
| 4211 | ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent |
| 4212 | ** (and thus suppressing the second one) even if they have different |
| 4213 | ** sort orders. |
| 4214 | ** |
| 4215 | ** If there are different collating sequences or if the columns of |
| 4216 | ** the constraint occur in different orders, then the constraints are |
| 4217 | ** considered distinct and both result in separate indices. |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4218 | */ |
| 4219 | Index *pIdx; |
| 4220 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 4221 | int k; |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4222 | assert( IsUniqueIndex(pIdx) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4223 | assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4224 | assert( IsUniqueIndex(pIndex) ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4225 | |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4226 | if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
| 4227 | for(k=0; k<pIdx->nKeyCol; k++){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4228 | const char *z1; |
| 4229 | const char *z2; |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 4230 | assert( pIdx->aiColumn[k]>=0 ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4231 | if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4232 | z1 = pIdx->azColl[k]; |
| 4233 | z2 = pIndex->azColl[k]; |
drh | c41c132 | 2016-02-11 13:30:36 +0000 | [diff] [blame] | 4234 | if( sqlite3StrICmp(z1, z2) ) break; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4235 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 4236 | if( k==pIdx->nKeyCol ){ |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 4237 | if( pIdx->onError!=pIndex->onError ){ |
| 4238 | /* This constraint creates the same index as a previous |
| 4239 | ** constraint specified somewhere in the CREATE TABLE statement. |
| 4240 | ** However the ON CONFLICT clauses are different. If both this |
| 4241 | ** constraint and the previous equivalent constraint have explicit |
| 4242 | ** ON CONFLICT clauses this is an error. Otherwise, use the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 4243 | ** explicitly specified behavior for the index. |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 4244 | */ |
| 4245 | if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
| 4246 | sqlite3ErrorMsg(pParse, |
| 4247 | "conflicting ON CONFLICT clauses specified", 0); |
| 4248 | } |
| 4249 | if( pIdx->onError==OE_Default ){ |
| 4250 | pIdx->onError = pIndex->onError; |
| 4251 | } |
| 4252 | } |
drh | 273bfe9 | 2016-06-02 16:22:53 +0000 | [diff] [blame] | 4253 | if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; |
drh | 885eeb6 | 2019-01-09 02:02:24 +0000 | [diff] [blame] | 4254 | if( IN_RENAME_OBJECT ){ |
| 4255 | pIndex->pNext = pParse->pNewIndex; |
| 4256 | pParse->pNewIndex = pIndex; |
| 4257 | pIndex = 0; |
| 4258 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4259 | goto exit_create_index; |
| 4260 | } |
| 4261 | } |
| 4262 | } |
| 4263 | |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4264 | if( !IN_RENAME_OBJECT ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 4265 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4266 | /* Link the new Index structure to its table and to the other |
| 4267 | ** in-memory database structures. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4268 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4269 | assert( pParse->nErr==0 ); |
| 4270 | if( db->init.busy ){ |
| 4271 | Index *p; |
| 4272 | assert( !IN_SPECIAL_PARSE ); |
| 4273 | assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4274 | if( pTblName!=0 ){ |
| 4275 | pIndex->tnum = db->init.newTnum; |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 4276 | if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ |
drh | 8bf4126 | 2019-01-30 19:50:07 +0000 | [diff] [blame] | 4277 | sqlite3ErrorMsg(pParse, "invalid rootpage"); |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 4278 | pParse->rc = SQLITE_CORRUPT_BKPT; |
| 4279 | goto exit_create_index; |
| 4280 | } |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4281 | } |
dan | da7a4c0 | 2019-01-30 19:12:13 +0000 | [diff] [blame] | 4282 | p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
| 4283 | pIndex->zName, pIndex); |
| 4284 | if( p ){ |
| 4285 | assert( p==pIndex ); /* Malloc must have failed */ |
| 4286 | sqlite3OomFault(db); |
| 4287 | goto exit_create_index; |
| 4288 | } |
| 4289 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4290 | } |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4291 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4292 | /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
| 4293 | ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
| 4294 | ** 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] | 4295 | ** the index in the sqlite_schema table and populate the index with |
| 4296 | ** 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] | 4297 | ** table to parse the schema, or if this index is the PRIMARY KEY index |
| 4298 | ** of a WITHOUT ROWID table. |
| 4299 | ** |
| 4300 | ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY |
| 4301 | ** or UNIQUE index in a CREATE TABLE statement. Since the table |
| 4302 | ** has just been created, it contains no data and the index initialization |
| 4303 | ** step can be skipped. |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4304 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4305 | else if( HasRowid(pTab) || pTblName!=0 ){ |
| 4306 | Vdbe *v; |
| 4307 | char *zStmt; |
| 4308 | int iMem = ++pParse->nMem; |
drh | 063336a | 2004-11-05 20:58:39 +0000 | [diff] [blame] | 4309 | |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4310 | v = sqlite3GetVdbe(pParse); |
| 4311 | if( v==0 ) goto exit_create_index; |
| 4312 | |
| 4313 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 4314 | |
| 4315 | /* Create the rootpage for the index using CreateIndex. But before |
| 4316 | ** doing so, code a Noop instruction and store its address in |
| 4317 | ** Index.tnum. This is required in case this index is actually a |
| 4318 | ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In |
| 4319 | ** that case the convertToWithoutRowidTable() routine will replace |
| 4320 | ** the Noop with a Goto to jump over the VDBE code generated below. */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4321 | pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4322 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); |
| 4323 | |
| 4324 | /* Gather the complete text of the CREATE INDEX statement into |
| 4325 | ** the zStmt variable |
| 4326 | */ |
drh | 55f66b3 | 2019-07-16 19:44:32 +0000 | [diff] [blame] | 4327 | assert( pName!=0 || pStart==0 ); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4328 | if( pStart ){ |
| 4329 | int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; |
| 4330 | if( pName->z[n-1]==';' ) n--; |
| 4331 | /* A named index with an explicit CREATE INDEX statement */ |
| 4332 | zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", |
| 4333 | onError==OE_None ? "" : " UNIQUE", n, pName->z); |
| 4334 | }else{ |
| 4335 | /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
| 4336 | /* zStmt = sqlite3MPrintf(""); */ |
| 4337 | zStmt = 0; |
| 4338 | } |
| 4339 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 4340 | /* Add an entry in sqlite_schema for this index |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4341 | */ |
| 4342 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 4343 | "INSERT INTO %Q." DFLT_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", |
| 4344 | db->aDb[iDb].zDbSName, |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4345 | pIndex->zName, |
| 4346 | pTab->zName, |
| 4347 | iMem, |
| 4348 | zStmt |
| 4349 | ); |
| 4350 | sqlite3DbFree(db, zStmt); |
| 4351 | |
| 4352 | /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 4353 | ** to invalidate all pre-compiled statements. |
| 4354 | */ |
| 4355 | if( pTblName ){ |
| 4356 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 4357 | sqlite3ChangeCookie(pParse, iDb); |
| 4358 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 4359 | sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0); |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4360 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); |
| 4361 | } |
| 4362 | |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 4363 | sqlite3VdbeJumpHere(v, (int)pIndex->tnum); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4364 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4365 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4366 | if( db->init.busy || pTblName==0 ){ |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4367 | pIndex->pNext = pTab->pIndex; |
| 4368 | pTab->pIndex = pIndex; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 4369 | pIndex = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4370 | } |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4371 | else if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4372 | assert( pParse->pNewIndex==0 ); |
| 4373 | pParse->pNewIndex = pIndex; |
| 4374 | pIndex = 0; |
| 4375 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 4376 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4377 | /* Clean up before exiting */ |
| 4378 | exit_create_index: |
dan | cf8f289 | 2018-08-09 20:47:01 +0000 | [diff] [blame] | 4379 | if( pIndex ) sqlite3FreeIndex(db, pIndex); |
drh | 97060e5 | 2021-03-21 17:52:47 +0000 | [diff] [blame] | 4380 | if( pTab ){ |
| 4381 | /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list. |
| 4382 | ** The list was already ordered when this routine was entered, so at this |
| 4383 | ** point at most a single index (the newly added index) will be out of |
| 4384 | ** order. So we have to reorder at most one index. */ |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4385 | Index **ppFrom = &pTab->pIndex; |
| 4386 | Index *pThis; |
| 4387 | for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ |
| 4388 | Index *pNext; |
| 4389 | if( pThis->onError!=OE_Replace ) continue; |
| 4390 | while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ |
| 4391 | *ppFrom = pNext; |
| 4392 | pThis->pNext = pNext->pNext; |
| 4393 | pNext->pNext = pThis; |
| 4394 | ppFrom = &pNext->pNext; |
| 4395 | } |
| 4396 | break; |
| 4397 | } |
drh | 97060e5 | 2021-03-21 17:52:47 +0000 | [diff] [blame] | 4398 | #ifdef SQLITE_DEBUG |
| 4399 | /* Verify that all REPLACE indexes really are now at the end |
| 4400 | ** of the index list. In other words, no other index type ever |
| 4401 | ** comes after a REPLACE index on the list. */ |
| 4402 | for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){ |
| 4403 | assert( pThis->onError!=OE_Replace |
| 4404 | || pThis->pNext==0 |
| 4405 | || pThis->pNext->onError==OE_Replace ); |
| 4406 | } |
| 4407 | #endif |
drh | d35bdd6 | 2019-12-15 02:49:32 +0000 | [diff] [blame] | 4408 | } |
drh | 1fe0537 | 2013-07-31 18:12:26 +0000 | [diff] [blame] | 4409 | sqlite3ExprDelete(db, pPIWhere); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4410 | sqlite3ExprListDelete(db, pList); |
| 4411 | sqlite3SrcListDelete(db, pTblName); |
| 4412 | sqlite3DbFree(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4416 | ** Fill the Index.aiRowEst[] array with default information - information |
drh | 91124b3 | 2005-08-18 18:15:05 +0000 | [diff] [blame] | 4417 | ** to be used when we have not run the ANALYZE command. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4418 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4419 | ** aiRowEst[0] is supposed to contain the number of elements in the index. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4420 | ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 4421 | ** number of rows in the table that match any particular value of the |
| 4422 | ** first column of the index. aiRowEst[2] is an estimate of the number |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4423 | ** of rows that match any particular combination of the first 2 columns |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4424 | ** of the index. And so forth. It must always be the case that |
| 4425 | * |
| 4426 | ** aiRowEst[N]<=aiRowEst[N-1] |
| 4427 | ** aiRowEst[N]>=1 |
| 4428 | ** |
| 4429 | ** Apart from that, we have little to go on besides intuition as to |
| 4430 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 4431 | ** are based on typical values found in actual indices. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4432 | */ |
| 4433 | void sqlite3DefaultRowEst(Index *pIdx){ |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4434 | /* 10, 9, 8, 7, 6 */ |
| 4435 | static const LogEst aVal[] = { 33, 32, 30, 28, 26 }; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4436 | LogEst *a = pIdx->aiRowLogEst; |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4437 | LogEst x; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4438 | int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4439 | int i; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4440 | |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 4441 | /* Indexes with default row estimates should not have stat1 data */ |
| 4442 | assert( !pIdx->hasStat1 ); |
| 4443 | |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4444 | /* Set the first entry (number of rows in the index) to the estimated |
drh | 8dc570b | 2016-06-08 18:07:21 +0000 | [diff] [blame] | 4445 | ** 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] | 4446 | ** for a partial index. |
| 4447 | ** |
| 4448 | ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 |
| 4449 | ** table but other parts we are having to guess at, then do not let the |
| 4450 | ** estimated number of rows in the table be less than 1000 (LogEst 99). |
| 4451 | ** 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] | 4452 | ** stat1 data to be ignored by the query planner. |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4453 | */ |
| 4454 | x = pIdx->pTable->nRowLogEst; |
| 4455 | assert( 99==sqlite3LogEst(1000) ); |
| 4456 | if( x<99 ){ |
| 4457 | pIdx->pTable->nRowLogEst = x = 99; |
| 4458 | } |
drh | 5f086dd | 2021-04-29 13:37:36 +0000 | [diff] [blame] | 4459 | if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } |
drh | 56c65c9 | 2020-05-28 00:45:16 +0000 | [diff] [blame] | 4460 | a[0] = x; |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4461 | |
| 4462 | /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
| 4463 | ** 6 and each subsequent value (if any) is 5. */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 4464 | memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4465 | for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ |
| 4466 | a[i] = 23; assert( 23==sqlite3LogEst(5) ); |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 4467 | } |
dan | 264d2b9 | 2014-04-29 19:01:57 +0000 | [diff] [blame] | 4468 | |
| 4469 | assert( 0==sqlite3LogEst(1) ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 4470 | if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 4474 | ** This routine will drop an existing named index. This routine |
| 4475 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4476 | */ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4477 | void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4478 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4479 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 4480 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4481 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4482 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4483 | assert( pParse->nErr==0 ); /* Never called with prior errors */ |
| 4484 | if( db->mallocFailed ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 4485 | goto exit_drop_index; |
| 4486 | } |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4487 | assert( pName->nSrc==1 ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 4488 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 4489 | goto exit_drop_index; |
| 4490 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4491 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4492 | if( pIndex==0 ){ |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4493 | if( !ifExists ){ |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 4494 | sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 4495 | }else{ |
| 4496 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase); |
drh | 31da7be | 2021-05-13 18:24:22 +0000 | [diff] [blame] | 4497 | sqlite3ForceNotReadOnly(pParse); |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 4498 | } |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 4499 | pParse->checkSchema = 1; |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4500 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4501 | } |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4502 | if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4503 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 4504 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4505 | goto exit_drop_index; |
| 4506 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4507 | iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4508 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 4509 | { |
| 4510 | int code = SQLITE_DROP_INDEX; |
| 4511 | Table *pTab = pIndex->pTable; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 4512 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4513 | const char *zTab = SCHEMA_TABLE(iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4514 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4515 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4516 | } |
drh | 93fd542 | 2021-06-14 20:41:20 +0000 | [diff] [blame] | 4517 | if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4518 | if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4519 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4520 | } |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 4521 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 4522 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4523 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 4524 | /* Generate code to remove the index and from the schema table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4525 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4526 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 +0000 | [diff] [blame] | 4527 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4528 | sqlite3NestedParse(pParse, |
drh | 346a70c | 2020-06-15 20:27:35 +0000 | [diff] [blame] | 4529 | "DELETE FROM %Q." DFLT_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
| 4530 | db->aDb[iDb].zDbSName, pIndex->zName |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4531 | ); |
drh | a5ae4c3 | 2011-08-07 01:31:52 +0000 | [diff] [blame] | 4532 | sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4533 | sqlite3ChangeCookie(pParse, iDb); |
drh | b17131a | 2004-11-05 22:18:49 +0000 | [diff] [blame] | 4534 | destroyRootPage(pParse, pIndex->tnum, iDb); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 4535 | sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 4538 | exit_drop_index: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4539 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | /* |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4543 | ** pArray is a pointer to an array of objects. Each object in the |
| 4544 | ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() |
| 4545 | ** 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] | 4546 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4547 | ** When this function is called, *pnEntry contains the current size of |
| 4548 | ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes |
| 4549 | ** in total). |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4550 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4551 | ** If the realloc() is successful (i.e. if no OOM condition occurs), the |
| 4552 | ** space allocated for the new object is zeroed, *pnEntry updated to |
| 4553 | ** reflect the new size of the array and a pointer to the new allocation |
| 4554 | ** 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] | 4555 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 +0000 | [diff] [blame] | 4556 | ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains |
| 4557 | ** unchanged and a copy of pArray returned. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4558 | */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4559 | void *sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4560 | sqlite3 *db, /* Connection to notify of malloc failures */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4561 | void *pArray, /* Array of objects. Might be reallocated */ |
| 4562 | int szEntry, /* Size of each object in the array */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4563 | int *pnEntry, /* Number of objects currently in use */ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4564 | int *pIdx /* Write the index of a new slot here */ |
| 4565 | ){ |
| 4566 | char *z; |
drh | f6ad201 | 2019-04-13 14:07:57 +0000 | [diff] [blame] | 4567 | sqlite3_int64 n = *pIdx = *pnEntry; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4568 | if( (n & (n-1))==0 ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 4569 | sqlite3_int64 sz = (n==0) ? 1 : 2*n; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4570 | void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4571 | if( pNew==0 ){ |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4572 | *pIdx = -1; |
| 4573 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4574 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4575 | pArray = pNew; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4576 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4577 | z = (char*)pArray; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 4578 | memset(&z[n * szEntry], 0, szEntry); |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4579 | ++*pnEntry; |
| 4580 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4584 | ** Append a new element to the given IdList. Create a new IdList if |
| 4585 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 4586 | ** |
| 4587 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4588 | */ |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 4589 | IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ |
| 4590 | sqlite3 *db = pParse->db; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4591 | int i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4592 | if( pList==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4593 | pList = sqlite3DbMallocZero(db, sizeof(IdList) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4594 | if( pList==0 ) return 0; |
| 4595 | } |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4596 | pList->a = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4597 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4598 | pList->a, |
| 4599 | sizeof(pList->a[0]), |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4600 | &pList->nId, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 4601 | &i |
| 4602 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4603 | if( i<0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4604 | sqlite3IdListDelete(db, pList); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4605 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4606 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4607 | pList->a[i].zName = sqlite3NameFromToken(db, pToken); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4608 | if( IN_RENAME_OBJECT && pList->a[i].zName ){ |
dan | 07e9523 | 2018-08-21 16:32:53 +0000 | [diff] [blame] | 4609 | sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); |
dan | 5496d6a | 2018-08-13 17:14:26 +0000 | [diff] [blame] | 4610 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4611 | return pList; |
| 4612 | } |
| 4613 | |
| 4614 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4615 | ** Delete an IdList. |
| 4616 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4617 | void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4618 | int i; |
| 4619 | if( pList==0 ) return; |
| 4620 | for(i=0; i<pList->nId; i++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4621 | sqlite3DbFree(db, pList->a[i].zName); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4622 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4623 | sqlite3DbFree(db, pList->a); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4624 | sqlite3DbFreeNN(db, pList); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | /* |
| 4628 | ** Return the index in pList of the identifier named zId. Return -1 |
| 4629 | ** if not found. |
| 4630 | */ |
| 4631 | int sqlite3IdListIndex(IdList *pList, const char *zName){ |
| 4632 | int i; |
| 4633 | if( pList==0 ) return -1; |
| 4634 | for(i=0; i<pList->nId; i++){ |
| 4635 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 4636 | } |
| 4637 | return -1; |
| 4638 | } |
| 4639 | |
| 4640 | /* |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4641 | ** Maximum size of a SrcList object. |
| 4642 | ** The SrcList object is used to represent the FROM clause of a |
| 4643 | ** SELECT statement, and the query planner cannot deal with more |
| 4644 | ** than 64 tables in a join. So any value larger than 64 here |
| 4645 | ** is sufficient for most uses. Smaller values, like say 10, are |
| 4646 | ** appropriate for small and memory-limited applications. |
| 4647 | */ |
| 4648 | #ifndef SQLITE_MAX_SRCLIST |
| 4649 | # define SQLITE_MAX_SRCLIST 200 |
| 4650 | #endif |
| 4651 | |
| 4652 | /* |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4653 | ** Expand the space allocated for the given SrcList object by |
| 4654 | ** creating nExtra new slots beginning at iStart. iStart is zero based. |
| 4655 | ** New slots are zeroed. |
| 4656 | ** |
| 4657 | ** For example, suppose a SrcList initially contains two entries: A,B. |
| 4658 | ** To append 3 new entries onto the end, do this: |
| 4659 | ** |
| 4660 | ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); |
| 4661 | ** |
| 4662 | ** After the call above it would contain: A, B, nil, nil, nil. |
| 4663 | ** If the iStart argument had been 1 instead of 2, then the result |
| 4664 | ** would have been: A, nil, nil, nil, B. To prepend the new slots, |
| 4665 | ** the iStart value would be 0. The result then would |
| 4666 | ** be: nil, nil, nil, A, B. |
| 4667 | ** |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4668 | ** If a memory allocation fails or the SrcList becomes too large, leave |
| 4669 | ** the original SrcList unchanged, return NULL, and leave an error message |
| 4670 | ** in pParse. |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4671 | */ |
| 4672 | SrcList *sqlite3SrcListEnlarge( |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4673 | Parse *pParse, /* Parsing context into which errors are reported */ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4674 | SrcList *pSrc, /* The SrcList to be enlarged */ |
| 4675 | int nExtra, /* Number of new slots to add to pSrc->a[] */ |
| 4676 | int iStart /* Index in pSrc->a[] of first new slot */ |
| 4677 | ){ |
| 4678 | int i; |
| 4679 | |
| 4680 | /* Sanity checking on calling parameters */ |
| 4681 | assert( iStart>=0 ); |
| 4682 | assert( nExtra>=1 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4683 | assert( pSrc!=0 ); |
| 4684 | assert( iStart<=pSrc->nSrc ); |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4685 | |
| 4686 | /* Allocate additional space if needed */ |
drh | fc5717c | 2014-03-05 19:04:46 +0000 | [diff] [blame] | 4687 | if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4688 | SrcList *pNew; |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 4689 | sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4690 | sqlite3 *db = pParse->db; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4691 | |
| 4692 | if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4693 | sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", |
| 4694 | SQLITE_MAX_SRCLIST); |
| 4695 | return 0; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 +0000 | [diff] [blame] | 4696 | } |
| 4697 | if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4698 | pNew = sqlite3DbRealloc(db, pSrc, |
| 4699 | sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); |
| 4700 | if( pNew==0 ){ |
| 4701 | assert( db->mallocFailed ); |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4702 | return 0; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4703 | } |
| 4704 | pSrc = pNew; |
drh | d0ee3a1 | 2019-02-06 01:18:36 +0000 | [diff] [blame] | 4705 | pSrc->nAlloc = nAlloc; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | /* Move existing slots that come after the newly inserted slots |
| 4709 | ** out of the way */ |
| 4710 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| 4711 | pSrc->a[i+nExtra] = pSrc->a[i]; |
| 4712 | } |
drh | 6d1626e | 2014-03-05 15:52:43 +0000 | [diff] [blame] | 4713 | pSrc->nSrc += nExtra; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4714 | |
| 4715 | /* Zero the newly allocated slots */ |
| 4716 | memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
| 4717 | for(i=iStart; i<iStart+nExtra; i++){ |
| 4718 | pSrc->a[i].iCursor = -1; |
| 4719 | } |
| 4720 | |
| 4721 | /* Return a pointer to the enlarged SrcList */ |
| 4722 | return pSrc; |
| 4723 | } |
| 4724 | |
| 4725 | |
| 4726 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4727 | ** 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] | 4728 | ** 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] | 4729 | ** |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4730 | ** A SrcList is returned, or NULL if there is an OOM error or if the |
| 4731 | ** SrcList grows to large. The returned |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4732 | ** SrcList might be the same as the SrcList that was input or it might be |
| 4733 | ** a new one. If an OOM error does occurs, then the prior value of pList |
| 4734 | ** that is input to this routine is automatically freed. |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4735 | ** |
| 4736 | ** If pDatabase is not null, it means that the table has an optional |
| 4737 | ** database name prefix. Like this: "database.table". The pDatabase |
| 4738 | ** points to the table name and the pTable points to the database name. |
| 4739 | ** The SrcList.a[].zName field is filled with the table name which might |
| 4740 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
| 4741 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 4742 | ** or with NULL if no database is specified. |
| 4743 | ** |
| 4744 | ** In other words, if call like this: |
| 4745 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4746 | ** sqlite3SrcListAppend(D,A,B,0); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4747 | ** |
| 4748 | ** Then B is a table name and the database name is unspecified. If called |
| 4749 | ** like this: |
| 4750 | ** |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4751 | ** sqlite3SrcListAppend(D,A,B,C); |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4752 | ** |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4753 | ** Then C is the table name and B is the database name. If C is defined |
| 4754 | ** then so is B. In other words, we never have a case where: |
| 4755 | ** |
| 4756 | ** sqlite3SrcListAppend(D,A,0,C); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 4757 | ** |
| 4758 | ** Both pTable and pDatabase are assumed to be quoted. They are dequoted |
| 4759 | ** before being added to the SrcList. |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4760 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4761 | SrcList *sqlite3SrcListAppend( |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4762 | Parse *pParse, /* Parsing context, in which errors are reported */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4763 | SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ |
| 4764 | Token *pTable, /* Table to append */ |
| 4765 | Token *pDatabase /* Database of the table */ |
| 4766 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4767 | SrcItem *pItem; |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4768 | sqlite3 *db; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4769 | assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4770 | assert( pParse!=0 ); |
| 4771 | assert( pParse->db!=0 ); |
| 4772 | db = pParse->db; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4773 | if( pList==0 ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4774 | pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4775 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 4776 | pList->nAlloc = 1; |
drh | ac178b3 | 2016-12-14 11:14:13 +0000 | [diff] [blame] | 4777 | pList->nSrc = 1; |
| 4778 | memset(&pList->a[0], 0, sizeof(pList->a[0])); |
| 4779 | pList->a[0].iCursor = -1; |
| 4780 | }else{ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4781 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); |
| 4782 | if( pNew==0 ){ |
| 4783 | sqlite3SrcListDelete(db, pList); |
| 4784 | return 0; |
| 4785 | }else{ |
| 4786 | pList = pNew; |
| 4787 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4788 | } |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4789 | pItem = &pList->a[pList->nSrc-1]; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4790 | if( pDatabase && pDatabase->z==0 ){ |
| 4791 | pDatabase = 0; |
| 4792 | } |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4793 | if( pDatabase ){ |
drh | 169a689 | 2017-07-06 01:02:09 +0000 | [diff] [blame] | 4794 | pItem->zName = sqlite3NameFromToken(db, pDatabase); |
| 4795 | pItem->zDatabase = sqlite3NameFromToken(db, pTable); |
| 4796 | }else{ |
| 4797 | pItem->zName = sqlite3NameFromToken(db, pTable); |
| 4798 | pItem->zDatabase = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 4799 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4800 | return pList; |
| 4801 | } |
| 4802 | |
| 4803 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 4804 | ** Assign VdbeCursor index numbers to all tables in a SrcList |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4805 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4806 | void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4807 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4808 | SrcItem *pItem; |
drh | 9da977f | 2021-04-20 12:14:12 +0000 | [diff] [blame] | 4809 | assert( pList || pParse->db->mallocFailed ); |
| 4810 | if( ALWAYS(pList) ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4811 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 4812 | if( pItem->iCursor>=0 ) continue; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 4813 | pItem->iCursor = pParse->nTab++; |
| 4814 | if( pItem->pSelect ){ |
| 4815 | sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); |
| 4816 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | } |
| 4820 | |
| 4821 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4822 | ** Delete an entire SrcList including all its substructure. |
| 4823 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4824 | void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4825 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4826 | SrcItem *pItem; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4827 | if( pList==0 ) return; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 4828 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4829 | if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4830 | sqlite3DbFree(db, pItem->zName); |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4831 | if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias); |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4832 | if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
| 4833 | if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 4834 | sqlite3DeleteTable(db, pItem->pTab); |
drh | 45d827c | 2020-08-15 23:48:22 +0000 | [diff] [blame] | 4835 | if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect); |
| 4836 | if( pItem->pOn ) sqlite3ExprDelete(db, pItem->pOn); |
| 4837 | if( pItem->pUsing ) sqlite3IdListDelete(db, pItem->pUsing); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4838 | } |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 4839 | sqlite3DbFreeNN(db, pList); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
drh | 982cef7 | 2000-05-30 16:27:03 +0000 | [diff] [blame] | 4842 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4843 | ** This routine is called by the parser to add a new term to the |
| 4844 | ** end of a growing FROM clause. The "p" parameter is the part of |
| 4845 | ** the FROM clause that has already been constructed. "p" is NULL |
| 4846 | ** if this is the first term of the FROM clause. pTable and pDatabase |
| 4847 | ** are the name of the table and database named in the FROM clause term. |
| 4848 | ** pDatabase is NULL if the database name qualifier is missing - the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4849 | ** usual case. If the term has an alias, then pAlias points to the |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4850 | ** alias token. If the term is a subquery, then pSubquery is the |
| 4851 | ** SELECT statement that the subquery encodes. The pTable and |
| 4852 | ** pDatabase parameters are NULL for subqueries. The pOn and pUsing |
| 4853 | ** parameters are the content of the ON and USING clauses. |
| 4854 | ** |
| 4855 | ** Return a new SrcList which encodes is the FROM with the new |
| 4856 | ** term added. |
| 4857 | */ |
| 4858 | SrcList *sqlite3SrcListAppendFromTerm( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4859 | Parse *pParse, /* Parsing context */ |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4860 | SrcList *p, /* The left part of the FROM clause already seen */ |
| 4861 | Token *pTable, /* Name of the table to add to the FROM clause */ |
| 4862 | Token *pDatabase, /* Name of the database containing pTable */ |
| 4863 | Token *pAlias, /* The right-hand side of the AS subexpression */ |
| 4864 | Select *pSubquery, /* A subquery used in place of a table name */ |
| 4865 | Expr *pOn, /* The ON clause of a join */ |
| 4866 | IdList *pUsing /* The USING clause of a join */ |
| 4867 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4868 | SrcItem *pItem; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4869 | sqlite3 *db = pParse->db; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4870 | if( !p && (pOn || pUsing) ){ |
| 4871 | sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", |
| 4872 | (pOn ? "ON" : "USING") |
| 4873 | ); |
| 4874 | goto append_from_error; |
| 4875 | } |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4876 | p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 4877 | if( p==0 ){ |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4878 | goto append_from_error; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4879 | } |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 4880 | assert( p->nSrc>0 ); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4881 | pItem = &p->a[p->nSrc-1]; |
drh | a488ec9 | 2018-09-07 18:52:25 +0000 | [diff] [blame] | 4882 | assert( (pTable==0)==(pDatabase==0) ); |
| 4883 | assert( pItem->zName==0 || pDatabase!=0 ); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4884 | if( IN_RENAME_OBJECT && pItem->zName ){ |
drh | a488ec9 | 2018-09-07 18:52:25 +0000 | [diff] [blame] | 4885 | Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 4886 | sqlite3RenameTokenMap(pParse, pItem->zName, pToken); |
| 4887 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4888 | assert( pAlias!=0 ); |
| 4889 | if( pAlias->n ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4890 | pItem->zAlias = sqlite3NameFromToken(db, pAlias); |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4891 | } |
| 4892 | pItem->pSelect = pSubquery; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4893 | pItem->pOn = pOn; |
| 4894 | pItem->pUsing = pUsing; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4895 | return p; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 +0000 | [diff] [blame] | 4896 | |
| 4897 | append_from_error: |
| 4898 | assert( p==0 ); |
| 4899 | sqlite3ExprDelete(db, pOn); |
| 4900 | sqlite3IdListDelete(db, pUsing); |
| 4901 | sqlite3SelectDelete(db, pSubquery); |
| 4902 | return 0; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | /* |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4906 | ** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
| 4907 | ** element of the source-list passed as the second argument. |
| 4908 | */ |
| 4909 | void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 4910 | assert( pIndexedBy!=0 ); |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 4911 | if( p && pIndexedBy->n>0 ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4912 | SrcItem *pItem; |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 4913 | assert( p->nSrc>0 ); |
| 4914 | pItem = &p->a[p->nSrc-1]; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4915 | assert( pItem->fg.notIndexed==0 ); |
| 4916 | assert( pItem->fg.isIndexedBy==0 ); |
| 4917 | assert( pItem->fg.isTabFunc==0 ); |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4918 | if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
| 4919 | /* A "NOT INDEXED" clause was supplied. See parse.y |
| 4920 | ** construct "indexed_opt" for details. */ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4921 | pItem->fg.notIndexed = 1; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4922 | }else{ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4923 | pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
drh | 8abc80b | 2017-08-12 01:09:06 +0000 | [diff] [blame] | 4924 | pItem->fg.isIndexedBy = 1; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 4925 | } |
| 4926 | } |
| 4927 | } |
| 4928 | |
| 4929 | /* |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 4930 | ** Append the contents of SrcList p2 to SrcList p1 and return the resulting |
| 4931 | ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 |
| 4932 | ** are deleted by this function. |
| 4933 | */ |
| 4934 | SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){ |
dan | 8b023cf | 2020-04-30 18:28:40 +0000 | [diff] [blame] | 4935 | assert( p1 && p1->nSrc==1 ); |
| 4936 | if( p2 ){ |
| 4937 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1); |
| 4938 | if( pNew==0 ){ |
| 4939 | sqlite3SrcListDelete(pParse->db, p2); |
| 4940 | }else{ |
| 4941 | p1 = pNew; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4942 | memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem)); |
drh | 525326e | 2020-07-15 21:53:53 +0000 | [diff] [blame] | 4943 | sqlite3DbFree(pParse->db, p2); |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 4944 | } |
| 4945 | } |
| 4946 | return p1; |
| 4947 | } |
| 4948 | |
| 4949 | /* |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 4950 | ** Add the list of function arguments to the SrcList entry for a |
| 4951 | ** table-valued-function. |
| 4952 | */ |
| 4953 | void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 4954 | if( p ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4955 | SrcItem *pItem = &p->a[p->nSrc-1]; |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 4956 | assert( pItem->fg.notIndexed==0 ); |
| 4957 | assert( pItem->fg.isIndexedBy==0 ); |
| 4958 | assert( pItem->fg.isTabFunc==0 ); |
| 4959 | pItem->u1.pFuncArg = pList; |
| 4960 | pItem->fg.isTabFunc = 1; |
drh | d8b1bfc | 2015-08-20 23:21:34 +0000 | [diff] [blame] | 4961 | }else{ |
| 4962 | sqlite3ExprListDelete(pParse->db, pList); |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 4963 | } |
| 4964 | } |
| 4965 | |
| 4966 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4967 | ** When building up a FROM clause in the parser, the join operator |
| 4968 | ** is initially attached to the left operand. But the code generator |
| 4969 | ** expects the join operator to be on the right operand. This routine |
| 4970 | ** Shifts all join operators from left to right for an entire FROM |
| 4971 | ** clause. |
| 4972 | ** |
| 4973 | ** Example: Suppose the join is like this: |
| 4974 | ** |
| 4975 | ** A natural cross join B |
| 4976 | ** |
| 4977 | ** The operator is "natural cross join". The A and B operands are stored |
| 4978 | ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
| 4979 | ** operator with A. This routine shifts that operator over to B. |
| 4980 | */ |
| 4981 | void sqlite3SrcListShiftJoinType(SrcList *p){ |
drh | d017ab9 | 2011-08-23 00:01:58 +0000 | [diff] [blame] | 4982 | if( p ){ |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4983 | int i; |
| 4984 | for(i=p->nSrc-1; i>0; i--){ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4985 | p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4986 | } |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 4987 | p->a[0].fg.jointype = 0; |
drh | 61dfc31 | 2006-12-16 16:25:15 +0000 | [diff] [blame] | 4988 | } |
| 4989 | } |
| 4990 | |
| 4991 | /* |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 4992 | ** Generate VDBE code for a BEGIN statement. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 4993 | */ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4994 | void sqlite3BeginTransaction(Parse *pParse, int type){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 4995 | sqlite3 *db; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4996 | Vdbe *v; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 4997 | int i; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4998 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 4999 | assert( pParse!=0 ); |
| 5000 | db = pParse->db; |
| 5001 | assert( db!=0 ); |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5002 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ |
| 5003 | return; |
| 5004 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5005 | v = sqlite3GetVdbe(pParse); |
| 5006 | if( !v ) return; |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 5007 | if( type!=TK_DEFERRED ){ |
| 5008 | for(i=0; i<db->nDb; i++){ |
drh | 1ca037f | 2020-10-12 13:24:00 +0000 | [diff] [blame] | 5009 | int eTxnType; |
| 5010 | Btree *pBt = db->aDb[i].pBt; |
| 5011 | if( pBt && sqlite3BtreeIsReadonly(pBt) ){ |
| 5012 | eTxnType = 0; /* Read txn */ |
| 5013 | }else if( type==TK_EXCLUSIVE ){ |
| 5014 | eTxnType = 2; /* Exclusive txn */ |
| 5015 | }else{ |
| 5016 | eTxnType = 1; /* Write txn */ |
| 5017 | } |
| 5018 | sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5019 | sqlite3VdbeUsesBtree(v, i); |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 5020 | } |
| 5021 | } |
drh | b0c8865 | 2016-02-01 13:21:13 +0000 | [diff] [blame] | 5022 | sqlite3VdbeAddOp0(v, OP_AutoCommit); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | /* |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5026 | ** Generate VDBE code for a COMMIT or ROLLBACK statement. |
| 5027 | ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise |
| 5028 | ** code is generated for a COMMIT. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5029 | */ |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5030 | void sqlite3EndTransaction(Parse *pParse, int eType){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5031 | Vdbe *v; |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5032 | int isRollback; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5033 | |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5034 | assert( pParse!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5035 | assert( pParse->db!=0 ); |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5036 | assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); |
| 5037 | isRollback = eType==TK_ROLLBACK; |
| 5038 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, |
| 5039 | isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5040 | return; |
| 5041 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5042 | v = sqlite3GetVdbe(pParse); |
| 5043 | if( v ){ |
drh | 07a3b11 | 2017-07-06 01:28:02 +0000 | [diff] [blame] | 5044 | sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); |
drh | 02f75f1 | 2004-02-24 01:04:11 +0000 | [diff] [blame] | 5045 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 5046 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 5047 | |
| 5048 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 5049 | ** This function is called by the parser when it parses a command to create, |
| 5050 | ** release or rollback an SQL savepoint. |
| 5051 | */ |
| 5052 | void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 5053 | char *zName = sqlite3NameFromToken(pParse->db, pName); |
| 5054 | if( zName ){ |
| 5055 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 5056 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | 558814f | 2010-06-02 05:53:53 +0000 | [diff] [blame] | 5057 | static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 +0000 | [diff] [blame] | 5058 | assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
| 5059 | #endif |
| 5060 | if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ |
| 5061 | sqlite3DbFree(pParse->db, zName); |
| 5062 | return; |
| 5063 | } |
| 5064 | sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | /* |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5069 | ** Make sure the TEMP database is open and available for use. Return |
| 5070 | ** the number of errors. Leave any error messages in the pParse structure. |
| 5071 | */ |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 5072 | int sqlite3OpenTempDatabase(Parse *pParse){ |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5073 | sqlite3 *db = pParse->db; |
| 5074 | if( db->aDb[1].pBt==0 && !pParse->explain ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5075 | int rc; |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 5076 | Btree *pBt; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5077 | static const int flags = |
| 5078 | SQLITE_OPEN_READWRITE | |
| 5079 | SQLITE_OPEN_CREATE | |
| 5080 | SQLITE_OPEN_EXCLUSIVE | |
| 5081 | SQLITE_OPEN_DELETEONCLOSE | |
| 5082 | SQLITE_OPEN_TEMP_DB; |
| 5083 | |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 5084 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5085 | if( rc!=SQLITE_OK ){ |
| 5086 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 5087 | "file for storing temporary tables"); |
| 5088 | pParse->rc = rc; |
| 5089 | return 1; |
| 5090 | } |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 5091 | db->aDb[1].pBt = pBt; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 5092 | assert( db->aDb[1].pSchema ); |
drh | e937df8 | 2020-05-07 01:56:57 +0000 | [diff] [blame] | 5093 | if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 5094 | sqlite3OomFault(db); |
drh | 7c9c986 | 2010-01-31 14:18:21 +0000 | [diff] [blame] | 5095 | return 1; |
drh | 10a76c9 | 2010-01-26 01:25:26 +0000 | [diff] [blame] | 5096 | } |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 5097 | } |
| 5098 | return 0; |
| 5099 | } |
| 5100 | |
| 5101 | /* |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 5102 | ** Record the fact that the schema cookie will need to be verified |
| 5103 | ** for database iDb. The code to actually verify the schema cookie |
| 5104 | ** will occur at the end of the top-level VDBE and will be generated |
| 5105 | ** later, by sqlite3FinishCoding(). |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5106 | */ |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5107 | static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){ |
| 5108 | assert( iDb>=0 && iDb<pToplevel->db->nDb ); |
| 5109 | assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 ); |
drh | 099b385 | 2021-03-10 16:35:37 +0000 | [diff] [blame] | 5110 | assert( iDb<SQLITE_MAX_DB ); |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5111 | assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5112 | if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ |
| 5113 | DbMaskSet(pToplevel->cookieMask, iDb); |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 5114 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 5115 | sqlite3OpenTempDatabase(pToplevel); |
| 5116 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5117 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5118 | } |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5119 | void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
| 5120 | sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb); |
| 5121 | } |
| 5122 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5123 | |
| 5124 | /* |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 5125 | ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
| 5126 | ** attached database. Otherwise, invoke it for the database named zDb only. |
| 5127 | */ |
| 5128 | void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ |
| 5129 | sqlite3 *db = pParse->db; |
| 5130 | int i; |
| 5131 | for(i=0; i<db->nDb; i++){ |
| 5132 | Db *pDb = &db->aDb[i]; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5133 | if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ |
dan | 5796675 | 2011-04-09 17:32:58 +0000 | [diff] [blame] | 5134 | sqlite3CodeVerifySchema(pParse, i); |
| 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 5140 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 5141 | ** might change the database. |
| 5142 | ** |
| 5143 | ** This routine starts a new transaction if we are not already within |
| 5144 | ** a transaction. If we are already within a transaction, then a checkpoint |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 5145 | ** is set if the setStatement parameter is true. A checkpoint should |
drh | c977f7f | 2002-05-21 11:38:11 +0000 | [diff] [blame] | 5146 | ** be set for operations that might fail (due to a constraint) part of |
| 5147 | ** the way through and which will need to undo some writes without having to |
| 5148 | ** rollback the whole transaction. For operations where all constraints |
| 5149 | ** can be checked before any changes are made to the database, it is never |
| 5150 | ** necessary to undo a write and the checkpoint should not be set. |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 5151 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 +0000 | [diff] [blame] | 5152 | void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 5153 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 1d8f892 | 2020-08-16 00:30:44 +0000 | [diff] [blame] | 5154 | sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb); |
drh | a7ab6d8 | 2014-07-21 15:44:39 +0000 | [diff] [blame] | 5155 | DbMaskSet(pToplevel->writeMask, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5156 | pToplevel->isMultiWrite |= setStatement; |
| 5157 | } |
| 5158 | |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 5159 | /* |
| 5160 | ** Indicate that the statement currently under construction might write |
| 5161 | ** more than one entry (example: deleting one row then inserting another, |
| 5162 | ** inserting multiple rows in a table, or inserting a row and index entries.) |
| 5163 | ** If an abort occurs after some of these writes have completed, then it will |
| 5164 | ** be necessary to undo the completed writes. |
| 5165 | */ |
| 5166 | void sqlite3MultiWrite(Parse *pParse){ |
| 5167 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5168 | pToplevel->isMultiWrite = 1; |
| 5169 | } |
| 5170 | |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5171 | /* |
drh | ff738bc | 2009-09-24 00:09:58 +0000 | [diff] [blame] | 5172 | ** The code generator calls this routine if is discovers that it is |
| 5173 | ** possible to abort a statement prior to completion. In order to |
| 5174 | ** perform this abort without corrupting the database, we need to make |
| 5175 | ** sure that the statement is protected by a statement transaction. |
| 5176 | ** |
| 5177 | ** Technically, we only need to set the mayAbort flag if the |
| 5178 | ** isMultiWrite flag was previously set. There is a time dependency |
| 5179 | ** such that the abort must occur after the multiwrite. This makes |
| 5180 | ** some statements involving the REPLACE conflict resolution algorithm |
| 5181 | ** go a little faster. But taking advantage of this time dependency |
| 5182 | ** makes it more difficult to prove that the code is correct (in |
| 5183 | ** particular, it prevents us from writing an effective |
| 5184 | ** implementation of sqlite3AssertMayAbort()) and so we have chosen |
| 5185 | ** to take the safe route and skip the optimization. |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5186 | */ |
| 5187 | void sqlite3MayAbort(Parse *pParse){ |
| 5188 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5189 | pToplevel->mayAbort = 1; |
| 5190 | } |
| 5191 | |
| 5192 | /* |
| 5193 | ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
| 5194 | ** error. The onError parameter determines which (if any) of the statement |
| 5195 | ** and/or current transaction is rolled back. |
| 5196 | */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5197 | void sqlite3HaltConstraint( |
| 5198 | Parse *pParse, /* Parsing context */ |
| 5199 | int errCode, /* extended error code */ |
| 5200 | int onError, /* Constraint type */ |
| 5201 | char *p4, /* Error message */ |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5202 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 5203 | u8 p5Errmsg /* P5_ErrMsg type */ |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5204 | ){ |
drh | 289a0c8 | 2020-08-15 22:23:00 +0000 | [diff] [blame] | 5205 | Vdbe *v; |
| 5206 | assert( pParse->pVdbe!=0 ); |
| 5207 | v = sqlite3GetVdbe(pParse); |
drh | 9e5fdc4 | 2020-05-08 19:02:21 +0000 | [diff] [blame] | 5208 | assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 5209 | if( onError==OE_Abort ){ |
| 5210 | sqlite3MayAbort(pParse); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5211 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 +0000 | [diff] [blame] | 5212 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
drh | 9b34abe | 2016-01-16 15:12:35 +0000 | [diff] [blame] | 5213 | sqlite3VdbeChangeP5(v, p5Errmsg); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | /* |
| 5217 | ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. |
| 5218 | */ |
| 5219 | void sqlite3UniqueConstraint( |
| 5220 | Parse *pParse, /* Parsing context */ |
| 5221 | int onError, /* Constraint type */ |
| 5222 | Index *pIdx /* The index that triggers the constraint */ |
| 5223 | ){ |
| 5224 | char *zErr; |
| 5225 | int j; |
| 5226 | StrAccum errMsg; |
| 5227 | Table *pTab = pIdx->pTable; |
| 5228 | |
drh | 86ec1ed | 2019-04-10 00:58:07 +0000 | [diff] [blame] | 5229 | sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, |
| 5230 | pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5231 | if( pIdx->aColExpr ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 5232 | sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5233 | }else{ |
| 5234 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 5235 | char *zCol; |
| 5236 | assert( pIdx->aiColumn[j]>=0 ); |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 5237 | zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName; |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 5238 | if( j ) sqlite3_str_append(&errMsg, ", ", 2); |
| 5239 | sqlite3_str_appendall(&errMsg, pTab->zName); |
| 5240 | sqlite3_str_append(&errMsg, ".", 1); |
| 5241 | sqlite3_str_appendall(&errMsg, zCol); |
drh | 8b57642 | 2015-08-31 23:09:42 +0000 | [diff] [blame] | 5242 | } |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5243 | } |
| 5244 | zErr = sqlite3StrAccumFinish(&errMsg); |
| 5245 | sqlite3HaltConstraint(pParse, |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 5246 | IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
| 5247 | : SQLITE_CONSTRAINT_UNIQUE, |
dan | 93889d9 | 2013-11-06 16:28:59 +0000 | [diff] [blame] | 5248 | onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | |
| 5252 | /* |
| 5253 | ** Code an OP_Halt due to non-unique rowid. |
| 5254 | */ |
| 5255 | void sqlite3RowidConstraint( |
| 5256 | Parse *pParse, /* Parsing context */ |
| 5257 | int onError, /* Conflict resolution algorithm */ |
| 5258 | Table *pTab /* The table with the non-unique rowid */ |
| 5259 | ){ |
| 5260 | char *zMsg; |
| 5261 | int rc; |
| 5262 | if( pTab->iPKey>=0 ){ |
| 5263 | zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 5264 | pTab->aCol[pTab->iPKey].zCnName); |
drh | f9c8ce3 | 2013-11-05 13:33:55 +0000 | [diff] [blame] | 5265 | rc = SQLITE_CONSTRAINT_PRIMARYKEY; |
| 5266 | }else{ |
| 5267 | zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); |
| 5268 | rc = SQLITE_CONSTRAINT_ROWID; |
| 5269 | } |
| 5270 | sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, |
| 5271 | P5_ConstraintUnique); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5274 | /* |
| 5275 | ** Check to see if pIndex uses the collating sequence pColl. Return |
| 5276 | ** true if it does and false if it does not. |
| 5277 | */ |
| 5278 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5279 | static int collationMatch(const char *zColl, Index *pIndex){ |
| 5280 | int i; |
drh | 0449171 | 2009-05-13 17:21:13 +0000 | [diff] [blame] | 5281 | assert( zColl!=0 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5282 | for(i=0; i<pIndex->nColumn; i++){ |
| 5283 | const char *z = pIndex->azColl[i]; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5284 | assert( z!=0 || pIndex->aiColumn[i]<0 ); |
| 5285 | if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5286 | return 1; |
| 5287 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5288 | } |
| 5289 | return 0; |
| 5290 | } |
| 5291 | #endif |
| 5292 | |
| 5293 | /* |
| 5294 | ** Recompute all indices of pTab that use the collating sequence pColl. |
| 5295 | ** If pColl==0 then recompute all indices of pTab. |
| 5296 | */ |
| 5297 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5298 | static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
dan | e6370e9 | 2019-01-11 17:41:23 +0000 | [diff] [blame] | 5299 | if( !IsVirtual(pTab) ){ |
| 5300 | Index *pIndex; /* An index associated with pTab */ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5301 | |
dan | e6370e9 | 2019-01-11 17:41:23 +0000 | [diff] [blame] | 5302 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 5303 | if( zColl==0 || collationMatch(zColl, pIndex) ){ |
| 5304 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 5305 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5306 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5307 | } |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5308 | } |
| 5309 | } |
| 5310 | } |
| 5311 | #endif |
| 5312 | |
| 5313 | /* |
| 5314 | ** Recompute all indices of all tables in all databases where the |
| 5315 | ** indices use the collating sequence pColl. If pColl==0 then recompute |
| 5316 | ** all indices everywhere. |
| 5317 | */ |
| 5318 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5319 | static void reindexDatabases(Parse *pParse, char const *zColl){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5320 | Db *pDb; /* A single database */ |
| 5321 | int iDb; /* The database index number */ |
| 5322 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5323 | HashElem *k; /* For looping over tables in pDb */ |
| 5324 | Table *pTab; /* A table in the database */ |
| 5325 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 5326 | assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5327 | for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 5328 | assert( pDb!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5329 | for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5330 | pTab = (Table*)sqliteHashData(k); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5331 | reindexTable(pParse, pTab, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5332 | } |
| 5333 | } |
| 5334 | } |
| 5335 | #endif |
| 5336 | |
| 5337 | /* |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 5338 | ** Generate code for the REINDEX command. |
| 5339 | ** |
| 5340 | ** REINDEX -- 1 |
| 5341 | ** REINDEX <collation> -- 2 |
| 5342 | ** REINDEX ?<database>.?<tablename> -- 3 |
| 5343 | ** REINDEX ?<database>.?<indexname> -- 4 |
| 5344 | ** |
| 5345 | ** Form 1 causes all indices in all attached databases to be rebuilt. |
| 5346 | ** Form 2 rebuilds all indices in all databases that use the named |
| 5347 | ** collating function. Forms 3 and 4 rebuild the named index or all |
| 5348 | ** indices associated with the named table. |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5349 | */ |
| 5350 | #ifndef SQLITE_OMIT_REINDEX |
| 5351 | void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
| 5352 | CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
| 5353 | char *z; /* Name of a table or index */ |
| 5354 | const char *zDb; /* Name of the database */ |
| 5355 | Table *pTab; /* A table in the database */ |
| 5356 | Index *pIndex; /* An index associated with pTab */ |
| 5357 | int iDb; /* The database index number */ |
| 5358 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5359 | Token *pObjName; /* Name of the table or index to be reindexed */ |
| 5360 | |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 5361 | /* Read the database schema. If an error occurs, leave an error message |
| 5362 | ** and code in pParse and return NULL. */ |
| 5363 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e63739a | 2005-01-27 00:33:37 +0000 | [diff] [blame] | 5364 | return; |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
drh | 8af73d4 | 2009-05-13 22:58:28 +0000 | [diff] [blame] | 5367 | if( pName1==0 ){ |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5368 | reindexDatabases(pParse, 0); |
| 5369 | return; |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5370 | }else if( NEVER(pName2==0) || pName2->z==0 ){ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 5371 | char *zColl; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5372 | assert( pName1->z ); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 5373 | zColl = sqlite3NameFromToken(pParse->db, pName1); |
| 5374 | if( !zColl ) return; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 5375 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5376 | if( pColl ){ |
drh | d300171 | 2009-05-12 17:46:53 +0000 | [diff] [blame] | 5377 | reindexDatabases(pParse, zColl); |
| 5378 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5379 | return; |
| 5380 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5381 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5382 | } |
| 5383 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 5384 | if( iDb<0 ) return; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5385 | z = sqlite3NameFromToken(db, pObjName); |
drh | 84f3112 | 2007-05-12 15:00:14 +0000 | [diff] [blame] | 5386 | if( z==0 ) return; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5387 | zDb = db->aDb[iDb].zDbSName; |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5388 | pTab = sqlite3FindTable(db, z, zDb); |
| 5389 | if( pTab ){ |
| 5390 | reindexTable(pParse, pTab, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5391 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5392 | return; |
| 5393 | } |
| 5394 | pIndex = sqlite3FindIndex(db, z, zDb); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5395 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 +0000 | [diff] [blame] | 5396 | if( pIndex ){ |
| 5397 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5398 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5399 | return; |
| 5400 | } |
| 5401 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| 5402 | } |
| 5403 | #endif |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5404 | |
| 5405 | /* |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5406 | ** Return a KeyInfo structure that is appropriate for the given Index. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5407 | ** |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5408 | ** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
| 5409 | ** when it has finished using it. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5410 | */ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5411 | KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5412 | int i; |
| 5413 | int nCol = pIdx->nColumn; |
| 5414 | int nKey = pIdx->nKeyCol; |
| 5415 | KeyInfo *pKey; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5416 | if( pParse->nErr ) return 0; |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5417 | if( pIdx->uniqNotNull ){ |
| 5418 | pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); |
| 5419 | }else{ |
| 5420 | pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); |
drh | 41e13e1 | 2013-11-07 14:09:39 +0000 | [diff] [blame] | 5421 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5422 | if( pKey ){ |
| 5423 | assert( sqlite3KeyInfoIsWriteable(pKey) ); |
| 5424 | for(i=0; i<nCol; i++){ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 5425 | const char *zColl = pIdx->azColl[i]; |
| 5426 | pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5427 | sqlite3LocateCollSeq(pParse, zColl); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 5428 | pKey->aSortFlags[i] = pIdx->aSortOrder[i]; |
| 5429 | assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 5430 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5431 | if( pParse->nErr ){ |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 5432 | assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); |
| 5433 | if( pIdx->bNoQuery==0 ){ |
| 5434 | /* Deactivate the index because it contains an unknown collating |
| 5435 | ** sequence. The only way to reactive the index is to reload the |
| 5436 | ** schema. Adding the missing collating sequence later does not |
| 5437 | ** reactive the index. The application had the chance to register |
| 5438 | ** the missing index using the collation-needed callback. For |
| 5439 | ** simplicity, SQLite will not give the application a second chance. |
| 5440 | */ |
| 5441 | pIdx->bNoQuery = 1; |
| 5442 | pParse->rc = SQLITE_ERROR_RETRY; |
| 5443 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5444 | sqlite3KeyInfoUnref(pKey); |
| 5445 | pKey = 0; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5446 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5447 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 +0000 | [diff] [blame] | 5448 | return pKey; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5449 | } |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5450 | |
| 5451 | #ifndef SQLITE_OMIT_CTE |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5452 | /* |
| 5453 | ** Create a new CTE object |
| 5454 | */ |
| 5455 | Cte *sqlite3CteNew( |
| 5456 | Parse *pParse, /* Parsing context */ |
| 5457 | Token *pName, /* Name of the common-table */ |
| 5458 | ExprList *pArglist, /* Optional column name list for the table */ |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5459 | Select *pQuery, /* Query used to initialize the table */ |
| 5460 | u8 eM10d /* The MATERIALIZED flag */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5461 | ){ |
| 5462 | Cte *pNew; |
| 5463 | sqlite3 *db = pParse->db; |
| 5464 | |
| 5465 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew)); |
| 5466 | assert( pNew!=0 || db->mallocFailed ); |
| 5467 | |
| 5468 | if( db->mallocFailed ){ |
| 5469 | sqlite3ExprListDelete(db, pArglist); |
| 5470 | sqlite3SelectDelete(db, pQuery); |
| 5471 | }else{ |
| 5472 | pNew->pSelect = pQuery; |
| 5473 | pNew->pCols = pArglist; |
| 5474 | pNew->zName = sqlite3NameFromToken(pParse->db, pName); |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5475 | pNew->eM10d = eM10d; |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5476 | } |
| 5477 | return pNew; |
| 5478 | } |
| 5479 | |
| 5480 | /* |
| 5481 | ** Clear information from a Cte object, but do not deallocate storage |
| 5482 | ** for the object itself. |
| 5483 | */ |
| 5484 | static void cteClear(sqlite3 *db, Cte *pCte){ |
| 5485 | assert( pCte!=0 ); |
| 5486 | sqlite3ExprListDelete(db, pCte->pCols); |
| 5487 | sqlite3SelectDelete(db, pCte->pSelect); |
| 5488 | sqlite3DbFree(db, pCte->zName); |
| 5489 | } |
| 5490 | |
| 5491 | /* |
| 5492 | ** Free the contents of the CTE object passed as the second argument. |
| 5493 | */ |
| 5494 | void sqlite3CteDelete(sqlite3 *db, Cte *pCte){ |
| 5495 | assert( pCte!=0 ); |
| 5496 | cteClear(db, pCte); |
| 5497 | sqlite3DbFree(db, pCte); |
| 5498 | } |
| 5499 | |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5500 | /* |
| 5501 | ** This routine is invoked once per CTE by the parser while parsing a |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5502 | ** WITH clause. The CTE described by teh third argument is added to |
| 5503 | ** the WITH clause of the second argument. If the second argument is |
| 5504 | ** NULL, then a new WITH argument is created. |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5505 | */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5506 | With *sqlite3WithAdd( |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5507 | Parse *pParse, /* Parsing context */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5508 | With *pWith, /* Existing WITH clause, or NULL */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5509 | Cte *pCte /* CTE to add to the WITH clause */ |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5510 | ){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5511 | sqlite3 *db = pParse->db; |
| 5512 | With *pNew; |
| 5513 | char *zName; |
| 5514 | |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5515 | if( pCte==0 ){ |
| 5516 | return pWith; |
| 5517 | } |
| 5518 | |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5519 | /* Check that the CTE name is unique within this WITH clause. If |
| 5520 | ** not, store an error in the Parse structure. */ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5521 | zName = pCte->zName; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5522 | if( zName && pWith ){ |
| 5523 | int i; |
| 5524 | for(i=0; i<pWith->nCte; i++){ |
| 5525 | if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ |
drh | 727a99f | 2014-01-16 21:59:51 +0000 | [diff] [blame] | 5526 | sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | if( pWith ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 5532 | sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5533 | pNew = sqlite3DbRealloc(db, pWith, nByte); |
| 5534 | }else{ |
| 5535 | pNew = sqlite3DbMallocZero(db, sizeof(*pWith)); |
| 5536 | } |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 5537 | assert( (pNew!=0 && zName!=0) || db->mallocFailed ); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5538 | |
drh | b84e574 | 2016-02-05 02:42:54 +0000 | [diff] [blame] | 5539 | if( db->mallocFailed ){ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5540 | sqlite3CteDelete(db, pCte); |
dan | a9f5c13 | 2014-01-13 16:36:40 +0000 | [diff] [blame] | 5541 | pNew = pWith; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5542 | }else{ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5543 | pNew->a[pNew->nCte++] = *pCte; |
| 5544 | sqlite3DbFree(db, pCte); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | return pNew; |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5550 | /* |
| 5551 | ** Free the contents of the With object passed as the second argument. |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5552 | */ |
dan | 7d562db | 2014-01-11 19:19:36 +0000 | [diff] [blame] | 5553 | void sqlite3WithDelete(sqlite3 *db, With *pWith){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5554 | if( pWith ){ |
| 5555 | int i; |
| 5556 | for(i=0; i<pWith->nCte; i++){ |
drh | f824b41 | 2021-02-20 14:57:16 +0000 | [diff] [blame] | 5557 | cteClear(db, &pWith->a[i]); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5558 | } |
| 5559 | sqlite3DbFree(db, pWith); |
| 5560 | } |
drh | 8b47186 | 2014-01-11 13:22:17 +0000 | [diff] [blame] | 5561 | } |
| 5562 | #endif /* !defined(SQLITE_OMIT_CTE) */ |