drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 | [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 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 | [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 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 | [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 | [diff] [blame] | 15 | ** |
drh | bed8690 | 2000-06-02 13:27:59 | [diff] [blame] | 16 | ** CREATE TABLE |
| 17 | ** DROP TABLE |
| 18 | ** CREATE INDEX |
| 19 | ** DROP INDEX |
drh | 832508b | 2002-03-02 17:04:07 | [diff] [blame] | 20 | ** creating ID lists |
drh | b19a2bc | 2001-09-16 00:13:26 | [diff] [blame] | 21 | ** BEGIN TRANSACTION |
| 22 | ** COMMIT |
| 23 | ** ROLLBACK |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 24 | */ |
| 25 | #include "sqliteInt.h" |
| 26 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [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 | [diff] [blame] | 33 | int iDb; /* The database containing the table to be locked */ |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 34 | Pgno iTab; /* The root page of the table to be locked */ |
drh | e0a04a3 | 2016-12-16 01:00:21 | [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 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 40 | ** Record the fact that we want to lock a table at run-time. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 41 | ** |
drh | d698bc1 | 2006-03-23 23:33:26 | [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 | [diff] [blame] | 48 | */ |
drh | 9430506 | 2021-05-17 11:19:32 | [diff] [blame] | 49 | static SQLITE_NOINLINE void lockTable( |
drh | d698bc1 | 2006-03-23 23:33:26 | [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 | [diff] [blame] | 52 | Pgno iTab, /* Root page number of the table to be locked */ |
drh | d698bc1 | 2006-03-23 23:33:26 | [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 | [diff] [blame] | 55 | ){ |
drh | 1d8f892 | 2020-08-16 00:30:44 | [diff] [blame] | 56 | Parse *pToplevel; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 57 | int i; |
| 58 | int nBytes; |
| 59 | TableLock *p; |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 60 | assert( iDb>=0 ); |
dan | 165921a | 2009-08-28 18:53:45 | [diff] [blame] | 61 | |
drh | 1d8f892 | 2020-08-16 00:30:44 | [diff] [blame] | 62 | pToplevel = sqlite3ParseToplevel(pParse); |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 63 | for(i=0; i<pToplevel->nTableLock; i++){ |
| 64 | p = &pToplevel->aTableLock[i]; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 65 | if( p->iDb==iDb && p->iTab==iTab ){ |
| 66 | p->isWriteLock = (p->isWriteLock || isWriteLock); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 71 | assert( pToplevel->nTableLock < 0x7fff0000 ); |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 72 | nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); |
| 73 | pToplevel->aTableLock = |
| 74 | sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); |
| 75 | if( pToplevel->aTableLock ){ |
| 76 | p = &pToplevel->aTableLock[pToplevel->nTableLock++]; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 77 | p->iDb = iDb; |
| 78 | p->iTab = iTab; |
| 79 | p->isWriteLock = isWriteLock; |
drh | e0a04a3 | 2016-12-16 01:00:21 | [diff] [blame] | 80 | p->zLockName = zName; |
drh | f3a65f7 | 2007-08-22 20:18:21 | [diff] [blame] | 81 | }else{ |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 82 | pToplevel->nTableLock = 0; |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 83 | sqlite3OomFault(pToplevel->db); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 84 | } |
| 85 | } |
drh | 9430506 | 2021-05-17 11:19:32 | [diff] [blame] | 86 | void sqlite3TableLock( |
| 87 | Parse *pParse, /* Parsing context */ |
| 88 | int iDb, /* Index of the database containing the table to lock */ |
| 89 | Pgno iTab, /* Root page number of the table to be locked */ |
| 90 | u8 isWriteLock, /* True for a write lock */ |
| 91 | const char *zName /* Name of the table to be locked */ |
| 92 | ){ |
| 93 | if( iDb==1 ) return; |
| 94 | if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return; |
| 95 | lockTable(pParse, iDb, iTab, isWriteLock, zName); |
| 96 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | ** Code an OP_TableLock instruction for each table locked by the |
| 100 | ** statement (configured by calls to sqlite3TableLock()). |
| 101 | */ |
| 102 | static void codeTableLocks(Parse *pParse){ |
| 103 | int i; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 104 | Vdbe *pVdbe = pParse->pVdbe; |
drh | 289a0c8 | 2020-08-15 22:23:00 | [diff] [blame] | 105 | assert( pVdbe!=0 ); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 106 | |
| 107 | for(i=0; i<pParse->nTableLock; i++){ |
| 108 | TableLock *p = &pParse->aTableLock[i]; |
| 109 | int p1 = p->iDb; |
drh | 6a9ad3d | 2008-04-02 16:29:30 | [diff] [blame] | 110 | sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, |
drh | e0a04a3 | 2016-12-16 01:00:21 | [diff] [blame] | 111 | p->zLockName, P4_STATIC); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | #else |
| 115 | #define codeTableLocks(x) |
| 116 | #endif |
| 117 | |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 118 | /* |
drh | a7ab6d8 | 2014-07-21 15:44:39 | [diff] [blame] | 119 | ** Return TRUE if the given yDbMask object is empty - if it contains no |
| 120 | ** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero() |
| 121 | ** macros when SQLITE_MAX_ATTACHED is greater than 30. |
| 122 | */ |
| 123 | #if SQLITE_MAX_ATTACHED>30 |
| 124 | int sqlite3DbMaskAllZero(yDbMask m){ |
| 125 | int i; |
| 126 | for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0; |
| 127 | return 1; |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | /* |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 132 | ** This routine is called after a single SQL statement has been |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 133 | ** parsed and a VDBE program to execute that statement has been |
| 134 | ** prepared. This routine puts the finishing touches on the |
| 135 | ** VDBE program and resets the pParse structure for the next |
| 136 | ** parse. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 137 | ** |
| 138 | ** Note that if an error occurred, it might be the case that |
| 139 | ** no VDBE code was generated. |
| 140 | */ |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 141 | void sqlite3FinishCoding(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 142 | sqlite3 *db; |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 143 | Vdbe *v; |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 144 | int iDb, i; |
drh | b86ccfb | 2003-01-28 23:13:10 | [diff] [blame] | 145 | |
dan | f78baaf | 2012-12-06 19:37:22 | [diff] [blame] | 146 | assert( pParse->pToplevel==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 147 | db = pParse->db; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 148 | assert( db->pParse==pParse ); |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 149 | if( pParse->nested ) return; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 150 | if( pParse->nErr ){ |
drh | a5c9a70 | 2022-01-25 00:03:25 | [diff] [blame] | 151 | if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM; |
drh | d99d283 | 2015-04-17 15:58:33 | [diff] [blame] | 152 | return; |
| 153 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 154 | assert( db->mallocFailed==0 ); |
danielk1977 | 48d0d86 | 2005-02-01 03:09:52 | [diff] [blame] | 155 | |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 156 | /* Begin by generating some termination code at the end of the |
| 157 | ** vdbe program |
| 158 | */ |
drh | 02c4aa3 | 2021-02-04 13:44:42 | [diff] [blame] | 159 | v = pParse->pVdbe; |
| 160 | if( v==0 ){ |
| 161 | if( db->init.busy ){ |
| 162 | pParse->rc = SQLITE_DONE; |
| 163 | return; |
| 164 | } |
| 165 | v = sqlite3GetVdbe(pParse); |
| 166 | if( v==0 ) pParse->rc = SQLITE_ERROR; |
drh | c8af879 | 2021-01-01 22:06:17 | [diff] [blame] | 167 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 168 | assert( !pParse->isMultiWrite |
dan | f367721 | 2009-09-10 16:14:50 | [diff] [blame] | 169 | || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 170 | if( v ){ |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 171 | if( pParse->bReturning ){ |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 172 | Returning *pReturning; |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 173 | int addrRewind; |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 174 | int reg; |
| 175 | |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 176 | assert( !pParse->isCreate ); |
| 177 | pReturning = pParse->u1.d.pReturning; |
drh | 1a6bac0 | 2022-04-25 10:43:19 | [diff] [blame] | 178 | if( pReturning->nRetCol ){ |
drh | 3b26b2b | 2021-12-01 19:17:14 | [diff] [blame] | 179 | sqlite3VdbeAddOp0(v, OP_FkCheck); |
drh | 7132f43 | 2021-10-20 12:52:12 | [diff] [blame] | 180 | addrRewind = |
| 181 | sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur); |
| 182 | VdbeCoverage(v); |
| 183 | reg = pReturning->iRetReg; |
| 184 | for(i=0; i<pReturning->nRetCol; i++){ |
| 185 | sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i); |
| 186 | } |
| 187 | sqlite3VdbeAddOp2(v, OP_ResultRow, reg, i); |
| 188 | sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1); |
| 189 | VdbeCoverage(v); |
| 190 | sqlite3VdbeJumpHere(v, addrRewind); |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 191 | } |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 192 | } |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 193 | sqlite3VdbeAddOp0(v, OP_Halt); |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 194 | |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 195 | /* The cookie mask contains one bit for each database file open. |
| 196 | ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
| 197 | ** set for each database that is used. Generate code to start a |
| 198 | ** transaction on each used database and to verify the schema cookie |
| 199 | ** on each used database. |
| 200 | */ |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 201 | assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init ); |
| 202 | sqlite3VdbeJumpHere(v, 0); |
| 203 | assert( db->nDb>0 ); |
| 204 | iDb = 0; |
| 205 | do{ |
| 206 | Schema *pSchema; |
| 207 | if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue; |
| 208 | sqlite3VdbeUsesBtree(v, iDb); |
| 209 | pSchema = db->aDb[iDb].pSchema; |
| 210 | sqlite3VdbeAddOp4Int(v, |
| 211 | OP_Transaction, /* Opcode */ |
| 212 | iDb, /* P1 */ |
| 213 | DbMaskTest(pParse->writeMask,iDb), /* P2 */ |
| 214 | pSchema->schema_cookie, /* P3 */ |
| 215 | pSchema->iGeneration /* P4 */ |
| 216 | ); |
| 217 | if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1); |
| 218 | VdbeComment((v, |
| 219 | "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite)); |
| 220 | }while( ++iDb<db->nDb ); |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 | [diff] [blame] | 221 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 222 | for(i=0; i<pParse->nVtabLock; i++){ |
| 223 | char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); |
| 224 | sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); |
| 225 | } |
| 226 | pParse->nVtabLock = 0; |
danielk1977 | f9e7dda | 2006-06-16 16:08:53 | [diff] [blame] | 227 | #endif |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 228 | |
drh | 40ee729 | 2023-06-20 17:45:19 | [diff] [blame] | 229 | #ifndef SQLITE_OMIT_SHARED_CACHE |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 230 | /* Once all the cookies have been verified and transactions opened, |
| 231 | ** obtain the required table-locks. This is a no-op unless the |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 232 | ** shared-cache feature is enabled. |
| 233 | */ |
drh | 40ee729 | 2023-06-20 17:45:19 | [diff] [blame] | 234 | if( pParse->nTableLock ) codeTableLocks(pParse); |
| 235 | #endif |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 236 | |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 237 | /* Initialize any AUTOINCREMENT data structures required. |
| 238 | */ |
drh | 40ee729 | 2023-06-20 17:45:19 | [diff] [blame] | 239 | if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse); |
drh | 0b9f50d | 2009-06-23 20:28:53 | [diff] [blame] | 240 | |
dan | ef2e433 | 2023-09-09 17:53:55 | [diff] [blame] | 241 | /* Code constant expressions that were factored out of inner loops. |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 242 | */ |
| 243 | if( pParse->pConstExpr ){ |
| 244 | ExprList *pEL = pParse->pConstExpr; |
| 245 | pParse->okConstFactor = 0; |
| 246 | for(i=0; i<pEL->nExpr; i++){ |
dan | ef2e433 | 2023-09-09 17:53:55 | [diff] [blame] | 247 | assert( pEL->a[i].u.iConstExprReg>0 ); |
| 248 | sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg); |
drh | f30a969 | 2013-11-15 01:10:18 | [diff] [blame] | 249 | } |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 250 | } |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 251 | |
| 252 | if( pParse->bReturning ){ |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 253 | Returning *pRet; |
| 254 | assert( !pParse->isCreate ); |
| 255 | pRet = pParse->u1.d.pReturning; |
drh | 7bace9e | 2022-07-23 12:51:48 | [diff] [blame] | 256 | if( pRet->nRetCol ){ |
| 257 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* Finally, jump back to the beginning of the executable code. */ |
| 262 | sqlite3VdbeGoto(v, 1); |
drh | 71c697e | 2004-08-08 23:39:19 | [diff] [blame] | 263 | } |
| 264 | |
drh | 8024205 | 2004-06-09 00:48:12 | [diff] [blame] | 265 | /* Get the VDBE program ready for execution |
| 266 | */ |
drh | 1da88b5 | 2022-01-24 19:38:56 | [diff] [blame] | 267 | assert( v!=0 || pParse->nErr ); |
| 268 | assert( db->mallocFailed==0 || pParse->nErr ); |
| 269 | if( pParse->nErr==0 ){ |
drh | 3492dd7 | 2009-09-14 23:47:24 | [diff] [blame] | 270 | /* A minimum of one cursor is required if autoincrement is used |
| 271 | * See ticket [a696379c1f08866] */ |
drh | 04ab586 | 2018-12-01 21:13:41 | [diff] [blame] | 272 | assert( pParse->pAinc==0 || pParse->nTab>0 ); |
drh | 124c0b4 | 2011-06-01 18:15:55 | [diff] [blame] | 273 | sqlite3VdbeMakeReady(v, pParse); |
danielk1977 | 441daf6 | 2005-02-01 03:46:43 | [diff] [blame] | 274 | pParse->rc = SQLITE_DONE; |
drh | e294da0 | 2010-02-25 23:44:15 | [diff] [blame] | 275 | }else{ |
drh | 483750b | 2003-01-29 18:46:51 | [diff] [blame] | 276 | pParse->rc = SQLITE_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | /* |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 281 | ** Run the parser and code generator recursively in order to generate |
| 282 | ** code for the SQL statement given onto the end of the pParse context |
drh | 3edc927 | 2021-08-04 13:42:12 | [diff] [blame] | 283 | ** currently under construction. Notes: |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 284 | ** |
drh | 3edc927 | 2021-08-04 13:42:12 | [diff] [blame] | 285 | ** * The final OP_Halt is not appended and other initialization |
| 286 | ** and finalization steps are omitted because those are handling by the |
| 287 | ** outermost parser. |
| 288 | ** |
| 289 | ** * Built-in SQL functions always take precedence over application-defined |
| 290 | ** SQL functions. In other words, it is not possible to override a |
| 291 | ** built-in function. |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 292 | */ |
| 293 | void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
| 294 | va_list ap; |
| 295 | char *zSql; |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 296 | sqlite3 *db = pParse->db; |
drh | 3edc927 | 2021-08-04 13:42:12 | [diff] [blame] | 297 | u32 savedDbFlags = db->mDbFlags; |
drh | cd9af60 | 2016-09-30 22:24:29 | [diff] [blame] | 298 | char saveBuf[PARSE_TAIL_SZ]; |
drh | f197484 | 2004-11-05 03:56:00 | [diff] [blame] | 299 | |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 300 | if( pParse->nErr ) return; |
drh | 12e1eb3 | 2022-12-29 18:54:15 | [diff] [blame] | 301 | if( pParse->eParseMode ) return; |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 302 | assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
| 303 | va_start(ap, zFormat); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 304 | zSql = sqlite3VMPrintf(db, zFormat, ap); |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 305 | va_end(ap); |
drh | 73c42a1 | 2004-11-20 18:13:10 | [diff] [blame] | 306 | if( zSql==0 ){ |
drh | 480c572 | 2019-02-22 16:18:12 | [diff] [blame] | 307 | /* This can result either from an OOM or because the formatted string |
| 308 | ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set |
| 309 | ** an error */ |
| 310 | if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG; |
drh | a2b6806 | 2019-03-28 04:03:17 | [diff] [blame] | 311 | pParse->nErr++; |
drh | 480c572 | 2019-02-22 16:18:12 | [diff] [blame] | 312 | return; |
drh | 73c42a1 | 2004-11-20 18:13:10 | [diff] [blame] | 313 | } |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 314 | pParse->nested++; |
drh | cd9af60 | 2016-09-30 22:24:29 | [diff] [blame] | 315 | memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ); |
| 316 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
drh | 3edc927 | 2021-08-04 13:42:12 | [diff] [blame] | 317 | db->mDbFlags |= DBFLAG_PreferBuiltin; |
drh | 54bc638 | 2021-12-31 19:20:42 | [diff] [blame] | 318 | sqlite3RunParser(pParse, zSql); |
drh | 3edc927 | 2021-08-04 13:42:12 | [diff] [blame] | 319 | db->mDbFlags = savedDbFlags; |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 320 | sqlite3DbFree(db, zSql); |
drh | cd9af60 | 2016-09-30 22:24:29 | [diff] [blame] | 321 | memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ); |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 322 | pParse->nested--; |
| 323 | } |
| 324 | |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 325 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 | [diff] [blame] | 326 | ** Locate the in-memory structure that describes a particular database |
| 327 | ** table given the name of that table and (optionally) the name of the |
| 328 | ** database containing the table. Return NULL if not found. |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 329 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 | [diff] [blame] | 330 | ** If zDatabase is 0, all databases are searched for the table and the |
| 331 | ** first matching table is returned. (No checking for duplicate table |
| 332 | ** names is done.) The search order is TEMP first, then MAIN, then any |
| 333 | ** auxiliary databases added using the ATTACH command. |
drh | f26e09c | 2003-05-31 16:21:12 | [diff] [blame] | 334 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 335 | ** See also sqlite3LocateTable(). |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 336 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 337 | Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 338 | Table *p = 0; |
| 339 | int i; |
drh | 9ca9573 | 2014-10-24 00:35:58 | [diff] [blame] | 340 | |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 341 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 342 | assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
drh | b2eb7e4 | 2020-05-16 21:01:00 | [diff] [blame] | 343 | if( zDatabase ){ |
| 344 | for(i=0; i<db->nDb; i++){ |
| 345 | if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break; |
| 346 | } |
| 347 | if( i>=db->nDb ){ |
| 348 | /* No match against the official names. But always match "main" |
| 349 | ** to schema 0 as a legacy fallback. */ |
| 350 | if( sqlite3StrICmp(zDatabase,"main")==0 ){ |
| 351 | i = 0; |
| 352 | }else{ |
| 353 | return 0; |
drh | e0a04a3 | 2016-12-16 01:00:21 | [diff] [blame] | 354 | } |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 355 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 | [diff] [blame] | 356 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 357 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 358 | if( i==1 ){ |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 359 | if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 |
| 360 | || sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 |
| 361 | || sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 362 | ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 363 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 364 | LEGACY_TEMP_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 365 | } |
| 366 | }else{ |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 367 | if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 368 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 369 | LEGACY_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 370 | } |
| 371 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 | [diff] [blame] | 372 | } |
| 373 | }else{ |
| 374 | /* Match against TEMP first */ |
| 375 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName); |
| 376 | if( p ) return p; |
| 377 | /* The main database is second */ |
| 378 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName); |
| 379 | if( p ) return p; |
| 380 | /* Attached databases are in order of attachment */ |
| 381 | for(i=2; i<db->nDb; i++){ |
| 382 | assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
| 383 | p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName); |
| 384 | if( p ) break; |
| 385 | } |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 386 | if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 387 | if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){ |
| 388 | p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE); |
| 389 | }else if( sqlite3StrICmp(zName+7, &PREFERRED_TEMP_SCHEMA_TABLE[7])==0 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 390 | p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 391 | LEGACY_TEMP_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 392 | } |
| 393 | } |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 394 | } |
drh | b2eb7e4 | 2020-05-16 21:01:00 | [diff] [blame] | 395 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 | [diff] [blame] | 399 | ** Locate the in-memory structure that describes a particular database |
| 400 | ** table given the name of that table and (optionally) the name of the |
| 401 | ** database containing the table. Return NULL if not found. Also leave an |
| 402 | ** error message in pParse->zErrMsg. |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 403 | ** |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 | [diff] [blame] | 404 | ** The difference between this routine and sqlite3FindTable() is that this |
| 405 | ** routine leaves an error message in pParse->zErrMsg where |
| 406 | ** sqlite3FindTable() does not. |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 407 | */ |
drh | ca42411 | 2008-01-25 15:04:48 | [diff] [blame] | 408 | Table *sqlite3LocateTable( |
| 409 | Parse *pParse, /* context in which to report errors */ |
drh | 4d249e6 | 2016-06-10 22:49:01 | [diff] [blame] | 410 | u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */ |
drh | ca42411 | 2008-01-25 15:04:48 | [diff] [blame] | 411 | const char *zName, /* Name of the table we are looking for */ |
| 412 | const char *zDbase /* Name of the database. Might be NULL */ |
| 413 | ){ |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 414 | Table *p; |
drh | b2c8559 | 2018-04-25 12:01:45 | [diff] [blame] | 415 | sqlite3 *db = pParse->db; |
drh | f26e09c | 2003-05-31 16:21:12 | [diff] [blame] | 416 | |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 | [diff] [blame] | 417 | /* Read the database schema. If an error occurs, leave an error message |
| 418 | ** and code in pParse and return NULL. */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 419 | if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 |
drh | b2c8559 | 2018-04-25 12:01:45 | [diff] [blame] | 420 | && SQLITE_OK!=sqlite3ReadSchema(pParse) |
| 421 | ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
drh | b2c8559 | 2018-04-25 12:01:45 | [diff] [blame] | 425 | p = sqlite3FindTable(db, zName, zDbase); |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 426 | if( p==0 ){ |
drh | d297592 | 2015-08-29 17:22:33 | [diff] [blame] | 427 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 9196c81 | 2018-11-05 16:38:10 | [diff] [blame] | 428 | /* If zName is the not the name of a table in the schema created using |
| 429 | ** CREATE, then check to see if it is the name of an virtual table that |
| 430 | ** can be an eponymous virtual table. */ |
drh | 7424aef | 2022-10-01 13:17:53 | [diff] [blame] | 431 | if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){ |
dan | 1ea0443 | 2018-12-21 19:29:11 | [diff] [blame] | 432 | Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName); |
| 433 | if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){ |
| 434 | pMod = sqlite3PragmaVtabRegister(db, zName); |
| 435 | } |
| 436 | if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){ |
dan | bd24e8f | 2021-07-08 18:29:25 | [diff] [blame] | 437 | testcase( pMod->pEpoTab==0 ); |
dan | 1ea0443 | 2018-12-21 19:29:11 | [diff] [blame] | 438 | return pMod->pEpoTab; |
| 439 | } |
drh | 51be387 | 2015-08-19 02:32:25 | [diff] [blame] | 440 | } |
| 441 | #endif |
dan | 1ea0443 | 2018-12-21 19:29:11 | [diff] [blame] | 442 | if( flags & LOCATE_NOERR ) return 0; |
| 443 | pParse->checkSchema = 1; |
drh | 7424aef | 2022-10-01 13:17:53 | [diff] [blame] | 444 | }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){ |
dan | 1ea0443 | 2018-12-21 19:29:11 | [diff] [blame] | 445 | p = 0; |
| 446 | } |
| 447 | |
| 448 | if( p==0 ){ |
| 449 | const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table"; |
| 450 | if( zDbase ){ |
| 451 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); |
| 452 | }else{ |
| 453 | sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 454 | } |
drh | 1bb89e9 | 2021-04-19 18:03:52 | [diff] [blame] | 455 | }else{ |
| 456 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 457 | } |
dan | fab1d40 | 2015-11-26 15:51:55 | [diff] [blame] | 458 | |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 459 | return p; |
| 460 | } |
| 461 | |
| 462 | /* |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 463 | ** Locate the table identified by *p. |
| 464 | ** |
| 465 | ** This is a wrapper around sqlite3LocateTable(). The difference between |
| 466 | ** sqlite3LocateTable() and this function is that this function restricts |
| 467 | ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be |
| 468 | ** non-NULL if it is part of a view or trigger program definition. See |
| 469 | ** sqlite3FixSrcList() for details. |
| 470 | */ |
| 471 | Table *sqlite3LocateTableItem( |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 472 | Parse *pParse, |
drh | 4d249e6 | 2016-06-10 22:49:01 | [diff] [blame] | 473 | u32 flags, |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 474 | SrcItem *p |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 475 | ){ |
| 476 | const char *zDb; |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 477 | if( p->fg.fixedSchema ){ |
| 478 | int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 479 | zDb = pParse->db->aDb[iDb].zDbSName; |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 480 | }else{ |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 481 | assert( !p->fg.isSubquery ); |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 482 | zDb = p->u4.zDatabase; |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 483 | } |
drh | 4d249e6 | 2016-06-10 22:49:01 | [diff] [blame] | 484 | return sqlite3LocateTable(pParse, flags, p->zName, zDb); |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 488 | ** Return the preferred table name for system tables. Translate legacy |
| 489 | ** names into the new preferred names, as appropriate. |
| 490 | */ |
| 491 | const char *sqlite3PreferredTableName(const char *zName){ |
| 492 | if( sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){ |
| 493 | if( sqlite3StrICmp(zName+7, &LEGACY_SCHEMA_TABLE[7])==0 ){ |
| 494 | return PREFERRED_SCHEMA_TABLE; |
| 495 | } |
| 496 | if( sqlite3StrICmp(zName+7, &LEGACY_TEMP_SCHEMA_TABLE[7])==0 ){ |
| 497 | return PREFERRED_TEMP_SCHEMA_TABLE; |
| 498 | } |
| 499 | } |
| 500 | return zName; |
| 501 | } |
| 502 | |
| 503 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 504 | ** Locate the in-memory structure that describes |
drh | a69d916 | 2003-04-17 22:57:53 | [diff] [blame] | 505 | ** a particular index given the name of that index |
| 506 | ** and the name of the database that contains the index. |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 507 | ** Return NULL if not found. |
drh | f26e09c | 2003-05-31 16:21:12 | [diff] [blame] | 508 | ** |
| 509 | ** If zDatabase is 0, all databases are searched for the |
| 510 | ** table and the first matching index is returned. (No checking |
| 511 | ** for duplicate index names is done.) The search order is |
| 512 | ** TEMP first, then MAIN, then any auxiliary databases added |
| 513 | ** using the ATTACH command. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 514 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 515 | Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 516 | Index *p = 0; |
| 517 | int i; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 518 | /* All mutexes are required for schema access. Make sure we hold them. */ |
| 519 | assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) ); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 520 | for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
drh | 812d7a2 | 2003-03-27 13:50:00 | [diff] [blame] | 521 | int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 | [diff] [blame] | 522 | Schema *pSchema = db->aDb[j].pSchema; |
drh | 0449171 | 2009-05-13 17:21:13 | [diff] [blame] | 523 | assert( pSchema ); |
dan | 465c2b8 | 2020-03-21 15:10:40 | [diff] [blame] | 524 | if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 525 | assert( sqlite3SchemaMutexHeld(db, j, 0) ); |
drh | acbcb7e | 2014-08-21 20:26:37 | [diff] [blame] | 526 | p = sqlite3HashFind(&pSchema->idxHash, zName); |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 527 | if( p ) break; |
| 528 | } |
drh | 74e24cd | 2002-01-09 03:19:59 | [diff] [blame] | 529 | return p; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 533 | ** Reclaim the memory used by an index |
| 534 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 535 | void sqlite3FreeIndex(sqlite3 *db, Index *p){ |
drh | 92aa5ea | 2009-09-11 14:05:06 | [diff] [blame] | 536 | #ifndef SQLITE_OMIT_ANALYZE |
dan | d46def7 | 2010-07-24 11:28:28 | [diff] [blame] | 537 | sqlite3DeleteIndexSamples(db, p); |
drh | 92aa5ea | 2009-09-11 14:05:06 | [diff] [blame] | 538 | #endif |
drh | 1fe0537 | 2013-07-31 18:12:26 | [diff] [blame] | 539 | sqlite3ExprDelete(db, p->pPartIdxWhere); |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 540 | sqlite3ExprListDelete(db, p->aColExpr); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 541 | sqlite3DbFree(db, p->zColAff); |
mistachkin | 5905f86 | 2015-12-31 19:04:42 | [diff] [blame] | 542 | if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl); |
drh | 175b8f0 | 2019-08-08 15:24:17 | [diff] [blame] | 543 | #ifdef SQLITE_ENABLE_STAT4 |
drh | 75b170b | 2014-10-04 00:07:44 | [diff] [blame] | 544 | sqlite3_free(p->aiRowEst); |
| 545 | #endif |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 546 | sqlite3DbFree(db, p); |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* |
drh | c96d853 | 2005-05-03 12:30:33 | [diff] [blame] | 550 | ** For the index called zIdxName which is found in the database iDb, |
| 551 | ** unlike that index from its Table then remove the index from |
| 552 | ** the index hash table and free all memory structures associated |
| 553 | ** with the index. |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 554 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 555 | void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 556 | Index *pIndex; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 557 | Hash *pHash; |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 558 | |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 559 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 560 | pHash = &db->aDb[iDb].pSchema->idxHash; |
drh | acbcb7e | 2014-08-21 20:26:37 | [diff] [blame] | 561 | pIndex = sqlite3HashInsert(pHash, zIdxName, 0); |
drh | 2264584 | 2011-03-24 01:34:03 | [diff] [blame] | 562 | if( ALWAYS(pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 563 | if( pIndex->pTable->pIndex==pIndex ){ |
| 564 | pIndex->pTable->pIndex = pIndex->pNext; |
| 565 | }else{ |
| 566 | Index *p; |
drh | 0449171 | 2009-05-13 17:21:13 | [diff] [blame] | 567 | /* Justification of ALWAYS(); The index must be on the list of |
| 568 | ** indices. */ |
| 569 | p = pIndex->pTable->pIndex; |
| 570 | while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
| 571 | if( ALWAYS(p && p->pNext==pIndex) ){ |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 572 | p->pNext = pIndex->pNext; |
| 573 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 574 | } |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 575 | sqlite3FreeIndex(db, pIndex); |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 576 | } |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 577 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 581 | ** Look through the list of open database files in db->aDb[] and if |
| 582 | ** any have been closed, remove them from the list. Reallocate the |
| 583 | ** db->aDb[] structure to a smaller size, if possible. |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 584 | ** |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 585 | ** Entry 0 (the "main" database) and entry 1 (the "temp" database) |
| 586 | ** are never candidates for being collapsed. |
drh | 74e24cd | 2002-01-09 03:19:59 | [diff] [blame] | 587 | */ |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 588 | void sqlite3CollapseDatabaseArray(sqlite3 *db){ |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 589 | int i, j; |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 590 | for(i=j=2; i<db->nDb; i++){ |
drh | 4d189ca | 2004-02-12 18:46:38 | [diff] [blame] | 591 | struct Db *pDb = &db->aDb[i]; |
| 592 | if( pDb->pBt==0 ){ |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 593 | sqlite3DbFree(db, pDb->zDbSName); |
| 594 | pDb->zDbSName = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 595 | continue; |
| 596 | } |
| 597 | if( j<i ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 | [diff] [blame] | 598 | db->aDb[j] = db->aDb[i]; |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 599 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 | [diff] [blame] | 600 | j++; |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 601 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 602 | db->nDb = j; |
| 603 | if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
| 604 | memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 605 | sqlite3DbFree(db, db->aDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 | [diff] [blame] | 606 | db->aDb = db->aDbStatic; |
| 607 | } |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 611 | ** Reset the schema for the database at index iDb. Also reset the |
drh | dc6b41e | 2017-08-17 02:26:35 | [diff] [blame] | 612 | ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero. |
| 613 | ** Deferred resets may be run by calling with iDb<0. |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 614 | */ |
| 615 | void sqlite3ResetOneSchema(sqlite3 *db, int iDb){ |
drh | dc6b41e | 2017-08-17 02:26:35 | [diff] [blame] | 616 | int i; |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 617 | assert( iDb<db->nDb ); |
| 618 | |
drh | dc6b41e | 2017-08-17 02:26:35 | [diff] [blame] | 619 | if( iDb>=0 ){ |
| 620 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 621 | DbSetProperty(db, iDb, DB_ResetWanted); |
| 622 | DbSetProperty(db, 1, DB_ResetWanted); |
drh | b2c8559 | 2018-04-25 12:01:45 | [diff] [blame] | 623 | db->mDbFlags &= ~DBFLAG_SchemaKnownOk; |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 624 | } |
drh | dc6b41e | 2017-08-17 02:26:35 | [diff] [blame] | 625 | |
| 626 | if( db->nSchemaLock==0 ){ |
| 627 | for(i=0; i<db->nDb; i++){ |
| 628 | if( DbHasProperty(db, i, DB_ResetWanted) ){ |
| 629 | sqlite3SchemaClear(db->aDb[i].pSchema); |
| 630 | } |
| 631 | } |
| 632 | } |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
| 636 | ** Erase all schema information from all attached databases (including |
| 637 | ** "main" and "temp") for a single database connection. |
| 638 | */ |
| 639 | void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){ |
| 640 | int i; |
| 641 | sqlite3BtreeEnterAll(db); |
| 642 | for(i=0; i<db->nDb; i++){ |
| 643 | Db *pDb = &db->aDb[i]; |
| 644 | if( pDb->pSchema ){ |
dan | 63e50b9 | 2018-11-27 19:47:55 | [diff] [blame] | 645 | if( db->nSchemaLock==0 ){ |
| 646 | sqlite3SchemaClear(pDb->pSchema); |
| 647 | }else{ |
| 648 | DbSetProperty(db, i, DB_ResetWanted); |
| 649 | } |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 650 | } |
| 651 | } |
drh | b2c8559 | 2018-04-25 12:01:45 | [diff] [blame] | 652 | db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk); |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 653 | sqlite3VtabUnlockList(db); |
| 654 | sqlite3BtreeLeaveAll(db); |
dan | 63e50b9 | 2018-11-27 19:47:55 | [diff] [blame] | 655 | if( db->nSchemaLock==0 ){ |
| 656 | sqlite3CollapseDatabaseArray(db); |
| 657 | } |
drh | 81028a4 | 2012-05-15 18:28:27 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 661 | ** This routine is called when a commit occurs. |
| 662 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 663 | void sqlite3CommitInternalChanges(sqlite3 *db){ |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 664 | db->mDbFlags &= ~DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 668 | ** Set the expression associated with a column. This is usually |
| 669 | ** the DEFAULT value, but might also be the expression that computes |
| 670 | ** the value for a generated column. |
| 671 | */ |
| 672 | void sqlite3ColumnSetExpr( |
| 673 | Parse *pParse, /* Parsing context */ |
| 674 | Table *pTab, /* The table containing the column */ |
| 675 | Column *pCol, /* The column to receive the new DEFAULT expression */ |
| 676 | Expr *pExpr /* The new default expression */ |
| 677 | ){ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 678 | ExprList *pList; |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 679 | assert( IsOrdinaryTable(pTab) ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 680 | pList = pTab->u.tab.pDfltList; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 681 | if( pCol->iDflt==0 |
drh | 324f91a | 2021-08-04 14:50:23 | [diff] [blame] | 682 | || NEVER(pList==0) |
| 683 | || NEVER(pList->nExpr<pCol->iDflt) |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 684 | ){ |
| 685 | pCol->iDflt = pList==0 ? 1 : pList->nExpr+1; |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 686 | pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr); |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 687 | }else{ |
| 688 | sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr); |
| 689 | pList->a[pCol->iDflt-1].pExpr = pExpr; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | ** Return the expression associated with a column. The expression might be |
| 695 | ** the DEFAULT clause or the AS clause of a generated column. |
| 696 | ** Return NULL if the column has no associated expression. |
| 697 | */ |
| 698 | Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){ |
| 699 | if( pCol->iDflt==0 ) return 0; |
drh | 768b6e3 | 2023-12-02 12:23:34 | [diff] [blame] | 700 | if( !IsOrdinaryTable(pTab) ) return 0; |
drh | 324f91a | 2021-08-04 14:50:23 | [diff] [blame] | 701 | if( NEVER(pTab->u.tab.pDfltList==0) ) return 0; |
| 702 | if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0; |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 703 | return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 707 | ** Set the collating sequence name for a column. |
| 708 | */ |
| 709 | void sqlite3ColumnSetColl( |
| 710 | sqlite3 *db, |
| 711 | Column *pCol, |
| 712 | const char *zColl |
| 713 | ){ |
drh | 913306a | 2021-11-26 17:10:18 | [diff] [blame] | 714 | i64 nColl; |
| 715 | i64 n; |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 716 | char *zNew; |
| 717 | assert( zColl!=0 ); |
| 718 | n = sqlite3Strlen30(pCol->zCnName) + 1; |
| 719 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 720 | n += sqlite3Strlen30(pCol->zCnName+n) + 1; |
| 721 | } |
| 722 | nColl = sqlite3Strlen30(zColl) + 1; |
| 723 | zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n); |
| 724 | if( zNew ){ |
| 725 | pCol->zCnName = zNew; |
| 726 | memcpy(pCol->zCnName + n, zColl, nColl); |
| 727 | pCol->colFlags |= COLFLAG_HASCOLL; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 732 | ** Return the collating sequence name for a column |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 733 | */ |
| 734 | const char *sqlite3ColumnColl(Column *pCol){ |
| 735 | const char *z; |
| 736 | if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0; |
| 737 | z = pCol->zCnName; |
| 738 | while( *z ){ z++; } |
| 739 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 740 | do{ z++; }while( *z ); |
| 741 | } |
| 742 | return z+1; |
| 743 | } |
| 744 | |
| 745 | /* |
dan | d46def7 | 2010-07-24 11:28:28 | [diff] [blame] | 746 | ** Delete memory allocated for the column names of a table or view (the |
| 747 | ** Table.aCol[] array). |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 748 | */ |
drh | 51be387 | 2015-08-19 02:32:25 | [diff] [blame] | 749 | void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){ |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 750 | int i; |
| 751 | Column *pCol; |
| 752 | assert( pTable!=0 ); |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 753 | assert( db!=0 ); |
drh | dd5b2fa | 2005-03-28 03:39:55 | [diff] [blame] | 754 | if( (pCol = pTable->aCol)!=0 ){ |
| 755 | for(i=0; i<pTable->nCol; i++, pCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 756 | assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) ); |
| 757 | sqlite3DbFree(db, pCol->zCnName); |
drh | dd5b2fa | 2005-03-28 03:39:55 | [diff] [blame] | 758 | } |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 759 | sqlite3DbNNFreeNN(db, pTable->aCol); |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 760 | if( IsOrdinaryTable(pTable) ){ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 761 | sqlite3ExprListDelete(db, pTable->u.tab.pDfltList); |
| 762 | } |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 763 | if( db->pnBytesFreed==0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 764 | pTable->aCol = 0; |
| 765 | pTable->nCol = 0; |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 766 | if( IsOrdinaryTable(pTable) ){ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 767 | pTable->u.tab.pDfltList = 0; |
| 768 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 769 | } |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 770 | } |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 774 | ** Remove the memory data structures associated with the given |
drh | 967e8b7 | 2000-06-21 13:59:10 | [diff] [blame] | 775 | ** Table. No changes are made to disk by this routine. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 776 | ** |
| 777 | ** This routine just deletes the data structure. It does not unlink |
drh | e61922a | 2009-05-02 13:29:37 | [diff] [blame] | 778 | ** the table data structure from the hash table. But it does destroy |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 779 | ** memory structures of the indices and foreign keys associated with |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 780 | ** the table. |
drh | 29ddd3a | 2012-05-15 12:49:32 | [diff] [blame] | 781 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 782 | ** The db parameter is optional. It is needed if the Table object |
drh | 29ddd3a | 2012-05-15 12:49:32 | [diff] [blame] | 783 | ** contains lookaside memory. (Table objects in the schema do not use |
| 784 | ** lookaside memory, but some ephemeral Table objects do.) Or the |
| 785 | ** db parameter can be used with db->pnBytesFreed to measure the memory |
| 786 | ** used by the Table object. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 787 | */ |
drh | e8da01c | 2016-05-07 12:15:34 | [diff] [blame] | 788 | static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 789 | Index *pIndex, *pNext; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 790 | |
drh | 52fb8e1 | 2017-08-29 20:21:12 | [diff] [blame] | 791 | #ifdef SQLITE_DEBUG |
drh | 29ddd3a | 2012-05-15 12:49:32 | [diff] [blame] | 792 | /* Record the number of outstanding lookaside allocations in schema Tables |
dan | bedf84c | 2019-07-08 13:45:02 | [diff] [blame] | 793 | ** prior to doing any free() operations. Since schema Tables do not use |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 794 | ** lookaside, this number should not change. |
dan | bedf84c | 2019-07-08 13:45:02 | [diff] [blame] | 795 | ** |
| 796 | ** If malloc has already failed, it may be that it failed while allocating |
| 797 | ** a Table object that was going to be marked ephemeral. So do not check |
| 798 | ** that no lookaside memory is used in this case either. */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 | [diff] [blame] | 799 | int nLookaside = 0; |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 800 | assert( db!=0 ); |
| 801 | if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){ |
drh | 52fb8e1 | 2017-08-29 20:21:12 | [diff] [blame] | 802 | nLookaside = sqlite3LookasideUsed(db, 0); |
| 803 | } |
| 804 | #endif |
drh | 29ddd3a | 2012-05-15 12:49:32 | [diff] [blame] | 805 | |
dan | d46def7 | 2010-07-24 11:28:28 | [diff] [blame] | 806 | /* Delete all indices associated with this table. */ |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 807 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 808 | pNext = pIndex->pNext; |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 809 | assert( pIndex->pSchema==pTable->pSchema |
| 810 | || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) ); |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 811 | if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 812 | char *zName = pIndex->zName; |
dan | d46def7 | 2010-07-24 11:28:28 | [diff] [blame] | 813 | TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
drh | acbcb7e | 2014-08-21 20:26:37 | [diff] [blame] | 814 | &pIndex->pSchema->idxHash, zName, 0 |
dan | d46def7 | 2010-07-24 11:28:28 | [diff] [blame] | 815 | ); |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 816 | assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | d46def7 | 2010-07-24 11:28:28 | [diff] [blame] | 817 | assert( pOld==pIndex || pOld==0 ); |
| 818 | } |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 819 | sqlite3FreeIndex(db, pIndex); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 820 | } |
| 821 | |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 822 | if( IsOrdinaryTable(pTable) ){ |
| 823 | sqlite3FkDelete(db, pTable); |
| 824 | } |
drh | e030619 | 2023-05-05 14:16:31 | [diff] [blame] | 825 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 826 | else if( IsVirtual(pTable) ){ |
| 827 | sqlite3VtabClear(db, pTable); |
| 828 | } |
| 829 | #endif |
| 830 | else{ |
| 831 | assert( IsView(pTable) ); |
| 832 | sqlite3SelectDelete(db, pTable->u.view.pSelect); |
| 833 | } |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 834 | |
| 835 | /* Delete the Table structure itself. |
| 836 | */ |
drh | 51be387 | 2015-08-19 02:32:25 | [diff] [blame] | 837 | sqlite3DeleteColumnNames(db, pTable); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 838 | sqlite3DbFree(db, pTable->zName); |
| 839 | sqlite3DbFree(db, pTable->zColAff); |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 840 | sqlite3ExprListDelete(db, pTable->pCheck); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 841 | sqlite3DbFree(db, pTable); |
drh | 29ddd3a | 2012-05-15 12:49:32 | [diff] [blame] | 842 | |
| 843 | /* Verify that no lookaside memory was used by schema tables */ |
drh | 52fb8e1 | 2017-08-29 20:21:12 | [diff] [blame] | 844 | assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) ); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 845 | } |
drh | e8da01c | 2016-05-07 12:15:34 | [diff] [blame] | 846 | void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 847 | /* Do not delete the table until the reference count reaches zero. */ |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 848 | assert( db!=0 ); |
drh | e8da01c | 2016-05-07 12:15:34 | [diff] [blame] | 849 | if( !pTable ) return; |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 850 | if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return; |
drh | e8da01c | 2016-05-07 12:15:34 | [diff] [blame] | 851 | deleteTable(db, pTable); |
| 852 | } |
drh | 82fc1b6 | 2023-12-06 18:25:41 | [diff] [blame] | 853 | void sqlite3DeleteTableGeneric(sqlite3 *db, void *pTable){ |
| 854 | sqlite3DeleteTable(db, (Table*)pTable); |
| 855 | } |
drh | e8da01c | 2016-05-07 12:15:34 | [diff] [blame] | 856 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 857 | |
| 858 | /* |
drh | 5edc312 | 2001-09-13 21:53:09 | [diff] [blame] | 859 | ** Unlink the given table from the hash tables and the delete the |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 860 | ** table structure with all its indices and foreign keys. |
drh | 5edc312 | 2001-09-13 21:53:09 | [diff] [blame] | 861 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 862 | void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 863 | Table *p; |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 864 | Db *pDb; |
| 865 | |
drh | d229ca9 | 2002-01-09 13:30:41 | [diff] [blame] | 866 | assert( db!=0 ); |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 867 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 972a231 | 2009-12-08 14:34:08 | [diff] [blame] | 868 | assert( zTabName ); |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 869 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 972a231 | 2009-12-08 14:34:08 | [diff] [blame] | 870 | testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
drh | 956bc92 | 2004-07-24 17:38:29 | [diff] [blame] | 871 | pDb = &db->aDb[iDb]; |
drh | acbcb7e | 2014-08-21 20:26:37 | [diff] [blame] | 872 | p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0); |
dan | 1feeaed | 2010-07-23 15:41:47 | [diff] [blame] | 873 | sqlite3DeleteTable(db, p); |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 874 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 74e24cd | 2002-01-09 03:19:59 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* |
drh | a99db3b | 2004-06-19 14:49:12 | [diff] [blame] | 878 | ** Given a token, return a string that consists of the text of that |
drh | 24fb627 | 2009-05-01 21:13:36 | [diff] [blame] | 879 | ** token. Space to hold the returned string |
drh | a99db3b | 2004-06-19 14:49:12 | [diff] [blame] | 880 | ** is obtained from sqliteMalloc() and must be freed by the calling |
| 881 | ** function. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 882 | ** |
drh | 24fb627 | 2009-05-01 21:13:36 | [diff] [blame] | 883 | ** Any quotation marks (ex: "name", 'name', [name], or `name`) that |
| 884 | ** surround the body of the token are removed. |
| 885 | ** |
drh | c96d853 | 2005-05-03 12:30:33 | [diff] [blame] | 886 | ** Tokens are often just pointers into the original SQL text and so |
drh | a99db3b | 2004-06-19 14:49:12 | [diff] [blame] | 887 | ** are not \000 terminated and are not persistent. The returned string |
| 888 | ** is \000 terminated and is persistent. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 889 | */ |
drh | b6dad52 | 2021-09-24 16:14:47 | [diff] [blame] | 890 | char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){ |
drh | a99db3b | 2004-06-19 14:49:12 | [diff] [blame] | 891 | char *zName; |
| 892 | if( pName ){ |
drh | b6dad52 | 2021-09-24 16:14:47 | [diff] [blame] | 893 | zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n); |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 894 | sqlite3Dequote(zName); |
drh | a99db3b | 2004-06-19 14:49:12 | [diff] [blame] | 895 | }else{ |
| 896 | zName = 0; |
| 897 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 898 | return zName; |
| 899 | } |
| 900 | |
| 901 | /* |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 902 | ** Open the sqlite_schema table stored in database number iDb for |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 903 | ** writing. The table is opened using cursor 0. |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 904 | */ |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 905 | void sqlite3OpenSchemaTable(Parse *p, int iDb){ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 906 | Vdbe *v = sqlite3GetVdbe(p); |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 907 | sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, LEGACY_SCHEMA_TABLE); |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 908 | sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 | [diff] [blame] | 909 | if( p->nTab==0 ){ |
| 910 | p->nTab = 1; |
| 911 | } |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* |
danielk1977 | 0410302 | 2009-02-03 16:51:24 | [diff] [blame] | 915 | ** Parameter zName points to a nul-terminated buffer containing the name |
| 916 | ** of a database ("main", "temp" or the name of an attached db). This |
| 917 | ** function returns the index of the named database in db->aDb[], or |
| 918 | ** -1 if the named db cannot be found. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 919 | */ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 | [diff] [blame] | 920 | int sqlite3FindDbName(sqlite3 *db, const char *zName){ |
| 921 | int i = -1; /* Database number */ |
drh | 73c42a1 | 2004-11-20 18:13:10 | [diff] [blame] | 922 | if( zName ){ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 | [diff] [blame] | 923 | Db *pDb; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 924 | for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
drh | 2951809 | 2016-12-24 19:37:16 | [diff] [blame] | 925 | if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break; |
| 926 | /* "main" is always an acceptable alias for the primary database |
| 927 | ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */ |
| 928 | if( i==0 && 0==sqlite3_stricmp("main", zName) ) break; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 929 | } |
| 930 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 931 | return i; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 932 | } |
| 933 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 | [diff] [blame] | 934 | /* |
| 935 | ** The token *pName contains the name of a database (either "main" or |
| 936 | ** "temp" or the name of an attached db). This routine returns the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 937 | ** index of the named database in db->aDb[], or -1 if the named db |
danielk1977 | 0410302 | 2009-02-03 16:51:24 | [diff] [blame] | 938 | ** does not exist. |
| 939 | */ |
| 940 | int sqlite3FindDb(sqlite3 *db, Token *pName){ |
| 941 | int i; /* Database number */ |
| 942 | char *zName; /* Name we are searching for */ |
| 943 | zName = sqlite3NameFromToken(db, pName); |
| 944 | i = sqlite3FindDbName(db, zName); |
| 945 | sqlite3DbFree(db, zName); |
| 946 | return i; |
| 947 | } |
| 948 | |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 949 | /* The table or view or trigger name is passed to this routine via tokens |
| 950 | ** pName1 and pName2. If the table name was fully qualified, for example: |
| 951 | ** |
| 952 | ** CREATE TABLE xxx.yyy (...); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 953 | ** |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 954 | ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
| 955 | ** the table name is not fully qualified, i.e.: |
| 956 | ** |
| 957 | ** CREATE TABLE yyy(...); |
| 958 | ** |
| 959 | ** Then pName1 is set to "yyy" and pName2 is "". |
| 960 | ** |
| 961 | ** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
| 962 | ** pName2) that stores the unqualified table name. The index of the |
| 963 | ** database "xxx" is returned. |
| 964 | */ |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 | [diff] [blame] | 965 | int sqlite3TwoPartName( |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 966 | Parse *pParse, /* Parsing and code generating context */ |
drh | 90f5ecb | 2004-07-22 01:19:35 | [diff] [blame] | 967 | Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 968 | Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
| 969 | Token **pUnqual /* Write the unqualified object name here */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 970 | ){ |
drh | 0e3d747 | 2004-06-19 17:33:07 | [diff] [blame] | 971 | int iDb; /* Database holding the object */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 972 | sqlite3 *db = pParse->db; |
| 973 | |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 974 | assert( pName2!=0 ); |
| 975 | if( pName2->n>0 ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 | [diff] [blame] | 976 | if( db->init.busy ) { |
| 977 | sqlite3ErrorMsg(pParse, "corrupt database"); |
shane | dcc50b7 | 2008-11-13 18:29:50 | [diff] [blame] | 978 | return -1; |
| 979 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 980 | *pUnqual = pName2; |
drh | ff2d5ea | 2005-07-23 00:41:48 | [diff] [blame] | 981 | iDb = sqlite3FindDb(db, pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 982 | if( iDb<0 ){ |
| 983 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 984 | return -1; |
| 985 | } |
| 986 | }else{ |
drh | d36bcec | 2021-05-29 21:50:05 | [diff] [blame] | 987 | assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 988 | || (db->mDbFlags & DBFLAG_Vacuum)!=0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 989 | iDb = db->init.iDb; |
| 990 | *pUnqual = pName1; |
| 991 | } |
| 992 | return iDb; |
| 993 | } |
| 994 | |
| 995 | /* |
drh | 0f1c2eb | 2018-11-03 17:31:48 | [diff] [blame] | 996 | ** True if PRAGMA writable_schema is ON |
| 997 | */ |
| 998 | int sqlite3WritableSchema(sqlite3 *db){ |
| 999 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 ); |
| 1000 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1001 | SQLITE_WriteSchema ); |
| 1002 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1003 | SQLITE_Defensive ); |
| 1004 | testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))== |
| 1005 | (SQLITE_WriteSchema|SQLITE_Defensive) ); |
| 1006 | return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1010 | ** This routine is used to check if the UTF-8 string zName is a legal |
| 1011 | ** unqualified name for a new schema object (table, index, view or |
| 1012 | ** trigger). All names are legal except those that begin with the string |
| 1013 | ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
| 1014 | ** is reserved for internal use. |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1015 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 1016 | ** When parsing the sqlite_schema table, this routine also checks to |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1017 | ** make sure the "type", "name", and "tbl_name" columns are consistent |
| 1018 | ** with the SQL. |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1019 | */ |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1020 | int sqlite3CheckObjectName( |
| 1021 | Parse *pParse, /* Parsing context */ |
| 1022 | const char *zName, /* Name of the object to check */ |
| 1023 | const char *zType, /* Type of this object */ |
| 1024 | const char *zTblName /* Parent table name for triggers and indexes */ |
| 1025 | ){ |
| 1026 | sqlite3 *db = pParse->db; |
drh | ca439a4 | 2020-07-22 21:05:23 | [diff] [blame] | 1027 | if( sqlite3WritableSchema(db) |
| 1028 | || db->init.imposterTable |
| 1029 | || !sqlite3Config.bExtraSchemaChecks |
| 1030 | ){ |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1031 | /* Skip these error checks for writable_schema=ON */ |
| 1032 | return SQLITE_OK; |
| 1033 | } |
| 1034 | if( db->init.busy ){ |
| 1035 | if( sqlite3_stricmp(zType, db->init.azInit[0]) |
| 1036 | || sqlite3_stricmp(zName, db->init.azInit[1]) |
| 1037 | || sqlite3_stricmp(zTblName, db->init.azInit[2]) |
| 1038 | ){ |
drh | ca439a4 | 2020-07-22 21:05:23 | [diff] [blame] | 1039 | sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */ |
| 1040 | return SQLITE_ERROR; |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1041 | } |
| 1042 | }else{ |
drh | 527cbd4 | 2019-11-16 14:15:19 | [diff] [blame] | 1043 | if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7)) |
| 1044 | || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName)) |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1045 | ){ |
| 1046 | sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", |
| 1047 | zName); |
| 1048 | return SQLITE_ERROR; |
| 1049 | } |
drh | 527cbd4 | 2019-11-16 14:15:19 | [diff] [blame] | 1050 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1051 | } |
| 1052 | return SQLITE_OK; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1056 | ** Return the PRIMARY KEY index of a table |
| 1057 | */ |
| 1058 | Index *sqlite3PrimaryKeyIndex(Table *pTab){ |
| 1059 | Index *p; |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 1060 | for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){} |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1061 | return p; |
| 1062 | } |
| 1063 | |
| 1064 | /* |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1065 | ** Convert an table column number into a index column number. That is, |
| 1066 | ** for the column iCol in the table (as defined by the CREATE TABLE statement) |
| 1067 | ** find the (first) offset of that column in index pIdx. Or return -1 |
| 1068 | ** if column iCol is not used in index pIdx. |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1069 | */ |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 1070 | int sqlite3TableColumnToIndex(Index *pIdx, int iCol){ |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1071 | int i; |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 1072 | i16 iCol16; |
| 1073 | assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN ); |
drh | 0f0450e | 2025-04-25 12:39:32 | [diff] [blame] | 1074 | assert( pIdx->nColumn<=SQLITE_MAX_COLUMN+1 ); |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 1075 | iCol16 = iCol; |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1076 | for(i=0; i<pIdx->nColumn; i++){ |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 1077 | if( iCol16==pIdx->aiColumn[i] ){ |
| 1078 | return i; |
| 1079 | } |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1080 | } |
| 1081 | return -1; |
| 1082 | } |
| 1083 | |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1084 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1085 | /* Convert a storage column number into a table column number. |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1086 | ** |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1087 | ** The storage column number (0,1,2,....) is the index of the value |
| 1088 | ** as it appears in the record on disk. The true column number |
| 1089 | ** is the index (0,1,2,...) of the column in the CREATE TABLE statement. |
| 1090 | ** |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1091 | ** The storage column number is less than the table column number if |
| 1092 | ** and only there are VIRTUAL columns to the left. |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1093 | ** |
| 1094 | ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro. |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1095 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1096 | i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){ |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1097 | if( pTab->tabFlags & TF_HasVirtual ){ |
| 1098 | int i; |
| 1099 | for(i=0; i<=iCol; i++){ |
| 1100 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++; |
| 1101 | } |
| 1102 | } |
| 1103 | return iCol; |
| 1104 | } |
| 1105 | #endif |
| 1106 | |
| 1107 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1108 | /* Convert a table column number into a storage column number. |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1109 | ** |
| 1110 | ** The storage column number (0,1,2,....) is the index of the value |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 1111 | ** as it appears in the record on disk. Or, if the input column is |
| 1112 | ** the N-th virtual column (zero-based) then the storage number is |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1113 | ** the number of non-virtual columns in the table plus N. |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1114 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 1115 | ** The true column number is the index (0,1,2,...) of the column in |
| 1116 | ** the CREATE TABLE statement. |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 1117 | ** |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 1118 | ** If the input column is a VIRTUAL column, then it should not appear |
| 1119 | ** in storage. But the value sometimes is cached in registers that |
| 1120 | ** follow the range of registers used to construct storage. This |
| 1121 | ** avoids computing the same VIRTUAL column multiple times, and provides |
| 1122 | ** values for use by OP_Param opcodes in triggers. Hence, if the |
| 1123 | ** input column is a VIRTUAL table, put it after all the other columns. |
| 1124 | ** |
| 1125 | ** In the following, N means "normal column", S means STORED, and |
| 1126 | ** V means VIRTUAL. Suppose the CREATE TABLE has columns like this: |
| 1127 | ** |
| 1128 | ** CREATE TABLE ex(N,S,V,N,S,V,N,S,V); |
| 1129 | ** -- 0 1 2 3 4 5 6 7 8 |
| 1130 | ** |
| 1131 | ** Then the mapping from this function is as follows: |
| 1132 | ** |
| 1133 | ** INPUTS: 0 1 2 3 4 5 6 7 8 |
| 1134 | ** OUTPUTS: 0 1 6 2 3 7 4 5 8 |
| 1135 | ** |
| 1136 | ** So, in other words, this routine shifts all the virtual columns to |
| 1137 | ** the end. |
| 1138 | ** |
| 1139 | ** If SQLITE_OMIT_GENERATED_COLUMNS then there are no virtual columns and |
drh | 7fe2fc0 | 2019-12-07 00:22:18 | [diff] [blame] | 1140 | ** this routine is a no-op macro. If the pTab does not have any virtual |
| 1141 | ** columns, then this routine is no-op that always return iCol. If iCol |
| 1142 | ** is negative (indicating the ROWID column) then this routine return iCol. |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1143 | */ |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1144 | i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){ |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1145 | int i; |
| 1146 | i16 n; |
| 1147 | assert( iCol<pTab->nCol ); |
drh | 7fe2fc0 | 2019-12-07 00:22:18 | [diff] [blame] | 1148 | if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1149 | for(i=0, n=0; i<iCol; i++){ |
| 1150 | if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++; |
| 1151 | } |
drh | dd6cc9b | 2019-10-19 18:47:27 | [diff] [blame] | 1152 | if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){ |
| 1153 | /* iCol is a virtual column itself */ |
| 1154 | return pTab->nNVCol + i - n; |
| 1155 | }else{ |
| 1156 | /* iCol is a normal or stored column */ |
| 1157 | return n; |
| 1158 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1159 | } |
| 1160 | #endif |
| 1161 | |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 1162 | /* |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 1163 | ** Insert a single OP_JournalMode query opcode in order to force the |
| 1164 | ** prepared statement to return false for sqlite3_stmt_readonly(). This |
| 1165 | ** is used by CREATE TABLE IF NOT EXISTS and similar if the table already |
| 1166 | ** exists, so that the prepared statement for CREATE TABLE IF NOT EXISTS |
| 1167 | ** will return false for sqlite3_stmt_readonly() even if that statement |
| 1168 | ** is a read-only no-op. |
| 1169 | */ |
| 1170 | static void sqlite3ForceNotReadOnly(Parse *pParse){ |
| 1171 | int iReg = ++pParse->nMem; |
| 1172 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1173 | if( v ){ |
| 1174 | sqlite3VdbeAddOp3(v, OP_JournalMode, 0, iReg, PAGER_JOURNALMODE_QUERY); |
dan | a8f249f | 2021-05-20 11:42:51 | [diff] [blame] | 1175 | sqlite3VdbeUsesBtree(v, 0); |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1180 | ** Begin constructing a new table representation in memory. This is |
| 1181 | ** the first of several action routines that get called in response |
drh | d9b0257 | 2001-04-15 00:37:09 | [diff] [blame] | 1182 | ** to a CREATE TABLE statement. In particular, this routine is called |
drh | 7416170 | 2006-02-24 02:53:49 | [diff] [blame] | 1183 | ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 1184 | ** flag is true if the table should be stored in the auxiliary database |
| 1185 | ** file instead of in the main database file. This is normally the case |
| 1186 | ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 1187 | ** CREATE and TABLE. |
drh | d9b0257 | 2001-04-15 00:37:09 | [diff] [blame] | 1188 | ** |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 1189 | ** The new table record is initialized and put in pParse->pNewTable. |
| 1190 | ** As more of the CREATE TABLE statement is parsed, additional action |
| 1191 | ** routines will be called to add more information to this record. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1192 | ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 1193 | ** is called to complete the construction of the new table record. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1194 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1195 | void sqlite3StartTable( |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 1196 | Parse *pParse, /* Parser context */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 1197 | Token *pName1, /* First part of the name of the table or view */ |
| 1198 | Token *pName2, /* Second part of the name of the table or view */ |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 1199 | int isTemp, /* True if this is a TEMP table */ |
drh | faa5955 | 2005-12-29 23:33:54 | [diff] [blame] | 1200 | int isView, /* True if this is a VIEW */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 | [diff] [blame] | 1201 | int isVirtual, /* True if this is a VIRTUAL table */ |
drh | faa5955 | 2005-12-29 23:33:54 | [diff] [blame] | 1202 | int noErr /* Do nothing if table already exists */ |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 1203 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1204 | Table *pTable; |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1205 | char *zName = 0; /* The name of the new table */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 1206 | sqlite3 *db = pParse->db; |
drh | adbca9c | 2001-09-27 15:11:53 | [diff] [blame] | 1207 | Vdbe *v; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 1208 | int iDb; /* Database number to create the table in */ |
| 1209 | Token *pName; /* Unqualified name of the table to create */ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1210 | |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 1211 | if( db->init.busy && db->init.newTnum==1 ){ |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 1212 | /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */ |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 1213 | iDb = db->init.iDb; |
| 1214 | zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)); |
| 1215 | pName = pName1; |
| 1216 | }else{ |
| 1217 | /* The common case */ |
| 1218 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
| 1219 | if( iDb<0 ) return; |
| 1220 | if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1221 | /* If creating a temp table, the name may not be qualified. Unless |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 1222 | ** the database name is "temp" anyway. */ |
| 1223 | sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
| 1224 | return; |
| 1225 | } |
| 1226 | if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
| 1227 | zName = sqlite3NameFromToken(db, pName); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1228 | if( IN_RENAME_OBJECT ){ |
| 1229 | sqlite3RenameTokenMap(pParse, (void*)zName, pName); |
| 1230 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 1231 | } |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 1232 | pParse->sNameToken = *pName; |
danielk1977 | e004840 | 2004-06-15 16:51:01 | [diff] [blame] | 1233 | if( zName==0 ) return; |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 1234 | if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1235 | goto begin_table_error; |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 1236 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 | [diff] [blame] | 1237 | if( db->init.iDb==1 ) isTemp = 1; |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 1238 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 1239 | assert( isTemp==0 || isTemp==1 ); |
| 1240 | assert( isView==0 || isView==1 ); |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 1241 | { |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 1242 | static const u8 aCode[] = { |
| 1243 | SQLITE_CREATE_TABLE, |
| 1244 | SQLITE_CREATE_TEMP_TABLE, |
| 1245 | SQLITE_CREATE_VIEW, |
| 1246 | SQLITE_CREATE_TEMP_VIEW |
| 1247 | }; |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 1248 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1249 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1250 | goto begin_table_error; |
drh | e22a334 | 2003-04-22 20:30:37 | [diff] [blame] | 1251 | } |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 1252 | if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView], |
| 1253 | zName, 0, zDb) ){ |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1254 | goto begin_table_error; |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | #endif |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 1258 | |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 1259 | /* Make sure the new table name does not collide with an existing |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 | [diff] [blame] | 1260 | ** index or table name in the same database. Issue an error message if |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 | [diff] [blame] | 1261 | ** it does. The exception is if the statement being parsed was passed |
| 1262 | ** to an sqlite3_declare_vtab() call. In that case only the column names |
| 1263 | ** and types will be used, so there is no need to test for namespace |
| 1264 | ** collisions. |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 1265 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 1266 | if( !IN_SPECIAL_PARSE ){ |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 1267 | char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 | [diff] [blame] | 1268 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 1269 | goto begin_table_error; |
drh | faa5955 | 2005-12-29 23:33:54 | [diff] [blame] | 1270 | } |
dan | a16d106 | 2010-09-28 17:37:28 | [diff] [blame] | 1271 | pTable = sqlite3FindTable(db, zName, zDb); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 | [diff] [blame] | 1272 | if( pTable ){ |
| 1273 | if( !noErr ){ |
larrybr | 6e85b27 | 2022-02-07 01:09:49 | [diff] [blame] | 1274 | sqlite3ErrorMsg(pParse, "%s %T already exists", |
| 1275 | (IsView(pTable)? "view" : "table"), pName); |
dan | 7687c83 | 2011-04-09 15:39:02 | [diff] [blame] | 1276 | }else{ |
drh | 33c59ec | 2015-04-19 20:39:17 | [diff] [blame] | 1277 | assert( !db->init.busy || CORRUPT_DB ); |
dan | 7687c83 | 2011-04-09 15:39:02 | [diff] [blame] | 1278 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 1279 | sqlite3ForceNotReadOnly(pParse); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 | [diff] [blame] | 1280 | } |
| 1281 | goto begin_table_error; |
| 1282 | } |
drh | 8a8a0d1 | 2010-09-28 20:26:44 | [diff] [blame] | 1283 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 | [diff] [blame] | 1284 | sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
| 1285 | goto begin_table_error; |
| 1286 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1287 | } |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 | [diff] [blame] | 1288 | |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 | [diff] [blame] | 1289 | pTable = sqlite3DbMallocZero(db, sizeof(Table)); |
drh | 6d4abfb | 2001-10-22 02:58:08 | [diff] [blame] | 1290 | if( pTable==0 ){ |
drh | 4df86af | 2016-02-04 11:48:00 | [diff] [blame] | 1291 | assert( db->mallocFailed ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 1292 | pParse->rc = SQLITE_NOMEM_BKPT; |
danielk1977 | e004840 | 2004-06-15 16:51:01 | [diff] [blame] | 1293 | pParse->nErr++; |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1294 | goto begin_table_error; |
drh | 6d4abfb | 2001-10-22 02:58:08 | [diff] [blame] | 1295 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1296 | pTable->zName = zName; |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1297 | pTable->iPKey = -1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 1298 | pTable->pSchema = db->aDb[iDb].pSchema; |
drh | 79df778 | 2016-12-14 14:07:35 | [diff] [blame] | 1299 | pTable->nTabRef = 1; |
drh | d1417ee | 2017-06-06 18:20:43 | [diff] [blame] | 1300 | #ifdef SQLITE_DEFAULT_ROWEST |
| 1301 | pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST); |
| 1302 | #else |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 1303 | pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | d1417ee | 2017-06-06 18:20:43 | [diff] [blame] | 1304 | #endif |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 1305 | assert( pParse->pNewTable==0 ); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1306 | pParse->pNewTable = pTable; |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 1307 | |
| 1308 | /* Begin generating the code that will insert the table record into |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 1309 | ** the schema table. Note in particular that we must go ahead |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 1310 | ** and allocate the record number for the table entry now. Before any |
| 1311 | ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1312 | ** indices to be created and the table record must come before the |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 1313 | ** indices. Hence, the record number for the table must be allocated |
| 1314 | ** now. |
| 1315 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1316 | if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1317 | int addr1; |
drh | e321c29 | 2006-01-12 01:56:43 | [diff] [blame] | 1318 | int fileFormat; |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 1319 | int reg1, reg2, reg3; |
drh | 3c03afd | 2015-09-09 13:28:06 | [diff] [blame] | 1320 | /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */ |
| 1321 | static const char nullRow[] = { 6, 0, 0, 0, 0, 0 }; |
drh | 0dd5cda | 2015-06-16 16:39:01 | [diff] [blame] | 1322 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 | [diff] [blame] | 1323 | |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 | [diff] [blame] | 1324 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1325 | if( isVirtual ){ |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 1326 | sqlite3VdbeAddOp0(v, OP_VBegin); |
danielk1977 | 20b1eaf | 2006-07-26 16:22:14 | [diff] [blame] | 1327 | } |
| 1328 | #endif |
| 1329 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1330 | /* If the file format and encoding in the database have not been set, |
danielk1977 | 36963fd | 2005-02-19 08:18:05 | [diff] [blame] | 1331 | ** set them now. |
danielk1977 | d008cfe | 2004-06-19 02:22:10 | [diff] [blame] | 1332 | */ |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 1333 | assert( pParse->isCreate ); |
| 1334 | reg1 = pParse->u1.cr.regRowid = ++pParse->nMem; |
| 1335 | reg2 = pParse->u1.cr.regRoot = ++pParse->nMem; |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 1336 | reg3 = ++pParse->nMem; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 | [diff] [blame] | 1337 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); |
drh | fb98264 | 2007-08-30 01:19:59 | [diff] [blame] | 1338 | sqlite3VdbeUsesBtree(v, iDb); |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1339 | addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v); |
drh | e321c29 | 2006-01-12 01:56:43 | [diff] [blame] | 1340 | fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
drh | 76fe803 | 2006-07-11 14:17:51 | [diff] [blame] | 1341 | 1 : SQLITE_MAX_FILE_FORMAT; |
drh | 1861afc | 2016-02-01 21:48:34 | [diff] [blame] | 1342 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat); |
| 1343 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db)); |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 1344 | sqlite3VdbeJumpHere(v, addr1); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 | [diff] [blame] | 1345 | |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 1346 | /* This just creates a place-holder record in the sqlite_schema table. |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 1347 | ** The record created does not contain anything yet. It will be replaced |
| 1348 | ** by the real entry in code generated at sqlite3EndTable(). |
drh | b17131a | 2004-11-05 22:18:49 | [diff] [blame] | 1349 | ** |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 1350 | ** The rowid for the new entry is left in register pParse->u1.cr.regRowid. |
| 1351 | ** The root page of the new table is left in reg pParse->u1.cr.regRoot. |
drh | 0fa991b | 2009-03-21 16:19:26 | [diff] [blame] | 1352 | ** The rowid and root page number values are needed by the code that |
| 1353 | ** sqlite3EndTable will generate. |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 1354 | */ |
danielk1977 | f1a381e | 2006-06-16 08:01:02 | [diff] [blame] | 1355 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 1356 | if( isView || isVirtual ){ |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 1357 | sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 | [diff] [blame] | 1358 | }else |
| 1359 | #endif |
| 1360 | { |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 1361 | assert( !pParse->bReturning ); |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 1362 | pParse->u1.cr.addrCrTab = |
drh | 0f3f766 | 2017-08-18 14:34:28 | [diff] [blame] | 1363 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 | [diff] [blame] | 1364 | } |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 1365 | sqlite3OpenSchemaTable(pParse, iDb); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 1366 | sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); |
drh | 3c03afd | 2015-09-09 13:28:06 | [diff] [blame] | 1367 | sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 1368 | sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); |
| 1369 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 1370 | sqlite3VdbeAddOp0(v, OP_Close); |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 1371 | } |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1372 | |
| 1373 | /* Normal (non-error) return. */ |
| 1374 | return; |
| 1375 | |
| 1376 | /* If an error occurs, we jump here */ |
| 1377 | begin_table_error: |
drh | c0495e8 | 2021-07-22 21:11:06 | [diff] [blame] | 1378 | pParse->checkSchema = 1; |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1379 | sqlite3DbFree(db, zName); |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1380 | return; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1381 | } |
| 1382 | |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1383 | /* Set properties of a table column based on the (magical) |
| 1384 | ** name of the column. |
| 1385 | */ |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1386 | #if SQLITE_ENABLE_HIDDEN_COLUMNS |
drh | e611050 | 2015-12-31 15:34:03 | [diff] [blame] | 1387 | void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){ |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 1388 | if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){ |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1389 | pCol->colFlags |= COLFLAG_HIDDEN; |
drh | 6f6e60d | 2021-02-18 15:45:34 | [diff] [blame] | 1390 | if( pTab ) pTab->tabFlags |= TF_HasHidden; |
dan | ba68f8f | 2015-11-19 16:46:46 | [diff] [blame] | 1391 | }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){ |
| 1392 | pTab->tabFlags |= TF_OOOHidden; |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1393 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1394 | } |
drh | e611050 | 2015-12-31 15:34:03 | [diff] [blame] | 1395 | #endif |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1396 | |
drh | 2053f31 | 2021-01-12 20:16:31 | [diff] [blame] | 1397 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 | [diff] [blame] | 1398 | ** Clean up the data structures associated with the RETURNING clause. |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1399 | */ |
drh | 82fc1b6 | 2023-12-06 18:25:41 | [diff] [blame] | 1400 | static void sqlite3DeleteReturning(sqlite3 *db, void *pArg){ |
| 1401 | Returning *pRet = (Returning*)pArg; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1402 | Hash *pHash; |
| 1403 | pHash = &(db->aDb[1].pSchema->trigHash); |
dan | 94331d4 | 2023-10-26 16:05:57 | [diff] [blame] | 1404 | sqlite3HashInsert(pHash, pRet->zName, 0); |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1405 | sqlite3ExprListDelete(db, pRet->pReturnEL); |
| 1406 | sqlite3DbFree(db, pRet); |
| 1407 | } |
| 1408 | |
| 1409 | /* |
drh | 28828c5 | 2021-01-30 21:55:38 | [diff] [blame] | 1410 | ** Add the RETURNING clause to the parse currently underway. |
| 1411 | ** |
| 1412 | ** This routine creates a special TEMP trigger that will fire for each row |
| 1413 | ** of the DML statement. That TEMP trigger contains a single SELECT |
| 1414 | ** statement with a result set that is the argument of the RETURNING clause. |
| 1415 | ** The trigger has the Trigger.bReturning flag and an opcode of |
| 1416 | ** TK_RETURNING instead of TK_SELECT, so that the trigger code generator |
| 1417 | ** knows to handle it specially. The TEMP trigger is automatically |
| 1418 | ** removed at the end of the parse. |
| 1419 | ** |
| 1420 | ** When this routine is called, we do not yet know if the RETURNING clause |
| 1421 | ** is attached to a DELETE, INSERT, or UPDATE, so construct it as a |
| 1422 | ** RETURNING trigger instead. It will then be converted into the appropriate |
| 1423 | ** type on the first call to sqlite3TriggersExist(). |
drh | 2053f31 | 2021-01-12 20:16:31 | [diff] [blame] | 1424 | */ |
| 1425 | void sqlite3AddReturning(Parse *pParse, ExprList *pList){ |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1426 | Returning *pRet; |
| 1427 | Hash *pHash; |
| 1428 | sqlite3 *db = pParse->db; |
drh | e1c9a4e | 2021-02-07 23:28:20 | [diff] [blame] | 1429 | if( pParse->pNewTrigger ){ |
| 1430 | sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger"); |
| 1431 | }else{ |
drh | a84ead1 | 2023-03-17 00:01:32 | [diff] [blame] | 1432 | assert( pParse->bReturning==0 || pParse->ifNotExists ); |
drh | e1c9a4e | 2021-02-07 23:28:20 | [diff] [blame] | 1433 | } |
drh | d086aa0 | 2021-01-29 21:31:59 | [diff] [blame] | 1434 | pParse->bReturning = 1; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1435 | pRet = sqlite3DbMallocZero(db, sizeof(*pRet)); |
| 1436 | if( pRet==0 ){ |
| 1437 | sqlite3ExprListDelete(db, pList); |
| 1438 | return; |
| 1439 | } |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 1440 | assert( !pParse->isCreate ); |
| 1441 | pParse->u1.d.pReturning = pRet; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1442 | pRet->pParse = pParse; |
| 1443 | pRet->pReturnEL = pList; |
drh | 82fc1b6 | 2023-12-06 18:25:41 | [diff] [blame] | 1444 | sqlite3ParserAddCleanup(pParse, sqlite3DeleteReturning, pRet); |
drh | 6d0053c | 2021-03-09 19:32:37 | [diff] [blame] | 1445 | testcase( pParse->earlyCleanup ); |
drh | cf4108b | 2021-01-30 03:06:19 | [diff] [blame] | 1446 | if( db->mallocFailed ) return; |
dan | 94331d4 | 2023-10-26 16:05:57 | [diff] [blame] | 1447 | sqlite3_snprintf(sizeof(pRet->zName), pRet->zName, |
| 1448 | "sqlite_returning_%p", pParse); |
| 1449 | pRet->retTrig.zName = pRet->zName; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1450 | pRet->retTrig.op = TK_RETURNING; |
| 1451 | pRet->retTrig.tr_tm = TRIGGER_AFTER; |
| 1452 | pRet->retTrig.bReturning = 1; |
| 1453 | pRet->retTrig.pSchema = db->aDb[1].pSchema; |
drh | a476768 | 2021-04-27 13:04:18 | [diff] [blame] | 1454 | pRet->retTrig.pTabSchema = db->aDb[1].pSchema; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1455 | pRet->retTrig.step_list = &pRet->retTStep; |
drh | dac9a5f | 2021-01-29 21:18:46 | [diff] [blame] | 1456 | pRet->retTStep.op = TK_RETURNING; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1457 | pRet->retTStep.pTrig = &pRet->retTrig; |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 1458 | pRet->retTStep.pExprList = pList; |
drh | b835247 | 2021-01-29 19:32:17 | [diff] [blame] | 1459 | pHash = &(db->aDb[1].pSchema->trigHash); |
dan | 94331d4 | 2023-10-26 16:05:57 | [diff] [blame] | 1460 | assert( sqlite3HashFind(pHash, pRet->zName)==0 |
drh | a84ead1 | 2023-03-17 00:01:32 | [diff] [blame] | 1461 | || pParse->nErr || pParse->ifNotExists ); |
dan | 94331d4 | 2023-10-26 16:05:57 | [diff] [blame] | 1462 | if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig) |
drh | 0166df0 | 2021-01-30 12:07:32 | [diff] [blame] | 1463 | ==&pRet->retTrig ){ |
| 1464 | sqlite3OomFault(db); |
| 1465 | } |
drh | 2053f31 | 2021-01-12 20:16:31 | [diff] [blame] | 1466 | } |
drh | 03d69a6 | 2015-11-19 13:53:57 | [diff] [blame] | 1467 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1468 | /* |
| 1469 | ** Add a new column to the table currently being constructed. |
drh | d9b0257 | 2001-04-15 00:37:09 | [diff] [blame] | 1470 | ** |
| 1471 | ** The parser calls this routine once for each column declaration |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1472 | ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
drh | d9b0257 | 2001-04-15 00:37:09 | [diff] [blame] | 1473 | ** first to get things going. Then this routine is called for each |
| 1474 | ** column. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1475 | */ |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 1476 | void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1477 | Table *p; |
drh | 97fc3d0 | 2002-05-22 21:27:03 | [diff] [blame] | 1478 | int i; |
drh | a99db3b | 2004-06-19 14:49:12 | [diff] [blame] | 1479 | char *z; |
drh | 94eaafa | 2016-02-29 15:53:11 | [diff] [blame] | 1480 | char *zType; |
drh | c9b84a1 | 2002-06-20 11:36:48 | [diff] [blame] | 1481 | Column *pCol; |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 1482 | sqlite3 *db = pParse->db; |
drh | 7b3c514 | 2021-07-30 12:47:35 | [diff] [blame] | 1483 | Column *aNew; |
drh | c2df4d6 | 2021-07-30 23:30:30 | [diff] [blame] | 1484 | u8 eType = COLTYPE_CUSTOM; |
| 1485 | u8 szEst = 1; |
| 1486 | char affinity = SQLITE_AFF_BLOB; |
drh | 3e992d1 | 2021-01-01 19:17:01 | [diff] [blame] | 1487 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1488 | if( (p = pParse->pNewTable)==0 ) return; |
drh | bb4957f | 2008-03-20 14:03:29 | [diff] [blame] | 1489 | if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
drh | e5c941b | 2007-05-08 13:58:26 | [diff] [blame] | 1490 | sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); |
| 1491 | return; |
| 1492 | } |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 1493 | if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(&sName); |
drh | e48f261 | 2021-07-30 20:09:08 | [diff] [blame] | 1494 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1495 | /* Because keywords GENERATE ALWAYS can be converted into identifiers |
drh | e48f261 | 2021-07-30 20:09:08 | [diff] [blame] | 1496 | ** by the parser, we can sometimes end up with a typename that ends |
| 1497 | ** with "generated always". Check for this case and omit the surplus |
| 1498 | ** text. */ |
| 1499 | if( sType.n>=16 |
| 1500 | && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0 |
| 1501 | ){ |
| 1502 | sType.n -= 6; |
| 1503 | while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 1504 | if( sType.n>=9 |
| 1505 | && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0 |
| 1506 | ){ |
| 1507 | sType.n -= 9; |
| 1508 | while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--; |
| 1509 | } |
| 1510 | } |
| 1511 | |
drh | c2df4d6 | 2021-07-30 23:30:30 | [diff] [blame] | 1512 | /* Check for standard typenames. For standard typenames we will |
| 1513 | ** set the Column.eType field rather than storing the typename after |
| 1514 | ** the column name, in order to save space. */ |
| 1515 | if( sType.n>=3 ){ |
| 1516 | sqlite3DequoteToken(&sType); |
| 1517 | for(i=0; i<SQLITE_N_STDTYPE; i++){ |
| 1518 | if( sType.n==sqlite3StdTypeLen[i] |
| 1519 | && sqlite3_strnicmp(sType.z, sqlite3StdType[i], sType.n)==0 |
| 1520 | ){ |
| 1521 | sType.n = 0; |
| 1522 | eType = i+1; |
| 1523 | affinity = sqlite3StdTypeAffinity[i]; |
| 1524 | if( affinity<=SQLITE_AFF_TEXT ) szEst = 5; |
| 1525 | break; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
drh | 913306a | 2021-11-26 17:10:18 | [diff] [blame] | 1530 | z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) ); |
drh | 97fc3d0 | 2002-05-22 21:27:03 | [diff] [blame] | 1531 | if( z==0 ) return; |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 1532 | if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName); |
| 1533 | memcpy(z, sName.z, sName.n); |
| 1534 | z[sName.n] = 0; |
drh | 94eaafa | 2016-02-29 15:53:11 | [diff] [blame] | 1535 | sqlite3Dequote(z); |
drh | 3bdebae | 2025-02-09 19:49:46 | [diff] [blame] | 1536 | if( p->nCol && sqlite3ColumnIndex(p, z)>=0 ){ |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 1537 | sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
| 1538 | sqlite3DbFree(db, z); |
| 1539 | return; |
drh | 97fc3d0 | 2002-05-22 21:27:03 | [diff] [blame] | 1540 | } |
drh | 913306a | 2021-11-26 17:10:18 | [diff] [blame] | 1541 | aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0])); |
drh | 7b3c514 | 2021-07-30 12:47:35 | [diff] [blame] | 1542 | if( aNew==0 ){ |
| 1543 | sqlite3DbFree(db, z); |
| 1544 | return; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1545 | } |
drh | 7b3c514 | 2021-07-30 12:47:35 | [diff] [blame] | 1546 | p->aCol = aNew; |
drh | c9b84a1 | 2002-06-20 11:36:48 | [diff] [blame] | 1547 | pCol = &p->aCol[p->nCol]; |
| 1548 | memset(pCol, 0, sizeof(p->aCol[0])); |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 1549 | pCol->zCnName = z; |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 1550 | pCol->hName = sqlite3StrIHash(z); |
dan | ba68f8f | 2015-11-19 16:46:46 | [diff] [blame] | 1551 | sqlite3ColumnPropertiesFromName(p, pCol); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1552 | |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 1553 | if( sType.n==0 ){ |
drh | 2881ab6 | 2016-02-27 23:25:36 | [diff] [blame] | 1554 | /* If there is no type specified, columns have the default affinity |
drh | bbade8d | 2018-04-18 14:48:08 | [diff] [blame] | 1555 | ** 'BLOB' with a default size of 4 bytes. */ |
drh | c2df4d6 | 2021-07-30 23:30:30 | [diff] [blame] | 1556 | pCol->affinity = affinity; |
drh | b70f2ea | 2021-08-18 12:05:22 | [diff] [blame] | 1557 | pCol->eCType = eType; |
drh | c2df4d6 | 2021-07-30 23:30:30 | [diff] [blame] | 1558 | pCol->szEst = szEst; |
drh | bbade8d | 2018-04-18 14:48:08 | [diff] [blame] | 1559 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | c2df4d6 | 2021-07-30 23:30:30 | [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 | [diff] [blame] | 1564 | } |
drh | bbade8d | 2018-04-18 14:48:08 | [diff] [blame] | 1565 | #endif |
drh | 2881ab6 | 2016-02-27 23:25:36 | [diff] [blame] | 1566 | }else{ |
drh | ddb2b4a | 2016-03-25 12:10:32 | [diff] [blame] | 1567 | zType = z + sqlite3Strlen30(z) + 1; |
drh | 77441fa | 2021-07-30 18:39:59 | [diff] [blame] | 1568 | memcpy(zType, sType.z, sType.n); |
| 1569 | zType[sType.n] = 0; |
drh | a6dddd9 | 2016-04-18 15:46:14 | [diff] [blame] | 1570 | sqlite3Dequote(zType); |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1571 | pCol->affinity = sqlite3AffinityType(zType, pCol); |
drh | d756486 | 2016-03-22 20:05:09 | [diff] [blame] | 1572 | pCol->colFlags |= COLFLAG_HASTYPE; |
drh | 2881ab6 | 2016-02-27 23:25:36 | [diff] [blame] | 1573 | } |
drh | 66172ce | 2025-02-08 16:16:08 | [diff] [blame] | 1574 | if( p->nCol<=0xff ){ |
| 1575 | u8 h = pCol->hName % sizeof(p->aHx); |
| 1576 | p->aHx[h] = p->nCol; |
| 1577 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 | [diff] [blame] | 1578 | p->nCol++; |
drh | f95909c | 2019-10-18 18:33:25 | [diff] [blame] | 1579 | p->nNVCol++; |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 1580 | assert( pParse->isCreate ); |
| 1581 | pParse->u1.cr.constraintName.n = 0; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* |
drh | 382c024 | 2001-10-06 16:33:02 | [diff] [blame] | 1585 | ** This routine is called by the parser while in the middle of |
| 1586 | ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
| 1587 | ** been seen on a column. This routine sets the notNull flag on |
| 1588 | ** the column currently under construction. |
| 1589 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 1590 | void sqlite3AddNotNull(Parse *pParse, int onError){ |
drh | 382c024 | 2001-10-06 16:33:02 | [diff] [blame] | 1591 | Table *p; |
dan | 26e731c | 2018-01-29 16:22:39 | [diff] [blame] | 1592 | Column *pCol; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 1593 | p = pParse->pNewTable; |
| 1594 | if( p==0 || NEVER(p->nCol<1) ) return; |
dan | 26e731c | 2018-01-29 16:22:39 | [diff] [blame] | 1595 | pCol = &p->aCol[p->nCol-1]; |
| 1596 | pCol->notNull = (u8)onError; |
drh | 8b174f2 | 2017-02-22 15:11:36 | [diff] [blame] | 1597 | p->tabFlags |= TF_HasNotNull; |
dan | 26e731c | 2018-01-29 16:22:39 | [diff] [blame] | 1598 | |
| 1599 | /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created |
| 1600 | ** on this column. */ |
| 1601 | if( pCol->colFlags & COLFLAG_UNIQUE ){ |
| 1602 | Index *pIdx; |
| 1603 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1604 | assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None ); |
| 1605 | if( pIdx->aiColumn[0]==p->nCol-1 ){ |
| 1606 | pIdx->uniqNotNull = 1; |
| 1607 | } |
| 1608 | } |
| 1609 | } |
drh | 382c024 | 2001-10-06 16:33:02 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 | [diff] [blame] | 1613 | ** Scan the column type name zType (length nType) and return the |
| 1614 | ** associated affinity type. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1615 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1616 | ** This routine does a case-independent search of zType for the |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1617 | ** substrings in the following table. If one of the substrings is |
| 1618 | ** found, the corresponding affinity is returned. If zType contains |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1619 | ** more than one of the substrings, entries toward the top of |
| 1620 | ** the table take priority. For example, if zType is 'BLOBINT', |
drh | 8a51256 | 2005-11-14 22:29:05 | [diff] [blame] | 1621 | ** SQLITE_AFF_INTEGER is returned. |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1622 | ** |
| 1623 | ** Substring | Affinity |
| 1624 | ** -------------------------------- |
| 1625 | ** 'INT' | SQLITE_AFF_INTEGER |
| 1626 | ** 'CHAR' | SQLITE_AFF_TEXT |
| 1627 | ** 'CLOB' | SQLITE_AFF_TEXT |
| 1628 | ** 'TEXT' | SQLITE_AFF_TEXT |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 1629 | ** 'BLOB' | SQLITE_AFF_BLOB |
drh | 8a51256 | 2005-11-14 22:29:05 | [diff] [blame] | 1630 | ** 'REAL' | SQLITE_AFF_REAL |
| 1631 | ** 'FLOA' | SQLITE_AFF_REAL |
| 1632 | ** 'DOUB' | SQLITE_AFF_REAL |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1633 | ** |
| 1634 | ** If none of the substrings in the above table are found, |
| 1635 | ** SQLITE_AFF_NUMERIC is returned. |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 | [diff] [blame] | 1636 | */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1637 | char sqlite3AffinityType(const char *zIn, Column *pCol){ |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1638 | u32 h = 0; |
| 1639 | char aff = SQLITE_AFF_NUMERIC; |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1640 | const char *zChar = 0; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 | [diff] [blame] | 1641 | |
drh | 2f1e02e | 2016-03-09 02:12:44 | [diff] [blame] | 1642 | assert( zIn!=0 ); |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 1643 | while( zIn[0] ){ |
drh | 267721e | 2024-01-04 13:01:02 | [diff] [blame] | 1644 | u8 x = *(u8*)zIn; |
| 1645 | h = (h<<8) + sqlite3UpperToLower[x]; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1646 | zIn++; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 | [diff] [blame] | 1647 | if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 1648 | aff = SQLITE_AFF_TEXT; |
| 1649 | zChar = zIn; |
danielk1977 | 201f716 | 2005-02-01 02:13:29 | [diff] [blame] | 1650 | }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
| 1651 | aff = SQLITE_AFF_TEXT; |
| 1652 | }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
| 1653 | aff = SQLITE_AFF_TEXT; |
| 1654 | }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
drh | 8a51256 | 2005-11-14 22:29:05 | [diff] [blame] | 1655 | && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 1656 | aff = SQLITE_AFF_BLOB; |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1657 | if( zIn[0]=='(' ) zChar = zIn; |
drh | 8a51256 | 2005-11-14 22:29:05 | [diff] [blame] | 1658 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1659 | }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
| 1660 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1661 | aff = SQLITE_AFF_REAL; |
| 1662 | }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
| 1663 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1664 | aff = SQLITE_AFF_REAL; |
| 1665 | }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
| 1666 | && aff==SQLITE_AFF_NUMERIC ){ |
| 1667 | aff = SQLITE_AFF_REAL; |
| 1668 | #endif |
danielk1977 | 201f716 | 2005-02-01 02:13:29 | [diff] [blame] | 1669 | }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
drh | 8a51256 | 2005-11-14 22:29:05 | [diff] [blame] | 1670 | aff = SQLITE_AFF_INTEGER; |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1671 | break; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 | [diff] [blame] | 1672 | } |
| 1673 | } |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1674 | |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1675 | /* If pCol is not NULL, store an estimate of the field size. The |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1676 | ** estimate is scaled so that the size of an integer is 1. */ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1677 | if( pCol ){ |
| 1678 | int v = 0; /* default size is approx 4 bytes */ |
drh | 7ea31cc | 2014-09-18 14:36:00 | [diff] [blame] | 1679 | if( aff<SQLITE_AFF_NUMERIC ){ |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1680 | if( zChar ){ |
| 1681 | while( zChar[0] ){ |
| 1682 | if( sqlite3Isdigit(zChar[0]) ){ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1683 | /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */ |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1684 | sqlite3GetInt32(zChar, &v); |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1685 | break; |
| 1686 | } |
| 1687 | zChar++; |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 1688 | } |
drh | d3037a4 | 2013-10-04 18:29:25 | [diff] [blame] | 1689 | }else{ |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1690 | v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/ |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 1691 | } |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 1692 | } |
drh | bbade8d | 2018-04-18 14:48:08 | [diff] [blame] | 1693 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1694 | if( v>=sqlite3GlobalConfig.szSorterRef ){ |
| 1695 | pCol->colFlags |= COLFLAG_SORTERREF; |
| 1696 | } |
drh | bbade8d | 2018-04-18 14:48:08 | [diff] [blame] | 1697 | #endif |
dan | 2e3a5a8 | 2018-04-16 21:12:42 | [diff] [blame] | 1698 | v = v/4 + 1; |
| 1699 | if( v>255 ) v = 255; |
| 1700 | pCol->szEst = v; |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 1701 | } |
danielk1977 | b3dff96 | 2005-02-01 01:21:55 | [diff] [blame] | 1702 | return aff; |
danielk1977 | 52a83fb | 2005-01-31 12:56:44 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | /* |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 | [diff] [blame] | 1706 | ** The expression is the default value for the most recently added column |
| 1707 | ** of the table currently under construction. |
| 1708 | ** |
| 1709 | ** Default value expressions must be constant. Raise an exception if this |
| 1710 | ** is not the case. |
drh | d9b0257 | 2001-04-15 00:37:09 | [diff] [blame] | 1711 | ** |
| 1712 | ** This routine is called by the parser while in the middle of |
| 1713 | ** parsing a CREATE TABLE statement. |
drh | 7020f65 | 2000-06-03 18:06:52 | [diff] [blame] | 1714 | */ |
drh | 1be266b | 2017-12-24 00:18:47 | [diff] [blame] | 1715 | void sqlite3AddDefaultValue( |
| 1716 | Parse *pParse, /* Parsing context */ |
| 1717 | Expr *pExpr, /* The parsed expression of the default value */ |
| 1718 | const char *zStart, /* Start of the default value text */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1719 | const char *zEnd /* First character past end of default value text */ |
drh | 1be266b | 2017-12-24 00:18:47 | [diff] [blame] | 1720 | ){ |
drh | 7020f65 | 2000-06-03 18:06:52 | [diff] [blame] | 1721 | Table *p; |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 | [diff] [blame] | 1722 | Column *pCol; |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1723 | sqlite3 *db = pParse->db; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 1724 | p = pParse->pNewTable; |
| 1725 | if( p!=0 ){ |
drh | 014fff2 | 2020-01-08 22:22:36 | [diff] [blame] | 1726 | int isInit = db->init.busy && db->init.iDb!=1; |
drh | 42b9d7c | 2005-08-13 00:56:27 | [diff] [blame] | 1727 | pCol = &(p->aCol[p->nCol-1]); |
drh | 014fff2 | 2020-01-08 22:22:36 | [diff] [blame] | 1728 | if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){ |
drh | 42b9d7c | 2005-08-13 00:56:27 | [diff] [blame] | 1729 | sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 1730 | pCol->zCnName); |
drh | 7e7fd73 | 2019-10-22 13:59:23 | [diff] [blame] | 1731 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1732 | }else if( pCol->colFlags & COLFLAG_GENERATED ){ |
drh | ab0992f | 2019-10-23 03:53:10 | [diff] [blame] | 1733 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1734 | testcase( pCol->colFlags & COLFLAG_STORED ); |
drh | 7e7fd73 | 2019-10-22 13:59:23 | [diff] [blame] | 1735 | sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column"); |
| 1736 | #endif |
drh | 42b9d7c | 2005-08-13 00:56:27 | [diff] [blame] | 1737 | }else{ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 | [diff] [blame] | 1738 | /* A copy of pExpr is used instead of the original, as pExpr contains |
drh | 424981d | 2018-03-28 15:56:55 | [diff] [blame] | 1739 | ** tokens that point to volatile memory. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 | [diff] [blame] | 1740 | */ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1741 | Expr x, *pDfltExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 | [diff] [blame] | 1742 | memset(&x, 0, sizeof(x)); |
| 1743 | x.op = TK_SPAN; |
drh | 9b2e043 | 2017-12-27 19:43:22 | [diff] [blame] | 1744 | x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd); |
drh | 1be266b | 2017-12-24 00:18:47 | [diff] [blame] | 1745 | x.pLeft = pExpr; |
drh | 94fa9c4 | 2016-02-27 21:16:04 | [diff] [blame] | 1746 | x.flags = EP_Skip; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1747 | pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE); |
drh | 94fa9c4 | 2016-02-27 21:16:04 | [diff] [blame] | 1748 | sqlite3DbFree(db, x.u.zToken); |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1749 | sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr); |
drh | 42b9d7c | 2005-08-13 00:56:27 | [diff] [blame] | 1750 | } |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 | [diff] [blame] | 1751 | } |
dan | 8900a48 | 2018-09-05 14:36:05 | [diff] [blame] | 1752 | if( IN_RENAME_OBJECT ){ |
| 1753 | sqlite3RenameExprUnmap(pParse, pExpr); |
| 1754 | } |
drh | 1be266b | 2017-12-24 00:18:47 | [diff] [blame] | 1755 | sqlite3ExprDelete(db, pExpr); |
drh | 7020f65 | 2000-06-03 18:06:52 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* |
drh | 153110a | 2015-11-01 21:19:13 | [diff] [blame] | 1759 | ** Backwards Compatibility Hack: |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1760 | ** |
drh | 153110a | 2015-11-01 21:19:13 | [diff] [blame] | 1761 | ** Historical versions of SQLite accepted strings as column names in |
| 1762 | ** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example: |
| 1763 | ** |
| 1764 | ** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim) |
| 1765 | ** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC); |
| 1766 | ** |
| 1767 | ** This is goofy. But to preserve backwards compatibility we continue to |
| 1768 | ** accept it. This routine does the necessary conversion. It converts |
| 1769 | ** the expression given in its argument from a TK_STRING into a TK_ID |
| 1770 | ** if the expression is just a TK_STRING with an optional COLLATE clause. |
drh | 6c59136 | 2019-04-27 20:16:42 | [diff] [blame] | 1771 | ** If the expression is anything other than TK_STRING, the expression is |
drh | 153110a | 2015-11-01 21:19:13 | [diff] [blame] | 1772 | ** unchanged. |
| 1773 | */ |
| 1774 | static void sqlite3StringToId(Expr *p){ |
| 1775 | if( p->op==TK_STRING ){ |
| 1776 | p->op = TK_ID; |
| 1777 | }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){ |
| 1778 | p->pLeft->op = TK_ID; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | /* |
drh | f4b1d8d | 2019-10-22 15:45:03 | [diff] [blame] | 1783 | ** Tag the given column as being part of the PRIMARY KEY |
| 1784 | */ |
| 1785 | static void makeColumnPartOfPrimaryKey(Parse *pParse, Column *pCol){ |
| 1786 | pCol->colFlags |= COLFLAG_PRIMKEY; |
| 1787 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1788 | if( pCol->colFlags & COLFLAG_GENERATED ){ |
| 1789 | testcase( pCol->colFlags & COLFLAG_VIRTUAL ); |
| 1790 | testcase( pCol->colFlags & COLFLAG_STORED ); |
| 1791 | sqlite3ErrorMsg(pParse, |
| 1792 | "generated columns cannot be part of the PRIMARY KEY"); |
| 1793 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1794 | #endif |
drh | f4b1d8d | 2019-10-22 15:45:03 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1798 | ** Designate the PRIMARY KEY for the table. pList is a list of names |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1799 | ** of columns that form the primary key. If pList is NULL, then the |
| 1800 | ** most recently added column of the table is the primary key. |
| 1801 | ** |
| 1802 | ** A table can have at most one primary key. If the table already has |
| 1803 | ** a primary key (and this is the second primary key) then create an |
| 1804 | ** error. |
| 1805 | ** |
| 1806 | ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 1807 | ** then we will try to use that column as the rowid. Set the Table.iPKey |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1808 | ** field of the table under construction to be the index of the |
| 1809 | ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
| 1810 | ** no INTEGER PRIMARY KEY. |
| 1811 | ** |
| 1812 | ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
| 1813 | ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
| 1814 | */ |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 1815 | void sqlite3AddPrimaryKey( |
| 1816 | Parse *pParse, /* Parsing context */ |
| 1817 | ExprList *pList, /* List of field names to be indexed */ |
| 1818 | int onError, /* What to do with a uniqueness conflict */ |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 1819 | int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
| 1820 | int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 1821 | ){ |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1822 | Table *pTab = pParse->pNewTable; |
drh | d756486 | 2016-03-22 20:05:09 | [diff] [blame] | 1823 | Column *pCol = 0; |
drh | 78100cc | 2003-08-23 22:40:53 | [diff] [blame] | 1824 | int iCol = -1, i; |
drh | 8ea30bf | 2013-10-22 01:18:17 | [diff] [blame] | 1825 | int nTerm; |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 1826 | if( pTab==0 ) goto primary_key_exit; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1827 | if( pTab->tabFlags & TF_HasPrimaryKey ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1828 | sqlite3ErrorMsg(pParse, |
drh | f7a9e1a | 2004-02-22 18:40:56 | [diff] [blame] | 1829 | "table \"%s\" has more than one primary key", pTab->zName); |
drh | e0194f2 | 2003-02-26 13:52:51 | [diff] [blame] | 1830 | goto primary_key_exit; |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1831 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1832 | pTab->tabFlags |= TF_HasPrimaryKey; |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1833 | if( pList==0 ){ |
| 1834 | iCol = pTab->nCol - 1; |
drh | d756486 | 2016-03-22 20:05:09 | [diff] [blame] | 1835 | pCol = &pTab->aCol[iCol]; |
drh | f4b1d8d | 2019-10-22 15:45:03 | [diff] [blame] | 1836 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | 8ea30bf | 2013-10-22 01:18:17 | [diff] [blame] | 1837 | nTerm = 1; |
drh | 78100cc | 2003-08-23 22:40:53 | [diff] [blame] | 1838 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 | [diff] [blame] | 1839 | nTerm = pList->nExpr; |
| 1840 | for(i=0; i<nTerm; i++){ |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 1841 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 | [diff] [blame] | 1842 | assert( pCExpr!=0 ); |
drh | 153110a | 2015-11-01 21:19:13 | [diff] [blame] | 1843 | sqlite3StringToId(pCExpr); |
drh | 7d3d9da | 2015-09-01 00:42:52 | [diff] [blame] | 1844 | if( pCExpr->op==TK_ID ){ |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 1845 | assert( !ExprHasProperty(pCExpr, EP_IntValue) ); |
drh | 9d90a3a | 2025-02-08 14:15:42 | [diff] [blame] | 1846 | iCol = sqlite3ColumnIndex(pTab, pCExpr->u.zToken); |
| 1847 | if( iCol>=0 ){ |
| 1848 | pCol = &pTab->aCol[iCol]; |
| 1849 | makeColumnPartOfPrimaryKey(pParse, pCol); |
drh | d3d39e9 | 2004-05-20 22:16:29 | [diff] [blame] | 1850 | } |
drh | 78100cc | 2003-08-23 22:40:53 | [diff] [blame] | 1851 | } |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1852 | } |
| 1853 | } |
drh | 8ea30bf | 2013-10-22 01:18:17 | [diff] [blame] | 1854 | if( nTerm==1 |
drh | d756486 | 2016-03-22 20:05:09 | [diff] [blame] | 1855 | && pCol |
drh | b70f2ea | 2021-08-18 12:05:22 | [diff] [blame] | 1856 | && pCol->eCType==COLTYPE_INTEGER |
drh | bc622bc | 2015-08-24 15:39:42 | [diff] [blame] | 1857 | && sortOrder!=SQLITE_SO_DESC |
drh | 8ea30bf | 2013-10-22 01:18:17 | [diff] [blame] | 1858 | ){ |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 1859 | if( IN_RENAME_OBJECT && pList ){ |
dan | 2381f6d | 2019-03-20 16:58:21 | [diff] [blame] | 1860 | Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 1861 | sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr); |
dan | 987db76 | 2018-08-14 20:18:50 | [diff] [blame] | 1862 | } |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1863 | pTab->iPKey = iCol; |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 1864 | pTab->keyConf = (u8)onError; |
drh | 7d10d5a | 2008-08-20 16:35:10 | [diff] [blame] | 1865 | assert( autoInc==0 || autoInc==1 ); |
| 1866 | pTab->tabFlags |= autoInc*TF_Autoincrement; |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 1867 | if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags; |
drh | 34ab941 | 2019-12-19 17:42:27 | [diff] [blame] | 1868 | (void)sqlite3HasExplicitNulls(pParse, pList); |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 1869 | }else if( autoInc ){ |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 1870 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
drh | 205f48e | 2004-11-05 00:43:11 | [diff] [blame] | 1871 | sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
| 1872 | "INTEGER PRIMARY KEY"); |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 1873 | #endif |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1874 | }else{ |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 1875 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, |
| 1876 | 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY); |
drh | e0194f2 | 2003-02-26 13:52:51 | [diff] [blame] | 1877 | pList = 0; |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1878 | } |
drh | e0194f2 | 2003-02-26 13:52:51 | [diff] [blame] | 1879 | |
| 1880 | primary_key_exit: |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1881 | sqlite3ExprListDelete(pParse->db, pList); |
drh | e0194f2 | 2003-02-26 13:52:51 | [diff] [blame] | 1882 | return; |
drh | 4a32431 | 2001-12-21 14:30:42 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 1886 | ** Add a new CHECK constraint to the table currently under construction. |
| 1887 | */ |
| 1888 | void sqlite3AddCheckConstraint( |
drh | 92e21ef | 2020-08-27 18:36:30 | [diff] [blame] | 1889 | Parse *pParse, /* Parsing context */ |
| 1890 | Expr *pCheckExpr, /* The check expression */ |
| 1891 | const char *zStart, /* Opening "(" */ |
| 1892 | const char *zEnd /* Closing ")" */ |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 1893 | ){ |
| 1894 | #ifndef SQLITE_OMIT_CHECK |
| 1895 | Table *pTab = pParse->pNewTable; |
drh | c9bbb01 | 2014-05-21 08:48:18 | [diff] [blame] | 1896 | sqlite3 *db = pParse->db; |
| 1897 | if( pTab && !IN_DECLARE_VTAB |
| 1898 | && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt) |
| 1899 | ){ |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 1900 | pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr); |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 1901 | assert( pParse->isCreate ); |
| 1902 | if( pParse->u1.cr.constraintName.n ){ |
| 1903 | sqlite3ExprListSetName(pParse, pTab->pCheck, |
| 1904 | &pParse->u1.cr.constraintName, 1); |
drh | 92e21ef | 2020-08-27 18:36:30 | [diff] [blame] | 1905 | }else{ |
| 1906 | Token t; |
| 1907 | for(zStart++; sqlite3Isspace(zStart[0]); zStart++){} |
| 1908 | while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; } |
| 1909 | t.z = zStart; |
| 1910 | t.n = (int)(zEnd - t.z); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1911 | sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1); |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 1912 | } |
drh | 33e619f | 2009-05-28 01:00:55 | [diff] [blame] | 1913 | }else |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 1914 | #endif |
drh | 33e619f | 2009-05-28 01:00:55 | [diff] [blame] | 1915 | { |
drh | 2938f92 | 2012-03-07 19:13:29 | [diff] [blame] | 1916 | sqlite3ExprDelete(pParse->db, pCheckExpr); |
drh | 33e619f | 2009-05-28 01:00:55 | [diff] [blame] | 1917 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 | [diff] [blame] | 1921 | ** Set the collation function of the most recently parsed table column |
| 1922 | ** to the CollSeq given. |
drh | 8e2ca02 | 2002-06-17 17:07:19 | [diff] [blame] | 1923 | */ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 | [diff] [blame] | 1924 | void sqlite3AddCollateType(Parse *pParse, Token *pToken){ |
drh | 8e2ca02 | 2002-06-17 17:07:19 | [diff] [blame] | 1925 | Table *p; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 1926 | int i; |
danielk1977 | 3900250 | 2007-11-12 09:50:26 | [diff] [blame] | 1927 | char *zColl; /* Dequoted name of collation sequence */ |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1928 | sqlite3 *db; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 | [diff] [blame] | 1929 | |
dan | 936a305 | 2020-10-12 15:27:50 | [diff] [blame] | 1930 | if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 1931 | i = p->nCol-1; |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 1932 | db = pParse->db; |
| 1933 | zColl = sqlite3NameFromToken(db, pToken); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 | [diff] [blame] | 1934 | if( !zColl ) return; |
| 1935 | |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 1936 | if( sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 1937 | Index *pIdx; |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 1938 | sqlite3ColumnSetColl(db, &p->aCol[i], zColl); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1939 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 1940 | /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
| 1941 | ** then an index may have been created on this column before the |
| 1942 | ** collation type was added. Correct this if it is the case. |
| 1943 | */ |
| 1944 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 1945 | assert( pIdx->nKeyCol==1 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 1946 | if( pIdx->aiColumn[0]==i ){ |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 1947 | pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | } |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 1951 | sqlite3DbFree(db, zColl); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 | [diff] [blame] | 1952 | } |
| 1953 | |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1954 | /* Change the most recently parsed column to be a GENERATED ALWAYS AS |
| 1955 | ** column. |
| 1956 | */ |
| 1957 | void sqlite3AddGenerated(Parse *pParse, Expr *pExpr, Token *pType){ |
| 1958 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
| 1959 | u8 eType = COLFLAG_VIRTUAL; |
| 1960 | Table *pTab = pParse->pNewTable; |
| 1961 | Column *pCol; |
drh | f68bf5f | 2019-12-04 03:31:29 | [diff] [blame] | 1962 | if( pTab==0 ){ |
| 1963 | /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */ |
| 1964 | goto generated_done; |
| 1965 | } |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1966 | pCol = &(pTab->aCol[pTab->nCol-1]); |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1967 | if( IN_DECLARE_VTAB ){ |
| 1968 | sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns"); |
| 1969 | goto generated_done; |
| 1970 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1971 | if( pCol->iDflt>0 ) goto generated_error; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1972 | if( pType ){ |
| 1973 | if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){ |
| 1974 | /* no-op */ |
| 1975 | }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){ |
| 1976 | eType = COLFLAG_STORED; |
| 1977 | }else{ |
| 1978 | goto generated_error; |
| 1979 | } |
| 1980 | } |
drh | f95909c | 2019-10-18 18:33:25 | [diff] [blame] | 1981 | if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1982 | pCol->colFlags |= eType; |
drh | c143114 | 2019-10-17 17:54:05 | [diff] [blame] | 1983 | assert( TF_HasVirtual==COLFLAG_VIRTUAL ); |
| 1984 | assert( TF_HasStored==COLFLAG_STORED ); |
| 1985 | pTab->tabFlags |= eType; |
drh | a0e16a2 | 2019-10-27 22:22:24 | [diff] [blame] | 1986 | if( pCol->colFlags & COLFLAG_PRIMKEY ){ |
| 1987 | makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */ |
| 1988 | } |
drh | fe83892 | 2022-12-21 14:13:49 | [diff] [blame] | 1989 | if( ALWAYS(pExpr) && pExpr->op==TK_ID ){ |
| 1990 | /* The value of a generated column needs to be a real expression, not |
| 1991 | ** just a reference to another column, in order for covering index |
| 1992 | ** optimizations to work correctly. So if the value is not an expression, |
| 1993 | ** turn it into one by adding a unary "+" operator. */ |
| 1994 | pExpr = sqlite3PExpr(pParse, TK_UPLUS, pExpr, 0); |
| 1995 | } |
drh | cad225d | 2023-03-07 23:47:38 | [diff] [blame] | 1996 | if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity; |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 1997 | sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr); |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1998 | pExpr = 0; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 1999 | goto generated_done; |
| 2000 | |
| 2001 | generated_error: |
drh | 7e7fd73 | 2019-10-22 13:59:23 | [diff] [blame] | 2002 | sqlite3ErrorMsg(pParse, "error in generated column \"%s\"", |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2003 | pCol->zCnName); |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2004 | generated_done: |
| 2005 | sqlite3ExprDelete(pParse->db, pExpr); |
| 2006 | #else |
| 2007 | /* Throw and error for the GENERATED ALWAYS AS clause if the |
| 2008 | ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */ |
drh | 7e7fd73 | 2019-10-22 13:59:23 | [diff] [blame] | 2009 | sqlite3ErrorMsg(pParse, "generated columns not supported"); |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2010 | sqlite3ExprDelete(pParse->db, pExpr); |
| 2011 | #endif |
| 2012 | } |
| 2013 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 | [diff] [blame] | 2014 | /* |
drh | 3f7d4e4 | 2004-07-24 14:35:58 | [diff] [blame] | 2015 | ** Generate code that will increment the schema cookie. |
drh | 50e5dad | 2001-09-15 00:57:28 | [diff] [blame] | 2016 | ** |
| 2017 | ** The schema cookie is used to determine when the schema for the |
| 2018 | ** database changes. After each schema change, the cookie value |
| 2019 | ** changes. When a process first reads the schema it records the |
| 2020 | ** cookie. Thereafter, whenever it goes to access the database, |
| 2021 | ** it checks the cookie to make sure the schema has not changed |
| 2022 | ** since it was last read. |
| 2023 | ** |
| 2024 | ** This plan is not completely bullet-proof. It is possible for |
| 2025 | ** the schema to change multiple times and for the cookie to be |
| 2026 | ** set back to prior value. But schema changes are infrequent |
| 2027 | ** and the probability of hitting the same cookie value is only |
| 2028 | ** 1 chance in 2^32. So we're safe enough. |
drh | 96fdcb4 | 2016-09-27 00:09:33 | [diff] [blame] | 2029 | ** |
| 2030 | ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments |
| 2031 | ** the schema-version whenever the schema changes. |
drh | 50e5dad | 2001-09-15 00:57:28 | [diff] [blame] | 2032 | */ |
drh | 9cbf342 | 2008-01-17 16:22:13 | [diff] [blame] | 2033 | void sqlite3ChangeCookie(Parse *pParse, int iDb){ |
drh | 9cbf342 | 2008-01-17 16:22:13 | [diff] [blame] | 2034 | sqlite3 *db = pParse->db; |
| 2035 | Vdbe *v = pParse->pVdbe; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 2036 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2037 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, |
drh | 3517b31 | 2018-04-09 00:46:42 | [diff] [blame] | 2038 | (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie)); |
drh | 50e5dad | 2001-09-15 00:57:28 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | /* |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2042 | ** Measure the number of characters needed to output the given |
| 2043 | ** identifier. The number returned includes any quotes used |
| 2044 | ** but does not include the null terminator. |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2045 | ** |
| 2046 | ** The estimate is conservative. It might be larger that what is |
| 2047 | ** really needed. |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2048 | */ |
| 2049 | static int identLength(const char *z){ |
| 2050 | int n; |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 2051 | for(n=0; *z; n++, z++){ |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2052 | if( *z=='"' ){ n++; } |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2053 | } |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2054 | return n + 2; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2058 | ** The first parameter is a pointer to an output buffer. The second |
danielk1977 | 1b870de | 2009-03-14 08:37:23 | [diff] [blame] | 2059 | ** parameter is a pointer to an integer that contains the offset at |
| 2060 | ** which to write into the output buffer. This function copies the |
| 2061 | ** nul-terminated string pointed to by the third parameter, zSignedIdent, |
| 2062 | ** to the specified offset in the buffer and updates *pIdx to refer |
| 2063 | ** to the first byte after the last byte written before returning. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2064 | ** |
larrybr | 55be216 | 2023-06-07 17:03:22 | [diff] [blame] | 2065 | ** If the string zSignedIdent consists entirely of alphanumeric |
danielk1977 | 1b870de | 2009-03-14 08:37:23 | [diff] [blame] | 2066 | ** characters, does not begin with a digit and is not an SQL keyword, |
| 2067 | ** then it is copied to the output buffer exactly as it is. Otherwise, |
| 2068 | ** it is quoted using double-quotes. |
| 2069 | */ |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2070 | static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
drh | 4c755c0 | 2004-08-08 20:22:17 | [diff] [blame] | 2071 | unsigned char *zIdent = (unsigned char*)zSignedIdent; |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 2072 | int i, j, needQuote; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2073 | i = *pIdx; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 | [diff] [blame] | 2074 | |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 2075 | for(j=0; zIdent[j]; j++){ |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 | [diff] [blame] | 2076 | if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
drh | 17f7193 | 2002-02-21 12:01:27 | [diff] [blame] | 2077 | } |
drh | c740752 | 2014-01-10 20:38:12 | [diff] [blame] | 2078 | needQuote = sqlite3Isdigit(zIdent[0]) |
| 2079 | || sqlite3KeywordCode(zIdent, j)!=TK_ID |
| 2080 | || zIdent[j]!=0 |
| 2081 | || j==0; |
danielk1977 | 1b870de | 2009-03-14 08:37:23 | [diff] [blame] | 2082 | |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2083 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2084 | for(j=0; zIdent[j]; j++){ |
| 2085 | z[i++] = zIdent[j]; |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2086 | if( zIdent[j]=='"' ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2087 | } |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2088 | if( needQuote ) z[i++] = '"'; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2089 | z[i] = 0; |
| 2090 | *pIdx = i; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | ** Generate a CREATE TABLE statement appropriate for the given |
| 2095 | ** table. Memory to hold the text of the statement is obtained |
| 2096 | ** from sqliteMalloc() and must be freed by the calling function. |
| 2097 | */ |
drh | 1d34fde | 2009-02-03 15:50:33 | [diff] [blame] | 2098 | static char *createTableStmt(sqlite3 *db, Table *p){ |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2099 | int i, k, len; |
| 2100 | i64 n; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2101 | char *zStmt; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2102 | char *zSep, *zSep2, *zEnd; |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2103 | Column *pCol; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2104 | n = 0; |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2105 | for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2106 | n += identLength(pCol->zCnName) + 5; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2107 | } |
| 2108 | n += identLength(p->zName); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2109 | if( n<50 ){ |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2110 | zSep = ""; |
| 2111 | zSep2 = ","; |
| 2112 | zEnd = ")"; |
| 2113 | }else{ |
| 2114 | zSep = "\n "; |
| 2115 | zSep2 = ",\n "; |
| 2116 | zEnd = "\n)"; |
| 2117 | } |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 2118 | n += 35 + 6*p->nCol; |
drh | b975598 | 2010-07-24 16:34:37 | [diff] [blame] | 2119 | zStmt = sqlite3DbMallocRaw(0, n); |
drh | 820a906 | 2008-01-31 13:35:48 | [diff] [blame] | 2120 | if( zStmt==0 ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 2121 | sqlite3OomFault(db); |
drh | 820a906 | 2008-01-31 13:35:48 | [diff] [blame] | 2122 | return 0; |
| 2123 | } |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2124 | assert( n>14 && n<=0x7fffffff ); |
| 2125 | memcpy(zStmt, "CREATE TABLE ", 13); |
| 2126 | k = 13; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2127 | identPut(zStmt, &k, p->zName); |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2128 | zStmt[k++] = '('; |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 2129 | for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2130 | static const char * const azType[] = { |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 2131 | /* SQLITE_AFF_BLOB */ "", |
drh | 7ea31cc | 2014-09-18 14:36:00 | [diff] [blame] | 2132 | /* SQLITE_AFF_TEXT */ " TEXT", |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2133 | /* SQLITE_AFF_NUMERIC */ " NUM", |
| 2134 | /* SQLITE_AFF_INTEGER */ " INT", |
drh | 00d6b27 | 2022-12-15 20:03:08 | [diff] [blame] | 2135 | /* SQLITE_AFF_REAL */ " REAL", |
| 2136 | /* SQLITE_AFF_FLEXNUM */ " NUM", |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2137 | }; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2138 | const char *zType; |
| 2139 | |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2140 | len = sqlite3Strlen30(zSep); |
| 2141 | assert( k+len<n ); |
| 2142 | memcpy(&zStmt[k], zSep, len); |
| 2143 | k += len; |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2144 | zSep = zSep2; |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2145 | identPut(zStmt, &k, pCol->zCnName); |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2146 | assert( k<n ); |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 2147 | assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 ); |
| 2148 | assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) ); |
| 2149 | testcase( pCol->affinity==SQLITE_AFF_BLOB ); |
drh | 7ea31cc | 2014-09-18 14:36:00 | [diff] [blame] | 2150 | testcase( pCol->affinity==SQLITE_AFF_TEXT ); |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2151 | testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); |
| 2152 | testcase( pCol->affinity==SQLITE_AFF_INTEGER ); |
| 2153 | testcase( pCol->affinity==SQLITE_AFF_REAL ); |
drh | 00d6b27 | 2022-12-15 20:03:08 | [diff] [blame] | 2154 | testcase( pCol->affinity==SQLITE_AFF_FLEXNUM ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2155 | |
drh | 05883a3 | 2015-06-02 15:32:08 | [diff] [blame] | 2156 | zType = azType[pCol->affinity - SQLITE_AFF_BLOB]; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2157 | len = sqlite3Strlen30(zType); |
drh | 00d6b27 | 2022-12-15 20:03:08 | [diff] [blame] | 2158 | assert( pCol->affinity==SQLITE_AFF_BLOB |
| 2159 | || pCol->affinity==SQLITE_AFF_FLEXNUM |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2160 | || pCol->affinity==sqlite3AffinityType(zType, 0) ); |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2161 | assert( k+len<n ); |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 2162 | memcpy(&zStmt[k], zType, len); |
| 2163 | k += len; |
| 2164 | assert( k<=n ); |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2165 | } |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 2166 | len = sqlite3Strlen30(zEnd); |
| 2167 | assert( k+len<n ); |
| 2168 | memcpy(&zStmt[k], zEnd, len+1); |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2169 | return zStmt; |
| 2170 | } |
| 2171 | |
| 2172 | /* |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2173 | ** Resize an Index object to hold N columns total. Return SQLITE_OK |
| 2174 | ** on success and SQLITE_NOMEM on an OOM error. |
| 2175 | */ |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 2176 | static int resizeIndexObject(Parse *pParse, Index *pIdx, int N){ |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2177 | char *zExtra; |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 2178 | u64 nByte; |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 2179 | sqlite3 *db; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2180 | if( pIdx->nColumn>=N ) return SQLITE_OK; |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 2181 | db = pParse->db; |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 2182 | assert( N>0 ); |
| 2183 | assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ ); |
| 2184 | testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] ); |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2185 | assert( pIdx->isResized==0 ); |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 2186 | nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*(u64)N; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2187 | zExtra = sqlite3DbMallocZero(db, nByte); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 2188 | if( zExtra==0 ) return SQLITE_NOMEM_BKPT; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2189 | memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn); |
drh | f19aa5f | 2015-12-30 16:51:20 | [diff] [blame] | 2190 | pIdx->azColl = (const char**)zExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2191 | zExtra += sizeof(char*)*N; |
dan | b5a6923 | 2020-09-15 20:48:30 | [diff] [blame] | 2192 | memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1)); |
| 2193 | pIdx->aiRowLogEst = (LogEst*)zExtra; |
| 2194 | zExtra += sizeof(LogEst)*N; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2195 | memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn); |
| 2196 | pIdx->aiColumn = (i16*)zExtra; |
| 2197 | zExtra += sizeof(i16)*N; |
| 2198 | memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn); |
| 2199 | pIdx->aSortOrder = (u8*)zExtra; |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 2200 | pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */ |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2201 | pIdx->isResized = 1; |
| 2202 | return SQLITE_OK; |
| 2203 | } |
| 2204 | |
| 2205 | /* |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2206 | ** Estimate the total row width for a table. |
| 2207 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2208 | static void estimateTableWidth(Table *pTab){ |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2209 | unsigned wTable = 0; |
| 2210 | const Column *pTabCol; |
| 2211 | int i; |
| 2212 | for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){ |
| 2213 | wTable += pTabCol->szEst; |
| 2214 | } |
| 2215 | if( pTab->iPKey<0 ) wTable++; |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2216 | pTab->szTabRow = sqlite3LogEst(wTable*4); |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | /* |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2220 | ** Estimate the average size of a row for an index. |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2221 | */ |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2222 | static void estimateIndexWidth(Index *pIdx){ |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 2223 | unsigned wIndex = 0; |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2224 | int i; |
| 2225 | const Column *aCol = pIdx->pTable->aCol; |
| 2226 | for(i=0; i<pIdx->nColumn; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 2227 | i16 x = pIdx->aiColumn[i]; |
| 2228 | assert( x<pIdx->pTable->nCol ); |
drh | 60fd5c3 | 2023-05-17 15:46:46 | [diff] [blame] | 2229 | wIndex += x<0 ? 1 : aCol[x].szEst; |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2230 | } |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2231 | pIdx->szIdxRow = sqlite3LogEst(wIndex*4); |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2232 | } |
| 2233 | |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2234 | /* Return true if column number x is any of the first nCol entries of aiCol[]. |
| 2235 | ** This is used to determine if the column number x appears in any of the |
| 2236 | ** first nCol entries of an index. |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2237 | */ |
| 2238 | static int hasColumn(const i16 *aiCol, int nCol, int x){ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2239 | while( nCol-- > 0 ){ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2240 | if( x==*(aiCol++) ){ |
| 2241 | return 1; |
| 2242 | } |
| 2243 | } |
| 2244 | return 0; |
| 2245 | } |
| 2246 | |
| 2247 | /* |
drh | c19b63c | 2019-04-29 13:30:16 | [diff] [blame] | 2248 | ** Return true if any of the first nKey entries of index pIdx exactly |
| 2249 | ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID |
| 2250 | ** PRIMARY KEY index. pIdx is an index on the same table. pIdx may |
| 2251 | ** or may not be the same index as pPk. |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2252 | ** |
drh | c19b63c | 2019-04-29 13:30:16 | [diff] [blame] | 2253 | ** The first nKey entries of pIdx are guaranteed to be ordinary columns, |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2254 | ** not a rowid or expression. |
| 2255 | ** |
| 2256 | ** This routine differs from hasColumn() in that both the column and the |
| 2257 | ** collating sequence must match for this routine, but for hasColumn() only |
| 2258 | ** the column name must match. |
| 2259 | */ |
drh | c19b63c | 2019-04-29 13:30:16 | [diff] [blame] | 2260 | static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2261 | int i, j; |
drh | c19b63c | 2019-04-29 13:30:16 | [diff] [blame] | 2262 | assert( nKey<=pIdx->nColumn ); |
| 2263 | assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) ); |
| 2264 | assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); |
| 2265 | assert( pPk->pTable->tabFlags & TF_WithoutRowid ); |
| 2266 | assert( pPk->pTable==pIdx->pTable ); |
| 2267 | testcase( pPk==pIdx ); |
| 2268 | j = pPk->aiColumn[iCol]; |
| 2269 | assert( j!=XN_ROWID && j!=XN_EXPR ); |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2270 | for(i=0; i<nKey; i++){ |
drh | c19b63c | 2019-04-29 13:30:16 | [diff] [blame] | 2271 | assert( pIdx->aiColumn[i]>=0 || j>=0 ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2272 | if( pIdx->aiColumn[i]==j |
drh | c19b63c | 2019-04-29 13:30:16 | [diff] [blame] | 2273 | && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2274 | ){ |
| 2275 | return 1; |
| 2276 | } |
| 2277 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2278 | return 0; |
| 2279 | } |
| 2280 | |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2281 | /* Recompute the colNotIdxed field of the Index. |
| 2282 | ** |
| 2283 | ** colNotIdxed is a bitmask that has a 0 bit representing each indexed |
drh | 5723c65 | 2022-10-22 13:49:35 | [diff] [blame] | 2284 | ** columns that are within the first 63 columns of the table and a 1 for |
| 2285 | ** all other bits (all columns that are not in the index). The |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2286 | ** high-order bit of colNotIdxed is always 1. All unindexed columns |
| 2287 | ** of the table have a 1. |
| 2288 | ** |
drh | c747673 | 2019-10-24 20:29:25 | [diff] [blame] | 2289 | ** 2019-10-24: For the purpose of this computation, virtual columns are |
| 2290 | ** not considered to be covered by the index, even if they are in the |
| 2291 | ** index, because we do not trust the logic in whereIndexExprTrans() to be |
| 2292 | ** able to find all instances of a reference to the indexed table column |
| 2293 | ** and convert them into references to the index. Hence we always want |
| 2294 | ** the actual table at hand in order to recompute the virtual column, if |
| 2295 | ** necessary. |
| 2296 | ** |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2297 | ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask |
| 2298 | ** to determine if the index is covering index. |
| 2299 | */ |
drh | 00eee7a | 2023-10-06 12:55:53 | [diff] [blame] | 2300 | static void recomputeColumnsNotIndexed(Index *pIdx){ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2301 | Bitmask m = 0; |
| 2302 | int j; |
drh | c747673 | 2019-10-24 20:29:25 | [diff] [blame] | 2303 | Table *pTab = pIdx->pTable; |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2304 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 2305 | int x = pIdx->aiColumn[j]; |
drh | c747673 | 2019-10-24 20:29:25 | [diff] [blame] | 2306 | if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2307 | testcase( x==BMS-1 ); |
| 2308 | testcase( x==BMS-2 ); |
| 2309 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 2310 | } |
| 2311 | } |
| 2312 | pIdx->colNotIdxed = ~m; |
drh | 5723c65 | 2022-10-22 13:49:35 | [diff] [blame] | 2313 | assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */ |
drh | 1fe3ac7 | 2018-06-09 01:12:08 | [diff] [blame] | 2314 | } |
| 2315 | |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2316 | /* |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2317 | ** This routine runs at the end of parsing a CREATE TABLE statement that |
| 2318 | ** has a WITHOUT ROWID clause. The job of this routine is to convert both |
| 2319 | ** internal schema data structures and the generated VDBE code so that they |
| 2320 | ** are appropriate for a WITHOUT ROWID table instead of a rowid table. |
| 2321 | ** Changes include: |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2322 | ** |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2323 | ** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2324 | ** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY |
drh | 0f3f766 | 2017-08-18 14:34:28 | [diff] [blame] | 2325 | ** into BTREE_BLOBKEY. |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2326 | ** (3) Bypass the creation of the sqlite_schema table entry |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 2327 | ** for the PRIMARY KEY as the primary key index is now |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2328 | ** identified by the sqlite_schema table entry of the table itself. |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2329 | ** (4) Set the Index.tnum of the PRIMARY KEY Index object in the |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2330 | ** schema to the rootpage from the main table. |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2331 | ** (5) Add all table columns to the PRIMARY KEY Index object |
| 2332 | ** so that the PRIMARY KEY is a covering index. The surplus |
drh | a485ad1 | 2017-08-02 22:43:14 | [diff] [blame] | 2333 | ** columns are part of KeyInfo.nAllField and are not used for |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2334 | ** sorting or lookup or uniqueness checks. |
| 2335 | ** (6) Replace the rowid tail on all automatically generated UNIQUE |
| 2336 | ** indices with the PRIMARY KEY columns. |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2337 | ** |
| 2338 | ** For virtual tables, only (1) is performed. |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2339 | */ |
| 2340 | static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){ |
| 2341 | Index *pIdx; |
| 2342 | Index *pPk; |
| 2343 | int nPk; |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2344 | int nExtra; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2345 | int i, j; |
| 2346 | sqlite3 *db = pParse->db; |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2347 | Vdbe *v = pParse->pVdbe; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2348 | |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2349 | /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables) |
| 2350 | */ |
| 2351 | if( !db->init.imposterTable ){ |
| 2352 | for(i=0; i<pTab->nCol; i++){ |
drh | fd46ec6 | 2021-08-18 22:26:51 | [diff] [blame] | 2353 | if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 |
| 2354 | && (pTab->aCol[i].notNull==OE_None) |
| 2355 | ){ |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2356 | pTab->aCol[i].notNull = OE_Abort; |
| 2357 | } |
| 2358 | } |
drh | cbda9c7 | 2019-10-26 17:08:06 | [diff] [blame] | 2359 | pTab->tabFlags |= TF_HasNotNull; |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2360 | } |
| 2361 | |
drh | 0f3f766 | 2017-08-18 14:34:28 | [diff] [blame] | 2362 | /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY |
| 2363 | ** into BTREE_BLOBKEY. |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2364 | */ |
drh | 381bdac | 2021-02-04 17:29:04 | [diff] [blame] | 2365 | assert( !pParse->bReturning ); |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2366 | if( pParse->u1.cr.addrCrTab ){ |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2367 | assert( v ); |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2368 | sqlite3VdbeChangeP3(v, pParse->u1.cr.addrCrTab, BTREE_BLOBKEY); |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2369 | } |
| 2370 | |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2371 | /* Locate the PRIMARY KEY index. Or, if this table was originally |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2372 | ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index. |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2373 | */ |
| 2374 | if( pTab->iPKey>=0 ){ |
| 2375 | ExprList *pList; |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 2376 | Token ipkToken; |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 2377 | sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2378 | pList = sqlite3ExprListAppend(pParse, 0, |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 2379 | sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
drh | 1bb89e9 | 2021-04-19 18:03:52 | [diff] [blame] | 2380 | if( pList==0 ){ |
| 2381 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2382 | return; |
| 2383 | } |
dan | f9b0c45 | 2019-05-06 16:15:28 | [diff] [blame] | 2384 | if( IN_RENAME_OBJECT ){ |
| 2385 | sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); |
| 2386 | } |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 2387 | pList->a[0].fg.sortFlags = pParse->iPkSortOrder; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2388 | assert( pParse->pNewTable==pTab ); |
dan | f9b0c45 | 2019-05-06 16:15:28 | [diff] [blame] | 2389 | pTab->iPKey = -1; |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2390 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 2391 | SQLITE_IDXTYPE_PRIMARYKEY); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 2392 | if( pParse->nErr ){ |
drh | 3bb9d75 | 2021-04-13 13:48:31 | [diff] [blame] | 2393 | pTab->tabFlags &= ~TF_WithoutRowid; |
| 2394 | return; |
| 2395 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 2396 | assert( db->mallocFailed==0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2397 | pPk = sqlite3PrimaryKeyIndex(pTab); |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2398 | assert( pPk->nKeyCol==1 ); |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 2399 | }else{ |
| 2400 | pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | f0c48b1 | 2019-02-11 01:58:34 | [diff] [blame] | 2401 | assert( pPk!=0 ); |
dan | c5b7358 | 2015-05-26 11:53:14 | [diff] [blame] | 2402 | |
drh | e385d88 | 2014-12-28 22:10:51 | [diff] [blame] | 2403 | /* |
| 2404 | ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 2405 | ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 2406 | ** code assumes the PRIMARY KEY contains no repeated columns. |
| 2407 | */ |
| 2408 | for(i=j=1; i<pPk->nKeyCol; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2409 | if( isDupColumn(pPk, j, pPk, i) ){ |
drh | e385d88 | 2014-12-28 22:10:51 | [diff] [blame] | 2410 | pPk->nColumn--; |
| 2411 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2412 | testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2413 | pPk->azColl[j] = pPk->azColl[i]; |
| 2414 | pPk->aSortOrder[j] = pPk->aSortOrder[i]; |
drh | e385d88 | 2014-12-28 22:10:51 | [diff] [blame] | 2415 | pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 2416 | } |
| 2417 | } |
| 2418 | pPk->nKeyCol = j; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2419 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2420 | assert( pPk!=0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 2421 | pPk->isCovering = 1; |
| 2422 | if( !db->init.imposterTable ) pPk->uniqNotNull = 1; |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2423 | nPk = pPk->nColumn = pPk->nKeyCol; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2424 | |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2425 | /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema |
drh | df94966 | 2017-07-30 18:40:52 | [diff] [blame] | 2426 | ** table entry. This is only required if currently generating VDBE |
| 2427 | ** code for a CREATE TABLE (not when parsing one as part of reading |
| 2428 | ** a database schema). */ |
| 2429 | if( v && pPk->tnum>0 ){ |
| 2430 | assert( db->init.busy==0 ); |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 2431 | sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto); |
drh | df94966 | 2017-07-30 18:40:52 | [diff] [blame] | 2432 | } |
| 2433 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2434 | /* The root page of the PRIMARY KEY is the table root page */ |
| 2435 | pPk->tnum = pTab->tnum; |
| 2436 | |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2437 | /* Update the in-memory representation of all UNIQUE indices by converting |
| 2438 | ** the final rowid column into one or more columns of the PRIMARY KEY. |
| 2439 | */ |
| 2440 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2441 | int n; |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 2442 | if( IsPrimaryKeyIndex(pIdx) ) continue; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2443 | for(i=n=0; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2444 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2445 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
| 2446 | n++; |
| 2447 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2448 | } |
drh | 5a9a37b | 2013-11-05 17:30:04 | [diff] [blame] | 2449 | if( n==0 ){ |
| 2450 | /* This index is a superset of the primary key */ |
| 2451 | pIdx->nColumn = pIdx->nKeyCol; |
| 2452 | continue; |
| 2453 | } |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 2454 | if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2455 | for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 2456 | if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 2457 | testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2458 | pIdx->aiColumn[j] = pPk->aiColumn[i]; |
| 2459 | pIdx->azColl[j] = pPk->azColl[i]; |
drh | bf9ff25 | 2019-05-14 00:43:13 | [diff] [blame] | 2460 | if( pPk->aSortOrder[i] ){ |
drh | 8a6f89c | 2025-04-10 10:18:07 | [diff] [blame] | 2461 | /* See ticket https://sqlite.org/src/info/bba7b69f9849b5bf */ |
drh | bf9ff25 | 2019-05-14 00:43:13 | [diff] [blame] | 2462 | pIdx->bAscKeyBug = 1; |
| 2463 | } |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2464 | j++; |
| 2465 | } |
| 2466 | } |
drh | 00012df | 2013-11-05 01:59:07 | [diff] [blame] | 2467 | assert( pIdx->nColumn>=pIdx->nKeyCol+n ); |
| 2468 | assert( pIdx->nColumn>=j ); |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /* Add all table columns to the PRIMARY KEY index |
| 2472 | */ |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2473 | nExtra = 0; |
| 2474 | for(i=0; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 2475 | if( !hasColumn(pPk->aiColumn, nPk, i) |
| 2476 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++; |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2477 | } |
drh | ce25007 | 2025-02-21 17:03:22 | [diff] [blame] | 2478 | if( resizeIndexObject(pParse, pPk, nPk+nExtra) ) return; |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2479 | for(i=0, j=nPk; i<pTab->nCol; i++){ |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 2480 | if( !hasColumn(pPk->aiColumn, j, i) |
| 2481 | && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 |
| 2482 | ){ |
dan | 1ff9407 | 2019-07-17 09:18:06 | [diff] [blame] | 2483 | assert( j<pPk->nColumn ); |
| 2484 | pPk->aiColumn[j] = i; |
| 2485 | pPk->azColl[j] = sqlite3StrBINARY; |
| 2486 | j++; |
| 2487 | } |
| 2488 | } |
| 2489 | assert( pPk->nColumn==j ); |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 2490 | assert( pTab->nNVCol<=j ); |
drh | 00eee7a | 2023-10-06 12:55:53 | [diff] [blame] | 2491 | recomputeColumnsNotIndexed(pPk); |
drh | 7f9c5db | 2013-10-23 00:32:58 | [diff] [blame] | 2492 | } |
| 2493 | |
drh | 3d863b5 | 2020-05-14 21:16:52 | [diff] [blame] | 2494 | |
| 2495 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2496 | /* |
| 2497 | ** Return true if pTab is a virtual table and zName is a shadow table name |
| 2498 | ** for that virtual table. |
| 2499 | */ |
| 2500 | int sqlite3IsShadowTableOf(sqlite3 *db, Table *pTab, const char *zName){ |
| 2501 | int nName; /* Length of zName */ |
| 2502 | Module *pMod; /* Module for the virtual table */ |
| 2503 | |
| 2504 | if( !IsVirtual(pTab) ) return 0; |
| 2505 | nName = sqlite3Strlen30(pTab->zName); |
| 2506 | if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0; |
| 2507 | if( zName[nName]!='_' ) return 0; |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 2508 | pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); |
drh | 3d863b5 | 2020-05-14 21:16:52 | [diff] [blame] | 2509 | if( pMod==0 ) return 0; |
| 2510 | if( pMod->pModule->iVersion<3 ) return 0; |
| 2511 | if( pMod->pModule->xShadowName==0 ) return 0; |
| 2512 | return pMod->pModule->xShadowName(zName+nName+1); |
| 2513 | } |
| 2514 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 2515 | |
dan | f6e015f | 2018-11-28 08:02:28 | [diff] [blame] | 2516 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2517 | /* |
drh | ddfec00 | 2021-11-04 00:51:53 | [diff] [blame] | 2518 | ** Table pTab is a virtual table. If it the virtual table implementation |
| 2519 | ** exists and has an xShadowName method, then loop over all other ordinary |
| 2520 | ** tables within the same schema looking for shadow tables of pTab, and mark |
| 2521 | ** any shadow tables seen using the TF_Shadow flag. |
| 2522 | */ |
| 2523 | void sqlite3MarkAllShadowTablesOf(sqlite3 *db, Table *pTab){ |
| 2524 | int nName; /* Length of pTab->zName */ |
| 2525 | Module *pMod; /* Module for the virtual table */ |
| 2526 | HashElem *k; /* For looping through the symbol table */ |
| 2527 | |
| 2528 | assert( IsVirtual(pTab) ); |
| 2529 | pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]); |
| 2530 | if( pMod==0 ) return; |
| 2531 | if( NEVER(pMod->pModule==0) ) return; |
drh | 62561b8 | 2021-11-06 10:59:27 | [diff] [blame] | 2532 | if( pMod->pModule->iVersion<3 ) return; |
drh | ddfec00 | 2021-11-04 00:51:53 | [diff] [blame] | 2533 | if( pMod->pModule->xShadowName==0 ) return; |
| 2534 | assert( pTab->zName!=0 ); |
| 2535 | nName = sqlite3Strlen30(pTab->zName); |
| 2536 | for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 2537 | Table *pOther = sqliteHashData(k); |
| 2538 | assert( pOther->zName!=0 ); |
| 2539 | if( !IsOrdinaryTable(pOther) ) continue; |
| 2540 | if( pOther->tabFlags & TF_Shadow ) continue; |
| 2541 | if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0 |
| 2542 | && pOther->zName[nName]=='_' |
| 2543 | && pMod->pModule->xShadowName(pOther->zName+nName+1) |
| 2544 | ){ |
| 2545 | pOther->tabFlags |= TF_Shadow; |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
| 2550 | |
| 2551 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2552 | /* |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2553 | ** Return true if zName is a shadow table name in the current database |
| 2554 | ** connection. |
| 2555 | ** |
| 2556 | ** zName is temporarily modified while this routine is running, but is |
| 2557 | ** restored to its original value prior to this routine returning. |
| 2558 | */ |
drh | 527cbd4 | 2019-11-16 14:15:19 | [diff] [blame] | 2559 | int sqlite3ShadowTableName(sqlite3 *db, const char *zName){ |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2560 | char *zTail; /* Pointer to the last "_" in zName */ |
| 2561 | Table *pTab; /* Table that zName is a shadow of */ |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2562 | zTail = strrchr(zName, '_'); |
| 2563 | if( zTail==0 ) return 0; |
| 2564 | *zTail = 0; |
| 2565 | pTab = sqlite3FindTable(db, zName, 0); |
| 2566 | *zTail = '_'; |
| 2567 | if( pTab==0 ) return 0; |
| 2568 | if( !IsVirtual(pTab) ) return 0; |
drh | 3d863b5 | 2020-05-14 21:16:52 | [diff] [blame] | 2569 | return sqlite3IsShadowTableOf(db, pTab, zName); |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2570 | } |
dan | f6e015f | 2018-11-28 08:02:28 | [diff] [blame] | 2571 | #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2572 | |
drh | 3d863b5 | 2020-05-14 21:16:52 | [diff] [blame] | 2573 | |
drh | e7375bf | 2020-03-10 19:24:38 | [diff] [blame] | 2574 | #ifdef SQLITE_DEBUG |
| 2575 | /* |
| 2576 | ** Mark all nodes of an expression as EP_Immutable, indicating that |
| 2577 | ** they should not be changed. Expressions attached to a table or |
| 2578 | ** index definition are tagged this way to help ensure that we do |
| 2579 | ** not pass them into code generator routines by mistake. |
| 2580 | */ |
| 2581 | static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){ |
drh | 3547e49 | 2022-12-23 14:49:24 | [diff] [blame] | 2582 | (void)pWalker; |
drh | e7375bf | 2020-03-10 19:24:38 | [diff] [blame] | 2583 | ExprSetVVAProperty(pExpr, EP_Immutable); |
| 2584 | return WRC_Continue; |
| 2585 | } |
| 2586 | static void markExprListImmutable(ExprList *pList){ |
| 2587 | if( pList ){ |
| 2588 | Walker w; |
| 2589 | memset(&w, 0, sizeof(w)); |
| 2590 | w.xExprCallback = markImmutableExprStep; |
| 2591 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 2592 | w.xSelectCallback2 = 0; |
| 2593 | sqlite3WalkExprList(&w, pList); |
| 2594 | } |
| 2595 | } |
| 2596 | #else |
| 2597 | #define markExprListImmutable(X) /* no-op */ |
| 2598 | #endif /* SQLITE_DEBUG */ |
| 2599 | |
| 2600 | |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2601 | /* |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2602 | ** This routine is called to report the final ")" that terminates |
| 2603 | ** a CREATE TABLE statement. |
| 2604 | ** |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 2605 | ** The table structure that other action routines have been building |
| 2606 | ** is added to the internal hash tables, assuming no errors have |
| 2607 | ** occurred. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2608 | ** |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 2609 | ** An entry for the table is made in the schema table on disk, unless |
drh | 1d85d93 | 2004-02-14 23:05:52 | [diff] [blame] | 2610 | ** this is a temporary table or db->init.busy==1. When db->init.busy==1 |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2611 | ** it means we are reading the sqlite_schema table because we just |
| 2612 | ** connected to the database or because the sqlite_schema table has |
drh | ddba9e5 | 2005-03-19 01:41:21 | [diff] [blame] | 2613 | ** recently changed, so the entry for this table already exists in |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2614 | ** the sqlite_schema table. We do not want to create it again. |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2615 | ** |
| 2616 | ** If the pSelect argument is not NULL, it means that this routine |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2617 | ** was called to create a table generated from a |
drh | 969fa7c | 2002-02-18 18:30:32 | [diff] [blame] | 2618 | ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
| 2619 | ** the new table will match the result set of the SELECT. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2620 | */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 | [diff] [blame] | 2621 | void sqlite3EndTable( |
| 2622 | Parse *pParse, /* Parse context */ |
| 2623 | Token *pCons, /* The ',' token after the last column defn. */ |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2624 | Token *pEnd, /* The ')' before options in the CREATE TABLE */ |
drh | 44183f8 | 2021-08-18 13:13:58 | [diff] [blame] | 2625 | u32 tabOpts, /* Extra table options. Usually 0. */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 | [diff] [blame] | 2626 | Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
| 2627 | ){ |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2628 | Table *p; /* The new table */ |
| 2629 | sqlite3 *db = pParse->db; /* The database connection */ |
| 2630 | int iDb; /* Database in which the table lives */ |
| 2631 | Index *pIdx; /* An implied index of the table */ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2632 | |
drh | 027616d | 2015-08-08 22:47:47 | [diff] [blame] | 2633 | if( pEnd==0 && pSelect==0 ){ |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2634 | return; |
danielk1977 | 261919c | 2005-12-06 12:52:59 | [diff] [blame] | 2635 | } |
drh | 2803757 | 2000-08-02 13:47:41 | [diff] [blame] | 2636 | p = pParse->pNewTable; |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2637 | if( p==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2638 | |
drh | 527cbd4 | 2019-11-16 14:15:19 | [diff] [blame] | 2639 | if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){ |
drh | 84c501b | 2018-11-05 23:01:45 | [diff] [blame] | 2640 | p->tabFlags |= TF_Shadow; |
| 2641 | } |
| 2642 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2643 | /* If the db->init.busy is 1 it means we are reading the SQL off the |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2644 | ** "sqlite_schema" or "sqlite_temp_schema" table on the disk. |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2645 | ** So do not write to the disk again. Extract the root page number |
| 2646 | ** for the table from the db->init.newTnum field. (The page number |
| 2647 | ** should have been put there by the sqliteOpenCb routine.) |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 2648 | ** |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 2649 | ** If the root page number is 1, that means this is the sqlite_schema |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 2650 | ** table itself. So mark it read-only. |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2651 | */ |
| 2652 | if( db->init.busy ){ |
drh | 54e3f94 | 2021-10-11 15:54:05 | [diff] [blame] | 2653 | if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){ |
drh | 1e9c47b | 2018-03-16 20:15:58 | [diff] [blame] | 2654 | sqlite3ErrorMsg(pParse, ""); |
| 2655 | return; |
| 2656 | } |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2657 | p->tnum = db->init.newTnum; |
drh | 055f298 | 2016-01-15 15:06:41 | [diff] [blame] | 2658 | if( p->tnum==1 ) p->tabFlags |= TF_Readonly; |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2659 | } |
| 2660 | |
drh | 71c770f | 2021-08-19 16:29:33 | [diff] [blame] | 2661 | /* Special processing for tables that include the STRICT keyword: |
| 2662 | ** |
| 2663 | ** * Do not allow custom column datatypes. Every column must have |
| 2664 | ** a datatype that is one of INT, INTEGER, REAL, TEXT, or BLOB. |
| 2665 | ** |
| 2666 | ** * If a PRIMARY KEY is defined, other than the INTEGER PRIMARY KEY, |
| 2667 | ** then all columns of the PRIMARY KEY must have a NOT NULL |
| 2668 | ** constraint. |
| 2669 | */ |
drh | 44183f8 | 2021-08-18 13:13:58 | [diff] [blame] | 2670 | if( tabOpts & TF_Strict ){ |
| 2671 | int ii; |
| 2672 | p->tabFlags |= TF_Strict; |
| 2673 | for(ii=0; ii<p->nCol; ii++){ |
drh | ab16578 | 2021-08-19 00:24:43 | [diff] [blame] | 2674 | Column *pCol = &p->aCol[ii]; |
drh | b9fd010 | 2021-08-23 10:28:02 | [diff] [blame] | 2675 | if( pCol->eCType==COLTYPE_CUSTOM ){ |
| 2676 | if( pCol->colFlags & COLFLAG_HASTYPE ){ |
| 2677 | sqlite3ErrorMsg(pParse, |
| 2678 | "unknown datatype for %s.%s: \"%s\"", |
| 2679 | p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "") |
| 2680 | ); |
| 2681 | }else{ |
| 2682 | sqlite3ErrorMsg(pParse, "missing datatype for %s.%s", |
| 2683 | p->zName, pCol->zCnName); |
| 2684 | } |
drh | 44183f8 | 2021-08-18 13:13:58 | [diff] [blame] | 2685 | return; |
drh | b9fd010 | 2021-08-23 10:28:02 | [diff] [blame] | 2686 | }else if( pCol->eCType==COLTYPE_ANY ){ |
| 2687 | pCol->affinity = SQLITE_AFF_BLOB; |
drh | 44183f8 | 2021-08-18 13:13:58 | [diff] [blame] | 2688 | } |
drh | ab16578 | 2021-08-19 00:24:43 | [diff] [blame] | 2689 | if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0 |
| 2690 | && p->iPKey!=ii |
| 2691 | && pCol->notNull == OE_None |
| 2692 | ){ |
drh | ab16578 | 2021-08-19 00:24:43 | [diff] [blame] | 2693 | pCol->notNull = OE_Abort; |
| 2694 | p->tabFlags |= TF_HasNotNull; |
| 2695 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2696 | } |
drh | 44183f8 | 2021-08-18 13:13:58 | [diff] [blame] | 2697 | } |
| 2698 | |
drh | 3cbd2b7 | 2019-02-19 13:51:58 | [diff] [blame] | 2699 | assert( (p->tabFlags & TF_HasPrimaryKey)==0 |
| 2700 | || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 ); |
| 2701 | assert( (p->tabFlags & TF_HasPrimaryKey)!=0 |
| 2702 | || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) ); |
| 2703 | |
drh | c6bd4e4 | 2013-11-02 14:37:18 | [diff] [blame] | 2704 | /* Special processing for WITHOUT ROWID Tables */ |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2705 | if( tabOpts & TF_WithoutRowid ){ |
drh | d2fe335 | 2013-11-09 18:15:35 | [diff] [blame] | 2706 | if( (p->tabFlags & TF_Autoincrement) ){ |
| 2707 | sqlite3ErrorMsg(pParse, |
| 2708 | "AUTOINCREMENT not allowed on WITHOUT ROWID tables"); |
| 2709 | return; |
| 2710 | } |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2711 | if( (p->tabFlags & TF_HasPrimaryKey)==0 ){ |
drh | d2fe335 | 2013-11-09 18:15:35 | [diff] [blame] | 2712 | sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName); |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 2713 | return; |
drh | 81eba73 | 2013-10-19 23:31:56 | [diff] [blame] | 2714 | } |
drh | 8e10d74 | 2019-10-18 17:42:47 | [diff] [blame] | 2715 | p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid; |
drh | f95909c | 2019-10-18 18:33:25 | [diff] [blame] | 2716 | convertToWithoutRowidTable(pParse, p); |
drh | 81eba73 | 2013-10-19 23:31:56 | [diff] [blame] | 2717 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 | [diff] [blame] | 2718 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 2719 | |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 2720 | #ifndef SQLITE_OMIT_CHECK |
| 2721 | /* Resolve names in all CHECK constraint expressions. |
| 2722 | */ |
| 2723 | if( p->pCheck ){ |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 2724 | sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 | [diff] [blame] | 2725 | if( pParse->nErr ){ |
| 2726 | /* If errors are seen, delete the CHECK constraints now, else they might |
| 2727 | ** actually be used if PRAGMA writable_schema=ON is set. */ |
| 2728 | sqlite3ExprListDelete(db, p->pCheck); |
| 2729 | p->pCheck = 0; |
drh | e7375bf | 2020-03-10 19:24:38 | [diff] [blame] | 2730 | }else{ |
| 2731 | markExprListImmutable(p->pCheck); |
drh | 9524a7e | 2019-12-22 18:06:49 | [diff] [blame] | 2732 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 2733 | } |
| 2734 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2735 | #ifndef SQLITE_OMIT_GENERATED_COLUMNS |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 2736 | if( p->tabFlags & TF_HasGenerated ){ |
drh | f4658b6 | 2019-10-29 03:39:17 | [diff] [blame] | 2737 | int ii, nNG = 0; |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 2738 | testcase( p->tabFlags & TF_HasVirtual ); |
| 2739 | testcase( p->tabFlags & TF_HasStored ); |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2740 | for(ii=0; ii<p->nCol; ii++){ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 | [diff] [blame] | 2741 | u32 colFlags = p->aCol[ii].colFlags; |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 2742 | if( (colFlags & COLFLAG_GENERATED)!=0 ){ |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 2743 | Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]); |
drh | 427b96a | 2019-10-22 13:01:24 | [diff] [blame] | 2744 | testcase( colFlags & COLFLAG_VIRTUAL ); |
| 2745 | testcase( colFlags & COLFLAG_STORED ); |
drh | 7e3f135 | 2019-12-14 19:55:31 | [diff] [blame] | 2746 | if( sqlite3ResolveSelfReference(pParse, p, NC_GenCol, pX, 0) ){ |
| 2747 | /* If there are errors in resolving the expression, change the |
| 2748 | ** expression to a NULL. This prevents code generators that operate |
| 2749 | ** on the expression from inserting extra parts into the expression |
| 2750 | ** tree that have been allocated from lookaside memory, which is |
drh | 2d58b7f | 2020-01-17 23:27:41 | [diff] [blame] | 2751 | ** illegal in a schema and will lead to errors or heap corruption |
| 2752 | ** when the database connection closes. */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2753 | sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii], |
drh | 79cf2b7 | 2021-07-31 20:30:41 | [diff] [blame] | 2754 | sqlite3ExprAlloc(db, TK_NULL, 0, 0)); |
drh | 7e3f135 | 2019-12-14 19:55:31 | [diff] [blame] | 2755 | } |
drh | f4658b6 | 2019-10-29 03:39:17 | [diff] [blame] | 2756 | }else{ |
| 2757 | nNG++; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2758 | } |
drh | 2c40a3e | 2019-10-29 01:26:24 | [diff] [blame] | 2759 | } |
drh | f4658b6 | 2019-10-29 03:39:17 | [diff] [blame] | 2760 | if( nNG==0 ){ |
| 2761 | sqlite3ErrorMsg(pParse, "must have at least one non-generated column"); |
drh | 2c40a3e | 2019-10-29 01:26:24 | [diff] [blame] | 2762 | return; |
drh | 81f7b37 | 2019-10-16 12:18:59 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 | [diff] [blame] | 2766 | |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2767 | /* Estimate the average row size for the table and for all implied indices */ |
| 2768 | estimateTableWidth(p); |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2769 | for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 2770 | estimateIndexWidth(pIdx); |
drh | fdaac67 | 2013-10-04 15:30:21 | [diff] [blame] | 2771 | } |
| 2772 | |
drh | e3c4137 | 2001-09-17 20:25:58 | [diff] [blame] | 2773 | /* If not initializing, then create a record for the new table |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 2774 | ** in the schema table of the database. |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 2775 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 | [diff] [blame] | 2776 | ** If this is a TEMPORARY table, write the entry into the auxiliary |
| 2777 | ** file instead of into the main database file. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2778 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 | [diff] [blame] | 2779 | if( !db->init.busy ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 2780 | int n; |
drh | d8bc708 | 2000-06-07 23:51:50 | [diff] [blame] | 2781 | Vdbe *v; |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2782 | char *zType; /* "view" or "table" */ |
| 2783 | char *zType2; /* "VIEW" or "TABLE" */ |
| 2784 | char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2785 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 2786 | v = sqlite3GetVdbe(pParse); |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2787 | if( NEVER(v==0) ) return; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 | [diff] [blame] | 2788 | |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 2789 | sqlite3VdbeAddOp1(v, OP_Close, 0); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 | [diff] [blame] | 2790 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2791 | /* |
drh | 0fa991b | 2009-03-21 16:19:26 | [diff] [blame] | 2792 | ** Initialize zType for the new view or table. |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2793 | */ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 2794 | if( IsOrdinaryTable(p) ){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 2795 | /* A regular table */ |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2796 | zType = "table"; |
| 2797 | zType2 = "TABLE"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 2798 | #ifndef SQLITE_OMIT_VIEW |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 2799 | }else{ |
| 2800 | /* A view */ |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2801 | zType = "view"; |
| 2802 | zType2 = "VIEW"; |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 2803 | #endif |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 2804 | } |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 | [diff] [blame] | 2805 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 | [diff] [blame] | 2806 | /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
| 2807 | ** statement to populate the new table. The root-page number for the |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2808 | ** new table is in register pParse->u1.cr.regRoot. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 | [diff] [blame] | 2809 | ** |
| 2810 | ** Once the SELECT has been coded by sqlite3Select(), it is in a |
| 2811 | ** suitable state to query for the column names and types to be used |
| 2812 | ** by the new table. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 2813 | ** |
| 2814 | ** A shared-cache write-lock is not required to write to the new table, |
| 2815 | ** as a schema-lock must have already been obtained to create it. Since |
| 2816 | ** a schema-lock excludes all other database users, the write-lock would |
| 2817 | ** be redundant. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 | [diff] [blame] | 2818 | */ |
| 2819 | if( pSelect ){ |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2820 | SelectDest dest; /* Where the SELECT should store results */ |
drh | 9df25c4 | 2015-05-20 15:51:09 | [diff] [blame] | 2821 | int regYield; /* Register holding co-routine entry-point */ |
| 2822 | int addrTop; /* Top of the co-routine */ |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2823 | int regRec; /* A record to be insert into the new table */ |
| 2824 | int regRowid; /* Rowid of the next row to insert */ |
| 2825 | int addrInsLoop; /* Top of the loop for inserting rows */ |
| 2826 | Table *pSelTab; /* A table that describes the SELECT results */ |
drh | d9eee78 | 2024-03-23 15:17:38 | [diff] [blame] | 2827 | int iCsr; /* Write cursor on the new table */ |
drh | 1013c93 | 2008-01-06 00:25:21 | [diff] [blame] | 2828 | |
drh | fde3043 | 2022-03-10 21:04:49 | [diff] [blame] | 2829 | if( IN_SPECIAL_PARSE ){ |
| 2830 | pParse->rc = SQLITE_ERROR; |
| 2831 | pParse->nErr++; |
| 2832 | return; |
| 2833 | } |
drh | d9eee78 | 2024-03-23 15:17:38 | [diff] [blame] | 2834 | iCsr = pParse->nTab++; |
drh | 9df25c4 | 2015-05-20 15:51:09 | [diff] [blame] | 2835 | regYield = ++pParse->nMem; |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2836 | regRec = ++pParse->nMem; |
| 2837 | regRowid = ++pParse->nMem; |
drh | 0dd5cda | 2015-06-16 16:39:01 | [diff] [blame] | 2838 | sqlite3MayAbort(pParse); |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2839 | assert( pParse->isCreate ); |
| 2840 | sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->u1.cr.regRoot, iDb); |
dan | 428c218 | 2012-08-06 18:50:11 | [diff] [blame] | 2841 | sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG); |
drh | 9df25c4 | 2015-05-20 15:51:09 | [diff] [blame] | 2842 | addrTop = sqlite3VdbeCurrentAddr(v) + 1; |
| 2843 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop); |
drh | 512795d | 2017-12-24 18:56:28 | [diff] [blame] | 2844 | if( pParse->nErr ) return; |
drh | 81506b8 | 2019-08-05 19:32:06 | [diff] [blame] | 2845 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB); |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2846 | if( pSelTab==0 ) return; |
| 2847 | assert( p->aCol==0 ); |
drh | f5f1915 | 2019-10-21 01:04:11 | [diff] [blame] | 2848 | p->nCol = p->nNVCol = pSelTab->nCol; |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2849 | p->aCol = pSelTab->aCol; |
| 2850 | pSelTab->nCol = 0; |
| 2851 | pSelTab->aCol = 0; |
| 2852 | sqlite3DeleteTable(db, pSelTab); |
drh | 755b0fd | 2017-12-23 12:33:40 | [diff] [blame] | 2853 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 2854 | sqlite3Select(pParse, pSelect, &dest); |
drh | 5060a67 | 2017-12-25 13:43:54 | [diff] [blame] | 2855 | if( pParse->nErr ) return; |
drh | 755b0fd | 2017-12-23 12:33:40 | [diff] [blame] | 2856 | sqlite3VdbeEndCoroutine(v, regYield); |
| 2857 | sqlite3VdbeJumpHere(v, addrTop - 1); |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2858 | addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); |
| 2859 | VdbeCoverage(v); |
| 2860 | sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec); |
| 2861 | sqlite3TableAffinity(v, p, 0); |
drh | d9eee78 | 2024-03-23 15:17:38 | [diff] [blame] | 2862 | sqlite3VdbeAddOp2(v, OP_NewRowid, iCsr, regRowid); |
| 2863 | sqlite3VdbeAddOp3(v, OP_Insert, iCsr, regRec, regRowid); |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 2864 | sqlite3VdbeGoto(v, addrInsLoop); |
drh | 9263220 | 2015-05-20 17:18:29 | [diff] [blame] | 2865 | sqlite3VdbeJumpHere(v, addrInsLoop); |
drh | d9eee78 | 2024-03-23 15:17:38 | [diff] [blame] | 2866 | sqlite3VdbeAddOp1(v, OP_Close, iCsr); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 | [diff] [blame] | 2867 | } |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2868 | |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2869 | /* Compute the complete text of the CREATE statement */ |
| 2870 | if( pSelect ){ |
drh | 1d34fde | 2009-02-03 15:50:33 | [diff] [blame] | 2871 | zStmt = createTableStmt(db, p); |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2872 | }else{ |
drh | 8ea30bf | 2013-10-22 01:18:17 | [diff] [blame] | 2873 | Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd; |
| 2874 | n = (int)(pEnd2->z - pParse->sNameToken.z); |
| 2875 | if( pEnd2->z[0]!=';' ) n += pEnd2->n; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2876 | zStmt = sqlite3MPrintf(db, |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 | [diff] [blame] | 2877 | "CREATE %s %.*s", zType2, n, pParse->sNameToken.z |
| 2878 | ); |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2879 | } |
| 2880 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2881 | /* A slot for the record has already been allocated in the |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 2882 | ** schema table. We just need to update that slot with all |
drh | 0fa991b | 2009-03-21 16:19:26 | [diff] [blame] | 2883 | ** the information we've collected. |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2884 | */ |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2885 | assert( pParse->isCreate ); |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2886 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 2887 | "UPDATE %Q." LEGACY_SCHEMA_TABLE |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 2888 | " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q" |
| 2889 | " WHERE rowid=#%d", |
| 2890 | db->aDb[iDb].zDbSName, |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2891 | zType, |
| 2892 | p->zName, |
| 2893 | p->zName, |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2894 | pParse->u1.cr.regRoot, |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 2895 | zStmt, |
drh | 7fd936e | 2025-02-07 15:49:21 | [diff] [blame] | 2896 | pParse->u1.cr.regRowid |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2897 | ); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 2898 | sqlite3DbFree(db, zStmt); |
drh | 9cbf342 | 2008-01-17 16:22:13 | [diff] [blame] | 2899 | sqlite3ChangeCookie(pParse, iDb); |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 2900 | |
| 2901 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2902 | /* Check to see if we need to create an sqlite_sequence table for |
| 2903 | ** keeping track of autoincrement keys. |
| 2904 | */ |
dan | 755ed41 | 2021-04-07 12:02:30 | [diff] [blame] | 2905 | if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 2906 | Db *pDb = &db->aDb[iDb]; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 2907 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 2908 | if( pDb->pSchema->pSeqTab==0 ){ |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 2909 | sqlite3NestedParse(pParse, |
drh | f338814 | 2004-11-13 03:48:06 | [diff] [blame] | 2910 | "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 2911 | pDb->zDbSName |
drh | 2958a4e | 2004-11-12 03:56:15 | [diff] [blame] | 2912 | ); |
| 2913 | } |
| 2914 | } |
| 2915 | #endif |
drh | 4794f73 | 2004-11-05 17:17:50 | [diff] [blame] | 2916 | |
| 2917 | /* Reparse everything to update our internal data structures */ |
drh | 5d9c9da | 2011-06-03 20:11:17 | [diff] [blame] | 2918 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 | [diff] [blame] | 2919 | sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0); |
drh | af52723 | 2023-10-13 13:49:46 | [diff] [blame] | 2920 | |
drh | 9132b88 | 2023-10-13 22:19:23 | [diff] [blame] | 2921 | /* Test for cycles in generated columns and illegal expressions |
| 2922 | ** in CHECK constraints and in DEFAULT clauses. */ |
drh | af52723 | 2023-10-13 13:49:46 | [diff] [blame] | 2923 | if( p->tabFlags & TF_HasGenerated ){ |
drh | 42eb6a9 | 2024-02-17 16:39:52 | [diff] [blame] | 2924 | sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0, |
drh | 9132b88 | 2023-10-13 22:19:23 | [diff] [blame] | 2925 | sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"", |
drh | af52723 | 2023-10-13 13:49:46 | [diff] [blame] | 2926 | db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC); |
| 2927 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2928 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 | [diff] [blame] | 2929 | |
| 2930 | /* Add the table to the in-memory representation of the database. |
| 2931 | */ |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 2932 | if( db->init.busy ){ |
drh | 17e9e29 | 2003-02-01 13:53:28 | [diff] [blame] | 2933 | Table *pOld; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 | [diff] [blame] | 2934 | Schema *pSchema = p->pSchema; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 2935 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 1bb89e9 | 2021-04-19 18:03:52 | [diff] [blame] | 2936 | assert( HasRowid(p) || p->iPKey<0 ); |
drh | acbcb7e | 2014-08-21 20:26:37 | [diff] [blame] | 2937 | pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p); |
drh | 17e9e29 | 2003-02-01 13:53:28 | [diff] [blame] | 2938 | if( pOld ){ |
| 2939 | assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 2940 | sqlite3OomFault(db); |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 2941 | return; |
drh | 17e9e29 | 2003-02-01 13:53:28 | [diff] [blame] | 2942 | } |
drh | 17e9e29 | 2003-02-01 13:53:28 | [diff] [blame] | 2943 | pParse->pNewTable = 0; |
drh | 8257aa8 | 2017-07-26 19:59:13 | [diff] [blame] | 2944 | db->mDbFlags |= DBFLAG_SchemaChange; |
dan | d4b6469 | 2021-04-06 16:16:15 | [diff] [blame] | 2945 | |
| 2946 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 2947 | ** then record a pointer to this table in the main database structure |
| 2948 | ** so that INSERT can find the table easily. */ |
| 2949 | assert( !pParse->nested ); |
| 2950 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 2951 | if( strcmp(p->zName, "sqlite_sequence")==0 ){ |
| 2952 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 2953 | p->pSchema->pSeqTab = p; |
| 2954 | } |
| 2955 | #endif |
dan | 578277c | 2021-02-19 18:39:32 | [diff] [blame] | 2956 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 | [diff] [blame] | 2957 | |
| 2958 | #ifndef SQLITE_OMIT_ALTERTABLE |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 2959 | if( !pSelect && IsOrdinaryTable(p) ){ |
dan | 578277c | 2021-02-19 18:39:32 | [diff] [blame] | 2960 | assert( pCons && pEnd ); |
| 2961 | if( pCons->z==0 ){ |
| 2962 | pCons = pEnd; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 | [diff] [blame] | 2963 | } |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 2964 | p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z); |
drh | 17e9e29 | 2003-02-01 13:53:28 | [diff] [blame] | 2965 | } |
dan | 578277c | 2021-02-19 18:39:32 | [diff] [blame] | 2966 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2967 | } |
| 2968 | |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 2969 | #ifndef SQLITE_OMIT_VIEW |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 2970 | /* |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 2971 | ** The parser calls this routine in order to create a new VIEW |
| 2972 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 2973 | void sqlite3CreateView( |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 2974 | Parse *pParse, /* The parsing context */ |
| 2975 | Token *pBegin, /* The CREATE token that begins the statement */ |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 | [diff] [blame] | 2976 | Token *pName1, /* The token that holds the name of the view */ |
| 2977 | Token *pName2, /* The token that holds the name of the view */ |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 2978 | ExprList *pCNames, /* Optional list of view column names */ |
drh | 6276c1c | 2002-07-08 22:03:32 | [diff] [blame] | 2979 | Select *pSelect, /* A SELECT statement that will become the new view */ |
drh | fdd48a7 | 2006-09-11 23:45:48 | [diff] [blame] | 2980 | int isTemp, /* TRUE for a TEMPORARY view */ |
| 2981 | int noErr /* Suppress error messages if VIEW already exists */ |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 2982 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 2983 | Table *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 | [diff] [blame] | 2984 | int n; |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 2985 | const char *z; |
drh | 4b59ab5 | 2002-08-24 18:24:51 | [diff] [blame] | 2986 | Token sEnd; |
drh | f26e09c | 2003-05-31 16:21:12 | [diff] [blame] | 2987 | DbFixer sFix; |
drh | 88caeac | 2011-08-24 15:12:08 | [diff] [blame] | 2988 | Token *pName = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 2989 | int iDb; |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 2990 | sqlite3 *db = pParse->db; |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 2991 | |
drh | 7c3d64f | 2005-06-06 15:32:08 | [diff] [blame] | 2992 | if( pParse->nVar>0 ){ |
| 2993 | sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
drh | 32498f1 | 2015-09-26 11:15:44 | [diff] [blame] | 2994 | goto create_view_fail; |
drh | 7c3d64f | 2005-06-06 15:32:08 | [diff] [blame] | 2995 | } |
drh | fdd48a7 | 2006-09-11 23:45:48 | [diff] [blame] | 2996 | sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 2997 | p = pParse->pNewTable; |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 2998 | if( p==0 || pParse->nErr ) goto create_view_fail; |
drh | 6e5020e | 2021-04-07 15:45:01 | [diff] [blame] | 2999 | |
| 3000 | /* Legacy versions of SQLite allowed the use of the magic "rowid" column |
| 3001 | ** on a view, even though views do not have rowids. The following flag |
| 3002 | ** setting fixes this problem. But the fix can be disabled by compiling |
| 3003 | ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that |
drh | 4b42b52 | 2024-03-19 13:31:54 | [diff] [blame] | 3004 | ** depend upon the old buggy behavior. The ability can also be toggled |
drh | 7128c78 | 2024-03-20 10:40:25 | [diff] [blame] | 3005 | ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */ |
drh | 4b42b52 | 2024-03-19 13:31:54 | [diff] [blame] | 3006 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 3007 | p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */ |
| 3008 | #else |
| 3009 | p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */ |
drh | 6e5020e | 2021-04-07 15:45:01 | [diff] [blame] | 3010 | #endif |
| 3011 | |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 | [diff] [blame] | 3012 | sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 3013 | iDb = sqlite3SchemaToIndex(db, p->pSchema); |
drh | d100f69 | 2013-10-03 15:39:44 | [diff] [blame] | 3014 | sqlite3FixInit(&sFix, pParse, iDb, "view", pName); |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3015 | if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail; |
drh | 174b619 | 2002-12-03 02:22:52 | [diff] [blame] | 3016 | |
drh | 4b59ab5 | 2002-08-24 18:24:51 | [diff] [blame] | 3017 | /* Make a copy of the entire SELECT statement that defines the view. |
| 3018 | ** This will force all the Expr.token.z values to be dynamically |
| 3019 | ** allocated rather than point to the input string - which means that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 | [diff] [blame] | 3020 | ** they will persist after the current sqlite3_exec() call returns. |
drh | 4b59ab5 | 2002-08-24 18:24:51 | [diff] [blame] | 3021 | */ |
dan | 3809696 | 2019-12-09 08:13:43 | [diff] [blame] | 3022 | pSelect->selFlags |= SF_View; |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 3023 | if( IN_RENAME_OBJECT ){ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3024 | p->u.view.pSelect = pSelect; |
dan | 987db76 | 2018-08-14 20:18:50 | [diff] [blame] | 3025 | pSelect = 0; |
| 3026 | }else{ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3027 | p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
dan | 987db76 | 2018-08-14 20:18:50 | [diff] [blame] | 3028 | } |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3029 | p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3030 | p->eTabType = TABTYP_VIEW; |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3031 | if( db->mallocFailed ) goto create_view_fail; |
drh | 4b59ab5 | 2002-08-24 18:24:51 | [diff] [blame] | 3032 | |
| 3033 | /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
| 3034 | ** the end. |
| 3035 | */ |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 3036 | sEnd = pParse->sLastToken; |
drh | 6116ee4 | 2018-01-10 00:40:06 | [diff] [blame] | 3037 | assert( sEnd.z[0]!=0 || sEnd.n==0 ); |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3038 | if( sEnd.z[0]!=';' ){ |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 3039 | sEnd.z += sEnd.n; |
| 3040 | } |
| 3041 | sEnd.n = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 3042 | n = (int)(sEnd.z - pBegin->z); |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3043 | assert( n>0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 3044 | z = pBegin->z; |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3045 | while( sqlite3Isspace(z[n-1]) ){ n--; } |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 3046 | sEnd.z = &z[n-1]; |
| 3047 | sEnd.n = 1; |
drh | 4b59ab5 | 2002-08-24 18:24:51 | [diff] [blame] | 3048 | |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 3049 | /* Use sqlite3EndTable() to add the view to the schema table */ |
drh | 5969da4 | 2013-10-21 02:14:45 | [diff] [blame] | 3050 | sqlite3EndTable(pParse, 0, &sEnd, 0, 0); |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3051 | |
| 3052 | create_view_fail: |
| 3053 | sqlite3SelectDelete(db, pSelect); |
dan | e8ab40d | 2018-09-12 08:51:48 | [diff] [blame] | 3054 | if( IN_RENAME_OBJECT ){ |
| 3055 | sqlite3RenameExprlistUnmap(pParse, pCNames); |
| 3056 | } |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3057 | sqlite3ExprListDelete(db, pCNames); |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 3058 | return; |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3059 | } |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3060 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 3061 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 | [diff] [blame] | 3062 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3063 | /* |
| 3064 | ** The Table structure pTable is really a VIEW. Fill in the names of |
drh | 4ed784d | 2024-05-25 23:13:15 | [diff] [blame] | 3065 | ** the columns of the view in the pTable structure. Return non-zero if |
| 3066 | ** there are errors. If an error is seen an error message is left |
| 3067 | ** in pParse->zErrMsg. |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3068 | */ |
drh | ef69d2b | 2022-07-25 22:31:04 | [diff] [blame] | 3069 | static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){ |
drh | 9b3187e | 2005-01-18 14:45:47 | [diff] [blame] | 3070 | Table *pSelTab; /* A fake table from which we get the result set */ |
| 3071 | Select *pSel; /* Copy of the SELECT that implements the view */ |
| 3072 | int nErr = 0; /* Number of errors encountered */ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 3073 | sqlite3 *db = pParse->db; /* Database connection for malloc errors */ |
drh | 424981d | 2018-03-28 15:56:55 | [diff] [blame] | 3074 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | dc6b41e | 2017-08-17 02:26:35 | [diff] [blame] | 3075 | int rc; |
| 3076 | #endif |
drh | a0daa75 | 2016-09-16 11:53:10 | [diff] [blame] | 3077 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | 32c6a48 | 2014-09-11 13:44:52 | [diff] [blame] | 3078 | sqlite3_xauth xAuth; /* Saved xAuth pointer */ |
drh | a0daa75 | 2016-09-16 11:53:10 | [diff] [blame] | 3079 | #endif |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3080 | |
| 3081 | assert( pTable ); |
| 3082 | |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 | [diff] [blame] | 3083 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | ddfec00 | 2021-11-04 00:51:53 | [diff] [blame] | 3084 | if( IsVirtual(pTable) ){ |
| 3085 | db->nSchemaLock++; |
| 3086 | rc = sqlite3VtabCallConnect(pParse, pTable); |
| 3087 | db->nSchemaLock--; |
| 3088 | return rc; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 | [diff] [blame] | 3089 | } |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 | [diff] [blame] | 3090 | #endif |
| 3091 | |
| 3092 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3093 | /* A positive nCol means the columns names for this view are |
drh | 509a630 | 2022-07-25 23:01:41 | [diff] [blame] | 3094 | ** already known. This routine is not called unless either the |
| 3095 | ** table is virtual or nCol is zero. |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3096 | */ |
drh | 509a630 | 2022-07-25 23:01:41 | [diff] [blame] | 3097 | assert( pTable->nCol<=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3098 | |
| 3099 | /* A negative nCol is a special marker meaning that we are currently |
| 3100 | ** trying to compute the column names. If we enter this routine with |
| 3101 | ** a negative nCol, it means two or more views form a loop, like this: |
| 3102 | ** |
| 3103 | ** CREATE VIEW one AS SELECT * FROM two; |
| 3104 | ** CREATE VIEW two AS SELECT * FROM one; |
drh | 3b167c7 | 2002-06-28 12:18:47 | [diff] [blame] | 3105 | ** |
drh | 768578e | 2009-05-12 00:40:12 | [diff] [blame] | 3106 | ** Actually, the error above is now caught prior to reaching this point. |
| 3107 | ** But the following test is still important as it does come up |
| 3108 | ** in the following: |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3109 | ** |
drh | 768578e | 2009-05-12 00:40:12 | [diff] [blame] | 3110 | ** CREATE TABLE main.ex1(a); |
| 3111 | ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; |
| 3112 | ** SELECT * FROM temp.ex1; |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3113 | */ |
| 3114 | if( pTable->nCol<0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3115 | sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3116 | return 1; |
| 3117 | } |
drh | 85c23c6 | 2005-08-20 03:03:04 | [diff] [blame] | 3118 | assert( pTable->nCol>=0 ); |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3119 | |
| 3120 | /* If we get this far, it means we need to compute the table names. |
drh | 9b3187e | 2005-01-18 14:45:47 | [diff] [blame] | 3121 | ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
| 3122 | ** "*" elements in the results set of the view and will assign cursors |
| 3123 | ** to the elements of the FROM clause. But we do not want these changes |
| 3124 | ** to be permanent. So the computation is done on a copy of the SELECT |
| 3125 | ** statement that defines the view. |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3126 | */ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3127 | assert( IsView(pTable) ); |
| 3128 | pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0); |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3129 | if( pSel ){ |
dan | 0208337 | 2018-09-17 08:27:23 | [diff] [blame] | 3130 | u8 eParseMode = pParse->eParseMode; |
drh | 7f41756 | 2022-04-25 14:49:48 | [diff] [blame] | 3131 | int nTab = pParse->nTab; |
| 3132 | int nSelect = pParse->nSelect; |
dan | 0208337 | 2018-09-17 08:27:23 | [diff] [blame] | 3133 | pParse->eParseMode = PARSE_MODE_NORMAL; |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3134 | sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
| 3135 | pTable->nCol = -1; |
drh | 31f6962 | 2019-10-05 14:39:36 | [diff] [blame] | 3136 | DisableLookaside; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 | [diff] [blame] | 3137 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3138 | xAuth = db->xAuth; |
| 3139 | db->xAuth = 0; |
drh | 96fb16e | 2019-08-06 14:37:24 | [diff] [blame] | 3140 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3141 | db->xAuth = xAuth; |
danielk1977 | db2d286 | 2007-10-15 07:08:44 | [diff] [blame] | 3142 | #else |
drh | 96fb16e | 2019-08-06 14:37:24 | [diff] [blame] | 3143 | pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE); |
danielk1977 | db2d286 | 2007-10-15 07:08:44 | [diff] [blame] | 3144 | #endif |
drh | 7f41756 | 2022-04-25 14:49:48 | [diff] [blame] | 3145 | pParse->nTab = nTab; |
| 3146 | pParse->nSelect = nSelect; |
dan | 5d59102 | 2019-12-28 08:26:47 | [diff] [blame] | 3147 | if( pSelTab==0 ){ |
| 3148 | pTable->nCol = 0; |
| 3149 | nErr++; |
| 3150 | }else if( pTable->pCheck ){ |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3151 | /* CREATE VIEW name(arglist) AS ... |
| 3152 | ** The names of the columns in the table are taken from |
| 3153 | ** arglist which is stored in pTable->pCheck. The pCheck field |
| 3154 | ** normally holds CHECK constraints on an ordinary table, but for |
| 3155 | ** a VIEW it holds the list of column names. |
| 3156 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3157 | sqlite3ColumnsFromExprList(pParse, pTable->pCheck, |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3158 | &pTable->nCol, &pTable->aCol); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 3159 | if( pParse->nErr==0 |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3160 | && pTable->nCol==pSel->pEList->nExpr |
| 3161 | ){ |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 3162 | assert( db->mallocFailed==0 ); |
drh | 9e66087 | 2022-12-13 15:54:43 | [diff] [blame] | 3163 | sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE); |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3164 | } |
dan | 5d59102 | 2019-12-28 08:26:47 | [diff] [blame] | 3165 | }else{ |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3166 | /* CREATE VIEW name AS... without an argument list. Construct |
| 3167 | ** the column names from the SELECT statement that defines the view. |
| 3168 | */ |
| 3169 | assert( pTable->aCol==0 ); |
drh | 0383661 | 2019-11-02 17:59:10 | [diff] [blame] | 3170 | pTable->nCol = pSelTab->nCol; |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3171 | pTable->aCol = pSelTab->aCol; |
dan | 3dc864b | 2021-02-25 16:55:47 | [diff] [blame] | 3172 | pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT); |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3173 | pSelTab->nCol = 0; |
| 3174 | pSelTab->aCol = 0; |
| 3175 | assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) ); |
danielk1977 | 261919c | 2005-12-06 12:52:59 | [diff] [blame] | 3176 | } |
drh | 0383661 | 2019-11-02 17:59:10 | [diff] [blame] | 3177 | pTable->nNVCol = pTable->nCol; |
drh | e8da01c | 2016-05-07 12:15:34 | [diff] [blame] | 3178 | sqlite3DeleteTable(db, pSelTab); |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3179 | sqlite3SelectDelete(db, pSel); |
drh | 31f6962 | 2019-10-05 14:39:36 | [diff] [blame] | 3180 | EnableLookaside; |
dan | 0208337 | 2018-09-17 08:27:23 | [diff] [blame] | 3181 | pParse->eParseMode = eParseMode; |
drh | ed06a13 | 2016-04-05 20:59:12 | [diff] [blame] | 3182 | } else { |
| 3183 | nErr++; |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3184 | } |
drh | 8981b90 | 2015-08-24 17:42:49 | [diff] [blame] | 3185 | pTable->pSchema->schemaFlags |= DB_UnresetViews; |
dan | 77f3f40 | 2018-07-09 18:55:44 | [diff] [blame] | 3186 | if( db->mallocFailed ){ |
| 3187 | sqlite3DeleteColumnNames(db, pTable); |
dan | 77f3f40 | 2018-07-09 18:55:44 | [diff] [blame] | 3188 | } |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3189 | #endif /* SQLITE_OMIT_VIEW */ |
drh | 4ed784d | 2024-05-25 23:13:15 | [diff] [blame] | 3190 | return nErr + pParse->nErr; |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 | [diff] [blame] | 3191 | } |
drh | ef69d2b | 2022-07-25 22:31:04 | [diff] [blame] | 3192 | int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
| 3193 | assert( pTable!=0 ); |
| 3194 | if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0; |
| 3195 | return viewGetColumnNames(pParse, pTable); |
| 3196 | } |
danielk1977 | fe3fcbe2 | 2006-06-12 12:08:45 | [diff] [blame] | 3197 | #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3198 | |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3199 | #ifndef SQLITE_OMIT_VIEW |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3200 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 | [diff] [blame] | 3201 | ** Clear the column names from every VIEW in database idx. |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3202 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 3203 | static void sqliteViewResetAll(sqlite3 *db, int idx){ |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3204 | HashElem *i; |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 3205 | assert( sqlite3SchemaMutexHeld(db, idx, 0) ); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 | [diff] [blame] | 3206 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3207 | for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3208 | Table *pTab = sqliteHashData(i); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3209 | if( IsView(pTab) ){ |
drh | 51be387 | 2015-08-19 02:32:25 | [diff] [blame] | 3210 | sqlite3DeleteColumnNames(db, pTab); |
drh | 417be79 | 2002-03-03 18:59:40 | [diff] [blame] | 3211 | } |
| 3212 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 | [diff] [blame] | 3213 | DbClearProperty(db, idx, DB_UnresetViews); |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 3214 | } |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3215 | #else |
| 3216 | # define sqliteViewResetAll(A,B) |
| 3217 | #endif /* SQLITE_OMIT_VIEW */ |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 3218 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3219 | /* |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3220 | ** This function is called by the VDBE to adjust the internal schema |
| 3221 | ** used by SQLite when the btree layer moves a table root page. The |
| 3222 | ** root-page of a table or index in database iDb has changed from iFrom |
| 3223 | ** to iTo. |
drh | 6205d4a | 2006-03-24 03:36:26 | [diff] [blame] | 3224 | ** |
| 3225 | ** Ticket #1728: The symbol table might still contain information |
| 3226 | ** on tables and/or indices that are the process of being deleted. |
| 3227 | ** If you are unlucky, one of those deleted indices or tables might |
| 3228 | ** have the same rootpage number as the real table or index that is |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3229 | ** being moved. So we cannot stop searching after the first match |
drh | 6205d4a | 2006-03-24 03:36:26 | [diff] [blame] | 3230 | ** because the first match might be for one of the deleted indices |
| 3231 | ** or tables and not the table/index that is actually being moved. |
| 3232 | ** We must continue looping until all tables and indices with |
| 3233 | ** rootpage==iFrom have been converted to have a rootpage of iTo |
| 3234 | ** in order to be certain that we got the right one. |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3235 | */ |
| 3236 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 3237 | void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3238 | HashElem *pElem; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3239 | Hash *pHash; |
drh | cdf011d | 2011-04-04 21:25:28 | [diff] [blame] | 3240 | Db *pDb; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3241 | |
drh | cdf011d | 2011-04-04 21:25:28 | [diff] [blame] | 3242 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 3243 | pDb = &db->aDb[iDb]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3244 | pHash = &pDb->pSchema->tblHash; |
| 3245 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3246 | Table *pTab = sqliteHashData(pElem); |
| 3247 | if( pTab->tnum==iFrom ){ |
| 3248 | pTab->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3249 | } |
| 3250 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3251 | pHash = &pDb->pSchema->idxHash; |
| 3252 | for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3253 | Index *pIdx = sqliteHashData(pElem); |
| 3254 | if( pIdx->tnum==iFrom ){ |
| 3255 | pIdx->tnum = iTo; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3256 | } |
| 3257 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3258 | } |
| 3259 | #endif |
| 3260 | |
| 3261 | /* |
| 3262 | ** Write code to erase the table with root-page iTable from database iDb. |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 3263 | ** Also write code to modify the sqlite_schema table and internal schema |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3264 | ** if a root-page of another table is moved by the btree-layer whilst |
| 3265 | ** erasing iTable (this can happen with an auto-vacuum database). |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3266 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 | [diff] [blame] | 3267 | static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
| 3268 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3269 | int r1 = sqlite3GetTempReg(pParse); |
drh | 1991888 | 2020-07-30 23:47:00 | [diff] [blame] | 3270 | if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema"); |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3271 | sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 | [diff] [blame] | 3272 | sqlite3MayAbort(pParse); |
drh | 40e016e | 2004-11-04 14:47:11 | [diff] [blame] | 3273 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3274 | /* OP_Destroy stores an in integer r1. If this integer |
drh | 4e0cff6 | 2004-11-05 05:10:28 | [diff] [blame] | 3275 | ** is non-zero, then it is the root page number of a table moved to |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 3276 | ** location iTable. The following code modifies the sqlite_schema table to |
drh | 4e0cff6 | 2004-11-05 05:10:28 | [diff] [blame] | 3277 | ** reflect this. |
| 3278 | ** |
drh | 0fa991b | 2009-03-21 16:19:26 | [diff] [blame] | 3279 | ** The "#NNN" in the SQL is a special constant that means whatever value |
drh | b74b101 | 2009-05-28 21:04:37 | [diff] [blame] | 3280 | ** is in register NNN. See grammar rules associated with the TK_REGISTER |
| 3281 | ** token for additional information. |
drh | 4e0cff6 | 2004-11-05 05:10:28 | [diff] [blame] | 3282 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3283 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 3284 | "UPDATE %Q." LEGACY_SCHEMA_TABLE |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 3285 | " SET rootpage=%d WHERE #%d AND rootpage=#%d", |
| 3286 | pParse->db->aDb[iDb].zDbSName, iTable, r1, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3287 | #endif |
drh | b765411 | 2008-01-12 12:48:07 | [diff] [blame] | 3288 | sqlite3ReleaseTempReg(pParse, r1); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | ** Write VDBE code to erase table pTab and all associated indices on disk. |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 3293 | ** Code to update the sqlite_schema tables and internal schema definitions |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3294 | ** in case a root-page belonging to another table is moved by the btree layer |
| 3295 | ** is also added (this can happen with an auto-vacuum database). |
| 3296 | */ |
drh | 4e0cff6 | 2004-11-05 05:10:28 | [diff] [blame] | 3297 | static void destroyTable(Parse *pParse, Table *pTab){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3298 | /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
| 3299 | ** is not defined), then it is important to call OP_Destroy on the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3300 | ** table and index root-pages in order, starting with the numerically |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3301 | ** largest root-page number. This guarantees that none of the root-pages |
| 3302 | ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
| 3303 | ** following were coded: |
| 3304 | ** |
| 3305 | ** OP_Destroy 4 0 |
| 3306 | ** ... |
| 3307 | ** OP_Destroy 5 0 |
| 3308 | ** |
| 3309 | ** and root page 5 happened to be the largest root-page number in the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3310 | ** database, then root page 5 would be moved to page 4 by the |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3311 | ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
| 3312 | ** a free-list page. |
| 3313 | */ |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 3314 | Pgno iTab = pTab->tnum; |
drh | 8deae5a | 2020-07-29 12:23:20 | [diff] [blame] | 3315 | Pgno iDestroyed = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3316 | |
| 3317 | while( 1 ){ |
| 3318 | Index *pIdx; |
drh | 8deae5a | 2020-07-29 12:23:20 | [diff] [blame] | 3319 | Pgno iLargest = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3320 | |
| 3321 | if( iDestroyed==0 || iTab<iDestroyed ){ |
| 3322 | iLargest = iTab; |
| 3323 | } |
| 3324 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 3325 | Pgno iIdx = pIdx->tnum; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3326 | assert( pIdx->pSchema==pTab->pSchema ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3327 | if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
| 3328 | iLargest = iIdx; |
| 3329 | } |
| 3330 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3331 | if( iLargest==0 ){ |
| 3332 | return; |
| 3333 | }else{ |
| 3334 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | 5a05be1 | 2012-10-09 18:51:44 | [diff] [blame] | 3335 | assert( iDb>=0 && iDb<pParse->db->nDb ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3336 | destroyRootPage(pParse, iLargest, iDb); |
| 3337 | iDestroyed = iLargest; |
| 3338 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3339 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | /* |
drh | 74e7c8f | 2011-10-21 19:06:32 | [diff] [blame] | 3343 | ** Remove entries from the sqlite_statN tables (for N in (1,2,3)) |
drh | a5ae4c3 | 2011-08-07 01:31:52 | [diff] [blame] | 3344 | ** after a DROP INDEX or DROP TABLE command. |
| 3345 | */ |
| 3346 | static void sqlite3ClearStatTables( |
| 3347 | Parse *pParse, /* The parsing context */ |
| 3348 | int iDb, /* The database number */ |
| 3349 | const char *zType, /* "idx" or "tbl" */ |
| 3350 | const char *zName /* Name of index or table */ |
| 3351 | ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 | [diff] [blame] | 3352 | int i; |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 3353 | const char *zDbName = pParse->db->aDb[iDb].zDbSName; |
dan | f52bb8d | 2013-08-03 20:24:58 | [diff] [blame] | 3354 | for(i=1; i<=4; i++){ |
drh | 74e7c8f | 2011-10-21 19:06:32 | [diff] [blame] | 3355 | char zTab[24]; |
| 3356 | sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i); |
| 3357 | if( sqlite3FindTable(pParse->db, zTab, zDbName) ){ |
drh | a5ae4c3 | 2011-08-07 01:31:52 | [diff] [blame] | 3358 | sqlite3NestedParse(pParse, |
| 3359 | "DELETE FROM %Q.%s WHERE %s=%Q", |
drh | 74e7c8f | 2011-10-21 19:06:32 | [diff] [blame] | 3360 | zDbName, zTab, zType, zName |
drh | a5ae4c3 | 2011-08-07 01:31:52 | [diff] [blame] | 3361 | ); |
| 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | /* |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3367 | ** Generate code to drop a table. |
| 3368 | */ |
| 3369 | void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){ |
| 3370 | Vdbe *v; |
| 3371 | sqlite3 *db = pParse->db; |
| 3372 | Trigger *pTrigger; |
| 3373 | Db *pDb = &db->aDb[iDb]; |
| 3374 | |
| 3375 | v = sqlite3GetVdbe(pParse); |
| 3376 | assert( v!=0 ); |
| 3377 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 3378 | |
| 3379 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3380 | if( IsVirtual(pTab) ){ |
| 3381 | sqlite3VdbeAddOp0(v, OP_VBegin); |
| 3382 | } |
| 3383 | #endif |
| 3384 | |
| 3385 | /* Drop all triggers associated with the table being dropped. Code |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 3386 | ** is generated to remove entries from sqlite_schema and/or |
| 3387 | ** sqlite_temp_schema if required. |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3388 | */ |
| 3389 | pTrigger = sqlite3TriggerList(pParse, pTab); |
| 3390 | while( pTrigger ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3391 | assert( pTrigger->pSchema==pTab->pSchema || |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3392 | pTrigger->pSchema==db->aDb[1].pSchema ); |
| 3393 | sqlite3DropTriggerPtr(pParse, pTrigger); |
| 3394 | pTrigger = pTrigger->pNext; |
| 3395 | } |
| 3396 | |
| 3397 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 3398 | /* Remove any entries of the sqlite_sequence table associated with |
| 3399 | ** the table being dropped. This is done before the table is dropped |
| 3400 | ** at the btree level, in case the sqlite_sequence table needs to |
| 3401 | ** move as a result of the drop (can happen in auto-vacuum mode). |
| 3402 | */ |
| 3403 | if( pTab->tabFlags & TF_Autoincrement ){ |
| 3404 | sqlite3NestedParse(pParse, |
| 3405 | "DELETE FROM %Q.sqlite_sequence WHERE name=%Q", |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 3406 | pDb->zDbSName, pTab->zName |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3407 | ); |
| 3408 | } |
| 3409 | #endif |
| 3410 | |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 3411 | /* Drop all entries in the schema table that refer to the |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 3412 | ** table. The program name loops through the schema table and deletes |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3413 | ** every row that refers to a table of the same name as the one being |
mistachkin | 48864df | 2013-03-21 21:20:32 | [diff] [blame] | 3414 | ** dropped. Triggers are handled separately because a trigger can be |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3415 | ** created in the temp database that refers to a table in another |
| 3416 | ** database. |
| 3417 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3418 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 3419 | "DELETE FROM %Q." LEGACY_SCHEMA_TABLE |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 3420 | " WHERE tbl_name=%Q and type!='trigger'", |
| 3421 | pDb->zDbSName, pTab->zName); |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3422 | if( !isView && !IsVirtual(pTab) ){ |
| 3423 | destroyTable(pParse, pTab); |
| 3424 | } |
| 3425 | |
| 3426 | /* Remove the table entry from SQLite's internal schema and modify |
| 3427 | ** the schema cookie. |
| 3428 | */ |
| 3429 | if( IsVirtual(pTab) ){ |
| 3430 | sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); |
dan | 1d4b164 | 2018-12-28 17:45:08 | [diff] [blame] | 3431 | sqlite3MayAbort(pParse); |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3432 | } |
| 3433 | sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); |
| 3434 | sqlite3ChangeCookie(pParse, iDb); |
| 3435 | sqliteViewResetAll(db, iDb); |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* |
drh | 070ae3b | 2019-11-16 13:51:31 | [diff] [blame] | 3439 | ** Return TRUE if shadow tables should be read-only in the current |
| 3440 | ** context. |
| 3441 | */ |
| 3442 | int sqlite3ReadOnlyShadowTables(sqlite3 *db){ |
| 3443 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3444 | if( (db->flags & SQLITE_Defensive)!=0 |
| 3445 | && db->pVtabCtx==0 |
| 3446 | && db->nVdbeExec==0 |
dan | 7398365 | 2021-07-19 14:00:29 | [diff] [blame] | 3447 | && !sqlite3VtabInSync(db) |
drh | 070ae3b | 2019-11-16 13:51:31 | [diff] [blame] | 3448 | ){ |
| 3449 | return 1; |
| 3450 | } |
| 3451 | #endif |
| 3452 | return 0; |
| 3453 | } |
| 3454 | |
| 3455 | /* |
drh | d0c51d1 | 2019-11-16 12:04:38 | [diff] [blame] | 3456 | ** Return true if it is not allowed to drop the given table |
| 3457 | */ |
drh | 070ae3b | 2019-11-16 13:51:31 | [diff] [blame] | 3458 | static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){ |
drh | d0c51d1 | 2019-11-16 12:04:38 | [diff] [blame] | 3459 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 3460 | if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0; |
| 3461 | if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0; |
| 3462 | return 1; |
| 3463 | } |
drh | 070ae3b | 2019-11-16 13:51:31 | [diff] [blame] | 3464 | if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){ |
| 3465 | return 1; |
drh | d0c51d1 | 2019-11-16 12:04:38 | [diff] [blame] | 3466 | } |
dan | 35c7312 | 2021-11-06 18:22:50 | [diff] [blame] | 3467 | if( pTab->tabFlags & TF_Eponymous ){ |
| 3468 | return 1; |
| 3469 | } |
drh | d0c51d1 | 2019-11-16 12:04:38 | [diff] [blame] | 3470 | return 0; |
| 3471 | } |
| 3472 | |
| 3473 | /* |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3474 | ** This routine is called to do the work of a DROP TABLE statement. |
drh | d9b0257 | 2001-04-15 00:37:09 | [diff] [blame] | 3475 | ** pName is the name of the table to be dropped. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3476 | */ |
drh | a073384 | 2005-12-29 01:11:36 | [diff] [blame] | 3477 | void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3478 | Table *pTab; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3479 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 3480 | sqlite3 *db = pParse->db; |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 3481 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3482 | |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 3483 | if( db->mallocFailed ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 | [diff] [blame] | 3484 | goto exit_drop_table; |
| 3485 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 3486 | assert( pParse->nErr==0 ); |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3487 | assert( pName->nSrc==1 ); |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 3488 | assert( pName->a[0].fg.fixedSchema==0 ); |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 3489 | assert( pName->a[0].fg.isSubquery==0 ); |
drh | 7520996 | 2015-04-19 22:31:45 | [diff] [blame] | 3490 | if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 3491 | if( noErr ) db->suppressErr++; |
drh | 4d249e6 | 2016-06-10 22:49:01 | [diff] [blame] | 3492 | assert( isView==0 || isView==LOCATE_VIEW ); |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 3493 | pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
drh | a756466 | 2010-02-22 19:32:31 | [diff] [blame] | 3494 | if( noErr ) db->suppressErr--; |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3495 | |
drh | a073384 | 2005-12-29 01:11:36 | [diff] [blame] | 3496 | if( pTab==0 ){ |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 3497 | if( noErr ){ |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 3498 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase); |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 3499 | sqlite3ForceNotReadOnly(pParse); |
| 3500 | } |
drh | a073384 | 2005-12-29 01:11:36 | [diff] [blame] | 3501 | goto exit_drop_table; |
| 3502 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3503 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | e22a334 | 2003-04-22 20:30:37 | [diff] [blame] | 3504 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | b5258c3 | 2007-10-04 18:11:15 | [diff] [blame] | 3505 | |
| 3506 | /* If pTab is a virtual table, call ViewGetColumnNames() to ensure |
| 3507 | ** it is initialized. |
| 3508 | */ |
| 3509 | if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 3510 | goto exit_drop_table; |
| 3511 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3512 | #ifndef SQLITE_OMIT_AUTHORIZATION |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3513 | { |
| 3514 | int code; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 3515 | const char *zTab = SCHEMA_TABLE(iDb); |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 3516 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | f1a381e | 2006-06-16 08:01:02 | [diff] [blame] | 3517 | const char *zArg2 = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3518 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3519 | goto exit_drop_table; |
drh | e22a334 | 2003-04-22 20:30:37 | [diff] [blame] | 3520 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3521 | if( isView ){ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 3522 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3523 | code = SQLITE_DROP_TEMP_VIEW; |
| 3524 | }else{ |
| 3525 | code = SQLITE_DROP_VIEW; |
| 3526 | } |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 | [diff] [blame] | 3527 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | f1a381e | 2006-06-16 08:01:02 | [diff] [blame] | 3528 | }else if( IsVirtual(pTab) ){ |
| 3529 | code = SQLITE_DROP_VTABLE; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 | [diff] [blame] | 3530 | zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 | [diff] [blame] | 3531 | #endif |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3532 | }else{ |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 3533 | if( !OMIT_TEMPDB && iDb==1 ){ |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3534 | code = SQLITE_DROP_TEMP_TABLE; |
| 3535 | }else{ |
| 3536 | code = SQLITE_DROP_TABLE; |
| 3537 | } |
| 3538 | } |
danielk1977 | f1a381e | 2006-06-16 08:01:02 | [diff] [blame] | 3539 | if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3540 | goto exit_drop_table; |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3541 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3542 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
| 3543 | goto exit_drop_table; |
drh | 77ad4e4 | 2003-01-14 02:49:27 | [diff] [blame] | 3544 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 3545 | } |
| 3546 | #endif |
drh | 070ae3b | 2019-11-16 13:51:31 | [diff] [blame] | 3547 | if( tableMayNotBeDropped(db, pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3548 | sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3549 | goto exit_drop_table; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3550 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 3551 | |
| 3552 | #ifndef SQLITE_OMIT_VIEW |
| 3553 | /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
| 3554 | ** on a table. |
| 3555 | */ |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3556 | if( isView && !IsView(pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3557 | sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
| 3558 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 3559 | } |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3560 | if( !isView && IsView(pTab) ){ |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3561 | sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
| 3562 | goto exit_drop_table; |
drh | 4ff6dfa | 2002-03-03 23:06:00 | [diff] [blame] | 3563 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 3564 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3565 | |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 3566 | /* Generate code to remove the table from the schema table |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 3567 | ** on disk. |
| 3568 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3569 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3570 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 | [diff] [blame] | 3571 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
mistachkin | 0fc2da3 | 2018-07-20 20:56:22 | [diff] [blame] | 3572 | if( !isView ){ |
| 3573 | sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName); |
| 3574 | sqlite3FkDropTable(pParse, pName, pTab); |
| 3575 | } |
drh | faacf17 | 2011-08-12 01:51:45 | [diff] [blame] | 3576 | sqlite3CodeDropTable(pParse, pTab, iDb, isView); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3577 | } |
danielk1977 | a885810 | 2004-05-28 12:11:21 | [diff] [blame] | 3578 | |
| 3579 | exit_drop_table: |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 3580 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | /* |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3584 | ** This routine is called to create a new foreign key on the table |
| 3585 | ** currently under construction. pFromCol determines which columns |
| 3586 | ** in the current table point to the foreign key. If pFromCol==0 then |
| 3587 | ** connect the key to the last column inserted. pTo is the name of |
drh | bd50a92 | 2013-11-03 02:27:58 | [diff] [blame] | 3588 | ** the table referred to (a.k.a the "parent" table). pToCol is a list |
| 3589 | ** of tables in the parent pTo table. flags contains all |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3590 | ** information about the conflict resolution algorithms specified |
| 3591 | ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
| 3592 | ** |
| 3593 | ** An FKey structure is created and added to the table currently |
drh | e61922a | 2009-05-02 13:29:37 | [diff] [blame] | 3594 | ** under construction in the pParse->pNewTable field. |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3595 | ** |
| 3596 | ** The foreign key is set for IMMEDIATE processing. A subsequent call |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3597 | ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3598 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3599 | void sqlite3CreateForeignKey( |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3600 | Parse *pParse, /* Parsing context */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3601 | ExprList *pFromCol, /* Columns in this table that point to other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3602 | Token *pTo, /* Name of the other table */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3603 | ExprList *pToCol, /* Columns in the other table */ |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3604 | int flags /* Conflict resolution algorithms. */ |
| 3605 | ){ |
danielk1977 | 1857693 | 2008-08-06 13:47:40 | [diff] [blame] | 3606 | sqlite3 *db = pParse->db; |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3607 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 40e016e | 2004-11-04 14:47:11 | [diff] [blame] | 3608 | FKey *pFKey = 0; |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 3609 | FKey *pNextTo; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3610 | Table *p = pParse->pNewTable; |
drh | 913306a | 2021-11-26 17:10:18 | [diff] [blame] | 3611 | i64 nByte; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3612 | int i; |
| 3613 | int nCol; |
| 3614 | char *z; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3615 | |
| 3616 | assert( pTo!=0 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 3617 | if( p==0 || IN_DECLARE_VTAB ) goto fk_end; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3618 | if( pFromCol==0 ){ |
| 3619 | int iCol = p->nCol-1; |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 3620 | if( NEVER(iCol<0) ) goto fk_end; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3621 | if( pToCol && pToCol->nExpr!=1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3622 | sqlite3ErrorMsg(pParse, "foreign key on %s" |
drh | f7a9e1a | 2004-02-22 18:40:56 | [diff] [blame] | 3623 | " should reference only one column of table %T", |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 3624 | p->aCol[iCol].zCnName, pTo); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3625 | goto fk_end; |
| 3626 | } |
| 3627 | nCol = 1; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3628 | }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3629 | sqlite3ErrorMsg(pParse, |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3630 | "number of columns in foreign key does not match the number of " |
drh | f7a9e1a | 2004-02-22 18:40:56 | [diff] [blame] | 3631 | "columns in the referenced table"); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3632 | goto fk_end; |
| 3633 | }else{ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3634 | nCol = pFromCol->nExpr; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3635 | } |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 3636 | nByte = SZ_FKEY(nCol) + pTo->n + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3637 | if( pToCol ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3638 | for(i=0; i<pToCol->nExpr; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 3639 | nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3640 | } |
| 3641 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 3642 | pFKey = sqlite3DbMallocZero(db, nByte ); |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 3643 | if( pFKey==0 ){ |
| 3644 | goto fk_end; |
| 3645 | } |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3646 | pFKey->pFrom = p; |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 3647 | assert( IsOrdinaryTable(p) ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3648 | pFKey->pNextFrom = p->u.tab.pFKey; |
drh | e61922a | 2009-05-02 13:29:37 | [diff] [blame] | 3649 | z = (char*)&pFKey->aCol[nCol]; |
drh | df68f6b | 2002-09-21 15:57:57 | [diff] [blame] | 3650 | pFKey->zTo = z; |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 3651 | if( IN_RENAME_OBJECT ){ |
| 3652 | sqlite3RenameTokenMap(pParse, (void*)z, pTo); |
| 3653 | } |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3654 | memcpy(z, pTo->z, pTo->n); |
| 3655 | z[pTo->n] = 0; |
danielk1977 | 70d9e9c | 2009-04-24 18:06:09 | [diff] [blame] | 3656 | sqlite3Dequote(z); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3657 | z += pTo->n+1; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3658 | pFKey->nCol = nCol; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3659 | if( pFromCol==0 ){ |
| 3660 | pFKey->aCol[0].iFrom = p->nCol-1; |
| 3661 | }else{ |
| 3662 | for(i=0; i<nCol; i++){ |
| 3663 | int j; |
| 3664 | for(j=0; j<p->nCol; j++){ |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 3665 | if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){ |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3666 | pFKey->aCol[i].iFrom = j; |
| 3667 | break; |
| 3668 | } |
| 3669 | } |
| 3670 | if( j>=p->nCol ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3671 | sqlite3ErrorMsg(pParse, |
| 3672 | "unknown column \"%s\" in foreign key definition", |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 3673 | pFromCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3674 | goto fk_end; |
| 3675 | } |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 3676 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 3677 | sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 3678 | } |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | if( pToCol ){ |
| 3682 | for(i=0; i<nCol; i++){ |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 3683 | int n = sqlite3Strlen30(pToCol->a[i].zEName); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3684 | pFKey->aCol[i].zCol = z; |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 3685 | if( IN_RENAME_OBJECT ){ |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 3686 | sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName); |
dan | 6fe7f23 | 2018-08-10 19:19:33 | [diff] [blame] | 3687 | } |
drh | 41cee66 | 2019-12-12 20:22:34 | [diff] [blame] | 3688 | memcpy(z, pToCol->a[i].zEName, n); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3689 | z[n] = 0; |
| 3690 | z += n+1; |
| 3691 | } |
| 3692 | } |
| 3693 | pFKey->isDeferred = 0; |
dan | 8099ce6 | 2009-09-23 08:43:35 | [diff] [blame] | 3694 | pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ |
| 3695 | pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3696 | |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 3697 | assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3698 | pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, |
drh | acbcb7e | 2014-08-21 20:26:37 | [diff] [blame] | 3699 | pFKey->zTo, (void *)pFKey |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 3700 | ); |
dan | f59c5ca | 2009-09-22 16:55:38 | [diff] [blame] | 3701 | if( pNextTo==pFKey ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 3702 | sqlite3OomFault(db); |
dan | f59c5ca | 2009-09-22 16:55:38 | [diff] [blame] | 3703 | goto fk_end; |
| 3704 | } |
dan | 1da40a3 | 2009-09-19 17:00:31 | [diff] [blame] | 3705 | if( pNextTo ){ |
| 3706 | assert( pNextTo->pPrevTo==0 ); |
| 3707 | pFKey->pNextTo = pNextTo; |
| 3708 | pNextTo->pPrevTo = pFKey; |
| 3709 | } |
| 3710 | |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3711 | /* Link the foreign key to the table as the last step. |
| 3712 | */ |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 3713 | assert( IsOrdinaryTable(p) ); |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3714 | p->u.tab.pFKey = pFKey; |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3715 | pFKey = 0; |
| 3716 | |
| 3717 | fk_end: |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 3718 | sqlite3DbFree(db, pFKey); |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3719 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 3720 | sqlite3ExprListDelete(db, pFromCol); |
| 3721 | sqlite3ExprListDelete(db, pToCol); |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /* |
| 3725 | ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
| 3726 | ** clause is seen as part of a foreign key definition. The isDeferred |
| 3727 | ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
| 3728 | ** The behavior of the most recently created foreign key is adjusted |
| 3729 | ** accordingly. |
| 3730 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 3731 | void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3732 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3733 | Table *pTab; |
| 3734 | FKey *pFKey; |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3735 | if( (pTab = pParse->pNewTable)==0 ) return; |
drh | 78b2fa8 | 2021-10-07 12:11:20 | [diff] [blame] | 3736 | if( NEVER(!IsOrdinaryTable(pTab)) ) return; |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 3737 | if( (pFKey = pTab->u.tab.pFKey)==0 ) return; |
drh | 4c42983 | 2009-10-12 22:30:49 | [diff] [blame] | 3738 | assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 3739 | pFKey->isDeferred = (u8)isDeferred; |
drh | b7f9164 | 2004-10-31 02:22:47 | [diff] [blame] | 3740 | #endif |
drh | c2eef3b | 2002-08-31 18:53:06 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | /* |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3744 | ** Generate code that will erase and refill index *pIdx. This is |
| 3745 | ** used to initialize a newly created index or to recompute the |
| 3746 | ** content of an index in response to a REINDEX command. |
| 3747 | ** |
| 3748 | ** if memRootPage is not negative, it means that the index is newly |
drh | 1db639c | 2008-01-17 02:36:28 | [diff] [blame] | 3749 | ** created. The register specified by memRootPage contains the |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3750 | ** root page number of the index. If memRootPage is negative, then |
| 3751 | ** the index already exists and must be cleared before being refilled and |
| 3752 | ** the root page number of the index is taken from pIndex->tnum. |
| 3753 | */ |
| 3754 | static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
| 3755 | Table *pTab = pIndex->pTable; /* The table that is indexed */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 | [diff] [blame] | 3756 | int iTab = pParse->nTab++; /* Btree cursor used for pTab */ |
| 3757 | int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ |
drh | b07028f | 2011-10-14 21:49:18 | [diff] [blame] | 3758 | int iSorter; /* Cursor opened by OpenSorter (if in use) */ |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3759 | int addr1; /* Address of top of loop */ |
dan | 5134d13 | 2011-09-02 10:31:11 | [diff] [blame] | 3760 | int addr2; /* Address to jump to for next iteration */ |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 3761 | Pgno tnum; /* Root page of index */ |
drh | b2b9d3d | 2013-08-01 01:14:43 | [diff] [blame] | 3762 | int iPartIdxLabel; /* Jump to this label to skip a row */ |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3763 | Vdbe *v; /* Generate code into this virtual machine */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 3764 | KeyInfo *pKey; /* KeyInfo for index */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 3765 | int regRecord; /* Register holding assembled index record */ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 3766 | sqlite3 *db = pParse->db; /* The database connection */ |
| 3767 | int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3768 | |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 | [diff] [blame] | 3769 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 3770 | if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 3771 | db->aDb[iDb].zDbSName ) ){ |
danielk1977 | 1d54df8 | 2004-11-23 15:41:16 | [diff] [blame] | 3772 | return; |
| 3773 | } |
| 3774 | #endif |
| 3775 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 | [diff] [blame] | 3776 | /* Require a write-lock on the table to perform this operation */ |
| 3777 | sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
| 3778 | |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3779 | v = sqlite3GetVdbe(pParse); |
| 3780 | if( v==0 ) return; |
| 3781 | if( memRootPage>=0 ){ |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 3782 | tnum = (Pgno)memRootPage; |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3783 | }else{ |
| 3784 | tnum = pIndex->tnum; |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3785 | } |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 3786 | pKey = sqlite3KeyInfoOfIndex(pParse, pIndex); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 3787 | assert( pKey!=0 || pParse->nErr ); |
dan | a20fde6 | 2011-07-12 14:28:05 | [diff] [blame] | 3788 | |
dan | 689ab89 | 2011-08-12 15:02:00 | [diff] [blame] | 3789 | /* Open the sorter cursor if we are to use one. */ |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3790 | iSorter = pParse->nTab++; |
dan | fad9f9a | 2014-04-01 18:41:51 | [diff] [blame] | 3791 | sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*) |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 3792 | sqlite3KeyInfoRef(pKey), P4_KEYINFO); |
dan | a20fde6 | 2011-07-12 14:28:05 | [diff] [blame] | 3793 | |
| 3794 | /* Open the table. Loop through all rows of the table, inserting index |
| 3795 | ** records into the sorter. */ |
drh | dd9930e | 2013-10-23 23:37:02 | [diff] [blame] | 3796 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3797 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v); |
drh | 2d401ab | 2008-01-10 23:50:11 | [diff] [blame] | 3798 | regRecord = sqlite3GetTempReg(pParse); |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 3799 | sqlite3MultiWrite(pParse); |
dan | a20fde6 | 2011-07-12 14:28:05 | [diff] [blame] | 3800 | |
drh | 1c2c0b7 | 2014-01-04 19:27:05 | [diff] [blame] | 3801 | sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0); |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3802 | sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); |
drh | 8774451 | 2014-04-13 19:15:49 | [diff] [blame] | 3803 | sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3804 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v); |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3805 | sqlite3VdbeJumpHere(v, addr1); |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 3806 | if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3807 | sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 3808 | (char *)pKey, P4_KEYINFO); |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 3809 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0)); |
| 3810 | |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3811 | addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v); |
drh | 60de73e | 2016-04-05 15:59:23 | [diff] [blame] | 3812 | if( IsUniqueIndex(pIndex) ){ |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 3813 | int j2 = sqlite3VdbeGoto(v, 1); |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3814 | addr2 = sqlite3VdbeCurrentAddr(v); |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 3815 | sqlite3VdbeVerifyAbortable(v, OE_Abort); |
drh | 1153c7b | 2013-11-01 22:02:56 | [diff] [blame] | 3816 | sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord, |
drh | ac50232 | 2014-07-30 13:56:48 | [diff] [blame] | 3817 | pIndex->nKeyCol); VdbeCoverage(v); |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 3818 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
drh | 4031baf | 2018-05-28 17:31:20 | [diff] [blame] | 3819 | sqlite3VdbeJumpHere(v, j2); |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3820 | }else{ |
dan | 7ed6c06 | 2019-05-21 16:32:41 | [diff] [blame] | 3821 | /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not |
| 3822 | ** abort. The exception is if one of the indexed expressions contains a |
| 3823 | ** user function that throws an exception when it is evaluated. But the |
| 3824 | ** overhead of adding a statement journal to a CREATE INDEX statement is |
| 3825 | ** very small (since most of the pages written do not contain content that |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3826 | ** needs to be restored if the statement aborts), so we call |
dan | 7ed6c06 | 2019-05-21 16:32:41 | [diff] [blame] | 3827 | ** sqlite3MayAbort() for all CREATE INDEX statements. */ |
dan | ef14abb | 2019-05-21 14:42:24 | [diff] [blame] | 3828 | sqlite3MayAbort(pParse); |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3829 | addr2 = sqlite3VdbeCurrentAddr(v); |
dan | 689ab89 | 2011-08-12 15:02:00 | [diff] [blame] | 3830 | } |
drh | 6cf4a7d | 2014-10-13 13:00:58 | [diff] [blame] | 3831 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
drh | bf9ff25 | 2019-05-14 00:43:13 | [diff] [blame] | 3832 | if( !pIndex->bAscKeyBug ){ |
| 3833 | /* This OP_SeekEnd opcode makes index insert for a REINDEX go much |
| 3834 | ** faster by avoiding unnecessary seeks. But the optimization does |
| 3835 | ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables |
| 3836 | ** with DESC primary keys, since those indexes have there keys in |
| 3837 | ** a different order from the main table. |
drh | 8a6f89c | 2025-04-10 10:18:07 | [diff] [blame] | 3838 | ** See ticket: https://sqlite.org/src/info/bba7b69f9849b5bf |
drh | bf9ff25 | 2019-05-14 00:43:13 | [diff] [blame] | 3839 | */ |
| 3840 | sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx); |
| 3841 | } |
drh | 9b4eaeb | 2016-11-09 00:10:33 | [diff] [blame] | 3842 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); |
drh | ca892a7 | 2011-09-03 00:17:51 | [diff] [blame] | 3843 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 2d401ab | 2008-01-10 23:50:11 | [diff] [blame] | 3844 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 688852a | 2014-02-17 22:40:43 | [diff] [blame] | 3845 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
drh | d654be8 | 2005-09-20 17:42:23 | [diff] [blame] | 3846 | sqlite3VdbeJumpHere(v, addr1); |
dan | a20fde6 | 2011-07-12 14:28:05 | [diff] [blame] | 3847 | |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 3848 | sqlite3VdbeAddOp1(v, OP_Close, iTab); |
| 3849 | sqlite3VdbeAddOp1(v, OP_Close, iIdx); |
dan | 689ab89 | 2011-08-12 15:02:00 | [diff] [blame] | 3850 | sqlite3VdbeAddOp1(v, OP_Close, iSorter); |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | /* |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3854 | ** Allocate heap space to hold an Index object with nCol columns. |
| 3855 | ** |
| 3856 | ** Increase the allocation size to provide an extra nExtra bytes |
| 3857 | ** of 8-byte aligned space after the Index object and return a |
| 3858 | ** pointer to this extra space in *ppExtra. |
| 3859 | */ |
| 3860 | Index *sqlite3AllocateIndexObject( |
| 3861 | sqlite3 *db, /* Database connection */ |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 3862 | int nCol, /* Total number of columns in the index */ |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3863 | int nExtra, /* Number of bytes of extra space to alloc */ |
| 3864 | char **ppExtra /* Pointer to the "extra" space */ |
| 3865 | ){ |
| 3866 | Index *p; /* Allocated index object */ |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 3867 | i64 nByte; /* Bytes of space for Index object + arrays */ |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3868 | |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 3869 | assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] ); |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3870 | nByte = ROUND8(sizeof(Index)) + /* Index structure */ |
| 3871 | ROUND8(sizeof(char*)*nCol) + /* Index.azColl */ |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 3872 | ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */ |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 3873 | sizeof(i16)*nCol + /* Index.aiColumn */ |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3874 | sizeof(u8)*nCol); /* Index.aSortOrder */ |
| 3875 | p = sqlite3DbMallocZero(db, nByte + nExtra); |
| 3876 | if( p ){ |
| 3877 | char *pExtra = ((char*)p)+ROUND8(sizeof(Index)); |
drh | f19aa5f | 2015-12-30 16:51:20 | [diff] [blame] | 3878 | p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol); |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 3879 | p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1); |
| 3880 | p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol; |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3881 | p->aSortOrder = (u8*)pExtra; |
drh | cc803b2 | 2025-02-21 20:35:37 | [diff] [blame] | 3882 | assert( nCol>0 ); |
| 3883 | p->nColumn = (u16)nCol; |
| 3884 | p->nKeyCol = (u16)(nCol - 1); |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 3885 | *ppExtra = ((char*)p) + nByte; |
| 3886 | } |
| 3887 | return p; |
| 3888 | } |
| 3889 | |
| 3890 | /* |
dan | 9105fd5 | 2019-08-19 17:26:32 | [diff] [blame] | 3891 | ** If expression list pList contains an expression that was parsed with |
| 3892 | ** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in |
| 3893 | ** pParse and return non-zero. Otherwise, return zero. |
| 3894 | */ |
| 3895 | int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){ |
| 3896 | if( pList ){ |
| 3897 | int i; |
| 3898 | for(i=0; i<pList->nExpr; i++){ |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 3899 | if( pList->a[i].fg.bNulls ){ |
| 3900 | u8 sf = pList->a[i].fg.sortFlags; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3901 | sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", |
dan | 9105fd5 | 2019-08-19 17:26:32 | [diff] [blame] | 3902 | (sf==0 || sf==3) ? "FIRST" : "LAST" |
| 3903 | ); |
| 3904 | return 1; |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
| 3911 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3912 | ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
| 3913 | ** and pTblList is the name of the table that is to be indexed. Both will |
drh | adbca9c | 2001-09-27 15:11:53 | [diff] [blame] | 3914 | ** be NULL for a primary key or an index that is created to satisfy a |
| 3915 | ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
drh | 382c024 | 2001-10-06 16:33:02 | [diff] [blame] | 3916 | ** as the table to be indexed. pParse->pNewTable is a table that is |
| 3917 | ** currently being constructed by a CREATE TABLE statement. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3918 | ** |
drh | 382c024 | 2001-10-06 16:33:02 | [diff] [blame] | 3919 | ** pList is a list of columns to be indexed. pList will be NULL if this |
| 3920 | ** is a primary key or unique-constraint on the most recent column added |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3921 | ** to the table currently under construction. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3922 | */ |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 3923 | void sqlite3CreateIndex( |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 3924 | Parse *pParse, /* All information about this parse */ |
| 3925 | Token *pName1, /* First part of index name. May be NULL */ |
| 3926 | Token *pName2, /* Second part of index name. May be NULL */ |
| 3927 | SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 3928 | ExprList *pList, /* A list of columns to be indexed */ |
drh | 23bf66d | 2004-12-14 03:34:34 | [diff] [blame] | 3929 | int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
drh | 1c55ba0 | 2007-07-02 19:31:27 | [diff] [blame] | 3930 | Token *pStart, /* The CREATE token that begins this statement */ |
drh | 1fe0537 | 2013-07-31 18:12:26 | [diff] [blame] | 3931 | Expr *pPIWhere, /* WHERE clause for partial indices */ |
drh | 4d91a70 | 2006-01-04 15:54:36 | [diff] [blame] | 3932 | int sortOrder, /* Sort order of primary key when pList==NULL */ |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 3933 | int ifNotExist, /* Omit error if index already exists */ |
| 3934 | u8 idxType /* The index type */ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3935 | ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 3936 | Table *pTab = 0; /* Table to be indexed */ |
| 3937 | Index *pIndex = 0; /* The index to be created */ |
| 3938 | char *zName = 0; /* Name of the index */ |
| 3939 | int nName; /* Number of characters in zName */ |
drh | beae319 | 2001-09-22 18:12:08 | [diff] [blame] | 3940 | int i, j; |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 3941 | DbFixer sFix; /* For assigning database names to pTable */ |
| 3942 | int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 3943 | sqlite3 *db = pParse->db; |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 3944 | Db *pDb; /* The specific table containing the indexed database */ |
| 3945 | int iDb; /* Index of the database that is being written */ |
| 3946 | Token *pName = 0; /* Unqualified name of the index to create */ |
| 3947 | struct ExprList_item *pListItem; /* For looping over pList */ |
drh | c28c4e5 | 2013-10-03 19:21:41 | [diff] [blame] | 3948 | int nExtra = 0; /* Space allocated for zExtra[] */ |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 3949 | int nExtraCol; /* Number of extra columns needed */ |
drh | 47b927d | 2013-12-03 00:11:40 | [diff] [blame] | 3950 | char *zExtra = 0; /* Extra space after the Index object */ |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 3951 | Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3952 | |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 3953 | assert( db->pParse==pParse ); |
| 3954 | if( pParse->nErr ){ |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 3955 | goto exit_create_index; |
| 3956 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 3957 | assert( db->mallocFailed==0 ); |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 3958 | if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){ |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 3959 | goto exit_create_index; |
| 3960 | } |
| 3961 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 | [diff] [blame] | 3962 | goto exit_create_index; |
| 3963 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 | [diff] [blame] | 3964 | if( sqlite3HasExplicitNulls(pParse, pList) ){ |
| 3965 | goto exit_create_index; |
| 3966 | } |
drh | daffd0e | 2001-04-11 14:28:42 | [diff] [blame] | 3967 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 3968 | /* |
| 3969 | ** Find the table that is to be indexed. Return early if not found. |
| 3970 | */ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3971 | if( pTblName!=0 ){ |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3972 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3973 | /* Use the two-part index name to determine the database |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 | [diff] [blame] | 3974 | ** to search for the table. 'Fix' the table name to this db |
| 3975 | ** before looking up the table. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3976 | */ |
| 3977 | assert( pName1 && pName2 ); |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 | [diff] [blame] | 3978 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3979 | if( iDb<0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 | [diff] [blame] | 3980 | assert( pName && pName->z ); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3981 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 3982 | #ifndef SQLITE_OMIT_TEMPDB |
mistachkin | d557843 | 2012-08-25 10:01:29 | [diff] [blame] | 3983 | /* If the index name was unqualified, check if the table |
danielk1977 | fe91033 | 2007-12-02 11:46:34 | [diff] [blame] | 3984 | ** is a temp table. If so, set the database to 1. Do not do this |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3985 | ** if initializing a database schema. |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 3986 | */ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 | [diff] [blame] | 3987 | if( !db->init.busy ){ |
| 3988 | pTab = sqlite3SrcListLookup(pParse, pTblName); |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 3989 | if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
danielk1977 | fe91033 | 2007-12-02 11:46:34 | [diff] [blame] | 3990 | iDb = 1; |
| 3991 | } |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 | [diff] [blame] | 3992 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 3993 | #endif |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 | [diff] [blame] | 3994 | |
drh | d100f69 | 2013-10-03 15:39:44 | [diff] [blame] | 3995 | sqlite3FixInit(&sFix, pParse, iDb, "index", pName); |
| 3996 | if( sqlite3FixSrcList(&sFix, pTblName) ){ |
drh | 85c23c6 | 2005-08-20 03:03:04 | [diff] [blame] | 3997 | /* Because the parser constructs pTblName from a single identifier, |
| 3998 | ** sqlite3FixSrcList can never fail. */ |
| 3999 | assert(0); |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 4000 | } |
dan | 41fb5cd | 2012-10-04 19:33:00 | [diff] [blame] | 4001 | pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]); |
drh | c31c7c1 | 2012-10-08 23:25:07 | [diff] [blame] | 4002 | assert( db->mallocFailed==0 || pTab==0 ); |
| 4003 | if( pTab==0 ) goto exit_create_index; |
drh | 989b116 | 2013-08-01 22:27:26 | [diff] [blame] | 4004 | if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4005 | sqlite3ErrorMsg(pParse, |
drh | 989b116 | 2013-08-01 22:27:26 | [diff] [blame] | 4006 | "cannot create a TEMP index on non-TEMP table \"%s\"", |
| 4007 | pTab->zName); |
| 4008 | goto exit_create_index; |
| 4009 | } |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4010 | if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4011 | }else{ |
drh | e3c4137 | 2001-09-17 20:25:58 | [diff] [blame] | 4012 | assert( pName==0 ); |
drh | b07028f | 2011-10-14 21:49:18 | [diff] [blame] | 4013 | assert( pStart==0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4014 | pTab = pParse->pNewTable; |
drh | a6370df | 2006-01-04 21:40:06 | [diff] [blame] | 4015 | if( !pTab ) goto exit_create_index; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4016 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4017 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 4018 | pDb = &db->aDb[iDb]; |
danielk1977 | cbb18d2 | 2004-05-28 11:37:27 | [diff] [blame] | 4019 | |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4020 | assert( pTab!=0 ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4021 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 |
drh | 3a3a03f | 2014-09-11 16:36:43 | [diff] [blame] | 4022 | && db->init.busy==0 |
drh | 346f4e2 | 2019-03-25 21:35:41 | [diff] [blame] | 4023 | && pTblName!=0 |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 4024 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4025 | sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
drh | 0be9df0 | 2003-03-30 00:19:49 | [diff] [blame] | 4026 | goto exit_create_index; |
| 4027 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 4028 | #ifndef SQLITE_OMIT_VIEW |
drh | f38524d | 2021-08-02 16:41:57 | [diff] [blame] | 4029 | if( IsView(pTab) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4030 | sqlite3ErrorMsg(pParse, "views may not be indexed"); |
drh | a76b5df | 2002-02-23 02:32:10 | [diff] [blame] | 4031 | goto exit_create_index; |
| 4032 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 | [diff] [blame] | 4033 | #endif |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 | [diff] [blame] | 4034 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4035 | if( IsVirtual(pTab) ){ |
| 4036 | sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); |
| 4037 | goto exit_create_index; |
| 4038 | } |
| 4039 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4040 | |
| 4041 | /* |
| 4042 | ** Find the name of the index. Make sure there is not already another |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4043 | ** index or table with the same name. |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 4044 | ** |
| 4045 | ** Exception: If we are reading the names of permanent indices from the |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 4046 | ** sqlite_schema table (because some other process changed the schema) and |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 4047 | ** one of the index names collides with the name of a temporary table or |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4048 | ** index, then we will continue to process this index. |
drh | f57b339 | 2001-10-08 13:22:32 | [diff] [blame] | 4049 | ** |
| 4050 | ** If pName==0 it means that we are |
drh | adbca9c | 2001-09-27 15:11:53 | [diff] [blame] | 4051 | ** dealing with a primary key or UNIQUE constraint. We have to invent our |
| 4052 | ** own name. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4053 | */ |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4054 | if( pName ){ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4055 | zName = sqlite3NameFromToken(db, pName); |
drh | e3c4137 | 2001-09-17 20:25:58 | [diff] [blame] | 4056 | if( zName==0 ) goto exit_create_index; |
drh | b07028f | 2011-10-14 21:49:18 | [diff] [blame] | 4057 | assert( pName->z!=0 ); |
drh | c5a93d4 | 2019-08-12 00:08:07 | [diff] [blame] | 4058 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4059 | goto exit_create_index; |
drh | e3c4137 | 2001-09-17 20:25:58 | [diff] [blame] | 4060 | } |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 4061 | if( !IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4062 | if( !db->init.busy ){ |
drh | 626bcc8 | 2022-08-09 16:13:21 | [diff] [blame] | 4063 | if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4064 | sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
| 4065 | goto exit_create_index; |
| 4066 | } |
| 4067 | } |
| 4068 | if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){ |
| 4069 | if( !ifNotExist ){ |
| 4070 | sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
| 4071 | }else{ |
| 4072 | assert( !db->init.busy ); |
| 4073 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 4074 | sqlite3ForceNotReadOnly(pParse); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4075 | } |
danielk1977 | d45a031 | 2007-03-13 16:32:25 | [diff] [blame] | 4076 | goto exit_create_index; |
| 4077 | } |
| 4078 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 | [diff] [blame] | 4079 | }else{ |
drh | adbca9c | 2001-09-27 15:11:53 | [diff] [blame] | 4080 | int n; |
| 4081 | Index *pLoop; |
| 4082 | for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
drh | f089aa4 | 2008-07-08 19:34:06 | [diff] [blame] | 4083 | zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 4084 | if( zName==0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 | [diff] [blame] | 4085 | goto exit_create_index; |
| 4086 | } |
drh | 0aafa9c | 2016-08-05 14:35:47 | [diff] [blame] | 4087 | |
| 4088 | /* Automatic index names generated from within sqlite3_declare_vtab() |
| 4089 | ** must have names that are distinct from normal automatic index names. |
| 4090 | ** The following statement converts "sqlite3_autoindex..." into |
| 4091 | ** "sqlite3_butoindex..." in order to make the names distinct. |
| 4092 | ** The "vtab_err.test" test demonstrates the need of this statement. */ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4093 | if( IN_SPECIAL_PARSE ) zName[7]++; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4094 | } |
| 4095 | |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 4096 | /* Check for authorization to create an index. |
| 4097 | */ |
| 4098 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 4099 | if( !IN_RENAME_OBJECT ){ |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 4100 | const char *zDb = pDb->zDbSName; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 4101 | if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 | [diff] [blame] | 4102 | goto exit_create_index; |
| 4103 | } |
| 4104 | i = SQLITE_CREATE_INDEX; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 | [diff] [blame] | 4105 | if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4106 | if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
drh | e22a334 | 2003-04-22 20:30:37 | [diff] [blame] | 4107 | goto exit_create_index; |
| 4108 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 4109 | } |
| 4110 | #endif |
| 4111 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4112 | /* If pList==0, it means this routine was called to make a primary |
drh | 1ccde15 | 2000-06-17 13:12:39 | [diff] [blame] | 4113 | ** key out of the last column added to the table under construction. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4114 | ** So create a fake list to simulate this. |
| 4115 | */ |
| 4116 | if( pList==0 ){ |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 4117 | Token prevCol; |
dan | 26e731c | 2018-01-29 16:22:39 | [diff] [blame] | 4118 | Column *pCol = &pTab->aCol[pTab->nCol-1]; |
| 4119 | pCol->colFlags |= COLFLAG_UNIQUE; |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 4120 | sqlite3TokenInit(&prevCol, pCol->zCnName); |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 4121 | pList = sqlite3ExprListAppend(pParse, 0, |
| 4122 | sqlite3ExprAlloc(db, TK_ID, &prevCol, 0)); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4123 | if( pList==0 ) goto exit_create_index; |
drh | bc622bc | 2015-08-24 15:39:42 | [diff] [blame] | 4124 | assert( pList->nExpr==1 ); |
dan | 5b32bdf | 2019-08-17 15:47:32 | [diff] [blame] | 4125 | sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED); |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 4126 | }else{ |
| 4127 | sqlite3ExprListCheckLength(pParse, pList, "index"); |
drh | 8fe25c6 | 2019-03-31 21:09:33 | [diff] [blame] | 4128 | if( pParse->nErr ) goto exit_create_index; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4129 | } |
| 4130 | |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 4131 | /* Figure out how many bytes of space are required to store explicitly |
| 4132 | ** specified collation sequence names. |
| 4133 | */ |
| 4134 | for(i=0; i<pList->nExpr; i++){ |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4135 | Expr *pExpr = pList->a[i].pExpr; |
drh | 7d3d9da | 2015-09-01 00:42:52 | [diff] [blame] | 4136 | assert( pExpr!=0 ); |
| 4137 | if( pExpr->op==TK_COLLATE ){ |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 4138 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
dan | 911ce41 | 2013-05-15 15:16:50 | [diff] [blame] | 4139 | nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken)); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 4140 | } |
| 4141 | } |
| 4142 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4143 | /* |
| 4144 | ** Allocate the index structure. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4145 | */ |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 4146 | nName = sqlite3Strlen30(zName); |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4147 | nExtraCol = pPk ? pPk->nKeyCol : 1; |
drh | 8fe25c6 | 2019-03-31 21:09:33 | [diff] [blame] | 4148 | assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ ); |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4149 | pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol, |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 4150 | nName + nExtra + 1, &zExtra); |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4151 | if( db->mallocFailed ){ |
| 4152 | goto exit_create_index; |
| 4153 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 4154 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) ); |
drh | e09b84c | 2011-11-14 02:53:54 | [diff] [blame] | 4155 | assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) ); |
drh | 77e57df | 2013-10-22 14:28:02 | [diff] [blame] | 4156 | pIndex->zName = zExtra; |
| 4157 | zExtra += nName + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 | [diff] [blame] | 4158 | memcpy(pIndex->zName, zName, nName+1); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4159 | pIndex->pTable = pTab; |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 4160 | pIndex->onError = (u8)onError; |
drh | 9eade08 | 2013-10-24 14:16:10 | [diff] [blame] | 4161 | pIndex->uniqNotNull = onError!=OE_None; |
drh | 62340f8 | 2016-05-31 21:18:15 | [diff] [blame] | 4162 | pIndex->idxType = idxType; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4163 | pIndex->pSchema = db->aDb[iDb].pSchema; |
drh | 72ffd09 | 2013-10-30 15:52:32 | [diff] [blame] | 4164 | pIndex->nKeyCol = pList->nExpr; |
drh | 3780be1 | 2013-07-31 19:05:22 | [diff] [blame] | 4165 | if( pPIWhere ){ |
| 4166 | sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0); |
| 4167 | pIndex->pPartIdxWhere = pPIWhere; |
| 4168 | pPIWhere = 0; |
| 4169 | } |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 4170 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4171 | |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 4172 | /* Check to see if we should honor DESC requests on index columns |
| 4173 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4174 | if( pDb->pSchema->file_format>=4 ){ |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 4175 | sortOrderMask = -1; /* Honor DESC */ |
drh | fdd6e85 | 2005-12-16 01:06:16 | [diff] [blame] | 4176 | }else{ |
| 4177 | sortOrderMask = 0; /* Ignore DESC */ |
| 4178 | } |
| 4179 | |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4180 | /* Analyze the list of expressions that form the terms of the index and |
| 4181 | ** report any errors. In the common case where the expression is exactly |
| 4182 | ** a table column, store that column in aiColumn[]. For general expressions, |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 4183 | ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[]. |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4184 | ** |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4185 | ** TODO: Issue a warning if two or more columns of the index are identical. |
| 4186 | ** TODO: Issue a warning if the table primary key is used as part of the |
| 4187 | ** index key. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4188 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4189 | pListItem = pList->a; |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 4190 | if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4191 | pIndex->aColExpr = pList; |
| 4192 | pList = 0; |
| 4193 | } |
| 4194 | for(i=0; i<pIndex->nKeyCol; i++, pListItem++){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4195 | Expr *pCExpr; /* The i-th index expression */ |
| 4196 | int requestedSortOrder; /* ASC or DESC on the i-th expression */ |
drh | f19aa5f | 2015-12-30 16:51:20 | [diff] [blame] | 4197 | const char *zColl; /* Collation sequence name */ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 4198 | |
drh | edb04ed | 2015-09-04 12:54:01 | [diff] [blame] | 4199 | sqlite3StringToId(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 | [diff] [blame] | 4200 | sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0); |
| 4201 | if( pParse->nErr ) goto exit_create_index; |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 4202 | pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr); |
drh | a514b8e | 2015-08-25 00:27:06 | [diff] [blame] | 4203 | if( pCExpr->op!=TK_COLUMN ){ |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4204 | if( pTab==pParse->pNewTable ){ |
| 4205 | sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and " |
| 4206 | "UNIQUE constraints"); |
| 4207 | goto exit_create_index; |
| 4208 | } |
| 4209 | if( pIndex->aColExpr==0 ){ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4210 | pIndex->aColExpr = pList; |
| 4211 | pList = 0; |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4212 | } |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 4213 | j = XN_EXPR; |
| 4214 | pIndex->aiColumn[i] = XN_EXPR; |
drh | 8492653 | 2015-08-31 19:38:42 | [diff] [blame] | 4215 | pIndex->uniqNotNull = 0; |
drh | 4bc1cc1 | 2022-10-13 21:08:34 | [diff] [blame] | 4216 | pIndex->bHasExpr = 1; |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4217 | }else{ |
| 4218 | j = pCExpr->iColumn; |
| 4219 | assert( j<=0x7fff ); |
| 4220 | if( j<0 ){ |
| 4221 | j = pTab->iPKey; |
drh | c747673 | 2019-10-24 20:29:25 | [diff] [blame] | 4222 | }else{ |
| 4223 | if( pTab->aCol[j].notNull==0 ){ |
| 4224 | pIndex->uniqNotNull = 0; |
| 4225 | } |
| 4226 | if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){ |
| 4227 | pIndex->bHasVCol = 1; |
drh | 0853584 | 2022-10-17 14:30:01 | [diff] [blame] | 4228 | pIndex->bHasExpr = 1; |
drh | c747673 | 2019-10-24 20:29:25 | [diff] [blame] | 4229 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4230 | } |
| 4231 | pIndex->aiColumn[i] = (i16)j; |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 4232 | } |
drh | a514b8e | 2015-08-25 00:27:06 | [diff] [blame] | 4233 | zColl = 0; |
drh | 108aa00 | 2015-08-24 20:21:20 | [diff] [blame] | 4234 | if( pListItem->pExpr->op==TK_COLLATE ){ |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4235 | int nColl; |
drh | f975107 | 2021-10-07 13:40:29 | [diff] [blame] | 4236 | assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) ); |
dan | 911ce41 | 2013-05-15 15:16:50 | [diff] [blame] | 4237 | zColl = pListItem->pExpr->u.zToken; |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4238 | nColl = sqlite3Strlen30(zColl) + 1; |
| 4239 | assert( nExtra>=nColl ); |
| 4240 | memcpy(zExtra, zColl, nColl); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 4241 | zColl = zExtra; |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4242 | zExtra += nColl; |
| 4243 | nExtra -= nColl; |
drh | a514b8e | 2015-08-25 00:27:06 | [diff] [blame] | 4244 | }else if( j>=0 ){ |
drh | 65b4009 | 2021-08-05 15:27:19 | [diff] [blame] | 4245 | zColl = sqlite3ColumnColl(&pTab->aCol[j]); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 | [diff] [blame] | 4246 | } |
drh | f19aa5f | 2015-12-30 16:51:20 | [diff] [blame] | 4247 | if( !zColl ) zColl = sqlite3StrBINARY; |
drh | b7f24de | 2009-05-13 17:35:23 | [diff] [blame] | 4248 | if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 | [diff] [blame] | 4249 | goto exit_create_index; |
| 4250 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 4251 | pIndex->azColl[i] = zColl; |
drh | d88fd53 | 2022-05-02 20:49:30 | [diff] [blame] | 4252 | requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask; |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 4253 | pIndex->aSortOrder[i] = (u8)requestedSortOrder; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4254 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4255 | |
| 4256 | /* Append the table key to the end of the index. For WITHOUT ROWID |
| 4257 | ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For |
| 4258 | ** normal tables (when pPk==0) this will be the rowid. |
| 4259 | */ |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4260 | if( pPk ){ |
drh | 7913e41 | 2013-11-01 20:30:36 | [diff] [blame] | 4261 | for(j=0; j<pPk->nKeyCol; j++){ |
| 4262 | int x = pPk->aiColumn[j]; |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4263 | assert( x>=0 ); |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 4264 | if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4265 | pIndex->nColumn--; |
drh | 7913e41 | 2013-11-01 20:30:36 | [diff] [blame] | 4266 | }else{ |
drh | f78d0f4 | 2019-04-28 19:27:02 | [diff] [blame] | 4267 | testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); |
drh | 7913e41 | 2013-11-01 20:30:36 | [diff] [blame] | 4268 | pIndex->aiColumn[i] = x; |
| 4269 | pIndex->azColl[i] = pPk->azColl[j]; |
| 4270 | pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
| 4271 | i++; |
| 4272 | } |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4273 | } |
drh | 7913e41 | 2013-11-01 20:30:36 | [diff] [blame] | 4274 | assert( i==pIndex->nColumn ); |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4275 | }else{ |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 4276 | pIndex->aiColumn[i] = XN_ROWID; |
drh | f19aa5f | 2015-12-30 16:51:20 | [diff] [blame] | 4277 | pIndex->azColl[i] = sqlite3StrBINARY; |
drh | 4415628 | 2013-10-23 22:23:03 | [diff] [blame] | 4278 | } |
drh | 51147ba | 2005-07-23 22:59:55 | [diff] [blame] | 4279 | sqlite3DefaultRowEst(pIndex); |
drh | e13e9f5 | 2013-10-05 19:18:00 | [diff] [blame] | 4280 | if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4281 | |
dan | f769cd6 | 2016-02-24 20:16:28 | [diff] [blame] | 4282 | /* If this index contains every column of its table, then mark |
| 4283 | ** it as a covering index */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4284 | assert( HasRowid(pTab) |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 4285 | || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 ); |
drh | 00eee7a | 2023-10-06 12:55:53 | [diff] [blame] | 4286 | recomputeColumnsNotIndexed(pIndex); |
dan | f769cd6 | 2016-02-24 20:16:28 | [diff] [blame] | 4287 | if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){ |
| 4288 | pIndex->isCovering = 1; |
| 4289 | for(j=0; j<pTab->nCol; j++){ |
| 4290 | if( j==pTab->iPKey ) continue; |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 4291 | if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue; |
dan | f769cd6 | 2016-02-24 20:16:28 | [diff] [blame] | 4292 | pIndex->isCovering = 0; |
| 4293 | break; |
| 4294 | } |
| 4295 | } |
| 4296 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4297 | if( pTab==pParse->pNewTable ){ |
| 4298 | /* This routine has been called to create an automatic index as a |
| 4299 | ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
| 4300 | ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
| 4301 | ** i.e. one of: |
| 4302 | ** |
| 4303 | ** CREATE TABLE t(x PRIMARY KEY, y); |
| 4304 | ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
| 4305 | ** |
| 4306 | ** Either way, check to see if the table already has such an index. If |
| 4307 | ** so, don't bother creating this one. This only applies to |
| 4308 | ** automatically created indices. Users can do as they wish with |
| 4309 | ** explicit indices. |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4310 | ** |
| 4311 | ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent |
| 4312 | ** (and thus suppressing the second one) even if they have different |
| 4313 | ** sort orders. |
| 4314 | ** |
| 4315 | ** If there are different collating sequences or if the columns of |
| 4316 | ** the constraint occur in different orders, then the constraints are |
| 4317 | ** considered distinct and both result in separate indices. |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4318 | */ |
| 4319 | Index *pIdx; |
| 4320 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 4321 | int k; |
drh | 5f1d1d9 | 2014-07-31 22:59:04 | [diff] [blame] | 4322 | assert( IsUniqueIndex(pIdx) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 4323 | assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 | [diff] [blame] | 4324 | assert( IsUniqueIndex(pIndex) ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4325 | |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 4326 | if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue; |
| 4327 | for(k=0; k<pIdx->nKeyCol; k++){ |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4328 | const char *z1; |
| 4329 | const char *z2; |
drh | 1f9ca2c | 2015-08-25 16:57:52 | [diff] [blame] | 4330 | assert( pIdx->aiColumn[k]>=0 ); |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4331 | if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4332 | z1 = pIdx->azColl[k]; |
| 4333 | z2 = pIndex->azColl[k]; |
drh | c41c132 | 2016-02-11 13:30:36 | [diff] [blame] | 4334 | if( sqlite3StrICmp(z1, z2) ) break; |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4335 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 4336 | if( k==pIdx->nKeyCol ){ |
danielk1977 | f736b77 | 2004-06-17 06:13:34 | [diff] [blame] | 4337 | if( pIdx->onError!=pIndex->onError ){ |
| 4338 | /* This constraint creates the same index as a previous |
| 4339 | ** constraint specified somewhere in the CREATE TABLE statement. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4340 | ** However the ON CONFLICT clauses are different. If both this |
danielk1977 | f736b77 | 2004-06-17 06:13:34 | [diff] [blame] | 4341 | ** constraint and the previous equivalent constraint have explicit |
| 4342 | ** ON CONFLICT clauses this is an error. Otherwise, use the |
mistachkin | 48864df | 2013-03-21 21:20:32 | [diff] [blame] | 4343 | ** explicitly specified behavior for the index. |
danielk1977 | f736b77 | 2004-06-17 06:13:34 | [diff] [blame] | 4344 | */ |
| 4345 | if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4346 | sqlite3ErrorMsg(pParse, |
danielk1977 | f736b77 | 2004-06-17 06:13:34 | [diff] [blame] | 4347 | "conflicting ON CONFLICT clauses specified", 0); |
| 4348 | } |
| 4349 | if( pIdx->onError==OE_Default ){ |
| 4350 | pIdx->onError = pIndex->onError; |
| 4351 | } |
| 4352 | } |
drh | 273bfe9 | 2016-06-02 16:22:53 | [diff] [blame] | 4353 | if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType; |
drh | 885eeb6 | 2019-01-09 02:02:24 | [diff] [blame] | 4354 | if( IN_RENAME_OBJECT ){ |
| 4355 | pIndex->pNext = pParse->pNewIndex; |
| 4356 | pParse->pNewIndex = pIndex; |
| 4357 | pIndex = 0; |
| 4358 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4359 | goto exit_create_index; |
| 4360 | } |
| 4361 | } |
| 4362 | } |
| 4363 | |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 4364 | if( !IN_RENAME_OBJECT ){ |
drh | d78eeee | 2001-09-13 16:18:53 | [diff] [blame] | 4365 | |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4366 | /* Link the new Index structure to its table and to the other |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4367 | ** in-memory database structures. |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 4368 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4369 | assert( pParse->nErr==0 ); |
| 4370 | if( db->init.busy ){ |
| 4371 | Index *p; |
| 4372 | assert( !IN_SPECIAL_PARSE ); |
| 4373 | assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) ); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4374 | if( pTblName!=0 ){ |
| 4375 | pIndex->tnum = db->init.newTnum; |
drh | 8d40673 | 2019-01-30 18:33:33 | [diff] [blame] | 4376 | if( sqlite3IndexHasDuplicateRootPage(pIndex) ){ |
drh | 8bf4126 | 2019-01-30 19:50:07 | [diff] [blame] | 4377 | sqlite3ErrorMsg(pParse, "invalid rootpage"); |
drh | 8d40673 | 2019-01-30 18:33:33 | [diff] [blame] | 4378 | pParse->rc = SQLITE_CORRUPT_BKPT; |
| 4379 | goto exit_create_index; |
| 4380 | } |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4381 | } |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4382 | p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
dan | da7a4c0 | 2019-01-30 19:12:13 | [diff] [blame] | 4383 | pIndex->zName, pIndex); |
| 4384 | if( p ){ |
| 4385 | assert( p==pIndex ); /* Malloc must have failed */ |
| 4386 | sqlite3OomFault(db); |
| 4387 | goto exit_create_index; |
| 4388 | } |
| 4389 | db->mDbFlags |= DBFLAG_SchemaChange; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4390 | } |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 4391 | |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4392 | /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the |
| 4393 | ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then |
| 4394 | ** emit code to allocate the index rootpage on disk and make an entry for |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 4395 | ** the index in the sqlite_schema table and populate the index with |
| 4396 | ** content. But, do not do this if we are simply reading the sqlite_schema |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4397 | ** table to parse the schema, or if this index is the PRIMARY KEY index |
| 4398 | ** of a WITHOUT ROWID table. |
| 4399 | ** |
| 4400 | ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY |
| 4401 | ** or UNIQUE index in a CREATE TABLE statement. Since the table |
| 4402 | ** has just been created, it contains no data and the index initialization |
| 4403 | ** step can be skipped. |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 4404 | */ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4405 | else if( HasRowid(pTab) || pTblName!=0 ){ |
| 4406 | Vdbe *v; |
| 4407 | char *zStmt; |
| 4408 | int iMem = ++pParse->nMem; |
drh | 063336a | 2004-11-05 20:58:39 | [diff] [blame] | 4409 | |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4410 | v = sqlite3GetVdbe(pParse); |
| 4411 | if( v==0 ) goto exit_create_index; |
| 4412 | |
| 4413 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
| 4414 | |
| 4415 | /* Create the rootpage for the index using CreateIndex. But before |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4416 | ** doing so, code a Noop instruction and store its address in |
| 4417 | ** Index.tnum. This is required in case this index is actually a |
| 4418 | ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4419 | ** that case the convertToWithoutRowidTable() routine will replace |
| 4420 | ** the Noop with a Goto to jump over the VDBE code generated below. */ |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 4421 | pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4422 | sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY); |
| 4423 | |
| 4424 | /* Gather the complete text of the CREATE INDEX statement into |
| 4425 | ** the zStmt variable |
| 4426 | */ |
drh | 55f66b3 | 2019-07-16 19:44:32 | [diff] [blame] | 4427 | assert( pName!=0 || pStart==0 ); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4428 | if( pStart ){ |
| 4429 | int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n; |
| 4430 | if( pName->z[n-1]==';' ) n--; |
| 4431 | /* A named index with an explicit CREATE INDEX statement */ |
| 4432 | zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", |
| 4433 | onError==OE_None ? "" : " UNIQUE", n, pName->z); |
| 4434 | }else{ |
| 4435 | /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
| 4436 | /* zStmt = sqlite3MPrintf(""); */ |
| 4437 | zStmt = 0; |
| 4438 | } |
| 4439 | |
drh | 1e32bed | 2020-06-19 13:33:53 | [diff] [blame] | 4440 | /* Add an entry in sqlite_schema for this index |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4441 | */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4442 | sqlite3NestedParse(pParse, |
drh | fd4bf77 | 2021-12-03 14:43:49 | [diff] [blame] | 4443 | "INSERT INTO %Q." LEGACY_SCHEMA_TABLE " VALUES('index',%Q,%Q,#%d,%Q);", |
| 4444 | db->aDb[iDb].zDbSName, |
| 4445 | pIndex->zName, |
| 4446 | pTab->zName, |
| 4447 | iMem, |
| 4448 | zStmt |
| 4449 | ); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4450 | sqlite3DbFree(db, zStmt); |
| 4451 | |
| 4452 | /* Fill the index with data and reparse the schema. Code an OP_Expire |
| 4453 | ** to invalidate all pre-compiled statements. |
| 4454 | */ |
| 4455 | if( pTblName ){ |
| 4456 | sqlite3RefillIndex(pParse, pIndex, iMem); |
| 4457 | sqlite3ChangeCookie(pParse, iDb); |
| 4458 | sqlite3VdbeAddParseSchemaOp(v, iDb, |
dan | 6a5a13d | 2021-02-17 20:08:22 | [diff] [blame] | 4459 | sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0); |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4460 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 1); |
| 4461 | } |
| 4462 | |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 4463 | sqlite3VdbeJumpHere(v, (int)pIndex->tnum); |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 4464 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4465 | } |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 4466 | if( db->init.busy || pTblName==0 ){ |
drh | d35bdd6 | 2019-12-15 02:49:32 | [diff] [blame] | 4467 | pIndex->pNext = pTab->pIndex; |
| 4468 | pTab->pIndex = pIndex; |
drh | 234c39d | 2004-07-24 03:30:47 | [diff] [blame] | 4469 | pIndex = 0; |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4470 | } |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 4471 | else if( IN_RENAME_OBJECT ){ |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4472 | assert( pParse->pNewIndex==0 ); |
| 4473 | pParse->pNewIndex = pIndex; |
| 4474 | pIndex = 0; |
| 4475 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 | [diff] [blame] | 4476 | |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4477 | /* Clean up before exiting */ |
| 4478 | exit_create_index: |
dan | cf8f289 | 2018-08-09 20:47:01 | [diff] [blame] | 4479 | if( pIndex ) sqlite3FreeIndex(db, pIndex); |
drh | 97060e5 | 2021-03-21 17:52:47 | [diff] [blame] | 4480 | if( pTab ){ |
| 4481 | /* Ensure all REPLACE indexes on pTab are at the end of the pIndex list. |
| 4482 | ** The list was already ordered when this routine was entered, so at this |
| 4483 | ** point at most a single index (the newly added index) will be out of |
| 4484 | ** order. So we have to reorder at most one index. */ |
drh | e85e1da | 2021-10-01 21:01:07 | [diff] [blame] | 4485 | Index **ppFrom; |
drh | d35bdd6 | 2019-12-15 02:49:32 | [diff] [blame] | 4486 | Index *pThis; |
| 4487 | for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){ |
| 4488 | Index *pNext; |
| 4489 | if( pThis->onError!=OE_Replace ) continue; |
| 4490 | while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){ |
| 4491 | *ppFrom = pNext; |
| 4492 | pThis->pNext = pNext->pNext; |
| 4493 | pNext->pNext = pThis; |
| 4494 | ppFrom = &pNext->pNext; |
| 4495 | } |
| 4496 | break; |
| 4497 | } |
drh | 97060e5 | 2021-03-21 17:52:47 | [diff] [blame] | 4498 | #ifdef SQLITE_DEBUG |
| 4499 | /* Verify that all REPLACE indexes really are now at the end |
| 4500 | ** of the index list. In other words, no other index type ever |
| 4501 | ** comes after a REPLACE index on the list. */ |
| 4502 | for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){ |
| 4503 | assert( pThis->onError!=OE_Replace |
| 4504 | || pThis->pNext==0 |
| 4505 | || pThis->pNext->onError==OE_Replace ); |
| 4506 | } |
| 4507 | #endif |
drh | d35bdd6 | 2019-12-15 02:49:32 | [diff] [blame] | 4508 | } |
drh | 1fe0537 | 2013-07-31 18:12:26 | [diff] [blame] | 4509 | sqlite3ExprDelete(db, pPIWhere); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 4510 | sqlite3ExprListDelete(db, pList); |
| 4511 | sqlite3SrcListDelete(db, pTblName); |
| 4512 | sqlite3DbFree(db, zName); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* |
drh | 51147ba | 2005-07-23 22:59:55 | [diff] [blame] | 4516 | ** Fill the Index.aiRowEst[] array with default information - information |
drh | 91124b3 | 2005-08-18 18:15:05 | [diff] [blame] | 4517 | ** to be used when we have not run the ANALYZE command. |
drh | 28c4cf4 | 2005-07-27 20:41:43 | [diff] [blame] | 4518 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 4519 | ** aiRowEst[0] is supposed to contain the number of elements in the index. |
drh | 28c4cf4 | 2005-07-27 20:41:43 | [diff] [blame] | 4520 | ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
| 4521 | ** number of rows in the table that match any particular value of the |
| 4522 | ** first column of the index. aiRowEst[2] is an estimate of the number |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 4523 | ** of rows that match any particular combination of the first 2 columns |
drh | 28c4cf4 | 2005-07-27 20:41:43 | [diff] [blame] | 4524 | ** of the index. And so forth. It must always be the case that |
| 4525 | * |
| 4526 | ** aiRowEst[N]<=aiRowEst[N-1] |
| 4527 | ** aiRowEst[N]>=1 |
| 4528 | ** |
| 4529 | ** Apart from that, we have little to go on besides intuition as to |
| 4530 | ** how aiRowEst[] should be initialized. The numbers generated here |
| 4531 | ** are based on typical values found in actual indices. |
drh | 51147ba | 2005-07-23 22:59:55 | [diff] [blame] | 4532 | */ |
| 4533 | void sqlite3DefaultRowEst(Index *pIdx){ |
drh | 56c65c9 | 2020-05-28 00:45:16 | [diff] [blame] | 4534 | /* 10, 9, 8, 7, 6 */ |
| 4535 | static const LogEst aVal[] = { 33, 32, 30, 28, 26 }; |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 4536 | LogEst *a = pIdx->aiRowLogEst; |
drh | 56c65c9 | 2020-05-28 00:45:16 | [diff] [blame] | 4537 | LogEst x; |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 4538 | int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol); |
drh | 51147ba | 2005-07-23 22:59:55 | [diff] [blame] | 4539 | int i; |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 4540 | |
drh | 33bec3f | 2017-02-17 13:38:15 | [diff] [blame] | 4541 | /* Indexes with default row estimates should not have stat1 data */ |
| 4542 | assert( !pIdx->hasStat1 ); |
| 4543 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4544 | /* Set the first entry (number of rows in the index) to the estimated |
drh | 8dc570b | 2016-06-08 18:07:21 | [diff] [blame] | 4545 | ** number of rows in the table, or half the number of rows in the table |
drh | 56c65c9 | 2020-05-28 00:45:16 | [diff] [blame] | 4546 | ** for a partial index. |
| 4547 | ** |
| 4548 | ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1 |
| 4549 | ** table but other parts we are having to guess at, then do not let the |
| 4550 | ** estimated number of rows in the table be less than 1000 (LogEst 99). |
| 4551 | ** Failure to do this can cause the indexes for which we do not have |
drh | 8c1fbe8 | 2020-08-11 17:20:02 | [diff] [blame] | 4552 | ** stat1 data to be ignored by the query planner. |
drh | 56c65c9 | 2020-05-28 00:45:16 | [diff] [blame] | 4553 | */ |
| 4554 | x = pIdx->pTable->nRowLogEst; |
| 4555 | assert( 99==sqlite3LogEst(1000) ); |
| 4556 | if( x<99 ){ |
| 4557 | pIdx->pTable->nRowLogEst = x = 99; |
| 4558 | } |
drh | 5f086dd | 2021-04-29 13:37:36 | [diff] [blame] | 4559 | if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); } |
drh | 56c65c9 | 2020-05-28 00:45:16 | [diff] [blame] | 4560 | a[0] = x; |
dan | 264d2b9 | 2014-04-29 19:01:57 | [diff] [blame] | 4561 | |
| 4562 | /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is |
| 4563 | ** 6 and each subsequent value (if any) is 5. */ |
dan | cfc9df7 | 2014-04-25 15:01:01 | [diff] [blame] | 4564 | memcpy(&a[1], aVal, nCopy*sizeof(LogEst)); |
dan | 264d2b9 | 2014-04-29 19:01:57 | [diff] [blame] | 4565 | for(i=nCopy+1; i<=pIdx->nKeyCol; i++){ |
| 4566 | a[i] = 23; assert( 23==sqlite3LogEst(5) ); |
drh | 28c4cf4 | 2005-07-27 20:41:43 | [diff] [blame] | 4567 | } |
dan | 264d2b9 | 2014-04-29 19:01:57 | [diff] [blame] | 4568 | |
| 4569 | assert( 0==sqlite3LogEst(1) ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 | [diff] [blame] | 4570 | if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0; |
drh | 51147ba | 2005-07-23 22:59:55 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | /* |
drh | 74e24cd | 2002-01-09 03:19:59 | [diff] [blame] | 4574 | ** This routine will drop an existing named index. This routine |
| 4575 | ** implements the DROP INDEX statement. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4576 | */ |
drh | 4d91a70 | 2006-01-04 15:54:36 | [diff] [blame] | 4577 | void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4578 | Index *pIndex; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4579 | Vdbe *v; |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 4580 | sqlite3 *db = pParse->db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4581 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4582 | |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 4583 | if( db->mallocFailed ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 | [diff] [blame] | 4584 | goto exit_drop_index; |
| 4585 | } |
drh | 3cdb139 | 2022-01-24 12:48:54 | [diff] [blame] | 4586 | assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4587 | assert( pName->nSrc==1 ); |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4588 | assert( pName->a[0].fg.fixedSchema==0 ); |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 4589 | assert( pName->a[0].fg.isSubquery==0 ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 | [diff] [blame] | 4590 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 4591 | goto exit_drop_index; |
| 4592 | } |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4593 | pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].u4.zDatabase); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4594 | if( pIndex==0 ){ |
drh | 4d91a70 | 2006-01-04 15:54:36 | [diff] [blame] | 4595 | if( !ifExists ){ |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 4596 | sqlite3ErrorMsg(pParse, "no such index: %S", pName->a); |
dan | 5796675 | 2011-04-09 17:32:58 | [diff] [blame] | 4597 | }else{ |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4598 | sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase); |
drh | 31da7be | 2021-05-13 18:24:22 | [diff] [blame] | 4599 | sqlite3ForceNotReadOnly(pParse); |
drh | 4d91a70 | 2006-01-04 15:54:36 | [diff] [blame] | 4600 | } |
drh | a6ecd33 | 2004-06-10 00:29:09 | [diff] [blame] | 4601 | pParse->checkSchema = 1; |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4602 | goto exit_drop_index; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4603 | } |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 4604 | if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4605 | sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
drh | 485b39b | 2002-07-13 03:11:52 | [diff] [blame] | 4606 | "or PRIMARY KEY constraint cannot be dropped", 0); |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4607 | goto exit_drop_index; |
| 4608 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4609 | iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 4610 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 4611 | { |
| 4612 | int code = SQLITE_DROP_INDEX; |
| 4613 | Table *pTab = pIndex->pTable; |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 4614 | const char *zDb = db->aDb[iDb].zDbSName; |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 4615 | const char *zTab = SCHEMA_TABLE(iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4616 | if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4617 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 4618 | } |
drh | 93fd542 | 2021-06-14 20:41:20 | [diff] [blame] | 4619 | if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4620 | if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4621 | goto exit_drop_index; |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 4622 | } |
drh | ed6c867 | 2003-01-12 18:02:16 | [diff] [blame] | 4623 | } |
drh | e5f9c64 | 2003-01-13 23:27:31 | [diff] [blame] | 4624 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4625 | |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 4626 | /* Generate code to remove the index and from the schema table */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4627 | v = sqlite3GetVdbe(pParse); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4628 | if( v ){ |
drh | 77658e2 | 2007-12-04 16:54:52 | [diff] [blame] | 4629 | sqlite3BeginWriteOperation(pParse, 1, iDb); |
drh | b17131a | 2004-11-05 22:18:49 | [diff] [blame] | 4630 | sqlite3NestedParse(pParse, |
drh | a4a871c | 2021-11-04 14:04:20 | [diff] [blame] | 4631 | "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'", |
drh | 346a70c | 2020-06-15 20:27:35 | [diff] [blame] | 4632 | db->aDb[iDb].zDbSName, pIndex->zName |
drh | b17131a | 2004-11-05 22:18:49 | [diff] [blame] | 4633 | ); |
drh | a5ae4c3 | 2011-08-07 01:31:52 | [diff] [blame] | 4634 | sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName); |
drh | 9cbf342 | 2008-01-17 16:22:13 | [diff] [blame] | 4635 | sqlite3ChangeCookie(pParse, iDb); |
drh | b17131a | 2004-11-05 22:18:49 | [diff] [blame] | 4636 | destroyRootPage(pParse, pIndex->tnum, iDb); |
drh | 66a5167 | 2008-01-03 00:01:23 | [diff] [blame] | 4637 | sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4638 | } |
| 4639 | |
drh | d24cc42 | 2003-03-27 12:51:24 | [diff] [blame] | 4640 | exit_drop_index: |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 4641 | sqlite3SrcListDelete(db, pName); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | /* |
dan | 9ace112 | 2012-03-29 07:51:45 | [diff] [blame] | 4645 | ** pArray is a pointer to an array of objects. Each object in the |
| 4646 | ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc() |
| 4647 | ** to extend the array so that there is space for a new object at the end. |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4648 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 | [diff] [blame] | 4649 | ** When this function is called, *pnEntry contains the current size of |
| 4650 | ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes |
| 4651 | ** in total). |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4652 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 | [diff] [blame] | 4653 | ** If the realloc() is successful (i.e. if no OOM condition occurs), the |
| 4654 | ** space allocated for the new object is zeroed, *pnEntry updated to |
| 4655 | ** reflect the new size of the array and a pointer to the new allocation |
| 4656 | ** returned. *pIdx is set to the index of the new array entry in this case. |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4657 | ** |
dan | 9ace112 | 2012-03-29 07:51:45 | [diff] [blame] | 4658 | ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains |
| 4659 | ** unchanged and a copy of pArray returned. |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4660 | */ |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4661 | void *sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4662 | sqlite3 *db, /* Connection to notify of malloc failures */ |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4663 | void *pArray, /* Array of objects. Might be reallocated */ |
| 4664 | int szEntry, /* Size of each object in the array */ |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4665 | int *pnEntry, /* Number of objects currently in use */ |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4666 | int *pIdx /* Write the index of a new slot here */ |
| 4667 | ){ |
| 4668 | char *z; |
drh | f6ad201 | 2019-04-13 14:07:57 | [diff] [blame] | 4669 | sqlite3_int64 n = *pIdx = *pnEntry; |
drh | 6c53515 | 2012-02-02 03:38:30 | [diff] [blame] | 4670 | if( (n & (n-1))==0 ){ |
drh | 0aa3231 | 2019-04-13 04:01:12 | [diff] [blame] | 4671 | sqlite3_int64 sz = (n==0) ? 1 : 2*n; |
drh | 6c53515 | 2012-02-02 03:38:30 | [diff] [blame] | 4672 | void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry); |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4673 | if( pNew==0 ){ |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4674 | *pIdx = -1; |
| 4675 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4676 | } |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4677 | pArray = pNew; |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4678 | } |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4679 | z = (char*)pArray; |
drh | 6c53515 | 2012-02-02 03:38:30 | [diff] [blame] | 4680 | memset(&z[n * szEntry], 0, szEntry); |
drh | cf64372 | 2007-03-27 13:36:37 | [diff] [blame] | 4681 | ++*pnEntry; |
| 4682 | return pArray; |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | /* |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4686 | ** Append a new element to the given IdList. Create a new IdList if |
| 4687 | ** need be. |
drh | daffd0e | 2001-04-11 14:28:42 | [diff] [blame] | 4688 | ** |
| 4689 | ** A new IdList is returned, or NULL if malloc() fails. |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4690 | */ |
dan | 5496d6a | 2018-08-13 17:14:26 | [diff] [blame] | 4691 | IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){ |
| 4692 | sqlite3 *db = pParse->db; |
drh | 1344989 | 2005-09-07 21:22:45 | [diff] [blame] | 4693 | int i; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4694 | if( pList==0 ){ |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 4695 | pList = sqlite3DbMallocZero(db, SZ_IDLIST(1)); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4696 | if( pList==0 ) return 0; |
drh | a99e325 | 2022-04-15 15:47:14 | [diff] [blame] | 4697 | }else{ |
| 4698 | IdList *pNew; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 4699 | pNew = sqlite3DbRealloc(db, pList, SZ_IDLIST(pList->nId+1)); |
drh | a99e325 | 2022-04-15 15:47:14 | [diff] [blame] | 4700 | if( pNew==0 ){ |
| 4701 | sqlite3IdListDelete(db, pList); |
| 4702 | return 0; |
| 4703 | } |
| 4704 | pList = pNew; |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4705 | } |
drh | a99e325 | 2022-04-15 15:47:14 | [diff] [blame] | 4706 | i = pList->nId++; |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4707 | pList->a[i].zName = sqlite3NameFromToken(db, pToken); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 4708 | if( IN_RENAME_OBJECT && pList->a[i].zName ){ |
dan | 07e9523 | 2018-08-21 16:32:53 | [diff] [blame] | 4709 | sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken); |
dan | 5496d6a | 2018-08-13 17:14:26 | [diff] [blame] | 4710 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4711 | return pList; |
| 4712 | } |
| 4713 | |
| 4714 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4715 | ** Delete an IdList. |
| 4716 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 4717 | void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4718 | int i; |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 4719 | assert( db!=0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4720 | if( pList==0 ) return; |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4721 | for(i=0; i<pList->nId; i++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 4722 | sqlite3DbFree(db, pList->a[i].zName); |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4723 | } |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 4724 | sqlite3DbNNFreeNN(db, pList); |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | /* |
| 4728 | ** Return the index in pList of the identifier named zId. Return -1 |
| 4729 | ** if not found. |
| 4730 | */ |
| 4731 | int sqlite3IdListIndex(IdList *pList, const char *zName){ |
| 4732 | int i; |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 4733 | assert( pList!=0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 | [diff] [blame] | 4734 | for(i=0; i<pList->nId; i++){ |
| 4735 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 4736 | } |
| 4737 | return -1; |
| 4738 | } |
| 4739 | |
| 4740 | /* |
drh | 0ad7aa8 | 2019-01-17 14:34:46 | [diff] [blame] | 4741 | ** Maximum size of a SrcList object. |
| 4742 | ** The SrcList object is used to represent the FROM clause of a |
| 4743 | ** SELECT statement, and the query planner cannot deal with more |
| 4744 | ** than 64 tables in a join. So any value larger than 64 here |
| 4745 | ** is sufficient for most uses. Smaller values, like say 10, are |
| 4746 | ** appropriate for small and memory-limited applications. |
| 4747 | */ |
| 4748 | #ifndef SQLITE_MAX_SRCLIST |
| 4749 | # define SQLITE_MAX_SRCLIST 200 |
| 4750 | #endif |
| 4751 | |
| 4752 | /* |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4753 | ** Expand the space allocated for the given SrcList object by |
| 4754 | ** creating nExtra new slots beginning at iStart. iStart is zero based. |
| 4755 | ** New slots are zeroed. |
| 4756 | ** |
| 4757 | ** For example, suppose a SrcList initially contains two entries: A,B. |
| 4758 | ** To append 3 new entries onto the end, do this: |
| 4759 | ** |
| 4760 | ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); |
| 4761 | ** |
| 4762 | ** After the call above it would contain: A, B, nil, nil, nil. |
| 4763 | ** If the iStart argument had been 1 instead of 2, then the result |
| 4764 | ** would have been: A, nil, nil, nil, B. To prepend the new slots, |
| 4765 | ** the iStart value would be 0. The result then would |
| 4766 | ** be: nil, nil, nil, A, B. |
| 4767 | ** |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4768 | ** If a memory allocation fails or the SrcList becomes too large, leave |
| 4769 | ** the original SrcList unchanged, return NULL, and leave an error message |
| 4770 | ** in pParse. |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4771 | */ |
| 4772 | SrcList *sqlite3SrcListEnlarge( |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4773 | Parse *pParse, /* Parsing context into which errors are reported */ |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4774 | SrcList *pSrc, /* The SrcList to be enlarged */ |
| 4775 | int nExtra, /* Number of new slots to add to pSrc->a[] */ |
| 4776 | int iStart /* Index in pSrc->a[] of first new slot */ |
| 4777 | ){ |
| 4778 | int i; |
| 4779 | |
| 4780 | /* Sanity checking on calling parameters */ |
| 4781 | assert( iStart>=0 ); |
| 4782 | assert( nExtra>=1 ); |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 4783 | assert( pSrc!=0 ); |
| 4784 | assert( iStart<=pSrc->nSrc ); |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4785 | |
| 4786 | /* Allocate additional space if needed */ |
drh | fc5717c | 2014-03-05 19:04:46 | [diff] [blame] | 4787 | if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){ |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4788 | SrcList *pNew; |
drh | 0aa3231 | 2019-04-13 04:01:12 | [diff] [blame] | 4789 | sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra; |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4790 | sqlite3 *db = pParse->db; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 | [diff] [blame] | 4791 | |
| 4792 | if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){ |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4793 | sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d", |
| 4794 | SQLITE_MAX_SRCLIST); |
| 4795 | return 0; |
drh | 0ad7aa8 | 2019-01-17 14:34:46 | [diff] [blame] | 4796 | } |
| 4797 | if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 4798 | pNew = sqlite3DbRealloc(db, pSrc, SZ_SRCLIST(nAlloc)); |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4799 | if( pNew==0 ){ |
| 4800 | assert( db->mallocFailed ); |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4801 | return 0; |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4802 | } |
| 4803 | pSrc = pNew; |
drh | d0ee3a1 | 2019-02-06 01:18:36 | [diff] [blame] | 4804 | pSrc->nAlloc = nAlloc; |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | /* Move existing slots that come after the newly inserted slots |
| 4808 | ** out of the way */ |
| 4809 | for(i=pSrc->nSrc-1; i>=iStart; i--){ |
| 4810 | pSrc->a[i+nExtra] = pSrc->a[i]; |
| 4811 | } |
drh | 6d1626e | 2014-03-05 15:52:43 | [diff] [blame] | 4812 | pSrc->nSrc += nExtra; |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4813 | |
| 4814 | /* Zero the newly allocated slots */ |
| 4815 | memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); |
| 4816 | for(i=iStart; i<iStart+nExtra; i++){ |
| 4817 | pSrc->a[i].iCursor = -1; |
| 4818 | } |
| 4819 | |
| 4820 | /* Return a pointer to the enlarged SrcList */ |
| 4821 | return pSrc; |
| 4822 | } |
| 4823 | |
| 4824 | |
| 4825 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4826 | ** Append a new table name to the given SrcList. Create a new SrcList if |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 4827 | ** need be. A new entry is created in the SrcList even if pTable is NULL. |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4828 | ** |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4829 | ** A SrcList is returned, or NULL if there is an OOM error or if the |
| 4830 | ** SrcList grows to large. The returned |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4831 | ** SrcList might be the same as the SrcList that was input or it might be |
| 4832 | ** a new one. If an OOM error does occurs, then the prior value of pList |
| 4833 | ** that is input to this routine is automatically freed. |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 4834 | ** |
| 4835 | ** If pDatabase is not null, it means that the table has an optional |
| 4836 | ** database name prefix. Like this: "database.table". The pDatabase |
| 4837 | ** points to the table name and the pTable points to the database name. |
| 4838 | ** The SrcList.a[].zName field is filled with the table name which might |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 4839 | ** come from pTable (if pDatabase is NULL) or from pDatabase. |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 4840 | ** SrcList.a[].zDatabase is filled with the database name from pTable, |
| 4841 | ** or with NULL if no database is specified. |
| 4842 | ** |
| 4843 | ** In other words, if call like this: |
| 4844 | ** |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4845 | ** sqlite3SrcListAppend(D,A,B,0); |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 4846 | ** |
| 4847 | ** Then B is a table name and the database name is unspecified. If called |
| 4848 | ** like this: |
| 4849 | ** |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4850 | ** sqlite3SrcListAppend(D,A,B,C); |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 4851 | ** |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4852 | ** Then C is the table name and B is the database name. If C is defined |
| 4853 | ** then so is B. In other words, we never have a case where: |
| 4854 | ** |
| 4855 | ** sqlite3SrcListAppend(D,A,0,C); |
drh | b7916a7 | 2009-05-27 10:31:29 | [diff] [blame] | 4856 | ** |
| 4857 | ** Both pTable and pDatabase are assumed to be quoted. They are dequoted |
| 4858 | ** before being added to the SrcList. |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4859 | */ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4860 | SrcList *sqlite3SrcListAppend( |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4861 | Parse *pParse, /* Parsing context, in which errors are reported */ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 4862 | SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ |
| 4863 | Token *pTable, /* Table to append */ |
| 4864 | Token *pDatabase /* Database of the table */ |
| 4865 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 4866 | SrcItem *pItem; |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4867 | sqlite3 *db; |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4868 | assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4869 | assert( pParse!=0 ); |
| 4870 | assert( pParse->db!=0 ); |
| 4871 | db = pParse->db; |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4872 | if( pList==0 ){ |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 4873 | pList = sqlite3DbMallocRawNN(pParse->db, SZ_SRCLIST(1)); |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4874 | if( pList==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 | [diff] [blame] | 4875 | pList->nAlloc = 1; |
drh | ac178b3 | 2016-12-14 11:14:13 | [diff] [blame] | 4876 | pList->nSrc = 1; |
| 4877 | memset(&pList->a[0], 0, sizeof(pList->a[0])); |
| 4878 | pList->a[0].iCursor = -1; |
| 4879 | }else{ |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 4880 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc); |
| 4881 | if( pNew==0 ){ |
| 4882 | sqlite3SrcListDelete(db, pList); |
| 4883 | return 0; |
| 4884 | }else{ |
| 4885 | pList = pNew; |
| 4886 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4887 | } |
drh | a78c22c | 2008-11-11 18:28:58 | [diff] [blame] | 4888 | pItem = &pList->a[pList->nSrc-1]; |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 4889 | if( pDatabase && pDatabase->z==0 ){ |
| 4890 | pDatabase = 0; |
| 4891 | } |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4892 | assert( pItem->fg.fixedSchema==0 ); |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 4893 | assert( pItem->fg.isSubquery==0 ); |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 4894 | if( pDatabase ){ |
drh | 169a689 | 2017-07-06 01:02:09 | [diff] [blame] | 4895 | pItem->zName = sqlite3NameFromToken(db, pDatabase); |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4896 | pItem->u4.zDatabase = sqlite3NameFromToken(db, pTable); |
drh | 169a689 | 2017-07-06 01:02:09 | [diff] [blame] | 4897 | }else{ |
| 4898 | pItem->zName = sqlite3NameFromToken(db, pTable); |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4899 | pItem->u4.zDatabase = 0; |
drh | 113088e | 2003-03-20 01:16:58 | [diff] [blame] | 4900 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4901 | return pList; |
| 4902 | } |
| 4903 | |
| 4904 | /* |
drh | dfe88ec | 2008-11-03 20:55:06 | [diff] [blame] | 4905 | ** Assign VdbeCursor index numbers to all tables in a SrcList |
drh | 63eb5f2 | 2003-04-29 16:20:44 | [diff] [blame] | 4906 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 | [diff] [blame] | 4907 | void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 | [diff] [blame] | 4908 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 4909 | SrcItem *pItem; |
drh | 9da977f | 2021-04-20 12:14:12 | [diff] [blame] | 4910 | assert( pList || pParse->db->mallocFailed ); |
| 4911 | if( ALWAYS(pList) ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 | [diff] [blame] | 4912 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
drh | 3405585 | 2020-10-19 01:23:48 | [diff] [blame] | 4913 | if( pItem->iCursor>=0 ) continue; |
danielk1977 | 261919c | 2005-12-06 12:52:59 | [diff] [blame] | 4914 | pItem->iCursor = pParse->nTab++; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 4915 | if( pItem->fg.isSubquery ){ |
| 4916 | assert( pItem->u4.pSubq!=0 ); |
| 4917 | assert( pItem->u4.pSubq->pSelect!=0 ); |
| 4918 | assert( pItem->u4.pSubq->pSelect->pSrc!=0 ); |
| 4919 | sqlite3SrcListAssignCursors(pParse, pItem->u4.pSubq->pSelect->pSrc); |
danielk1977 | 261919c | 2005-12-06 12:52:59 | [diff] [blame] | 4920 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 | [diff] [blame] | 4921 | } |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | /* |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 4926 | ** Delete a Subquery object and its substructure. |
| 4927 | */ |
| 4928 | void sqlite3SubqueryDelete(sqlite3 *db, Subquery *pSubq){ |
| 4929 | assert( pSubq!=0 && pSubq->pSelect!=0 ); |
| 4930 | sqlite3SelectDelete(db, pSubq->pSelect); |
| 4931 | sqlite3DbFree(db, pSubq); |
| 4932 | } |
| 4933 | |
| 4934 | /* |
| 4935 | ** Remove a Subquery from a SrcItem. Return the associated Select object. |
| 4936 | ** The returned Select becomes the responsibility of the caller. |
| 4937 | */ |
| 4938 | Select *sqlite3SubqueryDetach(sqlite3 *db, SrcItem *pItem){ |
| 4939 | Select *pSel; |
| 4940 | assert( pItem!=0 ); |
| 4941 | assert( pItem->fg.isSubquery ); |
| 4942 | pSel = pItem->u4.pSubq->pSelect; |
| 4943 | sqlite3DbFree(db, pItem->u4.pSubq); |
| 4944 | pItem->u4.pSubq = 0; |
| 4945 | pItem->fg.isSubquery = 0; |
| 4946 | return pSel; |
| 4947 | } |
| 4948 | |
| 4949 | /* |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4950 | ** Delete an entire SrcList including all its substructure. |
| 4951 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 4952 | void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4953 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 4954 | SrcItem *pItem; |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 4955 | assert( db!=0 ); |
drh | ad3cab5 | 2002-05-24 02:04:32 | [diff] [blame] | 4956 | if( pList==0 ) return; |
drh | be5c89a | 2004-07-26 00:31:09 | [diff] [blame] | 4957 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 4958 | |
| 4959 | /* Check invariants on SrcItem */ |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 4960 | assert( !pItem->fg.isIndexedBy || !pItem->fg.isTabFunc ); |
| 4961 | assert( !pItem->fg.isCte || !pItem->fg.isIndexedBy ); |
drh | 692c160 | 2024-08-20 19:09:59 | [diff] [blame] | 4962 | assert( !pItem->fg.fixedSchema || !pItem->fg.isSubquery ); |
| 4963 | assert( !pItem->fg.isSubquery || (pItem->u4.pSubq!=0 && |
| 4964 | pItem->u4.pSubq->pSelect!=0) ); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 4965 | |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 4966 | if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName); |
| 4967 | if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias); |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 4968 | if( pItem->fg.isSubquery ){ |
| 4969 | sqlite3SubqueryDelete(db, pItem->u4.pSubq); |
| 4970 | }else if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){ |
drh | 8797bd6 | 2024-08-17 19:46:49 | [diff] [blame] | 4971 | sqlite3DbNNFreeNN(db, pItem->u4.zDatabase); |
| 4972 | } |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 4973 | if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy); |
| 4974 | if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 4975 | sqlite3DeleteTable(db, pItem->pSTab); |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 4976 | if( pItem->fg.isUsing ){ |
| 4977 | sqlite3IdListDelete(db, pItem->u3.pUsing); |
| 4978 | }else if( pItem->u3.pOn ){ |
| 4979 | sqlite3ExprDelete(db, pItem->u3.pOn); |
| 4980 | } |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4981 | } |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 4982 | sqlite3DbNNFreeNN(db, pList); |
drh | 7589723 | 2000-05-29 14:26:00 | [diff] [blame] | 4983 | } |
| 4984 | |
drh | 982cef7 | 2000-05-30 16:27:03 | [diff] [blame] | 4985 | /* |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 4986 | ** Attach a Subquery object to pItem->uv.pSubq. Set the |
| 4987 | ** pSelect value but leave all the other values initialized |
| 4988 | ** to zero. |
| 4989 | ** |
| 4990 | ** A copy of the Select object is made if dupSelect is true, and the |
| 4991 | ** SrcItem takes responsibility for deleting the copy. If dupSelect is |
| 4992 | ** false, ownership of the Select passes to the SrcItem. Either way, |
| 4993 | ** the SrcItem will take responsibility for deleting the Select. |
| 4994 | ** |
| 4995 | ** When dupSelect is zero, that means the Select might get deleted right |
| 4996 | ** away if there is an OOM error. Beware. |
| 4997 | ** |
| 4998 | ** Return non-zero on success. Return zero on an OOM error. |
| 4999 | */ |
| 5000 | int sqlite3SrcItemAttachSubquery( |
| 5001 | Parse *pParse, /* Parsing context */ |
| 5002 | SrcItem *pItem, /* Item to which the subquery is to be attached */ |
| 5003 | Select *pSelect, /* The subquery SELECT. Must be non-NULL */ |
| 5004 | int dupSelect /* If true, attach a copy of pSelect, not pSelect itself.*/ |
| 5005 | ){ |
| 5006 | Subquery *p; |
drh | 0766cbf | 2024-08-20 20:01:21 | [diff] [blame] | 5007 | assert( pSelect!=0 ); |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 5008 | assert( pItem->fg.isSubquery==0 ); |
drh | ff4ad29 | 2024-08-20 16:50:21 | [diff] [blame] | 5009 | if( pItem->fg.fixedSchema ){ |
| 5010 | pItem->u4.pSchema = 0; |
| 5011 | pItem->fg.fixedSchema = 0; |
| 5012 | }else if( pItem->u4.zDatabase!=0 ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 5013 | sqlite3DbFree(pParse->db, pItem->u4.zDatabase); |
| 5014 | pItem->u4.zDatabase = 0; |
| 5015 | } |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 5016 | if( dupSelect ){ |
| 5017 | pSelect = sqlite3SelectDup(pParse->db, pSelect, 0); |
| 5018 | if( pSelect==0 ) return 0; |
| 5019 | } |
| 5020 | p = pItem->u4.pSubq = sqlite3DbMallocRawNN(pParse->db, sizeof(Subquery)); |
| 5021 | if( p==0 ){ |
| 5022 | sqlite3SelectDelete(pParse->db, pSelect); |
| 5023 | return 0; |
| 5024 | } |
| 5025 | pItem->fg.isSubquery = 1; |
| 5026 | p->pSelect = pSelect; |
| 5027 | assert( offsetof(Subquery, pSelect)==0 ); |
| 5028 | memset(((char*)p)+sizeof(p->pSelect), 0, sizeof(*p)-sizeof(p->pSelect)); |
| 5029 | return 1; |
| 5030 | } |
| 5031 | |
| 5032 | |
| 5033 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5034 | ** This routine is called by the parser to add a new term to the |
| 5035 | ** end of a growing FROM clause. The "p" parameter is the part of |
| 5036 | ** the FROM clause that has already been constructed. "p" is NULL |
| 5037 | ** if this is the first term of the FROM clause. pTable and pDatabase |
| 5038 | ** are the name of the table and database named in the FROM clause term. |
| 5039 | ** pDatabase is NULL if the database name qualifier is missing - the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 5040 | ** usual case. If the term has an alias, then pAlias points to the |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5041 | ** alias token. If the term is a subquery, then pSubquery is the |
| 5042 | ** SELECT statement that the subquery encodes. The pTable and |
| 5043 | ** pDatabase parameters are NULL for subqueries. The pOn and pUsing |
| 5044 | ** parameters are the content of the ON and USING clauses. |
| 5045 | ** |
| 5046 | ** Return a new SrcList which encodes is the FROM with the new |
| 5047 | ** term added. |
| 5048 | */ |
| 5049 | SrcList *sqlite3SrcListAppendFromTerm( |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 5050 | Parse *pParse, /* Parsing context */ |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5051 | SrcList *p, /* The left part of the FROM clause already seen */ |
| 5052 | Token *pTable, /* Name of the table to add to the FROM clause */ |
| 5053 | Token *pDatabase, /* Name of the database containing pTable */ |
| 5054 | Token *pAlias, /* The right-hand side of the AS subexpression */ |
| 5055 | Select *pSubquery, /* A subquery used in place of a table name */ |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 5056 | OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */ |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5057 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 5058 | SrcItem *pItem; |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 5059 | sqlite3 *db = pParse->db; |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 5060 | if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5061 | sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 5062 | (pOnUsing->pOn ? "ON" : "USING") |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 | [diff] [blame] | 5063 | ); |
| 5064 | goto append_from_error; |
| 5065 | } |
drh | 29c992c | 2019-01-17 15:40:41 | [diff] [blame] | 5066 | p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase); |
drh | 9d9c41e | 2017-10-31 03:40:15 | [diff] [blame] | 5067 | if( p==0 ){ |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 | [diff] [blame] | 5068 | goto append_from_error; |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5069 | } |
drh | 9d9c41e | 2017-10-31 03:40:15 | [diff] [blame] | 5070 | assert( p->nSrc>0 ); |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5071 | pItem = &p->a[p->nSrc-1]; |
drh | a488ec9 | 2018-09-07 18:52:25 | [diff] [blame] | 5072 | assert( (pTable==0)==(pDatabase==0) ); |
| 5073 | assert( pItem->zName==0 || pDatabase!=0 ); |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 5074 | if( IN_RENAME_OBJECT && pItem->zName ){ |
drh | a488ec9 | 2018-09-07 18:52:25 | [diff] [blame] | 5075 | Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable; |
dan | c9461ec | 2018-08-29 21:00:16 | [diff] [blame] | 5076 | sqlite3RenameTokenMap(pParse, pItem->zName, pToken); |
| 5077 | } |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 5078 | assert( pAlias!=0 ); |
| 5079 | if( pAlias->n ){ |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 5080 | pItem->zAlias = sqlite3NameFromToken(db, pAlias); |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5081 | } |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 5082 | assert( pSubquery==0 || pDatabase==0 ); |
drh | 815b782 | 2022-04-20 15:07:39 | [diff] [blame] | 5083 | if( pSubquery ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 5084 | if( sqlite3SrcItemAttachSubquery(pParse, pItem, pSubquery, 0) ){ |
| 5085 | if( pSubquery->selFlags & SF_NestedFrom ){ |
| 5086 | pItem->fg.isNestedFrom = 1; |
| 5087 | } |
drh | 815b782 | 2022-04-20 15:07:39 | [diff] [blame] | 5088 | } |
| 5089 | } |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 5090 | assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 ); |
| 5091 | assert( pItem->fg.isUsing==0 ); |
| 5092 | if( pOnUsing==0 ){ |
| 5093 | pItem->u3.pOn = 0; |
| 5094 | }else if( pOnUsing->pUsing ){ |
| 5095 | pItem->fg.isUsing = 1; |
| 5096 | pItem->u3.pUsing = pOnUsing->pUsing; |
| 5097 | }else{ |
| 5098 | pItem->u3.pOn = pOnUsing->pOn; |
| 5099 | } |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5100 | return p; |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 | [diff] [blame] | 5101 | |
drh | 46658d7 | 2021-12-08 18:50:30 | [diff] [blame] | 5102 | append_from_error: |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 | [diff] [blame] | 5103 | assert( p==0 ); |
drh | d44f8b2 | 2022-04-07 01:11:13 | [diff] [blame] | 5104 | sqlite3ClearOnOrUsing(db, pOnUsing); |
danielk1977 | bd1a0a4 | 2009-07-01 16:12:07 | [diff] [blame] | 5105 | sqlite3SelectDelete(db, pSubquery); |
| 5106 | return 0; |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5110 | ** Add an INDEXED BY or NOT INDEXED clause to the most recently added |
danielk1977 | b1c685b | 2008-10-06 16:18:39 | [diff] [blame] | 5111 | ** element of the source-list passed as the second argument. |
| 5112 | */ |
| 5113 | void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 5114 | assert( pIndexedBy!=0 ); |
drh | 8abc80b | 2017-08-12 01:09:06 | [diff] [blame] | 5115 | if( p && pIndexedBy->n>0 ){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 5116 | SrcItem *pItem; |
drh | 8abc80b | 2017-08-12 01:09:06 | [diff] [blame] | 5117 | assert( p->nSrc>0 ); |
| 5118 | pItem = &p->a[p->nSrc-1]; |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 5119 | assert( pItem->fg.notIndexed==0 ); |
| 5120 | assert( pItem->fg.isIndexedBy==0 ); |
| 5121 | assert( pItem->fg.isTabFunc==0 ); |
danielk1977 | b1c685b | 2008-10-06 16:18:39 | [diff] [blame] | 5122 | if( pIndexedBy->n==1 && !pIndexedBy->z ){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5123 | /* A "NOT INDEXED" clause was supplied. See parse.y |
danielk1977 | b1c685b | 2008-10-06 16:18:39 | [diff] [blame] | 5124 | ** construct "indexed_opt" for details. */ |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 5125 | pItem->fg.notIndexed = 1; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 | [diff] [blame] | 5126 | }else{ |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 5127 | pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy); |
drh | 8abc80b | 2017-08-12 01:09:06 | [diff] [blame] | 5128 | pItem->fg.isIndexedBy = 1; |
drh | dbfbb5a | 2021-10-07 23:04:50 | [diff] [blame] | 5129 | assert( pItem->fg.isCte==0 ); /* No collision on union u2 */ |
danielk1977 | b1c685b | 2008-10-06 16:18:39 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | /* |
dan | 69887c9 | 2020-04-27 20:55:33 | [diff] [blame] | 5135 | ** Append the contents of SrcList p2 to SrcList p1 and return the resulting |
| 5136 | ** SrcList. Or, if an error occurs, return NULL. In all cases, p1 and p2 |
| 5137 | ** are deleted by this function. |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5138 | */ |
dan | 69887c9 | 2020-04-27 20:55:33 | [diff] [blame] | 5139 | SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){ |
dan | 5525ac1 | 2024-06-07 21:00:42 | [diff] [blame] | 5140 | assert( p1 ); |
drh | 449b345 | 2025-07-08 17:28:09 | [diff] [blame] | 5141 | assert( p2 || pParse->nErr ); |
| 5142 | assert( p2==0 || p2->nSrc>=1 ); |
| 5143 | testcase( p1->nSrc==0 ); |
dan | 8b023cf | 2020-04-30 18:28:40 | [diff] [blame] | 5144 | if( p2 ){ |
dan | 5525ac1 | 2024-06-07 21:00:42 | [diff] [blame] | 5145 | int nOld = p1->nSrc; |
| 5146 | SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, nOld); |
dan | 8b023cf | 2020-04-30 18:28:40 | [diff] [blame] | 5147 | if( pNew==0 ){ |
| 5148 | sqlite3SrcListDelete(pParse->db, p2); |
| 5149 | }else{ |
| 5150 | p1 = pNew; |
dan | 5525ac1 | 2024-06-07 21:00:42 | [diff] [blame] | 5151 | memcpy(&p1->a[nOld], p2->a, p2->nSrc*sizeof(SrcItem)); |
drh | 449b345 | 2025-07-08 17:28:09 | [diff] [blame] | 5152 | assert( nOld==1 || (p2->a[0].fg.jointype & JT_LTORJ)==0 ); |
| 5153 | assert( p1->nSrc>=1 ); |
| 5154 | p1->a[0].fg.jointype |= (JT_LTORJ & p2->a[0].fg.jointype); |
drh | 525326e | 2020-07-15 21:53:53 | [diff] [blame] | 5155 | sqlite3DbFree(pParse->db, p2); |
dan | 69887c9 | 2020-04-27 20:55:33 | [diff] [blame] | 5156 | } |
| 5157 | } |
| 5158 | return p1; |
| 5159 | } |
| 5160 | |
| 5161 | /* |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 5162 | ** Add the list of function arguments to the SrcList entry for a |
| 5163 | ** table-valued-function. |
| 5164 | */ |
| 5165 | void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){ |
drh | 2029231 | 2015-11-21 13:24:46 | [diff] [blame] | 5166 | if( p ){ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 5167 | SrcItem *pItem = &p->a[p->nSrc-1]; |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 5168 | assert( pItem->fg.notIndexed==0 ); |
| 5169 | assert( pItem->fg.isIndexedBy==0 ); |
| 5170 | assert( pItem->fg.isTabFunc==0 ); |
| 5171 | pItem->u1.pFuncArg = pList; |
| 5172 | pItem->fg.isTabFunc = 1; |
drh | d8b1bfc | 2015-08-20 23:21:34 | [diff] [blame] | 5173 | }else{ |
| 5174 | sqlite3ExprListDelete(pParse->db, pList); |
drh | 01d230c | 2015-08-19 17:11:37 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | /* |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5179 | ** When building up a FROM clause in the parser, the join operator |
| 5180 | ** is initially attached to the left operand. But the code generator |
| 5181 | ** expects the join operator to be on the right operand. This routine |
| 5182 | ** Shifts all join operators from left to right for an entire FROM |
| 5183 | ** clause. |
| 5184 | ** |
| 5185 | ** Example: Suppose the join is like this: |
| 5186 | ** |
| 5187 | ** A natural cross join B |
| 5188 | ** |
| 5189 | ** The operator is "natural cross join". The A and B operands are stored |
| 5190 | ** in p->a[0] and p->a[1], respectively. The parser initially stores the |
| 5191 | ** operator with A. This routine shifts that operator over to B. |
drh | 62ed36b | 2022-04-10 20:28:41 | [diff] [blame] | 5192 | ** |
| 5193 | ** Additional changes: |
| 5194 | ** |
| 5195 | ** * All tables to the left of the right-most RIGHT JOIN are tagged with |
| 5196 | ** JT_LTORJ (mnemonic: Left Table Of Right Join) so that the |
| 5197 | ** code generator can easily tell that the table is part of |
| 5198 | ** the left operand of at least one RIGHT JOIN. |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5199 | */ |
drh | fdc621a | 2022-04-16 19:13:16 | [diff] [blame] | 5200 | void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){ |
drh | 6dab33b | 2022-04-21 19:25:51 | [diff] [blame] | 5201 | (void)pParse; |
drh | 62ed36b | 2022-04-10 20:28:41 | [diff] [blame] | 5202 | if( p && p->nSrc>1 ){ |
| 5203 | int i = p->nSrc-1; |
drh | a76ac88 | 2022-04-08 19:20:12 | [diff] [blame] | 5204 | u8 allFlags = 0; |
drh | 62ed36b | 2022-04-10 20:28:41 | [diff] [blame] | 5205 | do{ |
drh | a76ac88 | 2022-04-08 19:20:12 | [diff] [blame] | 5206 | allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype; |
drh | 62ed36b | 2022-04-10 20:28:41 | [diff] [blame] | 5207 | }while( (--i)>0 ); |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 5208 | p->a[0].fg.jointype = 0; |
drh | a76ac88 | 2022-04-08 19:20:12 | [diff] [blame] | 5209 | |
| 5210 | /* All terms to the left of a RIGHT JOIN should be tagged with the |
| 5211 | ** JT_LTORJ flags */ |
| 5212 | if( allFlags & JT_RIGHT ){ |
| 5213 | for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){} |
| 5214 | i--; |
| 5215 | assert( i>=0 ); |
| 5216 | do{ |
drh | fdc621a | 2022-04-16 19:13:16 | [diff] [blame] | 5217 | p->a[i].fg.jointype |= JT_LTORJ; |
| 5218 | }while( (--i)>=0 ); |
drh | a76ac88 | 2022-04-08 19:20:12 | [diff] [blame] | 5219 | } |
drh | 61dfc31 | 2006-12-16 16:25:15 | [diff] [blame] | 5220 | } |
| 5221 | } |
| 5222 | |
| 5223 | /* |
drh | b0c8865 | 2016-02-01 13:21:13 | [diff] [blame] | 5224 | ** Generate VDBE code for a BEGIN statement. |
drh | c4a3c77 | 2001-04-04 11:48:57 | [diff] [blame] | 5225 | */ |
drh | 684917c | 2004-10-05 02:41:42 | [diff] [blame] | 5226 | void sqlite3BeginTransaction(Parse *pParse, int type){ |
drh | 9bb575f | 2004-09-06 17:24:11 | [diff] [blame] | 5227 | sqlite3 *db; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 | [diff] [blame] | 5228 | Vdbe *v; |
drh | 684917c | 2004-10-05 02:41:42 | [diff] [blame] | 5229 | int i; |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 5230 | |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 5231 | assert( pParse!=0 ); |
| 5232 | db = pParse->db; |
| 5233 | assert( db!=0 ); |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 5234 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ |
| 5235 | return; |
| 5236 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 | [diff] [blame] | 5237 | v = sqlite3GetVdbe(pParse); |
| 5238 | if( !v ) return; |
drh | 684917c | 2004-10-05 02:41:42 | [diff] [blame] | 5239 | if( type!=TK_DEFERRED ){ |
| 5240 | for(i=0; i<db->nDb; i++){ |
drh | 1ca037f | 2020-10-12 13:24:00 | [diff] [blame] | 5241 | int eTxnType; |
| 5242 | Btree *pBt = db->aDb[i].pBt; |
| 5243 | if( pBt && sqlite3BtreeIsReadonly(pBt) ){ |
| 5244 | eTxnType = 0; /* Read txn */ |
| 5245 | }else if( type==TK_EXCLUSIVE ){ |
| 5246 | eTxnType = 2; /* Exclusive txn */ |
| 5247 | }else{ |
| 5248 | eTxnType = 1; /* Write txn */ |
| 5249 | } |
| 5250 | sqlite3VdbeAddOp2(v, OP_Transaction, i, eTxnType); |
drh | fb98264 | 2007-08-30 01:19:59 | [diff] [blame] | 5251 | sqlite3VdbeUsesBtree(v, i); |
drh | 684917c | 2004-10-05 02:41:42 | [diff] [blame] | 5252 | } |
| 5253 | } |
drh | b0c8865 | 2016-02-01 13:21:13 | [diff] [blame] | 5254 | sqlite3VdbeAddOp0(v, OP_AutoCommit); |
drh | c4a3c77 | 2001-04-04 11:48:57 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | /* |
drh | 07a3b11 | 2017-07-06 01:28:02 | [diff] [blame] | 5258 | ** Generate VDBE code for a COMMIT or ROLLBACK statement. |
| 5259 | ** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise |
| 5260 | ** code is generated for a COMMIT. |
drh | c4a3c77 | 2001-04-04 11:48:57 | [diff] [blame] | 5261 | */ |
drh | 07a3b11 | 2017-07-06 01:28:02 | [diff] [blame] | 5262 | void sqlite3EndTransaction(Parse *pParse, int eType){ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 | [diff] [blame] | 5263 | Vdbe *v; |
drh | 07a3b11 | 2017-07-06 01:28:02 | [diff] [blame] | 5264 | int isRollback; |
drh | 5e00f6c | 2001-09-13 13:46:56 | [diff] [blame] | 5265 | |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 5266 | assert( pParse!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 | [diff] [blame] | 5267 | assert( pParse->db!=0 ); |
drh | 07a3b11 | 2017-07-06 01:28:02 | [diff] [blame] | 5268 | assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK ); |
| 5269 | isRollback = eType==TK_ROLLBACK; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5270 | if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, |
drh | 07a3b11 | 2017-07-06 01:28:02 | [diff] [blame] | 5271 | isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){ |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 5272 | return; |
| 5273 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 | [diff] [blame] | 5274 | v = sqlite3GetVdbe(pParse); |
| 5275 | if( v ){ |
drh | 07a3b11 | 2017-07-06 01:28:02 | [diff] [blame] | 5276 | sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback); |
drh | 02f75f1 | 2004-02-24 01:04:11 | [diff] [blame] | 5277 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 | [diff] [blame] | 5278 | } |
drh | f57b14a | 2001-09-14 18:54:08 | [diff] [blame] | 5279 | |
| 5280 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 | [diff] [blame] | 5281 | ** This function is called by the parser when it parses a command to create, |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5282 | ** release or rollback an SQL savepoint. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 | [diff] [blame] | 5283 | */ |
| 5284 | void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 | [diff] [blame] | 5285 | char *zName = sqlite3NameFromToken(pParse->db, pName); |
| 5286 | if( zName ){ |
| 5287 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 5288 | #ifndef SQLITE_OMIT_AUTHORIZATION |
dan | 558814f | 2010-06-02 05:53:53 | [diff] [blame] | 5289 | static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; |
danielk1977 | ab9b703 | 2008-12-30 06:24:58 | [diff] [blame] | 5290 | assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); |
| 5291 | #endif |
| 5292 | if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ |
| 5293 | sqlite3DbFree(pParse->db, zName); |
| 5294 | return; |
| 5295 | } |
| 5296 | sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | /* |
drh | dc3ff9c | 2004-08-18 02:10:15 | [diff] [blame] | 5301 | ** Make sure the TEMP database is open and available for use. Return |
| 5302 | ** the number of errors. Leave any error messages in the pParse structure. |
| 5303 | */ |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 | [diff] [blame] | 5304 | int sqlite3OpenTempDatabase(Parse *pParse){ |
drh | dc3ff9c | 2004-08-18 02:10:15 | [diff] [blame] | 5305 | sqlite3 *db = pParse->db; |
| 5306 | if( db->aDb[1].pBt==0 && !pParse->explain ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 | [diff] [blame] | 5307 | int rc; |
drh | 10a76c9 | 2010-01-26 01:25:26 | [diff] [blame] | 5308 | Btree *pBt; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5309 | static const int flags = |
drh | 33f4e02 | 2007-09-03 15:19:34 | [diff] [blame] | 5310 | SQLITE_OPEN_READWRITE | |
| 5311 | SQLITE_OPEN_CREATE | |
| 5312 | SQLITE_OPEN_EXCLUSIVE | |
| 5313 | SQLITE_OPEN_DELETEONCLOSE | |
| 5314 | SQLITE_OPEN_TEMP_DB; |
| 5315 | |
dan | 3a6d8ae | 2011-04-23 15:54:54 | [diff] [blame] | 5316 | rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags); |
drh | dc3ff9c | 2004-08-18 02:10:15 | [diff] [blame] | 5317 | if( rc!=SQLITE_OK ){ |
| 5318 | sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
| 5319 | "file for storing temporary tables"); |
| 5320 | pParse->rc = rc; |
| 5321 | return 1; |
| 5322 | } |
drh | 10a76c9 | 2010-01-26 01:25:26 | [diff] [blame] | 5323 | db->aDb[1].pBt = pBt; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 | [diff] [blame] | 5324 | assert( db->aDb[1].pSchema ); |
drh | e937df8 | 2020-05-07 01:56:57 | [diff] [blame] | 5325 | if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 | [diff] [blame] | 5326 | sqlite3OomFault(db); |
drh | 7c9c986 | 2010-01-31 14:18:21 | [diff] [blame] | 5327 | return 1; |
drh | 10a76c9 | 2010-01-26 01:25:26 | [diff] [blame] | 5328 | } |
drh | dc3ff9c | 2004-08-18 02:10:15 | [diff] [blame] | 5329 | } |
| 5330 | return 0; |
| 5331 | } |
| 5332 | |
| 5333 | /* |
drh | aceb31b | 2014-02-08 01:40:27 | [diff] [blame] | 5334 | ** Record the fact that the schema cookie will need to be verified |
| 5335 | ** for database iDb. The code to actually verify the schema cookie |
| 5336 | ** will occur at the end of the top-level VDBE and will be generated |
| 5337 | ** later, by sqlite3FinishCoding(). |
drh | 001bbcb | 2003-03-19 03:14:00 | [diff] [blame] | 5338 | */ |
drh | 1d8f892 | 2020-08-16 00:30:44 | [diff] [blame] | 5339 | static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){ |
| 5340 | assert( iDb>=0 && iDb<pToplevel->db->nDb ); |
| 5341 | assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 ); |
drh | 099b385 | 2021-03-10 16:35:37 | [diff] [blame] | 5342 | assert( iDb<SQLITE_MAX_DB ); |
drh | 1d8f892 | 2020-08-16 00:30:44 | [diff] [blame] | 5343 | assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) ); |
drh | a7ab6d8 | 2014-07-21 15:44:39 | [diff] [blame] | 5344 | if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){ |
| 5345 | DbMaskSet(pToplevel->cookieMask, iDb); |
drh | aceb31b | 2014-02-08 01:40:27 | [diff] [blame] | 5346 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 5347 | sqlite3OpenTempDatabase(pToplevel); |
| 5348 | } |
drh | 001bbcb | 2003-03-19 03:14:00 | [diff] [blame] | 5349 | } |
drh | 001bbcb | 2003-03-19 03:14:00 | [diff] [blame] | 5350 | } |
drh | 1d8f892 | 2020-08-16 00:30:44 | [diff] [blame] | 5351 | void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
| 5352 | sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb); |
| 5353 | } |
| 5354 | |
drh | 001bbcb | 2003-03-19 03:14:00 | [diff] [blame] | 5355 | |
| 5356 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5357 | ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each |
dan | 5796675 | 2011-04-09 17:32:58 | [diff] [blame] | 5358 | ** attached database. Otherwise, invoke it for the database named zDb only. |
| 5359 | */ |
| 5360 | void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){ |
| 5361 | sqlite3 *db = pParse->db; |
| 5362 | int i; |
| 5363 | for(i=0; i<db->nDb; i++){ |
| 5364 | Db *pDb = &db->aDb[i]; |
drh | 69c3382 | 2016-08-18 14:33:11 | [diff] [blame] | 5365 | if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){ |
dan | 5796675 | 2011-04-09 17:32:58 | [diff] [blame] | 5366 | sqlite3CodeVerifySchema(pParse, i); |
| 5367 | } |
| 5368 | } |
| 5369 | } |
| 5370 | |
| 5371 | /* |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 5372 | ** Generate VDBE code that prepares for doing an operation that |
drh | c977f7f | 2002-05-21 11:38:11 | [diff] [blame] | 5373 | ** might change the database. |
| 5374 | ** |
| 5375 | ** This routine starts a new transaction if we are not already within |
| 5376 | ** a transaction. If we are already within a transaction, then a checkpoint |
drh | 7f0f12e | 2004-05-21 13:39:50 | [diff] [blame] | 5377 | ** is set if the setStatement parameter is true. A checkpoint should |
drh | c977f7f | 2002-05-21 11:38:11 | [diff] [blame] | 5378 | ** be set for operations that might fail (due to a constraint) part of |
| 5379 | ** the way through and which will need to undo some writes without having to |
| 5380 | ** rollback the whole transaction. For operations where all constraints |
| 5381 | ** can be checked before any changes are made to the database, it is never |
| 5382 | ** necessary to undo a write and the checkpoint should not be set. |
drh | 1c92853 | 2002-01-31 15:54:21 | [diff] [blame] | 5383 | */ |
drh | 7f0f12e | 2004-05-21 13:39:50 | [diff] [blame] | 5384 | void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 | [diff] [blame] | 5385 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | 1d8f892 | 2020-08-16 00:30:44 | [diff] [blame] | 5386 | sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb); |
drh | a7ab6d8 | 2014-07-21 15:44:39 | [diff] [blame] | 5387 | DbMaskSet(pToplevel->writeMask, iDb); |
dan | e0af83a | 2009-09-08 19:15:01 | [diff] [blame] | 5388 | pToplevel->isMultiWrite |= setStatement; |
| 5389 | } |
| 5390 | |
drh | ff738bc | 2009-09-24 00:09:58 | [diff] [blame] | 5391 | /* |
| 5392 | ** Indicate that the statement currently under construction might write |
| 5393 | ** more than one entry (example: deleting one row then inserting another, |
| 5394 | ** inserting multiple rows in a table, or inserting a row and index entries.) |
| 5395 | ** If an abort occurs after some of these writes have completed, then it will |
| 5396 | ** be necessary to undo the completed writes. |
| 5397 | */ |
| 5398 | void sqlite3MultiWrite(Parse *pParse){ |
| 5399 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5400 | pToplevel->isMultiWrite = 1; |
| 5401 | } |
| 5402 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5403 | /* |
drh | ff738bc | 2009-09-24 00:09:58 | [diff] [blame] | 5404 | ** The code generator calls this routine if is discovers that it is |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5405 | ** possible to abort a statement prior to completion. In order to |
drh | ff738bc | 2009-09-24 00:09:58 | [diff] [blame] | 5406 | ** perform this abort without corrupting the database, we need to make |
| 5407 | ** sure that the statement is protected by a statement transaction. |
| 5408 | ** |
| 5409 | ** Technically, we only need to set the mayAbort flag if the |
| 5410 | ** isMultiWrite flag was previously set. There is a time dependency |
| 5411 | ** such that the abort must occur after the multiwrite. This makes |
| 5412 | ** some statements involving the REPLACE conflict resolution algorithm |
| 5413 | ** go a little faster. But taking advantage of this time dependency |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5414 | ** makes it more difficult to prove that the code is correct (in |
drh | ff738bc | 2009-09-24 00:09:58 | [diff] [blame] | 5415 | ** particular, it prevents us from writing an effective |
| 5416 | ** implementation of sqlite3AssertMayAbort()) and so we have chosen |
| 5417 | ** to take the safe route and skip the optimization. |
dan | e0af83a | 2009-09-08 19:15:01 | [diff] [blame] | 5418 | */ |
| 5419 | void sqlite3MayAbort(Parse *pParse){ |
| 5420 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 5421 | pToplevel->mayAbort = 1; |
| 5422 | } |
| 5423 | |
| 5424 | /* |
| 5425 | ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT |
| 5426 | ** error. The onError parameter determines which (if any) of the statement |
| 5427 | ** and/or current transaction is rolled back. |
| 5428 | */ |
drh | d91c1a1 | 2013-02-09 13:58:25 | [diff] [blame] | 5429 | void sqlite3HaltConstraint( |
| 5430 | Parse *pParse, /* Parsing context */ |
| 5431 | int errCode, /* extended error code */ |
| 5432 | int onError, /* Constraint type */ |
| 5433 | char *p4, /* Error message */ |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 5434 | i8 p4type, /* P4_STATIC or P4_TRANSIENT */ |
| 5435 | u8 p5Errmsg /* P5_ErrMsg type */ |
drh | d91c1a1 | 2013-02-09 13:58:25 | [diff] [blame] | 5436 | ){ |
drh | 289a0c8 | 2020-08-15 22:23:00 | [diff] [blame] | 5437 | Vdbe *v; |
| 5438 | assert( pParse->pVdbe!=0 ); |
| 5439 | v = sqlite3GetVdbe(pParse); |
drh | 9e5fdc4 | 2020-05-08 19:02:21 | [diff] [blame] | 5440 | assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested ); |
dan | e0af83a | 2009-09-08 19:15:01 | [diff] [blame] | 5441 | if( onError==OE_Abort ){ |
| 5442 | sqlite3MayAbort(pParse); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 | [diff] [blame] | 5443 | } |
drh | d91c1a1 | 2013-02-09 13:58:25 | [diff] [blame] | 5444 | sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type); |
drh | 9b34abe | 2016-01-16 15:12:35 | [diff] [blame] | 5445 | sqlite3VdbeChangeP5(v, p5Errmsg); |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | /* |
| 5449 | ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation. |
| 5450 | */ |
| 5451 | void sqlite3UniqueConstraint( |
| 5452 | Parse *pParse, /* Parsing context */ |
| 5453 | int onError, /* Constraint type */ |
| 5454 | Index *pIdx /* The index that triggers the constraint */ |
| 5455 | ){ |
| 5456 | char *zErr; |
| 5457 | int j; |
| 5458 | StrAccum errMsg; |
| 5459 | Table *pTab = pIdx->pTable; |
| 5460 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5461 | sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, |
drh | 86ec1ed | 2019-04-10 00:58:07 | [diff] [blame] | 5462 | pParse->db->aLimit[SQLITE_LIMIT_LENGTH]); |
drh | 8b57642 | 2015-08-31 23:09:42 | [diff] [blame] | 5463 | if( pIdx->aColExpr ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 5464 | sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName); |
drh | 8b57642 | 2015-08-31 23:09:42 | [diff] [blame] | 5465 | }else{ |
| 5466 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 5467 | char *zCol; |
| 5468 | assert( pIdx->aiColumn[j]>=0 ); |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 5469 | zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName; |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 5470 | if( j ) sqlite3_str_append(&errMsg, ", ", 2); |
| 5471 | sqlite3_str_appendall(&errMsg, pTab->zName); |
| 5472 | sqlite3_str_append(&errMsg, ".", 1); |
| 5473 | sqlite3_str_appendall(&errMsg, zCol); |
drh | 8b57642 | 2015-08-31 23:09:42 | [diff] [blame] | 5474 | } |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 5475 | } |
| 5476 | zErr = sqlite3StrAccumFinish(&errMsg); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5477 | sqlite3HaltConstraint(pParse, |
| 5478 | IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY |
drh | 48dd1d8 | 2014-05-27 18:18:58 | [diff] [blame] | 5479 | : SQLITE_CONSTRAINT_UNIQUE, |
dan | 93889d9 | 2013-11-06 16:28:59 | [diff] [blame] | 5480 | onError, zErr, P4_DYNAMIC, P5_ConstraintUnique); |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 5481 | } |
| 5482 | |
| 5483 | |
| 5484 | /* |
| 5485 | ** Code an OP_Halt due to non-unique rowid. |
| 5486 | */ |
| 5487 | void sqlite3RowidConstraint( |
| 5488 | Parse *pParse, /* Parsing context */ |
| 5489 | int onError, /* Conflict resolution algorithm */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5490 | Table *pTab /* The table with the non-unique rowid */ |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 5491 | ){ |
| 5492 | char *zMsg; |
| 5493 | int rc; |
| 5494 | if( pTab->iPKey>=0 ){ |
| 5495 | zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName, |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 5496 | pTab->aCol[pTab->iPKey].zCnName); |
drh | f9c8ce3 | 2013-11-05 13:33:55 | [diff] [blame] | 5497 | rc = SQLITE_CONSTRAINT_PRIMARYKEY; |
| 5498 | }else{ |
| 5499 | zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName); |
| 5500 | rc = SQLITE_CONSTRAINT_ROWID; |
| 5501 | } |
| 5502 | sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC, |
| 5503 | P5_ConstraintUnique); |
drh | 663fc63 | 2002-02-02 18:49:19 | [diff] [blame] | 5504 | } |
| 5505 | |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5506 | /* |
| 5507 | ** Check to see if pIndex uses the collating sequence pColl. Return |
| 5508 | ** true if it does and false if it does not. |
| 5509 | */ |
| 5510 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5511 | static int collationMatch(const char *zColl, Index *pIndex){ |
| 5512 | int i; |
drh | 0449171 | 2009-05-13 17:21:13 | [diff] [blame] | 5513 | assert( zColl!=0 ); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5514 | for(i=0; i<pIndex->nColumn; i++){ |
| 5515 | const char *z = pIndex->azColl[i]; |
drh | bbbdc83 | 2013-10-22 18:01:40 | [diff] [blame] | 5516 | assert( z!=0 || pIndex->aiColumn[i]<0 ); |
| 5517 | if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5518 | return 1; |
| 5519 | } |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5520 | } |
| 5521 | return 0; |
| 5522 | } |
| 5523 | #endif |
| 5524 | |
| 5525 | /* |
| 5526 | ** Recompute all indices of pTab that use the collating sequence pColl. |
| 5527 | ** If pColl==0 then recompute all indices of pTab. |
| 5528 | */ |
| 5529 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5530 | static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
dan | e6370e9 | 2019-01-11 17:41:23 | [diff] [blame] | 5531 | if( !IsVirtual(pTab) ){ |
| 5532 | Index *pIndex; /* An index associated with pTab */ |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5533 | |
dan | e6370e9 | 2019-01-11 17:41:23 | [diff] [blame] | 5534 | for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
| 5535 | if( zColl==0 || collationMatch(zColl, pIndex) ){ |
| 5536 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 5537 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5538 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5539 | } |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5540 | } |
| 5541 | } |
| 5542 | } |
| 5543 | #endif |
| 5544 | |
| 5545 | /* |
| 5546 | ** Recompute all indices of all tables in all databases where the |
| 5547 | ** indices use the collating sequence pColl. If pColl==0 then recompute |
| 5548 | ** all indices everywhere. |
| 5549 | */ |
| 5550 | #ifndef SQLITE_OMIT_REINDEX |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5551 | static void reindexDatabases(Parse *pParse, char const *zColl){ |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5552 | Db *pDb; /* A single database */ |
| 5553 | int iDb; /* The database index number */ |
| 5554 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5555 | HashElem *k; /* For looping over tables in pDb */ |
| 5556 | Table *pTab; /* A table in the database */ |
| 5557 | |
drh | 2120608 | 2011-04-04 18:22:02 | [diff] [blame] | 5558 | assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */ |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5559 | for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
drh | 43617e9 | 2006-03-06 20:55:46 | [diff] [blame] | 5560 | assert( pDb!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 | [diff] [blame] | 5561 | for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5562 | pTab = (Table*)sqliteHashData(k); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5563 | reindexTable(pParse, pTab, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5564 | } |
| 5565 | } |
| 5566 | } |
| 5567 | #endif |
| 5568 | |
| 5569 | /* |
drh | eee46cf | 2004-11-06 00:02:48 | [diff] [blame] | 5570 | ** Generate code for the REINDEX command. |
| 5571 | ** |
| 5572 | ** REINDEX -- 1 |
| 5573 | ** REINDEX <collation> -- 2 |
| 5574 | ** REINDEX ?<database>.?<tablename> -- 3 |
| 5575 | ** REINDEX ?<database>.?<indexname> -- 4 |
| 5576 | ** |
| 5577 | ** Form 1 causes all indices in all attached databases to be rebuilt. |
| 5578 | ** Form 2 rebuilds all indices in all databases that use the named |
| 5579 | ** collating function. Forms 3 and 4 rebuild the named index or all |
| 5580 | ** indices associated with the named table. |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5581 | */ |
| 5582 | #ifndef SQLITE_OMIT_REINDEX |
| 5583 | void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
| 5584 | CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
| 5585 | char *z; /* Name of a table or index */ |
| 5586 | const char *zDb; /* Name of the database */ |
| 5587 | Table *pTab; /* A table in the database */ |
| 5588 | Index *pIndex; /* An index associated with pTab */ |
| 5589 | int iDb; /* The database index number */ |
| 5590 | sqlite3 *db = pParse->db; /* The database connection */ |
| 5591 | Token *pObjName; /* Name of the table or index to be reindexed */ |
| 5592 | |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 | [diff] [blame] | 5593 | /* Read the database schema. If an error occurs, leave an error message |
| 5594 | ** and code in pParse and return NULL. */ |
| 5595 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
danielk1977 | e63739a | 2005-01-27 00:33:37 | [diff] [blame] | 5596 | return; |
danielk1977 | 33a5edc | 2005-01-27 00:22:02 | [diff] [blame] | 5597 | } |
| 5598 | |
drh | 8af73d4 | 2009-05-13 22:58:28 | [diff] [blame] | 5599 | if( pName1==0 ){ |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5600 | reindexDatabases(pParse, 0); |
| 5601 | return; |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 5602 | }else if( NEVER(pName2==0) || pName2->z==0 ){ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 | [diff] [blame] | 5603 | char *zColl; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5604 | assert( pName1->z ); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 | [diff] [blame] | 5605 | zColl = sqlite3NameFromToken(pParse->db, pName1); |
| 5606 | if( !zColl ) return; |
drh | c4a64fa | 2009-05-11 20:53:28 | [diff] [blame] | 5607 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5608 | if( pColl ){ |
drh | d300171 | 2009-05-12 17:46:53 | [diff] [blame] | 5609 | reindexDatabases(pParse, zColl); |
| 5610 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5611 | return; |
| 5612 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 5613 | sqlite3DbFree(db, zColl); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5614 | } |
| 5615 | iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
| 5616 | if( iDb<0 ) return; |
drh | 1743575 | 2007-08-16 04:30:38 | [diff] [blame] | 5617 | z = sqlite3NameFromToken(db, pObjName); |
drh | 84f3112 | 2007-05-12 15:00:14 | [diff] [blame] | 5618 | if( z==0 ) return; |
drh | ff6905a | 2024-01-09 12:28:51 | [diff] [blame] | 5619 | zDb = pName2->n ? db->aDb[iDb].zDbSName : 0; |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5620 | pTab = sqlite3FindTable(db, z, zDb); |
| 5621 | if( pTab ){ |
| 5622 | reindexTable(pParse, pTab, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 5623 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5624 | return; |
| 5625 | } |
| 5626 | pIndex = sqlite3FindIndex(db, z, zDb); |
drh | 633e6d5 | 2008-07-28 19:34:53 | [diff] [blame] | 5627 | sqlite3DbFree(db, z); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5628 | if( pIndex ){ |
drh | ff6905a | 2024-01-09 12:28:51 | [diff] [blame] | 5629 | iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema); |
drh | 4343fea | 2004-11-05 23:46:15 | [diff] [blame] | 5630 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 5631 | sqlite3RefillIndex(pParse, pIndex, -1); |
| 5632 | return; |
| 5633 | } |
| 5634 | sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
| 5635 | } |
| 5636 | #endif |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5637 | |
| 5638 | /* |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 5639 | ** Return a KeyInfo structure that is appropriate for the given Index. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5640 | ** |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 5641 | ** The caller should invoke sqlite3KeyInfoUnref() on the returned object |
| 5642 | ** when it has finished using it. |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5643 | */ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 5644 | KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){ |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5645 | int i; |
| 5646 | int nCol = pIdx->nColumn; |
| 5647 | int nKey = pIdx->nKeyCol; |
| 5648 | KeyInfo *pKey; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 5649 | if( pParse->nErr ) return 0; |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5650 | if( pIdx->uniqNotNull ){ |
| 5651 | pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey); |
| 5652 | }else{ |
| 5653 | pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0); |
drh | 41e13e1 | 2013-11-07 14:09:39 | [diff] [blame] | 5654 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5655 | if( pKey ){ |
| 5656 | assert( sqlite3KeyInfoIsWriteable(pKey) ); |
| 5657 | for(i=0; i<nCol; i++){ |
drh | f19aa5f | 2015-12-30 16:51:20 | [diff] [blame] | 5658 | const char *zColl = pIdx->azColl[i]; |
| 5659 | pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 : |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5660 | sqlite3LocateCollSeq(pParse, zColl); |
dan | 6e11892 | 2019-08-12 16:36:38 | [diff] [blame] | 5661 | pKey->aSortFlags[i] = pIdx->aSortOrder[i]; |
| 5662 | assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 | [diff] [blame] | 5663 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5664 | if( pParse->nErr ){ |
drh | 7e8515d | 2017-12-08 19:37:04 | [diff] [blame] | 5665 | assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ ); |
| 5666 | if( pIdx->bNoQuery==0 ){ |
| 5667 | /* Deactivate the index because it contains an unknown collating |
| 5668 | ** sequence. The only way to reactive the index is to reload the |
| 5669 | ** schema. Adding the missing collating sequence later does not |
| 5670 | ** reactive the index. The application had the chance to register |
| 5671 | ** the missing index using the collation-needed callback. For |
| 5672 | ** simplicity, SQLite will not give the application a second chance. |
| 5673 | */ |
| 5674 | pIdx->bNoQuery = 1; |
| 5675 | pParse->rc = SQLITE_ERROR_RETRY; |
| 5676 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5677 | sqlite3KeyInfoUnref(pKey); |
| 5678 | pKey = 0; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5679 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5680 | } |
drh | 18b67f3 | 2014-12-12 00:20:37 | [diff] [blame] | 5681 | return pKey; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 | [diff] [blame] | 5682 | } |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5683 | |
| 5684 | #ifndef SQLITE_OMIT_CTE |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5685 | /* |
| 5686 | ** Create a new CTE object |
| 5687 | */ |
| 5688 | Cte *sqlite3CteNew( |
| 5689 | Parse *pParse, /* Parsing context */ |
| 5690 | Token *pName, /* Name of the common-table */ |
| 5691 | ExprList *pArglist, /* Optional column name list for the table */ |
drh | 745912e | 2021-02-22 03:04:25 | [diff] [blame] | 5692 | Select *pQuery, /* Query used to initialize the table */ |
| 5693 | u8 eM10d /* The MATERIALIZED flag */ |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5694 | ){ |
| 5695 | Cte *pNew; |
| 5696 | sqlite3 *db = pParse->db; |
| 5697 | |
| 5698 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew)); |
| 5699 | assert( pNew!=0 || db->mallocFailed ); |
| 5700 | |
| 5701 | if( db->mallocFailed ){ |
| 5702 | sqlite3ExprListDelete(db, pArglist); |
| 5703 | sqlite3SelectDelete(db, pQuery); |
| 5704 | }else{ |
| 5705 | pNew->pSelect = pQuery; |
| 5706 | pNew->pCols = pArglist; |
| 5707 | pNew->zName = sqlite3NameFromToken(pParse->db, pName); |
drh | 745912e | 2021-02-22 03:04:25 | [diff] [blame] | 5708 | pNew->eM10d = eM10d; |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5709 | } |
| 5710 | return pNew; |
| 5711 | } |
| 5712 | |
| 5713 | /* |
| 5714 | ** Clear information from a Cte object, but do not deallocate storage |
| 5715 | ** for the object itself. |
| 5716 | */ |
| 5717 | static void cteClear(sqlite3 *db, Cte *pCte){ |
| 5718 | assert( pCte!=0 ); |
| 5719 | sqlite3ExprListDelete(db, pCte->pCols); |
| 5720 | sqlite3SelectDelete(db, pCte->pSelect); |
| 5721 | sqlite3DbFree(db, pCte->zName); |
| 5722 | } |
| 5723 | |
| 5724 | /* |
| 5725 | ** Free the contents of the CTE object passed as the second argument. |
| 5726 | */ |
| 5727 | void sqlite3CteDelete(sqlite3 *db, Cte *pCte){ |
| 5728 | assert( pCte!=0 ); |
| 5729 | cteClear(db, pCte); |
| 5730 | sqlite3DbFree(db, pCte); |
| 5731 | } |
| 5732 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 5733 | /* |
| 5734 | ** This routine is invoked once per CTE by the parser while parsing a |
| 5735 | ** WITH clause. The CTE described by the third argument is added to |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5736 | ** the WITH clause of the second argument. If the second argument is |
| 5737 | ** NULL, then a new WITH argument is created. |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5738 | */ |
dan | 7d562db | 2014-01-11 19:19:36 | [diff] [blame] | 5739 | With *sqlite3WithAdd( |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5740 | Parse *pParse, /* Parsing context */ |
dan | 7d562db | 2014-01-11 19:19:36 | [diff] [blame] | 5741 | With *pWith, /* Existing WITH clause, or NULL */ |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5742 | Cte *pCte /* CTE to add to the WITH clause */ |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5743 | ){ |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5744 | sqlite3 *db = pParse->db; |
| 5745 | With *pNew; |
| 5746 | char *zName; |
| 5747 | |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5748 | if( pCte==0 ){ |
| 5749 | return pWith; |
| 5750 | } |
| 5751 | |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5752 | /* Check that the CTE name is unique within this WITH clause. If |
| 5753 | ** not, store an error in the Parse structure. */ |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5754 | zName = pCte->zName; |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5755 | if( zName && pWith ){ |
| 5756 | int i; |
| 5757 | for(i=0; i<pWith->nCte; i++){ |
| 5758 | if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){ |
drh | 727a99f | 2014-01-16 21:59:51 | [diff] [blame] | 5759 | sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName); |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | |
| 5764 | if( pWith ){ |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 5765 | pNew = sqlite3DbRealloc(db, pWith, SZ_WITH(pWith->nCte+1)); |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5766 | }else{ |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 5767 | pNew = sqlite3DbMallocZero(db, SZ_WITH(1)); |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5768 | } |
drh | b84e574 | 2016-02-05 02:42:54 | [diff] [blame] | 5769 | assert( (pNew!=0 && zName!=0) || db->mallocFailed ); |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5770 | |
drh | b84e574 | 2016-02-05 02:42:54 | [diff] [blame] | 5771 | if( db->mallocFailed ){ |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5772 | sqlite3CteDelete(db, pCte); |
dan | a9f5c13 | 2014-01-13 16:36:40 | [diff] [blame] | 5773 | pNew = pWith; |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5774 | }else{ |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5775 | pNew->a[pNew->nCte++] = *pCte; |
| 5776 | sqlite3DbFree(db, pCte); |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | return pNew; |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5780 | } |
| 5781 | |
dan | 7d562db | 2014-01-11 19:19:36 | [diff] [blame] | 5782 | /* |
| 5783 | ** Free the contents of the With object passed as the second argument. |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5784 | */ |
dan | 7d562db | 2014-01-11 19:19:36 | [diff] [blame] | 5785 | void sqlite3WithDelete(sqlite3 *db, With *pWith){ |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5786 | if( pWith ){ |
| 5787 | int i; |
| 5788 | for(i=0; i<pWith->nCte; i++){ |
drh | f824b41 | 2021-02-20 14:57:16 | [diff] [blame] | 5789 | cteClear(db, &pWith->a[i]); |
dan | 4e9119d | 2014-01-13 15:12:23 | [diff] [blame] | 5790 | } |
| 5791 | sqlite3DbFree(db, pWith); |
| 5792 | } |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5793 | } |
drh | 82fc1b6 | 2023-12-06 18:25:41 | [diff] [blame] | 5794 | void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){ |
| 5795 | sqlite3WithDelete(db, (With*)pWith); |
| 5796 | } |
drh | 8b47186 | 2014-01-11 13:22:17 | [diff] [blame] | 5797 | #endif /* !defined(SQLITE_OMIT_CTE) */ |