Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 5495cef18f49be626ad67c734b9757dfef59e129 [file] [log] [blame]
drh75897232000-05-29 14:26:001/*
drhb19a2bc2001-09-16 00:13:262** 2001 September 15
drh75897232000-05-29 14:26:003**
drhb19a2bc2001-09-16 00:13:264** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:006**
drhb19a2bc2001-09-16 00:13:267** 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.
drh75897232000-05-29 14:26:0010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:2612** 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:
drh75897232000-05-29 14:26:0015**
drhbed86902000-06-02 13:27:5916** CREATE TABLE
17** DROP TABLE
18** CREATE INDEX
19** DROP INDEX
drh832508b2002-03-02 17:04:0720** creating ID lists
drhb19a2bc2001-09-16 00:13:2621** BEGIN TRANSACTION
22** COMMIT
23** ROLLBACK
drh75897232000-05-29 14:26:0024*/
25#include "sqliteInt.h"
26
danielk1977c00da102006-01-07 13:21:0427#ifndef SQLITE_OMIT_SHARED_CACHE
28/*
29** The TableLock structure is only used by the sqlite3TableLock() and
30** codeTableLocks() functions.
31*/
32struct TableLock {
drhe0a04a32016-12-16 01:00:2133 int iDb; /* The database containing the table to be locked */
drhabc38152020-07-22 13:38:0434 Pgno iTab; /* The root page of the table to be locked */
drhe0a04a32016-12-16 01:00:2135 u8 isWriteLock; /* True for write lock. False for a read lock */
36 const char *zLockName; /* Name of the table */
danielk1977c00da102006-01-07 13:21:0437};
38
39/*
larrybrbc917382023-06-07 08:40:3140** Record the fact that we want to lock a table at run-time.
danielk1977c00da102006-01-07 13:21:0441**
drhd698bc12006-03-23 23:33:2642** 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().
danielk1977c00da102006-01-07 13:21:0448*/
drh94305062021-05-17 11:19:3249static SQLITE_NOINLINE void lockTable(
drhd698bc12006-03-23 23:33:2650 Parse *pParse, /* Parsing context */
51 int iDb, /* Index of the database containing the table to lock */
drhabc38152020-07-22 13:38:0452 Pgno iTab, /* Root page number of the table to be locked */
drhd698bc12006-03-23 23:33:2653 u8 isWriteLock, /* True for a write lock */
54 const char *zName /* Name of the table to be locked */
danielk1977c00da102006-01-07 13:21:0455){
drh1d8f8922020-08-16 00:30:4456 Parse *pToplevel;
danielk1977c00da102006-01-07 13:21:0457 int i;
58 int nBytes;
59 TableLock *p;
drh8af73d42009-05-13 22:58:2860 assert( iDb>=0 );
dan165921a2009-08-28 18:53:4561
drh1d8f8922020-08-16 00:30:4462 pToplevel = sqlite3ParseToplevel(pParse);
dan65a7cd12009-09-01 12:16:0163 for(i=0; i<pToplevel->nTableLock; i++){
64 p = &pToplevel->aTableLock[i];
danielk1977c00da102006-01-07 13:21:0465 if( p->iDb==iDb && p->iTab==iTab ){
66 p->isWriteLock = (p->isWriteLock || isWriteLock);
67 return;
68 }
69 }
70
drhef86b942025-02-17 17:33:1471 assert( pToplevel->nTableLock < 0x7fff0000 );
dan65a7cd12009-09-01 12:16:0172 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++];
danielk1977c00da102006-01-07 13:21:0477 p->iDb = iDb;
78 p->iTab = iTab;
79 p->isWriteLock = isWriteLock;
drhe0a04a32016-12-16 01:00:2180 p->zLockName = zName;
drhf3a65f72007-08-22 20:18:2181 }else{
dan65a7cd12009-09-01 12:16:0182 pToplevel->nTableLock = 0;
drh4a642b62016-02-05 01:55:2783 sqlite3OomFault(pToplevel->db);
danielk1977c00da102006-01-07 13:21:0484 }
85}
drh94305062021-05-17 11:19:3286void 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}
danielk1977c00da102006-01-07 13:21:0497
98/*
99** Code an OP_TableLock instruction for each table locked by the
100** statement (configured by calls to sqlite3TableLock()).
101*/
102static void codeTableLocks(Parse *pParse){
103 int i;
larrybrbc917382023-06-07 08:40:31104 Vdbe *pVdbe = pParse->pVdbe;
drh289a0c82020-08-15 22:23:00105 assert( pVdbe!=0 );
danielk1977c00da102006-01-07 13:21:04106
107 for(i=0; i<pParse->nTableLock; i++){
108 TableLock *p = &pParse->aTableLock[i];
109 int p1 = p->iDb;
drh6a9ad3d2008-04-02 16:29:30110 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
drhe0a04a32016-12-16 01:00:21111 p->zLockName, P4_STATIC);
danielk1977c00da102006-01-07 13:21:04112 }
113}
114#else
115 #define codeTableLocks(x)
116#endif
117
drhe0bc4042002-06-25 01:09:11118/*
drha7ab6d82014-07-21 15:44:39119** 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
124int 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/*
drh75897232000-05-29 14:26:00132** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12133** 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.
drh75897232000-05-29 14:26:00137**
138** Note that if an error occurred, it might be the case that
139** no VDBE code was generated.
140*/
drh80242052004-06-09 00:48:12141void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11142 sqlite3 *db;
drh80242052004-06-09 00:48:12143 Vdbe *v;
drh7bace9e2022-07-23 12:51:48144 int iDb, i;
drhb86ccfb2003-01-28 23:13:10145
danf78baaf2012-12-06 19:37:22146 assert( pParse->pToplevel==0 );
drh17435752007-08-16 04:30:38147 db = pParse->db;
drh0c7d3d32022-01-24 16:47:12148 assert( db->pParse==pParse );
drh205f48e2004-11-05 00:43:11149 if( pParse->nested ) return;
drh0c7d3d32022-01-24 16:47:12150 if( pParse->nErr ){
drha5c9a702022-01-25 00:03:25151 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
drhd99d2832015-04-17 15:58:33152 return;
153 }
drh0c7d3d32022-01-24 16:47:12154 assert( db->mallocFailed==0 );
danielk197748d0d862005-02-01 03:09:52155
drh80242052004-06-09 00:48:12156 /* Begin by generating some termination code at the end of the
157 ** vdbe program
158 */
drh02c4aa32021-02-04 13:44:42159 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;
drhc8af8792021-01-01 22:06:17167 }
larrybrbc917382023-06-07 08:40:31168 assert( !pParse->isMultiWrite
danf3677212009-09-10 16:14:50169 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
drh80242052004-06-09 00:48:12170 if( v ){
drh381bdac2021-02-04 17:29:04171 if( pParse->bReturning ){
drh7fd936e2025-02-07 15:49:21172 Returning *pReturning;
drh381bdac2021-02-04 17:29:04173 int addrRewind;
drh381bdac2021-02-04 17:29:04174 int reg;
175
drh7fd936e2025-02-07 15:49:21176 assert( !pParse->isCreate );
177 pReturning = pParse->u1.d.pReturning;
drh1a6bac02022-04-25 10:43:19178 if( pReturning->nRetCol ){
drh3b26b2b2021-12-01 19:17:14179 sqlite3VdbeAddOp0(v, OP_FkCheck);
drh7132f432021-10-20 12:52:12180 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);
drh381bdac2021-02-04 17:29:04191 }
drh381bdac2021-02-04 17:29:04192 }
drh66a51672008-01-03 00:01:23193 sqlite3VdbeAddOp0(v, OP_Halt);
drh0e3d7472004-06-19 17:33:07194
drh0e3d7472004-06-19 17:33:07195 /* 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 */
drh7bace9e2022-07-23 12:51:48201 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 );
danielk1977f9e7dda2006-06-16 16:08:53221#ifndef SQLITE_OMIT_VIRTUALTABLE
drh7bace9e2022-07-23 12:51:48222 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;
danielk1977f9e7dda2006-06-16 16:08:53227#endif
danielk1977c00da102006-01-07 13:21:04228
drh40ee7292023-06-20 17:45:19229#ifndef SQLITE_OMIT_SHARED_CACHE
larrybrbc917382023-06-07 08:40:31230 /* Once all the cookies have been verified and transactions opened,
231 ** obtain the required table-locks. This is a no-op unless the
drh7bace9e2022-07-23 12:51:48232 ** shared-cache feature is enabled.
233 */
drh40ee7292023-06-20 17:45:19234 if( pParse->nTableLock ) codeTableLocks(pParse);
235#endif
drh0b9f50d2009-06-23 20:28:53236
drh7bace9e2022-07-23 12:51:48237 /* Initialize any AUTOINCREMENT data structures required.
238 */
drh40ee7292023-06-20 17:45:19239 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
drh0b9f50d2009-06-23 20:28:53240
danef2e4332023-09-09 17:53:55241 /* Code constant expressions that were factored out of inner loops.
drh7bace9e2022-07-23 12:51:48242 */
243 if( pParse->pConstExpr ){
244 ExprList *pEL = pParse->pConstExpr;
245 pParse->okConstFactor = 0;
246 for(i=0; i<pEL->nExpr; i++){
danef2e4332023-09-09 17:53:55247 assert( pEL->a[i].u.iConstExprReg>0 );
248 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
drhf30a9692013-11-15 01:10:18249 }
drh80242052004-06-09 00:48:12250 }
drh7bace9e2022-07-23 12:51:48251
252 if( pParse->bReturning ){
drh7fd936e2025-02-07 15:49:21253 Returning *pRet;
254 assert( !pParse->isCreate );
255 pRet = pParse->u1.d.pReturning;
drh7bace9e2022-07-23 12:51:48256 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);
drh71c697e2004-08-08 23:39:19263 }
264
drh80242052004-06-09 00:48:12265 /* Get the VDBE program ready for execution
266 */
drh1da88b52022-01-24 19:38:56267 assert( v!=0 || pParse->nErr );
268 assert( db->mallocFailed==0 || pParse->nErr );
269 if( pParse->nErr==0 ){
drh3492dd72009-09-14 23:47:24270 /* A minimum of one cursor is required if autoincrement is used
271 * See ticket [a696379c1f08866] */
drh04ab5862018-12-01 21:13:41272 assert( pParse->pAinc==0 || pParse->nTab>0 );
drh124c0b42011-06-01 18:15:55273 sqlite3VdbeMakeReady(v, pParse);
danielk1977441daf62005-02-01 03:46:43274 pParse->rc = SQLITE_DONE;
drhe294da02010-02-25 23:44:15275 }else{
drh483750b2003-01-29 18:46:51276 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00277 }
278}
279
280/*
drh205f48e2004-11-05 00:43:11281** 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
drh3edc9272021-08-04 13:42:12283** currently under construction. Notes:
drh205f48e2004-11-05 00:43:11284**
drh3edc9272021-08-04 13:42:12285** * 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.
drh205f48e2004-11-05 00:43:11292*/
293void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
294 va_list ap;
295 char *zSql;
drh633e6d52008-07-28 19:34:53296 sqlite3 *db = pParse->db;
drh3edc9272021-08-04 13:42:12297 u32 savedDbFlags = db->mDbFlags;
drhcd9af602016-09-30 22:24:29298 char saveBuf[PARSE_TAIL_SZ];
drhf1974842004-11-05 03:56:00299
drh205f48e2004-11-05 00:43:11300 if( pParse->nErr ) return;
drh12e1eb32022-12-29 18:54:15301 if( pParse->eParseMode ) return;
drh205f48e2004-11-05 00:43:11302 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
303 va_start(ap, zFormat);
drh633e6d52008-07-28 19:34:53304 zSql = sqlite3VMPrintf(db, zFormat, ap);
drh205f48e2004-11-05 00:43:11305 va_end(ap);
drh73c42a12004-11-20 18:13:10306 if( zSql==0 ){
drh480c5722019-02-22 16:18:12307 /* 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;
drha2b68062019-03-28 04:03:17311 pParse->nErr++;
drh480c5722019-02-22 16:18:12312 return;
drh73c42a12004-11-20 18:13:10313 }
drh205f48e2004-11-05 00:43:11314 pParse->nested++;
drhcd9af602016-09-30 22:24:29315 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
316 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
drh3edc9272021-08-04 13:42:12317 db->mDbFlags |= DBFLAG_PreferBuiltin;
drh54bc6382021-12-31 19:20:42318 sqlite3RunParser(pParse, zSql);
drh3edc9272021-08-04 13:42:12319 db->mDbFlags = savedDbFlags;
drh633e6d52008-07-28 19:34:53320 sqlite3DbFree(db, zSql);
drhcd9af602016-09-30 22:24:29321 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
drh205f48e2004-11-05 00:43:11322 pParse->nested--;
323}
324
drh205f48e2004-11-05 00:43:11325/*
danielk19778a414492004-06-29 08:59:35326** 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.
drha69d9162003-04-17 22:57:53329**
danielk19778a414492004-06-29 08:59:35330** 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.
drhf26e09c2003-05-31 16:21:12334**
danielk19774adee202004-05-08 08:23:19335** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00336*/
drh9bb575f2004-09-06 17:24:11337Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24338 Table *p = 0;
339 int i;
drh9ca95732014-10-24 00:35:58340
drh21206082011-04-04 18:22:02341 /* All mutexes are required for schema access. Make sure we hold them. */
342 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
drhb2eb7e42020-05-16 21:01:00343 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;
drhe0a04a32016-12-16 01:00:21354 }
drh69c33822016-08-18 14:33:11355 }
drhb2eb7e42020-05-16 21:01:00356 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
drh346a70c2020-06-15 20:27:35357 if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
358 if( i==1 ){
drha4a871c2021-11-04 14:04:20359 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
drh346a70c2020-06-15 20:27:35362 ){
larrybrbc917382023-06-07 08:40:31363 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
drha4a871c2021-11-04 14:04:20364 LEGACY_TEMP_SCHEMA_TABLE);
drh346a70c2020-06-15 20:27:35365 }
366 }else{
drha4a871c2021-11-04 14:04:20367 if( sqlite3StrICmp(zName+7, &PREFERRED_SCHEMA_TABLE[7])==0 ){
drh346a70c2020-06-15 20:27:35368 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
drha4a871c2021-11-04 14:04:20369 LEGACY_SCHEMA_TABLE);
drh346a70c2020-06-15 20:27:35370 }
371 }
drhb2eb7e42020-05-16 21:01:00372 }
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 }
drh346a70c2020-06-15 20:27:35386 if( p==0 && sqlite3StrNICmp(zName, "sqlite_", 7)==0 ){
drha4a871c2021-11-04 14:04:20387 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 ){
larrybrbc917382023-06-07 08:40:31390 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
drha4a871c2021-11-04 14:04:20391 LEGACY_TEMP_SCHEMA_TABLE);
drh346a70c2020-06-15 20:27:35392 }
393 }
drhd24cc422003-03-27 12:51:24394 }
drhb2eb7e42020-05-16 21:01:00395 return p;
drh75897232000-05-29 14:26:00396}
397
398/*
danielk19778a414492004-06-29 08:59:35399** 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.
drha69d9162003-04-17 22:57:53403**
danielk19778a414492004-06-29 08:59:35404** The difference between this routine and sqlite3FindTable() is that this
405** routine leaves an error message in pParse->zErrMsg where
406** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53407*/
drhca424112008-01-25 15:04:48408Table *sqlite3LocateTable(
409 Parse *pParse, /* context in which to report errors */
drh4d249e62016-06-10 22:49:01410 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
drhca424112008-01-25 15:04:48411 const char *zName, /* Name of the table we are looking for */
412 const char *zDbase /* Name of the database. Might be NULL */
413){
drha69d9162003-04-17 22:57:53414 Table *p;
drhb2c85592018-04-25 12:01:45415 sqlite3 *db = pParse->db;
drhf26e09c2003-05-31 16:21:12416
danielk19778a414492004-06-29 08:59:35417 /* Read the database schema. If an error occurs, leave an error message
418 ** and code in pParse and return NULL. */
larrybrbc917382023-06-07 08:40:31419 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
drhb2c85592018-04-25 12:01:45420 && SQLITE_OK!=sqlite3ReadSchema(pParse)
421 ){
danielk19778a414492004-06-29 08:59:35422 return 0;
423 }
424
drhb2c85592018-04-25 12:01:45425 p = sqlite3FindTable(db, zName, zDbase);
drha69d9162003-04-17 22:57:53426 if( p==0 ){
drhd2975922015-08-29 17:22:33427#ifndef SQLITE_OMIT_VIRTUALTABLE
drh9196c812018-11-05 16:38:10428 /* 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. */
drh7424aef2022-10-01 13:17:53431 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
dan1ea04432018-12-21 19:29:11432 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) ){
danbd24e8f2021-07-08 18:29:25437 testcase( pMod->pEpoTab==0 );
dan1ea04432018-12-21 19:29:11438 return pMod->pEpoTab;
439 }
drh51be3872015-08-19 02:32:25440 }
441#endif
dan1ea04432018-12-21 19:29:11442 if( flags & LOCATE_NOERR ) return 0;
443 pParse->checkSchema = 1;
drh7424aef2022-10-01 13:17:53444 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
dan1ea04432018-12-21 19:29:11445 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);
drha69d9162003-04-17 22:57:53454 }
drh1bb89e92021-04-19 18:03:52455 }else{
456 assert( HasRowid(p) || p->iPKey<0 );
drha69d9162003-04-17 22:57:53457 }
danfab1d402015-11-26 15:51:55458
drha69d9162003-04-17 22:57:53459 return p;
460}
461
462/*
dan41fb5cd2012-10-04 19:33:00463** 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*/
471Table *sqlite3LocateTableItem(
larrybrbc917382023-06-07 08:40:31472 Parse *pParse,
drh4d249e62016-06-10 22:49:01473 u32 flags,
drh76012942021-02-21 21:04:54474 SrcItem *p
dan41fb5cd2012-10-04 19:33:00475){
476 const char *zDb;
drh8797bd62024-08-17 19:46:49477 if( p->fg.fixedSchema ){
478 int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema);
drh69c33822016-08-18 14:33:11479 zDb = pParse->db->aDb[iDb].zDbSName;
dan41fb5cd2012-10-04 19:33:00480 }else{
drh692c1602024-08-20 19:09:59481 assert( !p->fg.isSubquery );
drh8797bd62024-08-17 19:46:49482 zDb = p->u4.zDatabase;
dan41fb5cd2012-10-04 19:33:00483 }
drh4d249e62016-06-10 22:49:01484 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
dan41fb5cd2012-10-04 19:33:00485}
486
487/*
drha4a871c2021-11-04 14:04:20488** Return the preferred table name for system tables. Translate legacy
489** names into the new preferred names, as appropriate.
490*/
491const 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/*
larrybrbc917382023-06-07 08:40:31504** Locate the in-memory structure that describes
drha69d9162003-04-17 22:57:53505** a particular index given the name of that index
506** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32507** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12508**
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.
drh75897232000-05-29 14:26:00514*/
drh9bb575f2004-09-06 17:24:11515Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24516 Index *p = 0;
517 int i;
drh21206082011-04-04 18:22:02518 /* All mutexes are required for schema access. Make sure we hold them. */
519 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
danielk197753c0f742005-03-29 03:10:59520 for(i=OMIT_TEMPDB; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00521 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk1977e501b892006-01-09 06:29:47522 Schema *pSchema = db->aDb[j].pSchema;
drh04491712009-05-13 17:21:13523 assert( pSchema );
dan465c2b82020-03-21 15:10:40524 if( zDb && sqlite3DbIsNamed(db, j, zDb)==0 ) continue;
drh21206082011-04-04 18:22:02525 assert( sqlite3SchemaMutexHeld(db, j, 0) );
drhacbcb7e2014-08-21 20:26:37526 p = sqlite3HashFind(&pSchema->idxHash, zName);
drhd24cc422003-03-27 12:51:24527 if( p ) break;
528 }
drh74e24cd2002-01-09 03:19:59529 return p;
drh75897232000-05-29 14:26:00530}
531
532/*
drh956bc922004-07-24 17:38:29533** Reclaim the memory used by an index
534*/
dancf8f2892018-08-09 20:47:01535void sqlite3FreeIndex(sqlite3 *db, Index *p){
drh92aa5ea2009-09-11 14:05:06536#ifndef SQLITE_OMIT_ANALYZE
dand46def72010-07-24 11:28:28537 sqlite3DeleteIndexSamples(db, p);
drh92aa5ea2009-09-11 14:05:06538#endif
drh1fe05372013-07-31 18:12:26539 sqlite3ExprDelete(db, p->pPartIdxWhere);
drh1f9ca2c2015-08-25 16:57:52540 sqlite3ExprListDelete(db, p->aColExpr);
drh633e6d52008-07-28 19:34:53541 sqlite3DbFree(db, p->zColAff);
mistachkin5905f862015-12-31 19:04:42542 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
drh175b8f02019-08-08 15:24:17543#ifdef SQLITE_ENABLE_STAT4
drh75b170b2014-10-04 00:07:44544 sqlite3_free(p->aiRowEst);
545#endif
drh633e6d52008-07-28 19:34:53546 sqlite3DbFree(db, p);
drh956bc922004-07-24 17:38:29547}
548
549/*
drhc96d8532005-05-03 12:30:33550** 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.
drh5e00f6c2001-09-13 13:46:56554*/
drh9bb575f2004-09-06 17:24:11555void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
drh956bc922004-07-24 17:38:29556 Index *pIndex;
drh21206082011-04-04 18:22:02557 Hash *pHash;
drh956bc922004-07-24 17:38:29558
drh21206082011-04-04 18:22:02559 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
560 pHash = &db->aDb[iDb].pSchema->idxHash;
drhacbcb7e2014-08-21 20:26:37561 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
drh22645842011-03-24 01:34:03562 if( ALWAYS(pIndex) ){
drh956bc922004-07-24 17:38:29563 if( pIndex->pTable->pIndex==pIndex ){
564 pIndex->pTable->pIndex = pIndex->pNext;
565 }else{
566 Index *p;
drh04491712009-05-13 17:21:13567 /* 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) ){
drh956bc922004-07-24 17:38:29572 p->pNext = pIndex->pNext;
573 }
drh5e00f6c2001-09-13 13:46:56574 }
dancf8f2892018-08-09 20:47:01575 sqlite3FreeIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56576 }
drh8257aa82017-07-26 19:59:13577 db->mDbFlags |= DBFLAG_SchemaChange;
drh5e00f6c2001-09-13 13:46:56578}
579
580/*
drh81028a42012-05-15 18:28:27581** 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.
drh1c2d8412003-03-31 00:30:47584**
drh81028a42012-05-15 18:28:27585** Entry 0 (the "main" database) and entry 1 (the "temp" database)
586** are never candidates for being collapsed.
drh74e24cd2002-01-09 03:19:59587*/
drh81028a42012-05-15 18:28:27588void sqlite3CollapseDatabaseArray(sqlite3 *db){
drh1c2d8412003-03-31 00:30:47589 int i, j;
drh1c2d8412003-03-31 00:30:47590 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38591 struct Db *pDb = &db->aDb[i];
592 if( pDb->pBt==0 ){
drh69c33822016-08-18 14:33:11593 sqlite3DbFree(db, pDb->zDbSName);
594 pDb->zDbSName = 0;
drh1c2d8412003-03-31 00:30:47595 continue;
596 }
597 if( j<i ){
drh8bf8dc92003-05-17 17:35:10598 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47599 }
drh8bf8dc92003-05-17 17:35:10600 j++;
drh1c2d8412003-03-31 00:30:47601 }
drh1c2d8412003-03-31 00:30:47602 db->nDb = j;
603 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
604 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
drh633e6d52008-07-28 19:34:53605 sqlite3DbFree(db, db->aDb);
drh1c2d8412003-03-31 00:30:47606 db->aDb = db->aDbStatic;
607 }
drhe0bc4042002-06-25 01:09:11608}
609
610/*
drh81028a42012-05-15 18:28:27611** Reset the schema for the database at index iDb. Also reset the
drhdc6b41e2017-08-17 02:26:35612** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
613** Deferred resets may be run by calling with iDb<0.
drh81028a42012-05-15 18:28:27614*/
615void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
drhdc6b41e2017-08-17 02:26:35616 int i;
drh81028a42012-05-15 18:28:27617 assert( iDb<db->nDb );
618
drhdc6b41e2017-08-17 02:26:35619 if( iDb>=0 ){
620 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
621 DbSetProperty(db, iDb, DB_ResetWanted);
622 DbSetProperty(db, 1, DB_ResetWanted);
drhb2c85592018-04-25 12:01:45623 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
drh81028a42012-05-15 18:28:27624 }
drhdc6b41e2017-08-17 02:26:35625
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 }
drh81028a42012-05-15 18:28:27633}
634
635/*
636** Erase all schema information from all attached databases (including
637** "main" and "temp") for a single database connection.
638*/
639void 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 ){
dan63e50b92018-11-27 19:47:55645 if( db->nSchemaLock==0 ){
646 sqlite3SchemaClear(pDb->pSchema);
647 }else{
648 DbSetProperty(db, i, DB_ResetWanted);
649 }
drh81028a42012-05-15 18:28:27650 }
651 }
drhb2c85592018-04-25 12:01:45652 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
drh81028a42012-05-15 18:28:27653 sqlite3VtabUnlockList(db);
654 sqlite3BtreeLeaveAll(db);
dan63e50b92018-11-27 19:47:55655 if( db->nSchemaLock==0 ){
656 sqlite3CollapseDatabaseArray(db);
657 }
drh81028a42012-05-15 18:28:27658}
659
660/*
drhe0bc4042002-06-25 01:09:11661** This routine is called when a commit occurs.
662*/
drh9bb575f2004-09-06 17:24:11663void sqlite3CommitInternalChanges(sqlite3 *db){
drh8257aa82017-07-26 19:59:13664 db->mDbFlags &= ~DBFLAG_SchemaChange;
drh74e24cd2002-01-09 03:19:59665}
666
667/*
drh79cf2b72021-07-31 20:30:41668** 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*/
672void 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){
drhf38524d2021-08-02 16:41:57678 ExprList *pList;
drh78b2fa82021-10-07 12:11:20679 assert( IsOrdinaryTable(pTab) );
drhf38524d2021-08-02 16:41:57680 pList = pTab->u.tab.pDfltList;
drh79cf2b72021-07-31 20:30:41681 if( pCol->iDflt==0
drh324f91a2021-08-04 14:50:23682 || NEVER(pList==0)
683 || NEVER(pList->nExpr<pCol->iDflt)
drh79cf2b72021-07-31 20:30:41684 ){
685 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
drhf38524d2021-08-02 16:41:57686 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
drh79cf2b72021-07-31 20:30:41687 }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*/
698Expr *sqlite3ColumnExpr(Table *pTab, Column *pCol){
699 if( pCol->iDflt==0 ) return 0;
drh768b6e32023-12-02 12:23:34700 if( !IsOrdinaryTable(pTab) ) return 0;
drh324f91a2021-08-04 14:50:23701 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
702 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
drhf38524d2021-08-02 16:41:57703 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
drh79cf2b72021-07-31 20:30:41704}
705
706/*
drh65b40092021-08-05 15:27:19707** Set the collating sequence name for a column.
708*/
709void sqlite3ColumnSetColl(
710 sqlite3 *db,
711 Column *pCol,
712 const char *zColl
713){
drh913306a2021-11-26 17:10:18714 i64 nColl;
715 i64 n;
drh65b40092021-08-05 15:27:19716 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/*
larrybrbc917382023-06-07 08:40:31732** Return the collating sequence name for a column
drh65b40092021-08-05 15:27:19733*/
734const 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/*
dand46def72010-07-24 11:28:28746** Delete memory allocated for the column names of a table or view (the
747** Table.aCol[] array).
drh956bc922004-07-24 17:38:29748*/
drh51be3872015-08-19 02:32:25749void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
drh956bc922004-07-24 17:38:29750 int i;
751 Column *pCol;
752 assert( pTable!=0 );
drh41ce47c2022-08-22 02:00:26753 assert( db!=0 );
drhdd5b2fa2005-03-28 03:39:55754 if( (pCol = pTable->aCol)!=0 ){
755 for(i=0; i<pTable->nCol; i++, pCol++){
drhcf9d36d2021-08-02 18:03:43756 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
757 sqlite3DbFree(db, pCol->zCnName);
drhdd5b2fa2005-03-28 03:39:55758 }
drh41ce47c2022-08-22 02:00:26759 sqlite3DbNNFreeNN(db, pTable->aCol);
drh78b2fa82021-10-07 12:11:20760 if( IsOrdinaryTable(pTable) ){
drhf38524d2021-08-02 16:41:57761 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
762 }
drh41ce47c2022-08-22 02:00:26763 if( db->pnBytesFreed==0 ){
drh79cf2b72021-07-31 20:30:41764 pTable->aCol = 0;
765 pTable->nCol = 0;
drh78b2fa82021-10-07 12:11:20766 if( IsOrdinaryTable(pTable) ){
drhf38524d2021-08-02 16:41:57767 pTable->u.tab.pDfltList = 0;
768 }
drh79cf2b72021-07-31 20:30:41769 }
drh956bc922004-07-24 17:38:29770 }
drh956bc922004-07-24 17:38:29771}
772
773/*
drh75897232000-05-29 14:26:00774** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10775** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00776**
777** This routine just deletes the data structure. It does not unlink
drhe61922a2009-05-02 13:29:37778** the table data structure from the hash table. But it does destroy
larrybrbc917382023-06-07 08:40:31779** memory structures of the indices and foreign keys associated with
drhc2eef3b2002-08-31 18:53:06780** the table.
drh29ddd3a2012-05-15 12:49:32781**
larrybrbc917382023-06-07 08:40:31782** The db parameter is optional. It is needed if the Table object
drh29ddd3a2012-05-15 12:49:32783** 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.
drh75897232000-05-29 14:26:00787*/
drhe8da01c2016-05-07 12:15:34788static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
drh75897232000-05-29 14:26:00789 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06790
drh52fb8e12017-08-29 20:21:12791#ifdef SQLITE_DEBUG
drh29ddd3a2012-05-15 12:49:32792 /* Record the number of outstanding lookaside allocations in schema Tables
danbedf84c2019-07-08 13:45:02793 ** prior to doing any free() operations. Since schema Tables do not use
larrybrbc917382023-06-07 08:40:31794 ** lookaside, this number should not change.
danbedf84c2019-07-08 13:45:02795 **
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. */
drh52fb8e12017-08-29 20:21:12799 int nLookaside = 0;
drh41ce47c2022-08-22 02:00:26800 assert( db!=0 );
801 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
drh52fb8e12017-08-29 20:21:12802 nLookaside = sqlite3LookasideUsed(db, 0);
803 }
804#endif
drh29ddd3a2012-05-15 12:49:32805
dand46def72010-07-24 11:28:28806 /* Delete all indices associated with this table. */
drhc2eef3b2002-08-31 18:53:06807 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
808 pNext = pIndex->pNext;
drh62340f82016-05-31 21:18:15809 assert( pIndex->pSchema==pTable->pSchema
810 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
drh41ce47c2022-08-22 02:00:26811 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
larrybrbc917382023-06-07 08:40:31812 char *zName = pIndex->zName;
dand46def72010-07-24 11:28:28813 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
drhacbcb7e2014-08-21 20:26:37814 &pIndex->pSchema->idxHash, zName, 0
dand46def72010-07-24 11:28:28815 );
drh21206082011-04-04 18:22:02816 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
dand46def72010-07-24 11:28:28817 assert( pOld==pIndex || pOld==0 );
818 }
dancf8f2892018-08-09 20:47:01819 sqlite3FreeIndex(db, pIndex);
drhc2eef3b2002-08-31 18:53:06820 }
821
drhf38524d2021-08-02 16:41:57822 if( IsOrdinaryTable(pTable) ){
823 sqlite3FkDelete(db, pTable);
824 }
drhe0306192023-05-05 14:16:31825#ifndef SQLITE_OMIT_VIRTUALTABLE
drhf38524d2021-08-02 16:41:57826 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 }
drhc2eef3b2002-08-31 18:53:06834
835 /* Delete the Table structure itself.
836 */
drh51be3872015-08-19 02:32:25837 sqlite3DeleteColumnNames(db, pTable);
drh633e6d52008-07-28 19:34:53838 sqlite3DbFree(db, pTable->zName);
839 sqlite3DbFree(db, pTable->zColAff);
drh2938f922012-03-07 19:13:29840 sqlite3ExprListDelete(db, pTable->pCheck);
drh633e6d52008-07-28 19:34:53841 sqlite3DbFree(db, pTable);
drh29ddd3a2012-05-15 12:49:32842
843 /* Verify that no lookaside memory was used by schema tables */
drh52fb8e12017-08-29 20:21:12844 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
drh75897232000-05-29 14:26:00845}
drhe8da01c2016-05-07 12:15:34846void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
847 /* Do not delete the table until the reference count reaches zero. */
drh41ce47c2022-08-22 02:00:26848 assert( db!=0 );
drhe8da01c2016-05-07 12:15:34849 if( !pTable ) return;
drh41ce47c2022-08-22 02:00:26850 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
drhe8da01c2016-05-07 12:15:34851 deleteTable(db, pTable);
852}
drh82fc1b62023-12-06 18:25:41853void sqlite3DeleteTableGeneric(sqlite3 *db, void *pTable){
854 sqlite3DeleteTable(db, (Table*)pTable);
855}
drhe8da01c2016-05-07 12:15:34856
drh75897232000-05-29 14:26:00857
858/*
drh5edc3122001-09-13 21:53:09859** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06860** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09861*/
drh9bb575f2004-09-06 17:24:11862void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29863 Table *p;
drh956bc922004-07-24 17:38:29864 Db *pDb;
865
drhd229ca92002-01-09 13:30:41866 assert( db!=0 );
drh956bc922004-07-24 17:38:29867 assert( iDb>=0 && iDb<db->nDb );
drh972a2312009-12-08 14:34:08868 assert( zTabName );
drh21206082011-04-04 18:22:02869 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh972a2312009-12-08 14:34:08870 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
drh956bc922004-07-24 17:38:29871 pDb = &db->aDb[iDb];
drhacbcb7e2014-08-21 20:26:37872 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
dan1feeaed2010-07-23 15:41:47873 sqlite3DeleteTable(db, p);
drh8257aa82017-07-26 19:59:13874 db->mDbFlags |= DBFLAG_SchemaChange;
drh74e24cd2002-01-09 03:19:59875}
876
877/*
drha99db3b2004-06-19 14:49:12878** Given a token, return a string that consists of the text of that
drh24fb6272009-05-01 21:13:36879** token. Space to hold the returned string
drha99db3b2004-06-19 14:49:12880** is obtained from sqliteMalloc() and must be freed by the calling
881** function.
drh75897232000-05-29 14:26:00882**
drh24fb6272009-05-01 21:13:36883** Any quotation marks (ex: "name", 'name', [name], or `name`) that
884** surround the body of the token are removed.
885**
drhc96d8532005-05-03 12:30:33886** Tokens are often just pointers into the original SQL text and so
drha99db3b2004-06-19 14:49:12887** are not \000 terminated and are not persistent. The returned string
888** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00889*/
drhb6dad522021-09-24 16:14:47890char *sqlite3NameFromToken(sqlite3 *db, const Token *pName){
drha99db3b2004-06-19 14:49:12891 char *zName;
892 if( pName ){
drhb6dad522021-09-24 16:14:47893 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
drhb7916a72009-05-27 10:31:29894 sqlite3Dequote(zName);
drha99db3b2004-06-19 14:49:12895 }else{
896 zName = 0;
897 }
drh75897232000-05-29 14:26:00898 return zName;
899}
900
901/*
drh1e32bed2020-06-19 13:33:53902** Open the sqlite_schema table stored in database number iDb for
danielk1977cbb18d22004-05-28 11:37:27903** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11904*/
drh346a70c2020-06-15 20:27:35905void sqlite3OpenSchemaTable(Parse *p, int iDb){
danielk1977c00da102006-01-07 13:21:04906 Vdbe *v = sqlite3GetVdbe(p);
drha4a871c2021-11-04 14:04:20907 sqlite3TableLock(p, iDb, SCHEMA_ROOT, 1, LEGACY_SCHEMA_TABLE);
drh346a70c2020-06-15 20:27:35908 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, SCHEMA_ROOT, iDb, 5);
danielk19776ab3a2e2009-02-19 14:39:25909 if( p->nTab==0 ){
910 p->nTab = 1;
911 }
drhe0bc4042002-06-25 01:09:11912}
913
914/*
danielk197704103022009-02-03 16:51:24915** 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.
danielk1977cbb18d22004-05-28 11:37:27919*/
danielk197704103022009-02-03 16:51:24920int sqlite3FindDbName(sqlite3 *db, const char *zName){
921 int i = -1; /* Database number */
drh73c42a12004-11-20 18:13:10922 if( zName ){
danielk197704103022009-02-03 16:51:24923 Db *pDb;
danielk1977576ec6b2005-01-21 11:55:25924 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
drh29518092016-12-24 19:37:16925 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;
danielk1977cbb18d22004-05-28 11:37:27929 }
930 }
danielk1977576ec6b2005-01-21 11:55:25931 return i;
danielk1977cbb18d22004-05-28 11:37:27932}
933
danielk197704103022009-02-03 16:51:24934/*
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
larrybrbc917382023-06-07 08:40:31937** index of the named database in db->aDb[], or -1 if the named db
danielk197704103022009-02-03 16:51:24938** does not exist.
939*/
940int 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
drh0e3d7472004-06-19 17:33:07949/* 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 (...);
larrybrbc917382023-06-07 08:40:31953**
drh0e3d7472004-06-19 17:33:07954** 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*/
danielk1977ef2cb632004-05-29 02:37:19965int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07966 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35967 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07968 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
969 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27970){
drh0e3d7472004-06-19 17:33:07971 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27972 sqlite3 *db = pParse->db;
973
drh055f2982016-01-15 15:06:41974 assert( pName2!=0 );
975 if( pName2->n>0 ){
shanedcc50b72008-11-13 18:29:50976 if( db->init.busy ) {
977 sqlite3ErrorMsg(pParse, "corrupt database");
shanedcc50b72008-11-13 18:29:50978 return -1;
979 }
danielk1977cbb18d22004-05-28 11:37:27980 *pUnqual = pName2;
drhff2d5ea2005-07-23 00:41:48981 iDb = sqlite3FindDb(db, pName1);
danielk1977cbb18d22004-05-28 11:37:27982 if( iDb<0 ){
983 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
danielk1977cbb18d22004-05-28 11:37:27984 return -1;
985 }
986 }else{
drhd36bcec2021-05-29 21:50:05987 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
drh8257aa82017-07-26 19:59:13988 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
danielk1977cbb18d22004-05-28 11:37:27989 iDb = db->init.iDb;
990 *pUnqual = pName1;
991 }
992 return iDb;
993}
994
995/*
drh0f1c2eb2018-11-03 17:31:48996** True if PRAGMA writable_schema is ON
997*/
998int 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/*
danielk1977d8123362004-06-12 09:25:121010** 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.
drhc5a93d42019-08-12 00:08:071015**
drh1e32bed2020-06-19 13:33:531016** When parsing the sqlite_schema table, this routine also checks to
drhc5a93d42019-08-12 00:08:071017** make sure the "type", "name", and "tbl_name" columns are consistent
1018** with the SQL.
danielk1977d8123362004-06-12 09:25:121019*/
drhc5a93d42019-08-12 00:08:071020int 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;
drhca439a42020-07-22 21:05:231027 if( sqlite3WritableSchema(db)
1028 || db->init.imposterTable
1029 || !sqlite3Config.bExtraSchemaChecks
1030 ){
drhc5a93d42019-08-12 00:08:071031 /* 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 ){
drhca439a42020-07-22 21:05:231039 sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
1040 return SQLITE_ERROR;
drhc5a93d42019-08-12 00:08:071041 }
1042 }else{
drh527cbd42019-11-16 14:15:191043 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
1044 || (sqlite3ReadOnlyShadowTables(db) && sqlite3ShadowTableName(db, zName))
drhc5a93d42019-08-12 00:08:071045 ){
1046 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
1047 zName);
1048 return SQLITE_ERROR;
1049 }
drh527cbd42019-11-16 14:15:191050
danielk1977d8123362004-06-12 09:25:121051 }
1052 return SQLITE_OK;
1053}
1054
1055/*
drh44156282013-10-23 22:23:031056** Return the PRIMARY KEY index of a table
1057*/
1058Index *sqlite3PrimaryKeyIndex(Table *pTab){
1059 Index *p;
drh48dd1d82014-05-27 18:18:581060 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
drh44156282013-10-23 22:23:031061 return p;
1062}
1063
1064/*
drhb9bcf7c2019-10-19 13:29:101065** 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.
drh44156282013-10-23 22:23:031069*/
drhcc803b22025-02-21 20:35:371070int sqlite3TableColumnToIndex(Index *pIdx, int iCol){
drh44156282013-10-23 22:23:031071 int i;
drhce250072025-02-21 17:03:221072 i16 iCol16;
1073 assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN );
drh0f0450e2025-04-25 12:39:321074 assert( pIdx->nColumn<=SQLITE_MAX_COLUMN+1 );
drhce250072025-02-21 17:03:221075 iCol16 = iCol;
drh44156282013-10-23 22:23:031076 for(i=0; i<pIdx->nColumn; i++){
drhce250072025-02-21 17:03:221077 if( iCol16==pIdx->aiColumn[i] ){
1078 return i;
1079 }
drh44156282013-10-23 22:23:031080 }
1081 return -1;
1082}
1083
drh81f7b372019-10-16 12:18:591084#ifndef SQLITE_OMIT_GENERATED_COLUMNS
drhb9bcf7c2019-10-19 13:29:101085/* Convert a storage column number into a table column number.
drh81f7b372019-10-16 12:18:591086**
drh8e10d742019-10-18 17:42:471087** 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**
drhb9bcf7c2019-10-19 13:29:101091** The storage column number is less than the table column number if
1092** and only there are VIRTUAL columns to the left.
drh8e10d742019-10-18 17:42:471093**
1094** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
drh8e10d742019-10-18 17:42:471095*/
drhb9bcf7c2019-10-19 13:29:101096i16 sqlite3StorageColumnToTable(Table *pTab, i16 iCol){
drh8e10d742019-10-18 17:42:471097 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
drhb9bcf7c2019-10-19 13:29:101108/* Convert a table column number into a storage column number.
drh8e10d742019-10-18 17:42:471109**
1110** The storage column number (0,1,2,....) is the index of the value
drhdd6cc9b2019-10-19 18:47:271111** 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
larrybrbc917382023-06-07 08:40:311113** the number of non-virtual columns in the table plus N.
drh8e10d742019-10-18 17:42:471114**
drhdd6cc9b2019-10-19 18:47:271115** The true column number is the index (0,1,2,...) of the column in
1116** the CREATE TABLE statement.
drh8e10d742019-10-18 17:42:471117**
drhdd6cc9b2019-10-19 18:47:271118** 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
drh7fe2fc02019-12-07 00:22:181140** 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.
drh81f7b372019-10-16 12:18:591143*/
drhb9bcf7c2019-10-19 13:29:101144i16 sqlite3TableColumnToStorage(Table *pTab, i16 iCol){
drh81f7b372019-10-16 12:18:591145 int i;
1146 i16 n;
1147 assert( iCol<pTab->nCol );
drh7fe2fc02019-12-07 00:22:181148 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
drh81f7b372019-10-16 12:18:591149 for(i=0, n=0; i<iCol; i++){
1150 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
1151 }
drhdd6cc9b2019-10-19 18:47:271152 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 }
drh81f7b372019-10-16 12:18:591159}
1160#endif
1161
drh44156282013-10-23 22:23:031162/*
drh31da7be2021-05-13 18:24:221163** 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*/
1170static 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);
dana8f249f2021-05-20 11:42:511175 sqlite3VdbeUsesBtree(v, 0);
drh31da7be2021-05-13 18:24:221176 }
1177}
1178
1179/*
drh75897232000-05-29 14:26:001180** Begin constructing a new table representation in memory. This is
1181** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:091182** to a CREATE TABLE statement. In particular, this routine is called
drh74161702006-02-24 02:53:491183** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
drhe0bc4042002-06-25 01:09:111184** 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
drhf57b3392001-10-08 13:22:321187** CREATE and TABLE.
drhd9b02572001-04-15 00:37:091188**
drhf57b3392001-10-08 13:22:321189** 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.
danielk19774adee202004-05-08 08:23:191192** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:321193** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:001194*/
danielk19774adee202004-05-08 08:23:191195void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:311196 Parse *pParse, /* Parser context */
danielk1977cbb18d22004-05-28 11:37:271197 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 */
drhe5f9c642003-01-13 23:27:311199 int isTemp, /* True if this is a TEMP table */
drhfaa59552005-12-29 23:33:541200 int isView, /* True if this is a VIEW */
danielk1977f1a381e2006-06-16 08:01:021201 int isVirtual, /* True if this is a VIRTUAL table */
drhfaa59552005-12-29 23:33:541202 int noErr /* Do nothing if table already exists */
drhe5f9c642003-01-13 23:27:311203){
drh75897232000-05-29 14:26:001204 Table *pTable;
drh23bf66d2004-12-14 03:34:341205 char *zName = 0; /* The name of the new table */
drh9bb575f2004-09-06 17:24:111206 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:531207 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:271208 int iDb; /* Database number to create the table in */
1209 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:001210
drh055f2982016-01-15 15:06:411211 if( db->init.busy && db->init.newTnum==1 ){
drh1e32bed2020-06-19 13:33:531212 /* Special case: Parsing the sqlite_schema or sqlite_temp_schema schema */
drh055f2982016-01-15 15:06:411213 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 ){
larrybrbc917382023-06-07 08:40:311221 /* If creating a temp table, the name may not be qualified. Unless
drh055f2982016-01-15 15:06:411222 ** 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);
danc9461ec2018-08-29 21:00:161228 if( IN_RENAME_OBJECT ){
1229 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
1230 }
danielk1977cbb18d22004-05-28 11:37:271231 }
danielk1977cbb18d22004-05-28 11:37:271232 pParse->sNameToken = *pName;
danielk1977e0048402004-06-15 16:51:011233 if( zName==0 ) return;
drhc5a93d42019-08-12 00:08:071234 if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
drh23bf66d2004-12-14 03:34:341235 goto begin_table_error;
danielk1977d8123362004-06-12 09:25:121236 }
drh1d85d932004-02-14 23:05:521237 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:311238#ifndef SQLITE_OMIT_AUTHORIZATION
drh055f2982016-01-15 15:06:411239 assert( isTemp==0 || isTemp==1 );
1240 assert( isView==0 || isView==1 );
drhe5f9c642003-01-13 23:27:311241 {
drh055f2982016-01-15 15:06:411242 static const u8 aCode[] = {
1243 SQLITE_CREATE_TABLE,
1244 SQLITE_CREATE_TEMP_TABLE,
1245 SQLITE_CREATE_VIEW,
1246 SQLITE_CREATE_TEMP_VIEW
1247 };
drh69c33822016-08-18 14:33:111248 char *zDb = db->aDb[iDb].zDbSName;
danielk19774adee202004-05-08 08:23:191249 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drh23bf66d2004-12-14 03:34:341250 goto begin_table_error;
drhe22a3342003-04-22 20:30:371251 }
drh055f2982016-01-15 15:06:411252 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
1253 zName, 0, zDb) ){
drh23bf66d2004-12-14 03:34:341254 goto begin_table_error;
drhe5f9c642003-01-13 23:27:311255 }
1256 }
1257#endif
drhf57b3392001-10-08 13:22:321258
drhf57b3392001-10-08 13:22:321259 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:191260 ** index or table name in the same database. Issue an error message if
danielk19777e6ebfb2006-06-12 11:24:371261 ** 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.
drhf57b3392001-10-08 13:22:321265 */
dancf8f2892018-08-09 20:47:011266 if( !IN_SPECIAL_PARSE ){
drh69c33822016-08-18 14:33:111267 char *zDb = db->aDb[iDb].zDbSName;
danielk19777e6ebfb2006-06-12 11:24:371268 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
1269 goto begin_table_error;
drhfaa59552005-12-29 23:33:541270 }
dana16d1062010-09-28 17:37:281271 pTable = sqlite3FindTable(db, zName, zDb);
danielk19777e6ebfb2006-06-12 11:24:371272 if( pTable ){
1273 if( !noErr ){
larrybr6e85b272022-02-07 01:09:491274 sqlite3ErrorMsg(pParse, "%s %T already exists",
1275 (IsView(pTable)? "view" : "table"), pName);
dan7687c832011-04-09 15:39:021276 }else{
drh33c59ec2015-04-19 20:39:171277 assert( !db->init.busy || CORRUPT_DB );
dan7687c832011-04-09 15:39:021278 sqlite3CodeVerifySchema(pParse, iDb);
drh31da7be2021-05-13 18:24:221279 sqlite3ForceNotReadOnly(pParse);
danielk19777e6ebfb2006-06-12 11:24:371280 }
1281 goto begin_table_error;
1282 }
drh8a8a0d12010-09-28 20:26:441283 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
danielk19777e6ebfb2006-06-12 11:24:371284 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
1285 goto begin_table_error;
1286 }
drh75897232000-05-29 14:26:001287 }
danielk19777e6ebfb2006-06-12 11:24:371288
danielk197726783a52007-08-29 14:06:221289 pTable = sqlite3DbMallocZero(db, sizeof(Table));
drh6d4abfb2001-10-22 02:58:081290 if( pTable==0 ){
drh4df86af2016-02-04 11:48:001291 assert( db->mallocFailed );
mistachkinfad30392016-02-13 23:43:461292 pParse->rc = SQLITE_NOMEM_BKPT;
danielk1977e0048402004-06-15 16:51:011293 pParse->nErr++;
drh23bf66d2004-12-14 03:34:341294 goto begin_table_error;
drh6d4abfb2001-10-22 02:58:081295 }
drh75897232000-05-29 14:26:001296 pTable->zName = zName;
drh4a324312001-12-21 14:30:421297 pTable->iPKey = -1;
danielk1977da184232006-01-05 11:34:321298 pTable->pSchema = db->aDb[iDb].pSchema;
drh79df7782016-12-14 14:07:351299 pTable->nTabRef = 1;
drhd1417ee2017-06-06 18:20:431300#ifdef SQLITE_DEFAULT_ROWEST
1301 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
1302#else
dancfc9df72014-04-25 15:01:011303 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
drhd1417ee2017-06-06 18:20:431304#endif
drhc4a64fa2009-05-11 20:53:281305 assert( pParse->pNewTable==0 );
drh75897232000-05-29 14:26:001306 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:271307
1308 /* Begin generating the code that will insert the table record into
drh346a70c2020-06-15 20:27:351309 ** the schema table. Note in particular that we must go ahead
drh17f71932002-02-21 12:01:271310 ** and allocate the record number for the table entry now. Before any
1311 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
larrybrbc917382023-06-07 08:40:311312 ** indices to be created and the table record must come before the
drh17f71932002-02-21 12:01:271313 ** indices. Hence, the record number for the table must be allocated
1314 ** now.
1315 */
danielk19774adee202004-05-08 08:23:191316 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
drh728e0f92015-10-10 14:41:281317 int addr1;
drhe321c292006-01-12 01:56:431318 int fileFormat;
drhb7654112008-01-12 12:48:071319 int reg1, reg2, reg3;
drh3c03afd2015-09-09 13:28:061320 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
1321 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
drh0dd5cda2015-06-16 16:39:011322 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhb17131a2004-11-05 22:18:491323
danielk197720b1eaf2006-07-26 16:22:141324#ifndef SQLITE_OMIT_VIRTUALTABLE
1325 if( isVirtual ){
drh66a51672008-01-03 00:01:231326 sqlite3VdbeAddOp0(v, OP_VBegin);
danielk197720b1eaf2006-07-26 16:22:141327 }
1328#endif
1329
larrybrbc917382023-06-07 08:40:311330 /* If the file format and encoding in the database have not been set,
danielk197736963fd2005-02-19 08:18:051331 ** set them now.
danielk1977d008cfe2004-06-19 02:22:101332 */
drh7fd936e2025-02-07 15:49:211333 assert( pParse->isCreate );
1334 reg1 = pParse->u1.cr.regRowid = ++pParse->nMem;
1335 reg2 = pParse->u1.cr.regRoot = ++pParse->nMem;
drhb7654112008-01-12 12:48:071336 reg3 = ++pParse->nMem;
danielk19770d19f7a2009-06-03 11:25:071337 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
drhfb982642007-08-30 01:19:591338 sqlite3VdbeUsesBtree(v, iDb);
drh728e0f92015-10-10 14:41:281339 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
drhe321c292006-01-12 01:56:431340 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
drh76fe8032006-07-11 14:17:511341 1 : SQLITE_MAX_FILE_FORMAT;
drh1861afc2016-02-01 21:48:341342 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
1343 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
drh728e0f92015-10-10 14:41:281344 sqlite3VdbeJumpHere(v, addr1);
danielk1977d008cfe2004-06-19 02:22:101345
drh1e32bed2020-06-19 13:33:531346 /* This just creates a place-holder record in the sqlite_schema table.
drh4794f732004-11-05 17:17:501347 ** The record created does not contain anything yet. It will be replaced
1348 ** by the real entry in code generated at sqlite3EndTable().
drhb17131a2004-11-05 22:18:491349 **
drh7fd936e2025-02-07 15:49:211350 ** 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.
drh0fa991b2009-03-21 16:19:261352 ** The rowid and root page number values are needed by the code that
1353 ** sqlite3EndTable will generate.
drh4794f732004-11-05 17:17:501354 */
danielk1977f1a381e2006-06-16 08:01:021355#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
1356 if( isView || isVirtual ){
drhb7654112008-01-12 12:48:071357 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
danielk1977a21c6b62005-01-24 10:25:591358 }else
1359#endif
1360 {
drh381bdac2021-02-04 17:29:041361 assert( !pParse->bReturning );
drh7fd936e2025-02-07 15:49:211362 pParse->u1.cr.addrCrTab =
drh0f3f7662017-08-18 14:34:281363 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
danielk1977a21c6b62005-01-24 10:25:591364 }
drh346a70c2020-06-15 20:27:351365 sqlite3OpenSchemaTable(pParse, iDb);
drhb7654112008-01-12 12:48:071366 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
drh3c03afd2015-09-09 13:28:061367 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
drhb7654112008-01-12 12:48:071368 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
1369 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
drh66a51672008-01-03 00:01:231370 sqlite3VdbeAddOp0(v, OP_Close);
drh5e00f6c2001-09-13 13:46:561371 }
drh23bf66d2004-12-14 03:34:341372
1373 /* Normal (non-error) return. */
1374 return;
1375
1376 /* If an error occurs, we jump here */
1377begin_table_error:
drhc0495e82021-07-22 21:11:061378 pParse->checkSchema = 1;
drh633e6d52008-07-28 19:34:531379 sqlite3DbFree(db, zName);
drh23bf66d2004-12-14 03:34:341380 return;
drh75897232000-05-29 14:26:001381}
1382
drh03d69a62015-11-19 13:53:571383/* Set properties of a table column based on the (magical)
1384** name of the column.
1385*/
drh03d69a62015-11-19 13:53:571386#if SQLITE_ENABLE_HIDDEN_COLUMNS
drhe6110502015-12-31 15:34:031387void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
drhcf9d36d2021-08-02 18:03:431388 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
drh03d69a62015-11-19 13:53:571389 pCol->colFlags |= COLFLAG_HIDDEN;
drh6f6e60d2021-02-18 15:45:341390 if( pTab ) pTab->tabFlags |= TF_HasHidden;
danba68f8f2015-11-19 16:46:461391 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
1392 pTab->tabFlags |= TF_OOOHidden;
drh03d69a62015-11-19 13:53:571393 }
drh03d69a62015-11-19 13:53:571394}
drhe6110502015-12-31 15:34:031395#endif
drh03d69a62015-11-19 13:53:571396
drh2053f312021-01-12 20:16:311397/*
drh28828c52021-01-30 21:55:381398** Clean up the data structures associated with the RETURNING clause.
drhb8352472021-01-29 19:32:171399*/
drh82fc1b62023-12-06 18:25:411400static void sqlite3DeleteReturning(sqlite3 *db, void *pArg){
1401 Returning *pRet = (Returning*)pArg;
drhb8352472021-01-29 19:32:171402 Hash *pHash;
1403 pHash = &(db->aDb[1].pSchema->trigHash);
dan94331d42023-10-26 16:05:571404 sqlite3HashInsert(pHash, pRet->zName, 0);
drhb8352472021-01-29 19:32:171405 sqlite3ExprListDelete(db, pRet->pReturnEL);
1406 sqlite3DbFree(db, pRet);
1407}
1408
1409/*
drh28828c52021-01-30 21:55:381410** 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().
drh2053f312021-01-12 20:16:311424*/
1425void sqlite3AddReturning(Parse *pParse, ExprList *pList){
drhb8352472021-01-29 19:32:171426 Returning *pRet;
1427 Hash *pHash;
1428 sqlite3 *db = pParse->db;
drhe1c9a4e2021-02-07 23:28:201429 if( pParse->pNewTrigger ){
1430 sqlite3ErrorMsg(pParse, "cannot use RETURNING in a trigger");
1431 }else{
drha84ead12023-03-17 00:01:321432 assert( pParse->bReturning==0 || pParse->ifNotExists );
drhe1c9a4e2021-02-07 23:28:201433 }
drhd086aa02021-01-29 21:31:591434 pParse->bReturning = 1;
drhb8352472021-01-29 19:32:171435 pRet = sqlite3DbMallocZero(db, sizeof(*pRet));
1436 if( pRet==0 ){
1437 sqlite3ExprListDelete(db, pList);
1438 return;
1439 }
drh7fd936e2025-02-07 15:49:211440 assert( !pParse->isCreate );
1441 pParse->u1.d.pReturning = pRet;
drhb8352472021-01-29 19:32:171442 pRet->pParse = pParse;
1443 pRet->pReturnEL = pList;
drh82fc1b62023-12-06 18:25:411444 sqlite3ParserAddCleanup(pParse, sqlite3DeleteReturning, pRet);
drh6d0053c2021-03-09 19:32:371445 testcase( pParse->earlyCleanup );
drhcf4108b2021-01-30 03:06:191446 if( db->mallocFailed ) return;
dan94331d42023-10-26 16:05:571447 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
1448 "sqlite_returning_%p", pParse);
1449 pRet->retTrig.zName = pRet->zName;
drhb8352472021-01-29 19:32:171450 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;
drha4767682021-04-27 13:04:181454 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
drhb8352472021-01-29 19:32:171455 pRet->retTrig.step_list = &pRet->retTStep;
drhdac9a5f2021-01-29 21:18:461456 pRet->retTStep.op = TK_RETURNING;
drhb8352472021-01-29 19:32:171457 pRet->retTStep.pTrig = &pRet->retTrig;
drh381bdac2021-02-04 17:29:041458 pRet->retTStep.pExprList = pList;
drhb8352472021-01-29 19:32:171459 pHash = &(db->aDb[1].pSchema->trigHash);
dan94331d42023-10-26 16:05:571460 assert( sqlite3HashFind(pHash, pRet->zName)==0
drha84ead12023-03-17 00:01:321461 || pParse->nErr || pParse->ifNotExists );
dan94331d42023-10-26 16:05:571462 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
drh0166df02021-01-30 12:07:321463 ==&pRet->retTrig ){
1464 sqlite3OomFault(db);
1465 }
drh2053f312021-01-12 20:16:311466}
drh03d69a62015-11-19 13:53:571467
drh75897232000-05-29 14:26:001468/*
1469** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:091470**
1471** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:191472** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:091473** first to get things going. Then this routine is called for each
1474** column.
drh75897232000-05-29 14:26:001475*/
drh77441fa2021-07-30 18:39:591476void sqlite3AddColumn(Parse *pParse, Token sName, Token sType){
drh75897232000-05-29 14:26:001477 Table *p;
drh97fc3d02002-05-22 21:27:031478 int i;
drha99db3b2004-06-19 14:49:121479 char *z;
drh94eaafa2016-02-29 15:53:111480 char *zType;
drhc9b84a12002-06-20 11:36:481481 Column *pCol;
drhbb4957f2008-03-20 14:03:291482 sqlite3 *db = pParse->db;
drh7b3c5142021-07-30 12:47:351483 Column *aNew;
drhc2df4d62021-07-30 23:30:301484 u8 eType = COLTYPE_CUSTOM;
1485 u8 szEst = 1;
1486 char affinity = SQLITE_AFF_BLOB;
drh3e992d12021-01-01 19:17:011487
drh75897232000-05-29 14:26:001488 if( (p = pParse->pNewTable)==0 ) return;
drhbb4957f2008-03-20 14:03:291489 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
drhe5c941b2007-05-08 13:58:261490 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
1491 return;
1492 }
drh77441fa2021-07-30 18:39:591493 if( !IN_RENAME_OBJECT ) sqlite3DequoteToken(&sName);
drhe48f2612021-07-30 20:09:081494
larrybrbc917382023-06-07 08:40:311495 /* Because keywords GENERATE ALWAYS can be converted into identifiers
drhe48f2612021-07-30 20:09:081496 ** 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
drhc2df4d62021-07-30 23:30:301512 /* 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
drh913306a2021-11-26 17:10:181530 z = sqlite3DbMallocRaw(db, (i64)sName.n + 1 + (i64)sType.n + (sType.n>0) );
drh97fc3d02002-05-22 21:27:031531 if( z==0 ) return;
drh77441fa2021-07-30 18:39:591532 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, &sName);
1533 memcpy(z, sName.z, sName.n);
1534 z[sName.n] = 0;
drh94eaafa2016-02-29 15:53:111535 sqlite3Dequote(z);
drh3bdebae2025-02-09 19:49:461536 if( p->nCol && sqlite3ColumnIndex(p, z)>=0 ){
drh9d90a3a2025-02-08 14:15:421537 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
1538 sqlite3DbFree(db, z);
1539 return;
drh97fc3d02002-05-22 21:27:031540 }
drh913306a2021-11-26 17:10:181541 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
drh7b3c5142021-07-30 12:47:351542 if( aNew==0 ){
1543 sqlite3DbFree(db, z);
1544 return;
drh75897232000-05-29 14:26:001545 }
drh7b3c5142021-07-30 12:47:351546 p->aCol = aNew;
drhc9b84a12002-06-20 11:36:481547 pCol = &p->aCol[p->nCol];
1548 memset(pCol, 0, sizeof(p->aCol[0]));
drhcf9d36d2021-08-02 18:03:431549 pCol->zCnName = z;
drh9d90a3a2025-02-08 14:15:421550 pCol->hName = sqlite3StrIHash(z);
danba68f8f2015-11-19 16:46:461551 sqlite3ColumnPropertiesFromName(p, pCol);
larrybrbc917382023-06-07 08:40:311552
drh77441fa2021-07-30 18:39:591553 if( sType.n==0 ){
drh2881ab62016-02-27 23:25:361554 /* If there is no type specified, columns have the default affinity
drhbbade8d2018-04-18 14:48:081555 ** 'BLOB' with a default size of 4 bytes. */
drhc2df4d62021-07-30 23:30:301556 pCol->affinity = affinity;
drhb70f2ea2021-08-18 12:05:221557 pCol->eCType = eType;
drhc2df4d62021-07-30 23:30:301558 pCol->szEst = szEst;
drhbbade8d2018-04-18 14:48:081559#ifdef SQLITE_ENABLE_SORTER_REFERENCES
drhc2df4d62021-07-30 23:30:301560 if( affinity==SQLITE_AFF_BLOB ){
1561 if( 4>=sqlite3GlobalConfig.szSorterRef ){
1562 pCol->colFlags |= COLFLAG_SORTERREF;
1563 }
dan2e3a5a82018-04-16 21:12:421564 }
drhbbade8d2018-04-18 14:48:081565#endif
drh2881ab62016-02-27 23:25:361566 }else{
drhddb2b4a2016-03-25 12:10:321567 zType = z + sqlite3Strlen30(z) + 1;
drh77441fa2021-07-30 18:39:591568 memcpy(zType, sType.z, sType.n);
1569 zType[sType.n] = 0;
drha6dddd92016-04-18 15:46:141570 sqlite3Dequote(zType);
dan2e3a5a82018-04-16 21:12:421571 pCol->affinity = sqlite3AffinityType(zType, pCol);
drhd7564862016-03-22 20:05:091572 pCol->colFlags |= COLFLAG_HASTYPE;
drh2881ab62016-02-27 23:25:361573 }
drh66172ce2025-02-08 16:16:081574 if( p->nCol<=0xff ){
1575 u8 h = pCol->hName % sizeof(p->aHx);
1576 p->aHx[h] = p->nCol;
1577 }
drhc9b84a12002-06-20 11:36:481578 p->nCol++;
drhf95909c2019-10-18 18:33:251579 p->nNVCol++;
drh7fd936e2025-02-07 15:49:211580 assert( pParse->isCreate );
1581 pParse->u1.cr.constraintName.n = 0;
drh75897232000-05-29 14:26:001582}
1583
1584/*
drh382c0242001-10-06 16:33:021585** 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*/
danielk19774adee202004-05-08 08:23:191590void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:021591 Table *p;
dan26e731c2018-01-29 16:22:391592 Column *pCol;
drhc4a64fa2009-05-11 20:53:281593 p = pParse->pNewTable;
1594 if( p==0 || NEVER(p->nCol<1) ) return;
dan26e731c2018-01-29 16:22:391595 pCol = &p->aCol[p->nCol-1];
1596 pCol->notNull = (u8)onError;
drh8b174f22017-02-22 15:11:361597 p->tabFlags |= TF_HasNotNull;
dan26e731c2018-01-29 16:22:391598
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 }
drh382c0242001-10-06 16:33:021610}
1611
1612/*
danielk197752a83fb2005-01-31 12:56:441613** Scan the column type name zType (length nType) and return the
1614** associated affinity type.
danielk1977b3dff962005-02-01 01:21:551615**
larrybrbc917382023-06-07 08:40:311616** This routine does a case-independent search of zType for the
danielk1977b3dff962005-02-01 01:21:551617** substrings in the following table. If one of the substrings is
1618** found, the corresponding affinity is returned. If zType contains
larrybrbc917382023-06-07 08:40:311619** more than one of the substrings, entries toward the top of
1620** the table take priority. For example, if zType is 'BLOBINT',
drh8a512562005-11-14 22:29:051621** SQLITE_AFF_INTEGER is returned.
danielk1977b3dff962005-02-01 01:21:551622**
1623** Substring | Affinity
1624** --------------------------------
1625** 'INT' | SQLITE_AFF_INTEGER
1626** 'CHAR' | SQLITE_AFF_TEXT
1627** 'CLOB' | SQLITE_AFF_TEXT
1628** 'TEXT' | SQLITE_AFF_TEXT
drh05883a32015-06-02 15:32:081629** 'BLOB' | SQLITE_AFF_BLOB
drh8a512562005-11-14 22:29:051630** 'REAL' | SQLITE_AFF_REAL
1631** 'FLOA' | SQLITE_AFF_REAL
1632** 'DOUB' | SQLITE_AFF_REAL
danielk1977b3dff962005-02-01 01:21:551633**
1634** If none of the substrings in the above table are found,
1635** SQLITE_AFF_NUMERIC is returned.
danielk197752a83fb2005-01-31 12:56:441636*/
dan2e3a5a82018-04-16 21:12:421637char sqlite3AffinityType(const char *zIn, Column *pCol){
danielk1977b3dff962005-02-01 01:21:551638 u32 h = 0;
1639 char aff = SQLITE_AFF_NUMERIC;
drhd3037a42013-10-04 18:29:251640 const char *zChar = 0;
danielk197752a83fb2005-01-31 12:56:441641
drh2f1e02e2016-03-09 02:12:441642 assert( zIn!=0 );
drhfdaac672013-10-04 15:30:211643 while( zIn[0] ){
drh267721e2024-01-04 13:01:021644 u8 x = *(u8*)zIn;
1645 h = (h<<8) + sqlite3UpperToLower[x];
danielk1977b3dff962005-02-01 01:21:551646 zIn++;
danielk1977201f7162005-02-01 02:13:291647 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
drhfdaac672013-10-04 15:30:211648 aff = SQLITE_AFF_TEXT;
1649 zChar = zIn;
danielk1977201f7162005-02-01 02:13:291650 }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 */
drh8a512562005-11-14 22:29:051655 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
drh05883a32015-06-02 15:32:081656 aff = SQLITE_AFF_BLOB;
drhd3037a42013-10-04 18:29:251657 if( zIn[0]=='(' ) zChar = zIn;
drh8a512562005-11-14 22:29:051658#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
danielk1977201f7162005-02-01 02:13:291669 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
drh8a512562005-11-14 22:29:051670 aff = SQLITE_AFF_INTEGER;
danielk1977b3dff962005-02-01 01:21:551671 break;
danielk197752a83fb2005-01-31 12:56:441672 }
1673 }
drhd3037a42013-10-04 18:29:251674
dan2e3a5a82018-04-16 21:12:421675 /* If pCol is not NULL, store an estimate of the field size. The
drhd3037a42013-10-04 18:29:251676 ** estimate is scaled so that the size of an integer is 1. */
dan2e3a5a82018-04-16 21:12:421677 if( pCol ){
1678 int v = 0; /* default size is approx 4 bytes */
drh7ea31cc2014-09-18 14:36:001679 if( aff<SQLITE_AFF_NUMERIC ){
drhd3037a42013-10-04 18:29:251680 if( zChar ){
1681 while( zChar[0] ){
1682 if( sqlite3Isdigit(zChar[0]) ){
dan2e3a5a82018-04-16 21:12:421683 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
drhd3037a42013-10-04 18:29:251684 sqlite3GetInt32(zChar, &v);
drhd3037a42013-10-04 18:29:251685 break;
1686 }
1687 zChar++;
drhfdaac672013-10-04 15:30:211688 }
drhd3037a42013-10-04 18:29:251689 }else{
dan2e3a5a82018-04-16 21:12:421690 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
drhfdaac672013-10-04 15:30:211691 }
drhfdaac672013-10-04 15:30:211692 }
drhbbade8d2018-04-18 14:48:081693#ifdef SQLITE_ENABLE_SORTER_REFERENCES
dan2e3a5a82018-04-16 21:12:421694 if( v>=sqlite3GlobalConfig.szSorterRef ){
1695 pCol->colFlags |= COLFLAG_SORTERREF;
1696 }
drhbbade8d2018-04-18 14:48:081697#endif
dan2e3a5a82018-04-16 21:12:421698 v = v/4 + 1;
1699 if( v>255 ) v = 255;
1700 pCol->szEst = v;
drhfdaac672013-10-04 15:30:211701 }
danielk1977b3dff962005-02-01 01:21:551702 return aff;
danielk197752a83fb2005-01-31 12:56:441703}
1704
1705/*
danielk19777977a172004-11-09 12:44:371706** 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.
drhd9b02572001-04-15 00:37:091711**
1712** This routine is called by the parser while in the middle of
1713** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:521714*/
drh1be266b2017-12-24 00:18:471715void 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 */
larrybrbc917382023-06-07 08:40:311719 const char *zEnd /* First character past end of default value text */
drh1be266b2017-12-24 00:18:471720){
drh7020f652000-06-03 18:06:521721 Table *p;
danielk19777977a172004-11-09 12:44:371722 Column *pCol;
drh633e6d52008-07-28 19:34:531723 sqlite3 *db = pParse->db;
drhc4a64fa2009-05-11 20:53:281724 p = pParse->pNewTable;
1725 if( p!=0 ){
drh014fff22020-01-08 22:22:361726 int isInit = db->init.busy && db->init.iDb!=1;
drh42b9d7c2005-08-13 00:56:271727 pCol = &(p->aCol[p->nCol-1]);
drh014fff22020-01-08 22:22:361728 if( !sqlite3ExprIsConstantOrFunction(pExpr, isInit) ){
drh42b9d7c2005-08-13 00:56:271729 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
drhcf9d36d2021-08-02 18:03:431730 pCol->zCnName);
drh7e7fd732019-10-22 13:59:231731#ifndef SQLITE_OMIT_GENERATED_COLUMNS
1732 }else if( pCol->colFlags & COLFLAG_GENERATED ){
drhab0992f2019-10-23 03:53:101733 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
1734 testcase( pCol->colFlags & COLFLAG_STORED );
drh7e7fd732019-10-22 13:59:231735 sqlite3ErrorMsg(pParse, "cannot use DEFAULT on a generated column");
1736#endif
drh42b9d7c2005-08-13 00:56:271737 }else{
danielk19776ab3a2e2009-02-19 14:39:251738 /* A copy of pExpr is used instead of the original, as pExpr contains
drh424981d2018-03-28 15:56:551739 ** tokens that point to volatile memory.
danielk19776ab3a2e2009-02-19 14:39:251740 */
drh79cf2b72021-07-31 20:30:411741 Expr x, *pDfltExpr;
drh94fa9c42016-02-27 21:16:041742 memset(&x, 0, sizeof(x));
1743 x.op = TK_SPAN;
drh9b2e0432017-12-27 19:43:221744 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
drh1be266b2017-12-24 00:18:471745 x.pLeft = pExpr;
drh94fa9c42016-02-27 21:16:041746 x.flags = EP_Skip;
drh79cf2b72021-07-31 20:30:411747 pDfltExpr = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
drh94fa9c42016-02-27 21:16:041748 sqlite3DbFree(db, x.u.zToken);
drh79cf2b72021-07-31 20:30:411749 sqlite3ColumnSetExpr(pParse, p, pCol, pDfltExpr);
drh42b9d7c2005-08-13 00:56:271750 }
danielk19777977a172004-11-09 12:44:371751 }
dan8900a482018-09-05 14:36:051752 if( IN_RENAME_OBJECT ){
1753 sqlite3RenameExprUnmap(pParse, pExpr);
1754 }
drh1be266b2017-12-24 00:18:471755 sqlite3ExprDelete(db, pExpr);
drh7020f652000-06-03 18:06:521756}
1757
1758/*
drh153110a2015-11-01 21:19:131759** Backwards Compatibility Hack:
larrybrbc917382023-06-07 08:40:311760**
drh153110a2015-11-01 21:19:131761** 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.
drh6c591362019-04-27 20:16:421771** If the expression is anything other than TK_STRING, the expression is
drh153110a2015-11-01 21:19:131772** unchanged.
1773*/
1774static 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/*
drhf4b1d8d2019-10-22 15:45:031783** Tag the given column as being part of the PRIMARY KEY
1784*/
1785static 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 }
larrybrbc917382023-06-07 08:40:311794#endif
drhf4b1d8d2019-10-22 15:45:031795}
1796
1797/*
larrybrbc917382023-06-07 08:40:311798** Designate the PRIMARY KEY for the table. pList is a list of names
drh4a324312001-12-21 14:30:421799** 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,
drh23bf66d2004-12-14 03:34:341807** then we will try to use that column as the rowid. Set the Table.iPKey
drh4a324312001-12-21 14:30:421808** 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*/
drh205f48e2004-11-05 00:43:111815void 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 */
drhfdd6e852005-12-16 01:06:161819 int autoInc, /* True if the AUTOINCREMENT keyword is present */
1820 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
drh205f48e2004-11-05 00:43:111821){
drh4a324312001-12-21 14:30:421822 Table *pTab = pParse->pNewTable;
drhd7564862016-03-22 20:05:091823 Column *pCol = 0;
drh78100cc2003-08-23 22:40:531824 int iCol = -1, i;
drh8ea30bf2013-10-22 01:18:171825 int nTerm;
drh62340f82016-05-31 21:18:151826 if( pTab==0 ) goto primary_key_exit;
drh7d10d5a2008-08-20 16:35:101827 if( pTab->tabFlags & TF_HasPrimaryKey ){
larrybrbc917382023-06-07 08:40:311828 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:561829 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:511830 goto primary_key_exit;
drh4a324312001-12-21 14:30:421831 }
drh7d10d5a2008-08-20 16:35:101832 pTab->tabFlags |= TF_HasPrimaryKey;
drh4a324312001-12-21 14:30:421833 if( pList==0 ){
1834 iCol = pTab->nCol - 1;
drhd7564862016-03-22 20:05:091835 pCol = &pTab->aCol[iCol];
drhf4b1d8d2019-10-22 15:45:031836 makeColumnPartOfPrimaryKey(pParse, pCol);
drh8ea30bf2013-10-22 01:18:171837 nTerm = 1;
drh78100cc2003-08-23 22:40:531838 }else{
drh8ea30bf2013-10-22 01:18:171839 nTerm = pList->nExpr;
1840 for(i=0; i<nTerm; i++){
drh108aa002015-08-24 20:21:201841 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
drh7d3d9da2015-09-01 00:42:521842 assert( pCExpr!=0 );
drh153110a2015-11-01 21:19:131843 sqlite3StringToId(pCExpr);
drh7d3d9da2015-09-01 00:42:521844 if( pCExpr->op==TK_ID ){
drhf9751072021-10-07 13:40:291845 assert( !ExprHasProperty(pCExpr, EP_IntValue) );
drh9d90a3a2025-02-08 14:15:421846 iCol = sqlite3ColumnIndex(pTab, pCExpr->u.zToken);
1847 if( iCol>=0 ){
1848 pCol = &pTab->aCol[iCol];
1849 makeColumnPartOfPrimaryKey(pParse, pCol);
drhd3d39e92004-05-20 22:16:291850 }
drh78100cc2003-08-23 22:40:531851 }
drh4a324312001-12-21 14:30:421852 }
1853 }
drh8ea30bf2013-10-22 01:18:171854 if( nTerm==1
drhd7564862016-03-22 20:05:091855 && pCol
drhb70f2ea2021-08-18 12:05:221856 && pCol->eCType==COLTYPE_INTEGER
drhbc622bc2015-08-24 15:39:421857 && sortOrder!=SQLITE_SO_DESC
drh8ea30bf2013-10-22 01:18:171858 ){
danc9461ec2018-08-29 21:00:161859 if( IN_RENAME_OBJECT && pList ){
dan2381f6d2019-03-20 16:58:211860 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
1861 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
dan987db762018-08-14 20:18:501862 }
drh4a324312001-12-21 14:30:421863 pTab->iPKey = iCol;
drh1bd10f82008-12-10 21:19:561864 pTab->keyConf = (u8)onError;
drh7d10d5a2008-08-20 16:35:101865 assert( autoInc==0 || autoInc==1 );
1866 pTab->tabFlags |= autoInc*TF_Autoincrement;
drhd88fd532022-05-02 20:49:301867 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
drh34ab9412019-12-19 17:42:271868 (void)sqlite3HasExplicitNulls(pParse, pList);
drh205f48e2004-11-05 00:43:111869 }else if( autoInc ){
drh4794f732004-11-05 17:17:501870#ifndef SQLITE_OMIT_AUTOINCREMENT
drh205f48e2004-11-05 00:43:111871 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1872 "INTEGER PRIMARY KEY");
drh4794f732004-11-05 17:17:501873#endif
drh4a324312001-12-21 14:30:421874 }else{
drh62340f82016-05-31 21:18:151875 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
1876 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
drhe0194f22003-02-26 13:52:511877 pList = 0;
drh4a324312001-12-21 14:30:421878 }
drhe0194f22003-02-26 13:52:511879
1880primary_key_exit:
drh633e6d52008-07-28 19:34:531881 sqlite3ExprListDelete(pParse->db, pList);
drhe0194f22003-02-26 13:52:511882 return;
drh4a324312001-12-21 14:30:421883}
1884
1885/*
drhffe07b22005-11-03 00:41:171886** Add a new CHECK constraint to the table currently under construction.
1887*/
1888void sqlite3AddCheckConstraint(
drh92e21ef2020-08-27 18:36:301889 Parse *pParse, /* Parsing context */
1890 Expr *pCheckExpr, /* The check expression */
1891 const char *zStart, /* Opening "(" */
1892 const char *zEnd /* Closing ")" */
drhffe07b22005-11-03 00:41:171893){
1894#ifndef SQLITE_OMIT_CHECK
1895 Table *pTab = pParse->pNewTable;
drhc9bbb012014-05-21 08:48:181896 sqlite3 *db = pParse->db;
1897 if( pTab && !IN_DECLARE_VTAB
1898 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
1899 ){
drh2938f922012-03-07 19:13:291900 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
drh7fd936e2025-02-07 15:49:211901 assert( pParse->isCreate );
1902 if( pParse->u1.cr.constraintName.n ){
1903 sqlite3ExprListSetName(pParse, pTab->pCheck,
1904 &pParse->u1.cr.constraintName, 1);
drh92e21ef2020-08-27 18:36:301905 }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);
larrybrbc917382023-06-07 08:40:311911 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
drh2938f922012-03-07 19:13:291912 }
drh33e619f2009-05-28 01:00:551913 }else
drhffe07b22005-11-03 00:41:171914#endif
drh33e619f2009-05-28 01:00:551915 {
drh2938f922012-03-07 19:13:291916 sqlite3ExprDelete(pParse->db, pCheckExpr);
drh33e619f2009-05-28 01:00:551917 }
drhffe07b22005-11-03 00:41:171918}
1919
1920/*
drhd3d39e92004-05-20 22:16:291921** Set the collation function of the most recently parsed table column
1922** to the CollSeq given.
drh8e2ca022002-06-17 17:07:191923*/
danielk197739002502007-11-12 09:50:261924void sqlite3AddCollateType(Parse *pParse, Token *pToken){
drh8e2ca022002-06-17 17:07:191925 Table *p;
danielk19770202b292004-06-09 09:55:161926 int i;
danielk197739002502007-11-12 09:50:261927 char *zColl; /* Dequoted name of collation sequence */
drh633e6d52008-07-28 19:34:531928 sqlite3 *db;
danielk1977a37cdde2004-05-16 11:15:361929
dan936a3052020-10-12 15:27:501930 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
danielk19770202b292004-06-09 09:55:161931 i = p->nCol-1;
drh633e6d52008-07-28 19:34:531932 db = pParse->db;
1933 zColl = sqlite3NameFromToken(db, pToken);
danielk197739002502007-11-12 09:50:261934 if( !zColl ) return;
1935
drhc4a64fa2009-05-11 20:53:281936 if( sqlite3LocateCollSeq(pParse, zColl) ){
danielk1977b3bf5562006-01-10 17:58:231937 Index *pIdx;
drh65b40092021-08-05 15:27:191938 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
larrybrbc917382023-06-07 08:40:311939
danielk1977b3bf5562006-01-10 17:58:231940 /* 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){
drhbbbdc832013-10-22 18:01:401945 assert( pIdx->nKeyCol==1 );
danielk1977b3bf5562006-01-10 17:58:231946 if( pIdx->aiColumn[0]==i ){
drh65b40092021-08-05 15:27:191947 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
danielk19777cedc8d2004-06-10 10:50:081948 }
1949 }
1950 }
drh65b40092021-08-05 15:27:191951 sqlite3DbFree(db, zColl);
danielk19777cedc8d2004-06-10 10:50:081952}
1953
drh81f7b372019-10-16 12:18:591954/* Change the most recently parsed column to be a GENERATED ALWAYS AS
1955** column.
1956*/
1957void 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;
drhf68bf5f2019-12-04 03:31:291962 if( pTab==0 ){
1963 /* generated column in an CREATE TABLE IF NOT EXISTS that already exists */
1964 goto generated_done;
1965 }
drh81f7b372019-10-16 12:18:591966 pCol = &(pTab->aCol[pTab->nCol-1]);
drhb9bcf7c2019-10-19 13:29:101967 if( IN_DECLARE_VTAB ){
1968 sqlite3ErrorMsg(pParse, "virtual tables cannot use computed columns");
1969 goto generated_done;
1970 }
drh79cf2b72021-07-31 20:30:411971 if( pCol->iDflt>0 ) goto generated_error;
drh81f7b372019-10-16 12:18:591972 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 }
drhf95909c2019-10-18 18:33:251981 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
drh81f7b372019-10-16 12:18:591982 pCol->colFlags |= eType;
drhc1431142019-10-17 17:54:051983 assert( TF_HasVirtual==COLFLAG_VIRTUAL );
1984 assert( TF_HasStored==COLFLAG_STORED );
1985 pTab->tabFlags |= eType;
drha0e16a22019-10-27 22:22:241986 if( pCol->colFlags & COLFLAG_PRIMKEY ){
1987 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
1988 }
drhfe838922022-12-21 14:13:491989 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 }
drhcad225d2023-03-07 23:47:381996 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
drh79cf2b72021-07-31 20:30:411997 sqlite3ColumnSetExpr(pParse, pTab, pCol, pExpr);
drhb9bcf7c2019-10-19 13:29:101998 pExpr = 0;
drh81f7b372019-10-16 12:18:591999 goto generated_done;
2000
2001generated_error:
drh7e7fd732019-10-22 13:59:232002 sqlite3ErrorMsg(pParse, "error in generated column \"%s\"",
drhcf9d36d2021-08-02 18:03:432003 pCol->zCnName);
drh81f7b372019-10-16 12:18:592004generated_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. */
drh7e7fd732019-10-22 13:59:232009 sqlite3ErrorMsg(pParse, "generated columns not supported");
drh81f7b372019-10-16 12:18:592010 sqlite3ExprDelete(pParse->db, pExpr);
2011#endif
2012}
2013
danielk1977466be562004-06-10 02:16:012014/*
drh3f7d4e42004-07-24 14:35:582015** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:282016**
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.
drh96fdcb42016-09-27 00:09:332029**
2030** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
2031** the schema-version whenever the schema changes.
drh50e5dad2001-09-15 00:57:282032*/
drh9cbf3422008-01-17 16:22:132033void sqlite3ChangeCookie(Parse *pParse, int iDb){
drh9cbf3422008-01-17 16:22:132034 sqlite3 *db = pParse->db;
2035 Vdbe *v = pParse->pVdbe;
drh21206082011-04-04 18:22:022036 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
larrybrbc917382023-06-07 08:40:312037 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
drh3517b312018-04-09 00:46:422038 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
drh50e5dad2001-09-15 00:57:282039}
2040
2041/*
drh969fa7c2002-02-18 18:30:322042** 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.
drh234c39d2004-07-24 03:30:472045**
2046** The estimate is conservative. It might be larger that what is
2047** really needed.
drh969fa7c2002-02-18 18:30:322048*/
2049static int identLength(const char *z){
2050 int n;
drh17f71932002-02-21 12:01:272051 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:472052 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:322053 }
drh234c39d2004-07-24 03:30:472054 return n + 2;
drh969fa7c2002-02-18 18:30:322055}
2056
2057/*
larrybrbc917382023-06-07 08:40:312058** The first parameter is a pointer to an output buffer. The second
danielk19771b870de2009-03-14 08:37:232059** 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.
larrybrbc917382023-06-07 08:40:312064**
larrybr55be2162023-06-07 17:03:222065** If the string zSignedIdent consists entirely of alphanumeric
danielk19771b870de2009-03-14 08:37:232066** 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*/
drhc4a64fa2009-05-11 20:53:282070static void identPut(char *z, int *pIdx, char *zSignedIdent){
drh4c755c02004-08-08 20:22:172071 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:272072 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:322073 i = *pIdx;
danielk19771b870de2009-03-14 08:37:232074
drh17f71932002-02-21 12:01:272075 for(j=0; zIdent[j]; j++){
danielk197778ca0e72009-01-20 16:53:392076 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
drh17f71932002-02-21 12:01:272077 }
drhc7407522014-01-10 20:38:122078 needQuote = sqlite3Isdigit(zIdent[0])
2079 || sqlite3KeywordCode(zIdent, j)!=TK_ID
2080 || zIdent[j]!=0
2081 || j==0;
danielk19771b870de2009-03-14 08:37:232082
drh234c39d2004-07-24 03:30:472083 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:322084 for(j=0; zIdent[j]; j++){
2085 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:472086 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:322087 }
drh234c39d2004-07-24 03:30:472088 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:322089 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*/
drh1d34fde2009-02-03 15:50:332098static char *createTableStmt(sqlite3 *db, Table *p){
drhef86b942025-02-17 17:33:142099 int i, k, len;
2100 i64 n;
drh969fa7c2002-02-18 18:30:322101 char *zStmt;
drhc4a64fa2009-05-11 20:53:282102 char *zSep, *zSep2, *zEnd;
drh234c39d2004-07-24 03:30:472103 Column *pCol;
drh969fa7c2002-02-18 18:30:322104 n = 0;
drh234c39d2004-07-24 03:30:472105 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
drhcf9d36d2021-08-02 18:03:432106 n += identLength(pCol->zCnName) + 5;
drh969fa7c2002-02-18 18:30:322107 }
2108 n += identLength(p->zName);
larrybrbc917382023-06-07 08:40:312109 if( n<50 ){
drh969fa7c2002-02-18 18:30:322110 zSep = "";
2111 zSep2 = ",";
2112 zEnd = ")";
2113 }else{
2114 zSep = "\n ";
2115 zSep2 = ",\n ";
2116 zEnd = "\n)";
2117 }
drhe0bc4042002-06-25 01:09:112118 n += 35 + 6*p->nCol;
drhb9755982010-07-24 16:34:372119 zStmt = sqlite3DbMallocRaw(0, n);
drh820a9062008-01-31 13:35:482120 if( zStmt==0 ){
drh4a642b62016-02-05 01:55:272121 sqlite3OomFault(db);
drh820a9062008-01-31 13:35:482122 return 0;
2123 }
drhef86b942025-02-17 17:33:142124 assert( n>14 && n<=0x7fffffff );
2125 memcpy(zStmt, "CREATE TABLE ", 13);
2126 k = 13;
drhc4a64fa2009-05-11 20:53:282127 identPut(zStmt, &k, p->zName);
drh969fa7c2002-02-18 18:30:322128 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:472129 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drhc4a64fa2009-05-11 20:53:282130 static const char * const azType[] = {
drh05883a32015-06-02 15:32:082131 /* SQLITE_AFF_BLOB */ "",
drh7ea31cc2014-09-18 14:36:002132 /* SQLITE_AFF_TEXT */ " TEXT",
drhc4a64fa2009-05-11 20:53:282133 /* SQLITE_AFF_NUMERIC */ " NUM",
2134 /* SQLITE_AFF_INTEGER */ " INT",
drh00d6b272022-12-15 20:03:082135 /* SQLITE_AFF_REAL */ " REAL",
2136 /* SQLITE_AFF_FLEXNUM */ " NUM",
drhc4a64fa2009-05-11 20:53:282137 };
drhc4a64fa2009-05-11 20:53:282138 const char *zType;
2139
drhef86b942025-02-17 17:33:142140 len = sqlite3Strlen30(zSep);
2141 assert( k+len<n );
2142 memcpy(&zStmt[k], zSep, len);
2143 k += len;
drh969fa7c2002-02-18 18:30:322144 zSep = zSep2;
drhcf9d36d2021-08-02 18:03:432145 identPut(zStmt, &k, pCol->zCnName);
drhef86b942025-02-17 17:33:142146 assert( k<n );
drh05883a32015-06-02 15:32:082147 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
2148 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
2149 testcase( pCol->affinity==SQLITE_AFF_BLOB );
drh7ea31cc2014-09-18 14:36:002150 testcase( pCol->affinity==SQLITE_AFF_TEXT );
drhc4a64fa2009-05-11 20:53:282151 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
2152 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
2153 testcase( pCol->affinity==SQLITE_AFF_REAL );
drh00d6b272022-12-15 20:03:082154 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
larrybrbc917382023-06-07 08:40:312155
drh05883a32015-06-02 15:32:082156 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
drhc4a64fa2009-05-11 20:53:282157 len = sqlite3Strlen30(zType);
drh00d6b272022-12-15 20:03:082158 assert( pCol->affinity==SQLITE_AFF_BLOB
2159 || pCol->affinity==SQLITE_AFF_FLEXNUM
drhfdaac672013-10-04 15:30:212160 || pCol->affinity==sqlite3AffinityType(zType, 0) );
drhef86b942025-02-17 17:33:142161 assert( k+len<n );
drhc4a64fa2009-05-11 20:53:282162 memcpy(&zStmt[k], zType, len);
2163 k += len;
2164 assert( k<=n );
drh969fa7c2002-02-18 18:30:322165 }
drhef86b942025-02-17 17:33:142166 len = sqlite3Strlen30(zEnd);
2167 assert( k+len<n );
2168 memcpy(&zStmt[k], zEnd, len+1);
drh969fa7c2002-02-18 18:30:322169 return zStmt;
2170}
2171
2172/*
drh7f9c5db2013-10-23 00:32:582173** Resize an Index object to hold N columns total. Return SQLITE_OK
2174** on success and SQLITE_NOMEM on an OOM error.
2175*/
drhce250072025-02-21 17:03:222176static int resizeIndexObject(Parse *pParse, Index *pIdx, int N){
drh7f9c5db2013-10-23 00:32:582177 char *zExtra;
drhcc803b22025-02-21 20:35:372178 u64 nByte;
drhce250072025-02-21 17:03:222179 sqlite3 *db;
drh7f9c5db2013-10-23 00:32:582180 if( pIdx->nColumn>=N ) return SQLITE_OK;
drhce250072025-02-21 17:03:222181 db = pParse->db;
drhcc803b22025-02-21 20:35:372182 assert( N>0 );
2183 assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ );
2184 testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] );
drh7f9c5db2013-10-23 00:32:582185 assert( pIdx->isResized==0 );
drhcc803b22025-02-21 20:35:372186 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*(u64)N;
drh7f9c5db2013-10-23 00:32:582187 zExtra = sqlite3DbMallocZero(db, nByte);
mistachkinfad30392016-02-13 23:43:462188 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
drh7f9c5db2013-10-23 00:32:582189 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
drhf19aa5f2015-12-30 16:51:202190 pIdx->azColl = (const char**)zExtra;
drh7f9c5db2013-10-23 00:32:582191 zExtra += sizeof(char*)*N;
danb5a69232020-09-15 20:48:302192 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
2193 pIdx->aiRowLogEst = (LogEst*)zExtra;
2194 zExtra += sizeof(LogEst)*N;
drh7f9c5db2013-10-23 00:32:582195 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;
drhcc803b22025-02-21 20:35:372200 pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */
drh7f9c5db2013-10-23 00:32:582201 pIdx->isResized = 1;
2202 return SQLITE_OK;
2203}
2204
2205/*
drhfdaac672013-10-04 15:30:212206** Estimate the total row width for a table.
2207*/
drhe13e9f52013-10-05 19:18:002208static void estimateTableWidth(Table *pTab){
drhfdaac672013-10-04 15:30:212209 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++;
drhe13e9f52013-10-05 19:18:002216 pTab->szTabRow = sqlite3LogEst(wTable*4);
drhfdaac672013-10-04 15:30:212217}
2218
2219/*
drhe13e9f52013-10-05 19:18:002220** Estimate the average size of a row for an index.
drhfdaac672013-10-04 15:30:212221*/
drhe13e9f52013-10-05 19:18:002222static void estimateIndexWidth(Index *pIdx){
drhbbbdc832013-10-22 18:01:402223 unsigned wIndex = 0;
drhfdaac672013-10-04 15:30:212224 int i;
2225 const Column *aCol = pIdx->pTable->aCol;
2226 for(i=0; i<pIdx->nColumn; i++){
drhbbbdc832013-10-22 18:01:402227 i16 x = pIdx->aiColumn[i];
2228 assert( x<pIdx->pTable->nCol );
drh60fd5c32023-05-17 15:46:462229 wIndex += x<0 ? 1 : aCol[x].szEst;
drhfdaac672013-10-04 15:30:212230 }
drhe13e9f52013-10-05 19:18:002231 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
drhfdaac672013-10-04 15:30:212232}
2233
drhf78d0f42019-04-28 19:27:022234/* 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.
drh7f9c5db2013-10-23 00:32:582237*/
2238static int hasColumn(const i16 *aiCol, int nCol, int x){
drhf78d0f42019-04-28 19:27:022239 while( nCol-- > 0 ){
drhf78d0f42019-04-28 19:27:022240 if( x==*(aiCol++) ){
2241 return 1;
2242 }
2243 }
2244 return 0;
2245}
2246
2247/*
drhc19b63c2019-04-29 13:30:162248** 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.
drhf78d0f42019-04-28 19:27:022252**
drhc19b63c2019-04-29 13:30:162253** The first nKey entries of pIdx are guaranteed to be ordinary columns,
drhf78d0f42019-04-28 19:27:022254** 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*/
drhc19b63c2019-04-29 13:30:162260static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
drhf78d0f42019-04-28 19:27:022261 int i, j;
drhc19b63c2019-04-29 13:30:162262 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 );
drhf78d0f42019-04-28 19:27:022270 for(i=0; i<nKey; i++){
drhc19b63c2019-04-29 13:30:162271 assert( pIdx->aiColumn[i]>=0 || j>=0 );
larrybrbc917382023-06-07 08:40:312272 if( pIdx->aiColumn[i]==j
drhc19b63c2019-04-29 13:30:162273 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
drhf78d0f42019-04-28 19:27:022274 ){
2275 return 1;
2276 }
2277 }
drh7f9c5db2013-10-23 00:32:582278 return 0;
2279}
2280
drh1fe3ac72018-06-09 01:12:082281/* Recompute the colNotIdxed field of the Index.
2282**
2283** colNotIdxed is a bitmask that has a 0 bit representing each indexed
drh5723c652022-10-22 13:49:352284** 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
drh1fe3ac72018-06-09 01:12:082286** high-order bit of colNotIdxed is always 1. All unindexed columns
2287** of the table have a 1.
2288**
drhc7476732019-10-24 20:29:252289** 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**
drh1fe3ac72018-06-09 01:12:082297** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
2298** to determine if the index is covering index.
2299*/
drh00eee7a2023-10-06 12:55:532300static void recomputeColumnsNotIndexed(Index *pIdx){
drh1fe3ac72018-06-09 01:12:082301 Bitmask m = 0;
2302 int j;
drhc7476732019-10-24 20:29:252303 Table *pTab = pIdx->pTable;
drh1fe3ac72018-06-09 01:12:082304 for(j=pIdx->nColumn-1; j>=0; j--){
2305 int x = pIdx->aiColumn[j];
drhc7476732019-10-24 20:29:252306 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
drh1fe3ac72018-06-09 01:12:082307 testcase( x==BMS-1 );
2308 testcase( x==BMS-2 );
2309 if( x<BMS-1 ) m |= MASKBIT(x);
2310 }
2311 }
2312 pIdx->colNotIdxed = ~m;
drh5723c652022-10-22 13:49:352313 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
drh1fe3ac72018-06-09 01:12:082314}
2315
drh7f9c5db2013-10-23 00:32:582316/*
drhc6bd4e42013-11-02 14:37:182317** 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:
drh7f9c5db2013-10-23 00:32:582322**
drh62340f82016-05-31 21:18:152323** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
larrybrbc917382023-06-07 08:40:312324** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
drh0f3f7662017-08-18 14:34:282325** into BTREE_BLOBKEY.
drh1e32bed2020-06-19 13:33:532326** (3) Bypass the creation of the sqlite_schema table entry
peter.d.reid60ec9142014-09-06 16:39:462327** for the PRIMARY KEY as the primary key index is now
drh1e32bed2020-06-19 13:33:532328** identified by the sqlite_schema table entry of the table itself.
drh62340f82016-05-31 21:18:152329** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
drhc6bd4e42013-11-02 14:37:182330** schema to the rootpage from the main table.
drhc6bd4e42013-11-02 14:37:182331** (5) Add all table columns to the PRIMARY KEY Index object
2332** so that the PRIMARY KEY is a covering index. The surplus
drha485ad12017-08-02 22:43:142333** columns are part of KeyInfo.nAllField and are not used for
drhc6bd4e42013-11-02 14:37:182334** sorting or lookup or uniqueness checks.
2335** (6) Replace the rowid tail on all automatically generated UNIQUE
2336** indices with the PRIMARY KEY columns.
drh62340f82016-05-31 21:18:152337**
2338** For virtual tables, only (1) is performed.
drh7f9c5db2013-10-23 00:32:582339*/
2340static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
2341 Index *pIdx;
2342 Index *pPk;
2343 int nPk;
dan1ff94072019-07-17 09:18:062344 int nExtra;
drh7f9c5db2013-10-23 00:32:582345 int i, j;
2346 sqlite3 *db = pParse->db;
drhc6bd4e42013-11-02 14:37:182347 Vdbe *v = pParse->pVdbe;
drh7f9c5db2013-10-23 00:32:582348
drh62340f82016-05-31 21:18:152349 /* 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++){
drhfd46ec62021-08-18 22:26:512353 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
2354 && (pTab->aCol[i].notNull==OE_None)
2355 ){
drh62340f82016-05-31 21:18:152356 pTab->aCol[i].notNull = OE_Abort;
2357 }
2358 }
drhcbda9c72019-10-26 17:08:062359 pTab->tabFlags |= TF_HasNotNull;
drh62340f82016-05-31 21:18:152360 }
2361
drh0f3f7662017-08-18 14:34:282362 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
2363 ** into BTREE_BLOBKEY.
drh7f9c5db2013-10-23 00:32:582364 */
drh381bdac2021-02-04 17:29:042365 assert( !pParse->bReturning );
drh7fd936e2025-02-07 15:49:212366 if( pParse->u1.cr.addrCrTab ){
drhc6bd4e42013-11-02 14:37:182367 assert( v );
drh7fd936e2025-02-07 15:49:212368 sqlite3VdbeChangeP3(v, pParse->u1.cr.addrCrTab, BTREE_BLOBKEY);
drhc6bd4e42013-11-02 14:37:182369 }
2370
drh7f9c5db2013-10-23 00:32:582371 /* Locate the PRIMARY KEY index. Or, if this table was originally
larrybrbc917382023-06-07 08:40:312372 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
drh7f9c5db2013-10-23 00:32:582373 */
2374 if( pTab->iPKey>=0 ){
2375 ExprList *pList;
drh108aa002015-08-24 20:21:202376 Token ipkToken;
drhcf9d36d2021-08-02 18:03:432377 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
larrybrbc917382023-06-07 08:40:312378 pList = sqlite3ExprListAppend(pParse, 0,
drh108aa002015-08-24 20:21:202379 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
drh1bb89e92021-04-19 18:03:522380 if( pList==0 ){
2381 pTab->tabFlags &= ~TF_WithoutRowid;
2382 return;
2383 }
danf9b0c452019-05-06 16:15:282384 if( IN_RENAME_OBJECT ){
2385 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
2386 }
drhd88fd532022-05-02 20:49:302387 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
drh7f9c5db2013-10-23 00:32:582388 assert( pParse->pNewTable==pTab );
danf9b0c452019-05-06 16:15:282389 pTab->iPKey = -1;
drh62340f82016-05-31 21:18:152390 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
2391 SQLITE_IDXTYPE_PRIMARYKEY);
drh0c7d3d32022-01-24 16:47:122392 if( pParse->nErr ){
drh3bb9d752021-04-13 13:48:312393 pTab->tabFlags &= ~TF_WithoutRowid;
2394 return;
2395 }
drh0c7d3d32022-01-24 16:47:122396 assert( db->mallocFailed==0 );
drh62340f82016-05-31 21:18:152397 pPk = sqlite3PrimaryKeyIndex(pTab);
dan1ff94072019-07-17 09:18:062398 assert( pPk->nKeyCol==1 );
drh44156282013-10-23 22:23:032399 }else{
2400 pPk = sqlite3PrimaryKeyIndex(pTab);
drhf0c48b12019-02-11 01:58:342401 assert( pPk!=0 );
danc5b73582015-05-26 11:53:142402
drhe385d882014-12-28 22:10:512403 /*
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++){
drhf78d0f42019-04-28 19:27:022409 if( isDupColumn(pPk, j, pPk, i) ){
drhe385d882014-12-28 22:10:512410 pPk->nColumn--;
2411 }else{
drhf78d0f42019-04-28 19:27:022412 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
dan1ff94072019-07-17 09:18:062413 pPk->azColl[j] = pPk->azColl[i];
2414 pPk->aSortOrder[j] = pPk->aSortOrder[i];
drhe385d882014-12-28 22:10:512415 pPk->aiColumn[j++] = pPk->aiColumn[i];
2416 }
2417 }
2418 pPk->nKeyCol = j;
drh7f9c5db2013-10-23 00:32:582419 }
drh7f9c5db2013-10-23 00:32:582420 assert( pPk!=0 );
drh62340f82016-05-31 21:18:152421 pPk->isCovering = 1;
2422 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
dan1ff94072019-07-17 09:18:062423 nPk = pPk->nColumn = pPk->nKeyCol;
drh7f9c5db2013-10-23 00:32:582424
drh1e32bed2020-06-19 13:33:532425 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_schema
drhdf949662017-07-30 18:40:522426 ** 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 );
drhabc38152020-07-22 13:38:042431 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
drhdf949662017-07-30 18:40:522432 }
2433
drhc6bd4e42013-11-02 14:37:182434 /* The root page of the PRIMARY KEY is the table root page */
2435 pPk->tnum = pTab->tnum;
2436
drh7f9c5db2013-10-23 00:32:582437 /* 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;
drh48dd1d82014-05-27 18:18:582442 if( IsPrimaryKeyIndex(pIdx) ) continue;
drh7f9c5db2013-10-23 00:32:582443 for(i=n=0; i<nPk; i++){
drhf78d0f42019-04-28 19:27:022444 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
2445 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
2446 n++;
2447 }
drh7f9c5db2013-10-23 00:32:582448 }
drh5a9a37b2013-11-05 17:30:042449 if( n==0 ){
2450 /* This index is a superset of the primary key */
2451 pIdx->nColumn = pIdx->nKeyCol;
2452 continue;
2453 }
drhce250072025-02-21 17:03:222454 if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return;
drh7f9c5db2013-10-23 00:32:582455 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
drhf78d0f42019-04-28 19:27:022456 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
2457 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
drh7f9c5db2013-10-23 00:32:582458 pIdx->aiColumn[j] = pPk->aiColumn[i];
2459 pIdx->azColl[j] = pPk->azColl[i];
drhbf9ff252019-05-14 00:43:132460 if( pPk->aSortOrder[i] ){
drh8a6f89c2025-04-10 10:18:072461 /* See ticket https://sqlite.org/src/info/bba7b69f9849b5bf */
drhbf9ff252019-05-14 00:43:132462 pIdx->bAscKeyBug = 1;
2463 }
drh7f9c5db2013-10-23 00:32:582464 j++;
2465 }
2466 }
drh00012df2013-11-05 01:59:072467 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
2468 assert( pIdx->nColumn>=j );
drh7f9c5db2013-10-23 00:32:582469 }
2470
2471 /* Add all table columns to the PRIMARY KEY index
2472 */
dan1ff94072019-07-17 09:18:062473 nExtra = 0;
2474 for(i=0; i<pTab->nCol; i++){
drh8e10d742019-10-18 17:42:472475 if( !hasColumn(pPk->aiColumn, nPk, i)
2476 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
drh7f9c5db2013-10-23 00:32:582477 }
drhce250072025-02-21 17:03:222478 if( resizeIndexObject(pParse, pPk, nPk+nExtra) ) return;
dan1ff94072019-07-17 09:18:062479 for(i=0, j=nPk; i<pTab->nCol; i++){
drh8e10d742019-10-18 17:42:472480 if( !hasColumn(pPk->aiColumn, j, i)
2481 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
2482 ){
dan1ff94072019-07-17 09:18:062483 assert( j<pPk->nColumn );
2484 pPk->aiColumn[j] = i;
2485 pPk->azColl[j] = sqlite3StrBINARY;
2486 j++;
2487 }
2488 }
2489 assert( pPk->nColumn==j );
drh8e10d742019-10-18 17:42:472490 assert( pTab->nNVCol<=j );
drh00eee7a2023-10-06 12:55:532491 recomputeColumnsNotIndexed(pPk);
drh7f9c5db2013-10-23 00:32:582492}
2493
drh3d863b52020-05-14 21:16:522494
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*/
2500int 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;
drhf38524d2021-08-02 16:41:572508 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
drh3d863b52020-05-14 21:16:522509 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
danf6e015f2018-11-28 08:02:282516#ifndef SQLITE_OMIT_VIRTUALTABLE
drhfdaac672013-10-04 15:30:212517/*
drhddfec002021-11-04 00:51:532518** 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*/
2523void 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;
drh62561b82021-11-06 10:59:272532 if( pMod->pModule->iVersion<3 ) return;
drhddfec002021-11-04 00:51:532533 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/*
drh84c501b2018-11-05 23:01:452553** 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*/
drh527cbd42019-11-16 14:15:192559int sqlite3ShadowTableName(sqlite3 *db, const char *zName){
drh84c501b2018-11-05 23:01:452560 char *zTail; /* Pointer to the last "_" in zName */
2561 Table *pTab; /* Table that zName is a shadow of */
drh84c501b2018-11-05 23:01:452562 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;
drh3d863b52020-05-14 21:16:522569 return sqlite3IsShadowTableOf(db, pTab, zName);
drh84c501b2018-11-05 23:01:452570}
danf6e015f2018-11-28 08:02:282571#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drh84c501b2018-11-05 23:01:452572
drh3d863b52020-05-14 21:16:522573
drhe7375bf2020-03-10 19:24:382574#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*/
2581static int markImmutableExprStep(Walker *pWalker, Expr *pExpr){
drh3547e492022-12-23 14:49:242582 (void)pWalker;
drhe7375bf2020-03-10 19:24:382583 ExprSetVVAProperty(pExpr, EP_Immutable);
2584 return WRC_Continue;
2585}
2586static 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
drh84c501b2018-11-05 23:01:452601/*
drh75897232000-05-29 14:26:002602** This routine is called to report the final ")" that terminates
2603** a CREATE TABLE statement.
2604**
drhf57b3392001-10-08 13:22:322605** The table structure that other action routines have been building
2606** is added to the internal hash tables, assuming no errors have
2607** occurred.
drh75897232000-05-29 14:26:002608**
drh067b92b2020-06-19 15:24:122609** An entry for the table is made in the schema table on disk, unless
drh1d85d932004-02-14 23:05:522610** this is a temporary table or db->init.busy==1. When db->init.busy==1
drh1e32bed2020-06-19 13:33:532611** it means we are reading the sqlite_schema table because we just
2612** connected to the database or because the sqlite_schema table has
drhddba9e52005-03-19 01:41:212613** recently changed, so the entry for this table already exists in
drh1e32bed2020-06-19 13:33:532614** the sqlite_schema table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:322615**
2616** If the pSelect argument is not NULL, it means that this routine
larrybrbc917382023-06-07 08:40:312617** was called to create a table generated from a
drh969fa7c2002-02-18 18:30:322618** "CREATE TABLE ... AS SELECT ..." statement. The column names of
2619** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:002620*/
danielk197719a8e7e2005-03-17 05:03:382621void sqlite3EndTable(
2622 Parse *pParse, /* Parse context */
2623 Token *pCons, /* The ',' token after the last column defn. */
drh5969da42013-10-21 02:14:452624 Token *pEnd, /* The ')' before options in the CREATE TABLE */
drh44183f82021-08-18 13:13:582625 u32 tabOpts, /* Extra table options. Usually 0. */
danielk197719a8e7e2005-03-17 05:03:382626 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
2627){
drhfdaac672013-10-04 15:30:212628 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 */
drh75897232000-05-29 14:26:002632
drh027616d2015-08-08 22:47:472633 if( pEnd==0 && pSelect==0 ){
drh5969da42013-10-21 02:14:452634 return;
danielk1977261919c2005-12-06 12:52:592635 }
drh28037572000-08-02 13:47:412636 p = pParse->pNewTable;
drh5969da42013-10-21 02:14:452637 if( p==0 ) return;
drh75897232000-05-29 14:26:002638
drh527cbd42019-11-16 14:15:192639 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
drh84c501b2018-11-05 23:01:452640 p->tabFlags |= TF_Shadow;
2641 }
2642
drhc6bd4e42013-11-02 14:37:182643 /* If the db->init.busy is 1 it means we are reading the SQL off the
drh1e32bed2020-06-19 13:33:532644 ** "sqlite_schema" or "sqlite_temp_schema" table on the disk.
drhc6bd4e42013-11-02 14:37:182645 ** 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.)
drh055f2982016-01-15 15:06:412648 **
drh1e32bed2020-06-19 13:33:532649 ** If the root page number is 1, that means this is the sqlite_schema
drh055f2982016-01-15 15:06:412650 ** table itself. So mark it read-only.
drhc6bd4e42013-11-02 14:37:182651 */
2652 if( db->init.busy ){
drh54e3f942021-10-11 15:54:052653 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
drh1e9c47b2018-03-16 20:15:582654 sqlite3ErrorMsg(pParse, "");
2655 return;
2656 }
drhc6bd4e42013-11-02 14:37:182657 p->tnum = db->init.newTnum;
drh055f2982016-01-15 15:06:412658 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
drhc6bd4e42013-11-02 14:37:182659 }
2660
drh71c770f2021-08-19 16:29:332661 /* 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 */
drh44183f82021-08-18 13:13:582670 if( tabOpts & TF_Strict ){
2671 int ii;
2672 p->tabFlags |= TF_Strict;
2673 for(ii=0; ii<p->nCol; ii++){
drhab165782021-08-19 00:24:432674 Column *pCol = &p->aCol[ii];
drhb9fd0102021-08-23 10:28:022675 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 }
drh44183f82021-08-18 13:13:582685 return;
drhb9fd0102021-08-23 10:28:022686 }else if( pCol->eCType==COLTYPE_ANY ){
2687 pCol->affinity = SQLITE_AFF_BLOB;
drh44183f82021-08-18 13:13:582688 }
drhab165782021-08-19 00:24:432689 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
2690 && p->iPKey!=ii
2691 && pCol->notNull == OE_None
2692 ){
drhab165782021-08-19 00:24:432693 pCol->notNull = OE_Abort;
2694 p->tabFlags |= TF_HasNotNull;
2695 }
larrybrbc917382023-06-07 08:40:312696 }
drh44183f82021-08-18 13:13:582697 }
2698
drh3cbd2b72019-02-19 13:51:582699 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
drhc6bd4e42013-11-02 14:37:182704 /* Special processing for WITHOUT ROWID Tables */
drh5969da42013-10-21 02:14:452705 if( tabOpts & TF_WithoutRowid ){
drhd2fe3352013-11-09 18:15:352706 if( (p->tabFlags & TF_Autoincrement) ){
2707 sqlite3ErrorMsg(pParse,
2708 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
2709 return;
2710 }
drh5969da42013-10-21 02:14:452711 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
drhd2fe3352013-11-09 18:15:352712 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
drh8e10d742019-10-18 17:42:472713 return;
drh81eba732013-10-19 23:31:562714 }
drh8e10d742019-10-18 17:42:472715 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
drhf95909c2019-10-18 18:33:252716 convertToWithoutRowidTable(pParse, p);
drh81eba732013-10-19 23:31:562717 }
drhb9bb7c12006-06-11 23:41:552718 iDb = sqlite3SchemaToIndex(db, p->pSchema);
danielk1977da184232006-01-05 11:34:322719
drhffe07b22005-11-03 00:41:172720#ifndef SQLITE_OMIT_CHECK
2721 /* Resolve names in all CHECK constraint expressions.
2722 */
2723 if( p->pCheck ){
drh3780be12013-07-31 19:05:222724 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
drh9524a7e2019-12-22 18:06:492725 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;
drhe7375bf2020-03-10 19:24:382730 }else{
2731 markExprListImmutable(p->pCheck);
drh9524a7e2019-12-22 18:06:492732 }
drhffe07b22005-11-03 00:41:172733 }
2734#endif /* !defined(SQLITE_OMIT_CHECK) */
drh81f7b372019-10-16 12:18:592735#ifndef SQLITE_OMIT_GENERATED_COLUMNS
drh427b96a2019-10-22 13:01:242736 if( p->tabFlags & TF_HasGenerated ){
drhf4658b62019-10-29 03:39:172737 int ii, nNG = 0;
drh427b96a2019-10-22 13:01:242738 testcase( p->tabFlags & TF_HasVirtual );
2739 testcase( p->tabFlags & TF_HasStored );
drh81f7b372019-10-16 12:18:592740 for(ii=0; ii<p->nCol; ii++){
drh0b0b3a92019-10-17 18:35:572741 u32 colFlags = p->aCol[ii].colFlags;
drh427b96a2019-10-22 13:01:242742 if( (colFlags & COLFLAG_GENERATED)!=0 ){
drh79cf2b72021-07-31 20:30:412743 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
drh427b96a2019-10-22 13:01:242744 testcase( colFlags & COLFLAG_VIRTUAL );
2745 testcase( colFlags & COLFLAG_STORED );
drh7e3f1352019-12-14 19:55:312746 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
drh2d58b7f2020-01-17 23:27:412751 ** illegal in a schema and will lead to errors or heap corruption
2752 ** when the database connection closes. */
larrybrbc917382023-06-07 08:40:312753 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
drh79cf2b72021-07-31 20:30:412754 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
drh7e3f1352019-12-14 19:55:312755 }
drhf4658b62019-10-29 03:39:172756 }else{
2757 nNG++;
drh81f7b372019-10-16 12:18:592758 }
drh2c40a3e2019-10-29 01:26:242759 }
drhf4658b62019-10-29 03:39:172760 if( nNG==0 ){
2761 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
drh2c40a3e2019-10-29 01:26:242762 return;
drh81f7b372019-10-16 12:18:592763 }
2764 }
2765#endif
drhffe07b22005-11-03 00:41:172766
drhe13e9f52013-10-05 19:18:002767 /* Estimate the average row size for the table and for all implied indices */
2768 estimateTableWidth(p);
drhfdaac672013-10-04 15:30:212769 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
drhe13e9f52013-10-05 19:18:002770 estimateIndexWidth(pIdx);
drhfdaac672013-10-04 15:30:212771 }
2772
drhe3c41372001-09-17 20:25:582773 /* If not initializing, then create a record for the new table
drh346a70c2020-06-15 20:27:352774 ** in the schema table of the database.
drhf57b3392001-10-08 13:22:322775 **
drhe0bc4042002-06-25 01:09:112776 ** If this is a TEMPORARY table, write the entry into the auxiliary
2777 ** file instead of into the main database file.
drh75897232000-05-29 14:26:002778 */
drh1d85d932004-02-14 23:05:522779 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:002780 int n;
drhd8bc7082000-06-07 23:51:502781 Vdbe *v;
drh4794f732004-11-05 17:17:502782 char *zType; /* "view" or "table" */
2783 char *zType2; /* "VIEW" or "TABLE" */
2784 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
drh75897232000-05-29 14:26:002785
danielk19774adee202004-05-08 08:23:192786 v = sqlite3GetVdbe(pParse);
drh5969da42013-10-21 02:14:452787 if( NEVER(v==0) ) return;
danielk1977517eb642004-06-07 10:00:312788
drh66a51672008-01-03 00:01:232789 sqlite3VdbeAddOp1(v, OP_Close, 0);
danielk1977e6efa742004-11-10 11:55:102790
larrybrbc917382023-06-07 08:40:312791 /*
drh0fa991b2009-03-21 16:19:262792 ** Initialize zType for the new view or table.
drh4794f732004-11-05 17:17:502793 */
drhf38524d2021-08-02 16:41:572794 if( IsOrdinaryTable(p) ){
drh4ff6dfa2002-03-03 23:06:002795 /* A regular table */
drh4794f732004-11-05 17:17:502796 zType = "table";
2797 zType2 = "TABLE";
danielk1977576ec6b2005-01-21 11:55:252798#ifndef SQLITE_OMIT_VIEW
drh4ff6dfa2002-03-03 23:06:002799 }else{
2800 /* A view */
drh4794f732004-11-05 17:17:502801 zType = "view";
2802 zType2 = "VIEW";
danielk1977576ec6b2005-01-21 11:55:252803#endif
drh4ff6dfa2002-03-03 23:06:002804 }
danielk1977517eb642004-06-07 10:00:312805
danielk1977517eb642004-06-07 10:00:312806 /* 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
drh7fd936e2025-02-07 15:49:212808 ** new table is in register pParse->u1.cr.regRoot.
danielk1977517eb642004-06-07 10:00:312809 **
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.
danielk1977c00da102006-01-07 13:21:042813 **
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.
danielk1977517eb642004-06-07 10:00:312818 */
2819 if( pSelect ){
drh92632202015-05-20 17:18:292820 SelectDest dest; /* Where the SELECT should store results */
drh9df25c42015-05-20 15:51:092821 int regYield; /* Register holding co-routine entry-point */
2822 int addrTop; /* Top of the co-routine */
drh92632202015-05-20 17:18:292823 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 */
drhd9eee782024-03-23 15:17:382827 int iCsr; /* Write cursor on the new table */
drh1013c932008-01-06 00:25:212828
drhfde30432022-03-10 21:04:492829 if( IN_SPECIAL_PARSE ){
2830 pParse->rc = SQLITE_ERROR;
2831 pParse->nErr++;
2832 return;
2833 }
drhd9eee782024-03-23 15:17:382834 iCsr = pParse->nTab++;
drh9df25c42015-05-20 15:51:092835 regYield = ++pParse->nMem;
drh92632202015-05-20 17:18:292836 regRec = ++pParse->nMem;
2837 regRowid = ++pParse->nMem;
drh0dd5cda2015-06-16 16:39:012838 sqlite3MayAbort(pParse);
drh7fd936e2025-02-07 15:49:212839 assert( pParse->isCreate );
2840 sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->u1.cr.regRoot, iDb);
dan428c2182012-08-06 18:50:112841 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
drh9df25c42015-05-20 15:51:092842 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
2843 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
drh512795d2017-12-24 18:56:282844 if( pParse->nErr ) return;
drh81506b82019-08-05 19:32:062845 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
drh92632202015-05-20 17:18:292846 if( pSelTab==0 ) return;
2847 assert( p->aCol==0 );
drhf5f19152019-10-21 01:04:112848 p->nCol = p->nNVCol = pSelTab->nCol;
drh92632202015-05-20 17:18:292849 p->aCol = pSelTab->aCol;
2850 pSelTab->nCol = 0;
2851 pSelTab->aCol = 0;
2852 sqlite3DeleteTable(db, pSelTab);
drh755b0fd2017-12-23 12:33:402853 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
2854 sqlite3Select(pParse, pSelect, &dest);
drh5060a672017-12-25 13:43:542855 if( pParse->nErr ) return;
drh755b0fd2017-12-23 12:33:402856 sqlite3VdbeEndCoroutine(v, regYield);
2857 sqlite3VdbeJumpHere(v, addrTop - 1);
drh92632202015-05-20 17:18:292858 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);
drhd9eee782024-03-23 15:17:382862 sqlite3VdbeAddOp2(v, OP_NewRowid, iCsr, regRowid);
2863 sqlite3VdbeAddOp3(v, OP_Insert, iCsr, regRec, regRowid);
drh076e85f2015-09-03 13:46:122864 sqlite3VdbeGoto(v, addrInsLoop);
drh92632202015-05-20 17:18:292865 sqlite3VdbeJumpHere(v, addrInsLoop);
drhd9eee782024-03-23 15:17:382866 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
danielk1977517eb642004-06-07 10:00:312867 }
drh4794f732004-11-05 17:17:502868
drh4794f732004-11-05 17:17:502869 /* Compute the complete text of the CREATE statement */
2870 if( pSelect ){
drh1d34fde2009-02-03 15:50:332871 zStmt = createTableStmt(db, p);
drh4794f732004-11-05 17:17:502872 }else{
drh8ea30bf2013-10-22 01:18:172873 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
2874 n = (int)(pEnd2->z - pParse->sNameToken.z);
2875 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
larrybrbc917382023-06-07 08:40:312876 zStmt = sqlite3MPrintf(db,
danielk19771e536952007-08-16 10:09:012877 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
2878 );
drh4794f732004-11-05 17:17:502879 }
2880
larrybrbc917382023-06-07 08:40:312881 /* A slot for the record has already been allocated in the
drh346a70c2020-06-15 20:27:352882 ** schema table. We just need to update that slot with all
drh0fa991b2009-03-21 16:19:262883 ** the information we've collected.
drh4794f732004-11-05 17:17:502884 */
drh7fd936e2025-02-07 15:49:212885 assert( pParse->isCreate );
drh4794f732004-11-05 17:17:502886 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:202887 "UPDATE %Q." LEGACY_SCHEMA_TABLE
drh346a70c2020-06-15 20:27:352888 " SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q"
2889 " WHERE rowid=#%d",
2890 db->aDb[iDb].zDbSName,
drh4794f732004-11-05 17:17:502891 zType,
2892 p->zName,
2893 p->zName,
drh7fd936e2025-02-07 15:49:212894 pParse->u1.cr.regRoot,
drhb7654112008-01-12 12:48:072895 zStmt,
drh7fd936e2025-02-07 15:49:212896 pParse->u1.cr.regRowid
drh4794f732004-11-05 17:17:502897 );
drh633e6d52008-07-28 19:34:532898 sqlite3DbFree(db, zStmt);
drh9cbf3422008-01-17 16:22:132899 sqlite3ChangeCookie(pParse, iDb);
drh2958a4e2004-11-12 03:56:152900
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 */
dan755ed412021-04-07 12:02:302905 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
danielk1977da184232006-01-05 11:34:322906 Db *pDb = &db->aDb[iDb];
drh21206082011-04-04 18:22:022907 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:322908 if( pDb->pSchema->pSeqTab==0 ){
drh2958a4e2004-11-12 03:56:152909 sqlite3NestedParse(pParse,
drhf3388142004-11-13 03:48:062910 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
drh69c33822016-08-18 14:33:112911 pDb->zDbSName
drh2958a4e2004-11-12 03:56:152912 );
2913 }
2914 }
2915#endif
drh4794f732004-11-05 17:17:502916
2917 /* Reparse everything to update our internal data structures */
drh5d9c9da2011-06-03 20:11:172918 sqlite3VdbeAddParseSchemaOp(v, iDb,
dan6a5a13d2021-02-17 20:08:222919 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
drhaf527232023-10-13 13:49:462920
drh9132b882023-10-13 22:19:232921 /* Test for cycles in generated columns and illegal expressions
2922 ** in CHECK constraints and in DEFAULT clauses. */
drhaf527232023-10-13 13:49:462923 if( p->tabFlags & TF_HasGenerated ){
drh42eb6a92024-02-17 16:39:522924 sqlite3VdbeAddOp4(v, OP_SqlExec, 0x0001, 0, 0,
drh9132b882023-10-13 22:19:232925 sqlite3MPrintf(db, "SELECT*FROM\"%w\".\"%w\"",
drhaf527232023-10-13 13:49:462926 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
2927 }
drh75897232000-05-29 14:26:002928 }
drh17e9e292003-02-01 13:53:282929
2930 /* Add the table to the in-memory representation of the database.
2931 */
drh8af73d42009-05-13 22:58:282932 if( db->init.busy ){
drh17e9e292003-02-01 13:53:282933 Table *pOld;
danielk1977e501b892006-01-09 06:29:472934 Schema *pSchema = p->pSchema;
drh21206082011-04-04 18:22:022935 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh1bb89e92021-04-19 18:03:522936 assert( HasRowid(p) || p->iPKey<0 );
drhacbcb7e2014-08-21 20:26:372937 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
drh17e9e292003-02-01 13:53:282938 if( pOld ){
2939 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh4a642b62016-02-05 01:55:272940 sqlite3OomFault(db);
drh5969da42013-10-21 02:14:452941 return;
drh17e9e292003-02-01 13:53:282942 }
drh17e9e292003-02-01 13:53:282943 pParse->pNewTable = 0;
drh8257aa82017-07-26 19:59:132944 db->mDbFlags |= DBFLAG_SchemaChange;
dand4b64692021-04-06 16:16:152945
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
dan578277c2021-02-19 18:39:322956 }
danielk197719a8e7e2005-03-17 05:03:382957
2958#ifndef SQLITE_OMIT_ALTERTABLE
drhf38524d2021-08-02 16:41:572959 if( !pSelect && IsOrdinaryTable(p) ){
dan578277c2021-02-19 18:39:322960 assert( pCons && pEnd );
2961 if( pCons->z==0 ){
2962 pCons = pEnd;
danielk197719a8e7e2005-03-17 05:03:382963 }
drhf38524d2021-08-02 16:41:572964 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
drh17e9e292003-02-01 13:53:282965 }
dan578277c2021-02-19 18:39:322966#endif
drh75897232000-05-29 14:26:002967}
2968
drhb7f91642004-10-31 02:22:472969#ifndef SQLITE_OMIT_VIEW
drh75897232000-05-29 14:26:002970/*
drha76b5df2002-02-23 02:32:102971** The parser calls this routine in order to create a new VIEW
2972*/
danielk19774adee202004-05-08 08:23:192973void sqlite3CreateView(
drha76b5df2002-02-23 02:32:102974 Parse *pParse, /* The parsing context */
2975 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:302976 Token *pName1, /* The token that holds the name of the view */
2977 Token *pName2, /* The token that holds the name of the view */
drh8981b902015-08-24 17:42:492978 ExprList *pCNames, /* Optional list of view column names */
drh6276c1c2002-07-08 22:03:322979 Select *pSelect, /* A SELECT statement that will become the new view */
drhfdd48a72006-09-11 23:45:482980 int isTemp, /* TRUE for a TEMPORARY view */
2981 int noErr /* Suppress error messages if VIEW already exists */
drha76b5df2002-02-23 02:32:102982){
drha76b5df2002-02-23 02:32:102983 Table *p;
drh4b59ab52002-08-24 18:24:512984 int n;
drhb7916a72009-05-27 10:31:292985 const char *z;
drh4b59ab52002-08-24 18:24:512986 Token sEnd;
drhf26e09c2003-05-31 16:21:122987 DbFixer sFix;
drh88caeac2011-08-24 15:12:082988 Token *pName = 0;
danielk1977da184232006-01-05 11:34:322989 int iDb;
drh17435752007-08-16 04:30:382990 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:102991
drh7c3d64f2005-06-06 15:32:082992 if( pParse->nVar>0 ){
2993 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
drh32498f12015-09-26 11:15:442994 goto create_view_fail;
drh7c3d64f2005-06-06 15:32:082995 }
drhfdd48a72006-09-11 23:45:482996 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
drha76b5df2002-02-23 02:32:102997 p = pParse->pNewTable;
drh8981b902015-08-24 17:42:492998 if( p==0 || pParse->nErr ) goto create_view_fail;
drh6e5020e2021-04-07 15:45:012999
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
drh4b42b522024-03-19 13:31:543004 ** depend upon the old buggy behavior. The ability can also be toggled
drh7128c782024-03-20 10:40:253005 ** using sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW,...) */
drh4b42b522024-03-19 13:31:543006#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 */
drh6e5020e2021-04-07 15:45:013010#endif
3011
danielk1977ef2cb632004-05-29 02:37:193012 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
drh17435752007-08-16 04:30:383013 iDb = sqlite3SchemaToIndex(db, p->pSchema);
drhd100f692013-10-03 15:39:443014 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
drh8981b902015-08-24 17:42:493015 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
drh174b6192002-12-03 02:22:523016
drh4b59ab52002-08-24 18:24:513017 /* 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
danielk197724b03fd2004-05-10 10:34:343020 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:513021 */
dan38096962019-12-09 08:13:433022 pSelect->selFlags |= SF_View;
danc9461ec2018-08-29 21:00:163023 if( IN_RENAME_OBJECT ){
drhf38524d2021-08-02 16:41:573024 p->u.view.pSelect = pSelect;
dan987db762018-08-14 20:18:503025 pSelect = 0;
3026 }else{
drhf38524d2021-08-02 16:41:573027 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
dan987db762018-08-14 20:18:503028 }
drh8981b902015-08-24 17:42:493029 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
drhf38524d2021-08-02 16:41:573030 p->eTabType = TABTYP_VIEW;
drh8981b902015-08-24 17:42:493031 if( db->mallocFailed ) goto create_view_fail;
drh4b59ab52002-08-24 18:24:513032
3033 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
3034 ** the end.
3035 */
drha76b5df2002-02-23 02:32:103036 sEnd = pParse->sLastToken;
drh6116ee42018-01-10 00:40:063037 assert( sEnd.z[0]!=0 || sEnd.n==0 );
drh8981b902015-08-24 17:42:493038 if( sEnd.z[0]!=';' ){
drha76b5df2002-02-23 02:32:103039 sEnd.z += sEnd.n;
3040 }
3041 sEnd.n = 0;
drh1bd10f82008-12-10 21:19:563042 n = (int)(sEnd.z - pBegin->z);
drh8981b902015-08-24 17:42:493043 assert( n>0 );
drhb7916a72009-05-27 10:31:293044 z = pBegin->z;
drh8981b902015-08-24 17:42:493045 while( sqlite3Isspace(z[n-1]) ){ n--; }
drh4ff6dfa2002-03-03 23:06:003046 sEnd.z = &z[n-1];
3047 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:513048
drh346a70c2020-06-15 20:27:353049 /* Use sqlite3EndTable() to add the view to the schema table */
drh5969da42013-10-21 02:14:453050 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
drh8981b902015-08-24 17:42:493051
3052create_view_fail:
3053 sqlite3SelectDelete(db, pSelect);
dane8ab40d2018-09-12 08:51:483054 if( IN_RENAME_OBJECT ){
3055 sqlite3RenameExprlistUnmap(pParse, pCNames);
3056 }
drh8981b902015-08-24 17:42:493057 sqlite3ExprListDelete(db, pCNames);
drha76b5df2002-02-23 02:32:103058 return;
drh417be792002-03-03 18:59:403059}
drhb7f91642004-10-31 02:22:473060#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:103061
danielk1977fe3fcbe22006-06-12 12:08:453062#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
drh417be792002-03-03 18:59:403063/*
3064** The Table structure pTable is really a VIEW. Fill in the names of
drh4ed784d2024-05-25 23:13:153065** 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.
drh417be792002-03-03 18:59:403068*/
drhef69d2b2022-07-25 22:31:043069static SQLITE_NOINLINE int viewGetColumnNames(Parse *pParse, Table *pTable){
drh9b3187e2005-01-18 14:45:473070 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 */
drh17435752007-08-16 04:30:383073 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
drh424981d2018-03-28 15:56:553074#ifndef SQLITE_OMIT_VIRTUALTABLE
drhdc6b41e2017-08-17 02:26:353075 int rc;
3076#endif
drha0daa752016-09-16 11:53:103077#ifndef SQLITE_OMIT_AUTHORIZATION
drh32c6a482014-09-11 13:44:523078 sqlite3_xauth xAuth; /* Saved xAuth pointer */
drha0daa752016-09-16 11:53:103079#endif
drh417be792002-03-03 18:59:403080
3081 assert( pTable );
3082
danielk1977fe3fcbe22006-06-12 12:08:453083#ifndef SQLITE_OMIT_VIRTUALTABLE
drhddfec002021-11-04 00:51:533084 if( IsVirtual(pTable) ){
3085 db->nSchemaLock++;
3086 rc = sqlite3VtabCallConnect(pParse, pTable);
3087 db->nSchemaLock--;
3088 return rc;
danielk1977fe3fcbe22006-06-12 12:08:453089 }
danielk1977fe3fcbe22006-06-12 12:08:453090#endif
3091
3092#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:403093 /* A positive nCol means the columns names for this view are
drh509a6302022-07-25 23:01:413094 ** already known. This routine is not called unless either the
3095 ** table is virtual or nCol is zero.
drh417be792002-03-03 18:59:403096 */
drh509a6302022-07-25 23:01:413097 assert( pTable->nCol<=0 );
drh417be792002-03-03 18:59:403098
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;
drh3b167c72002-06-28 12:18:473105 **
drh768578e2009-05-12 00:40:123106 ** 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:
larrybrbc917382023-06-07 08:40:313109 **
drh768578e2009-05-12 00:40:123110 ** CREATE TABLE main.ex1(a);
3111 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
3112 ** SELECT * FROM temp.ex1;
drh417be792002-03-03 18:59:403113 */
3114 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:193115 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:403116 return 1;
3117 }
drh85c23c62005-08-20 03:03:043118 assert( pTable->nCol>=0 );
drh417be792002-03-03 18:59:403119
3120 /* If we get this far, it means we need to compute the table names.
drh9b3187e2005-01-18 14:45:473121 ** 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.
drh417be792002-03-03 18:59:403126 */
drhf38524d2021-08-02 16:41:573127 assert( IsView(pTable) );
3128 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
drhed06a132016-04-05 20:59:123129 if( pSel ){
dan02083372018-09-17 08:27:233130 u8 eParseMode = pParse->eParseMode;
drh7f417562022-04-25 14:49:483131 int nTab = pParse->nTab;
3132 int nSelect = pParse->nSelect;
dan02083372018-09-17 08:27:233133 pParse->eParseMode = PARSE_MODE_NORMAL;
drhed06a132016-04-05 20:59:123134 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
3135 pTable->nCol = -1;
drh31f69622019-10-05 14:39:363136 DisableLookaside;
danielk1977db2d2862007-10-15 07:08:443137#ifndef SQLITE_OMIT_AUTHORIZATION
drhed06a132016-04-05 20:59:123138 xAuth = db->xAuth;
3139 db->xAuth = 0;
drh96fb16e2019-08-06 14:37:243140 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
drhed06a132016-04-05 20:59:123141 db->xAuth = xAuth;
danielk1977db2d2862007-10-15 07:08:443142#else
drh96fb16e2019-08-06 14:37:243143 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
danielk1977db2d2862007-10-15 07:08:443144#endif
drh7f417562022-04-25 14:49:483145 pParse->nTab = nTab;
3146 pParse->nSelect = nSelect;
dan5d591022019-12-28 08:26:473147 if( pSelTab==0 ){
3148 pTable->nCol = 0;
3149 nErr++;
3150 }else if( pTable->pCheck ){
drhed06a132016-04-05 20:59:123151 /* 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 */
larrybrbc917382023-06-07 08:40:313157 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
drhed06a132016-04-05 20:59:123158 &pTable->nCol, &pTable->aCol);
drh0c7d3d32022-01-24 16:47:123159 if( pParse->nErr==0
drhed06a132016-04-05 20:59:123160 && pTable->nCol==pSel->pEList->nExpr
3161 ){
drh0c7d3d32022-01-24 16:47:123162 assert( db->mallocFailed==0 );
drh9e660872022-12-13 15:54:433163 sqlite3SubqueryColumnTypes(pParse, pTable, pSel, SQLITE_AFF_NONE);
drh8981b902015-08-24 17:42:493164 }
dan5d591022019-12-28 08:26:473165 }else{
drhed06a132016-04-05 20:59:123166 /* 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 );
drh03836612019-11-02 17:59:103170 pTable->nCol = pSelTab->nCol;
drhed06a132016-04-05 20:59:123171 pTable->aCol = pSelTab->aCol;
dan3dc864b2021-02-25 16:55:473172 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
drhed06a132016-04-05 20:59:123173 pSelTab->nCol = 0;
3174 pSelTab->aCol = 0;
3175 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
danielk1977261919c2005-12-06 12:52:593176 }
drh03836612019-11-02 17:59:103177 pTable->nNVCol = pTable->nCol;
drhe8da01c2016-05-07 12:15:343178 sqlite3DeleteTable(db, pSelTab);
drhed06a132016-04-05 20:59:123179 sqlite3SelectDelete(db, pSel);
drh31f69622019-10-05 14:39:363180 EnableLookaside;
dan02083372018-09-17 08:27:233181 pParse->eParseMode = eParseMode;
drhed06a132016-04-05 20:59:123182 } else {
3183 nErr++;
drh417be792002-03-03 18:59:403184 }
drh8981b902015-08-24 17:42:493185 pTable->pSchema->schemaFlags |= DB_UnresetViews;
dan77f3f402018-07-09 18:55:443186 if( db->mallocFailed ){
3187 sqlite3DeleteColumnNames(db, pTable);
dan77f3f402018-07-09 18:55:443188 }
drhb7f91642004-10-31 02:22:473189#endif /* SQLITE_OMIT_VIEW */
drh4ed784d2024-05-25 23:13:153190 return nErr + pParse->nErr;
danielk1977fe3fcbe22006-06-12 12:08:453191}
drhef69d2b2022-07-25 22:31:043192int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
3193 assert( pTable!=0 );
3194 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
3195 return viewGetColumnNames(pParse, pTable);
3196}
danielk1977fe3fcbe22006-06-12 12:08:453197#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
drh417be792002-03-03 18:59:403198
drhb7f91642004-10-31 02:22:473199#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:403200/*
drh8bf8dc92003-05-17 17:35:103201** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:403202*/
drh9bb575f2004-09-06 17:24:113203static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:403204 HashElem *i;
drh21206082011-04-04 18:22:023205 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
drh8bf8dc92003-05-17 17:35:103206 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
danielk1977da184232006-01-05 11:34:323207 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:403208 Table *pTab = sqliteHashData(i);
drhf38524d2021-08-02 16:41:573209 if( IsView(pTab) ){
drh51be3872015-08-19 02:32:253210 sqlite3DeleteColumnNames(db, pTab);
drh417be792002-03-03 18:59:403211 }
3212 }
drh8bf8dc92003-05-17 17:35:103213 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:103214}
drhb7f91642004-10-31 02:22:473215#else
3216# define sqliteViewResetAll(A,B)
3217#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:103218
drh75897232000-05-29 14:26:003219/*
danielk1977a0bf2652004-11-04 14:30:043220** 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.
drh6205d4a2006-03-24 03:36:263224**
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
larrybrbc917382023-06-07 08:40:313229** being moved. So we cannot stop searching after the first match
drh6205d4a2006-03-24 03:36:263230** 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.
danielk1977a0bf2652004-11-04 14:30:043235*/
3236#ifndef SQLITE_OMIT_AUTOVACUUM
drhabc38152020-07-22 13:38:043237void sqlite3RootPageMoved(sqlite3 *db, int iDb, Pgno iFrom, Pgno iTo){
danielk1977a0bf2652004-11-04 14:30:043238 HashElem *pElem;
danielk1977da184232006-01-05 11:34:323239 Hash *pHash;
drhcdf011d2011-04-04 21:25:283240 Db *pDb;
danielk1977da184232006-01-05 11:34:323241
drhcdf011d2011-04-04 21:25:283242 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3243 pDb = &db->aDb[iDb];
danielk1977da184232006-01-05 11:34:323244 pHash = &pDb->pSchema->tblHash;
3245 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
danielk1977a0bf2652004-11-04 14:30:043246 Table *pTab = sqliteHashData(pElem);
3247 if( pTab->tnum==iFrom ){
3248 pTab->tnum = iTo;
danielk1977a0bf2652004-11-04 14:30:043249 }
3250 }
danielk1977da184232006-01-05 11:34:323251 pHash = &pDb->pSchema->idxHash;
3252 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
danielk1977a0bf2652004-11-04 14:30:043253 Index *pIdx = sqliteHashData(pElem);
3254 if( pIdx->tnum==iFrom ){
3255 pIdx->tnum = iTo;
danielk1977a0bf2652004-11-04 14:30:043256 }
3257 }
danielk1977a0bf2652004-11-04 14:30:043258}
3259#endif
3260
3261/*
3262** Write code to erase the table with root-page iTable from database iDb.
drh1e32bed2020-06-19 13:33:533263** Also write code to modify the sqlite_schema table and internal schema
danielk1977a0bf2652004-11-04 14:30:043264** 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).
larrybrbc917382023-06-07 08:40:313266*/
drh4e0cff62004-11-05 05:10:283267static void destroyRootPage(Parse *pParse, int iTable, int iDb){
3268 Vdbe *v = sqlite3GetVdbe(pParse);
drhb7654112008-01-12 12:48:073269 int r1 = sqlite3GetTempReg(pParse);
drh19918882020-07-30 23:47:003270 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
drhb7654112008-01-12 12:48:073271 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
dane0af83a2009-09-08 19:15:013272 sqlite3MayAbort(pParse);
drh40e016e2004-11-04 14:47:113273#ifndef SQLITE_OMIT_AUTOVACUUM
drhb7654112008-01-12 12:48:073274 /* OP_Destroy stores an in integer r1. If this integer
drh4e0cff62004-11-05 05:10:283275 ** is non-zero, then it is the root page number of a table moved to
drh1e32bed2020-06-19 13:33:533276 ** location iTable. The following code modifies the sqlite_schema table to
drh4e0cff62004-11-05 05:10:283277 ** reflect this.
3278 **
drh0fa991b2009-03-21 16:19:263279 ** The "#NNN" in the SQL is a special constant that means whatever value
drhb74b1012009-05-28 21:04:373280 ** is in register NNN. See grammar rules associated with the TK_REGISTER
3281 ** token for additional information.
drh4e0cff62004-11-05 05:10:283282 */
larrybrbc917382023-06-07 08:40:313283 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:203284 "UPDATE %Q." LEGACY_SCHEMA_TABLE
drh346a70c2020-06-15 20:27:353285 " SET rootpage=%d WHERE #%d AND rootpage=#%d",
3286 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
danielk1977a0bf2652004-11-04 14:30:043287#endif
drhb7654112008-01-12 12:48:073288 sqlite3ReleaseTempReg(pParse, r1);
danielk1977a0bf2652004-11-04 14:30:043289}
3290
3291/*
3292** Write VDBE code to erase table pTab and all associated indices on disk.
drh1e32bed2020-06-19 13:33:533293** Code to update the sqlite_schema tables and internal schema definitions
danielk1977a0bf2652004-11-04 14:30:043294** 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*/
drh4e0cff62004-11-05 05:10:283297static void destroyTable(Parse *pParse, Table *pTab){
danielk1977a0bf2652004-11-04 14:30:043298 /* 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
larrybrbc917382023-06-07 08:40:313300 ** table and index root-pages in order, starting with the numerically
danielk1977a0bf2652004-11-04 14:30:043301 ** 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
larrybrbc917382023-06-07 08:40:313310 ** database, then root page 5 would be moved to page 4 by the
danielk1977a0bf2652004-11-04 14:30:043311 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
3312 ** a free-list page.
3313 */
drhabc38152020-07-22 13:38:043314 Pgno iTab = pTab->tnum;
drh8deae5a2020-07-29 12:23:203315 Pgno iDestroyed = 0;
danielk1977a0bf2652004-11-04 14:30:043316
3317 while( 1 ){
3318 Index *pIdx;
drh8deae5a2020-07-29 12:23:203319 Pgno iLargest = 0;
danielk1977a0bf2652004-11-04 14:30:043320
3321 if( iDestroyed==0 || iTab<iDestroyed ){
3322 iLargest = iTab;
3323 }
3324 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhabc38152020-07-22 13:38:043325 Pgno iIdx = pIdx->tnum;
danielk1977da184232006-01-05 11:34:323326 assert( pIdx->pSchema==pTab->pSchema );
danielk1977a0bf2652004-11-04 14:30:043327 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
3328 iLargest = iIdx;
3329 }
3330 }
danielk1977da184232006-01-05 11:34:323331 if( iLargest==0 ){
3332 return;
3333 }else{
3334 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drh5a05be12012-10-09 18:51:443335 assert( iDb>=0 && iDb<pParse->db->nDb );
danielk1977da184232006-01-05 11:34:323336 destroyRootPage(pParse, iLargest, iDb);
3337 iDestroyed = iLargest;
3338 }
danielk1977a0bf2652004-11-04 14:30:043339 }
danielk1977a0bf2652004-11-04 14:30:043340}
3341
3342/*
drh74e7c8f2011-10-21 19:06:323343** Remove entries from the sqlite_statN tables (for N in (1,2,3))
drha5ae4c32011-08-07 01:31:523344** after a DROP INDEX or DROP TABLE command.
3345*/
3346static 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){
drha5ae4c32011-08-07 01:31:523352 int i;
drh69c33822016-08-18 14:33:113353 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
danf52bb8d2013-08-03 20:24:583354 for(i=1; i<=4; i++){
drh74e7c8f2011-10-21 19:06:323355 char zTab[24];
3356 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
3357 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
drha5ae4c32011-08-07 01:31:523358 sqlite3NestedParse(pParse,
3359 "DELETE FROM %Q.%s WHERE %s=%Q",
drh74e7c8f2011-10-21 19:06:323360 zDbName, zTab, zType, zName
drha5ae4c32011-08-07 01:31:523361 );
3362 }
3363 }
3364}
3365
3366/*
drhfaacf172011-08-12 01:51:453367** Generate code to drop a table.
3368*/
3369void 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
drh1e32bed2020-06-19 13:33:533386 ** is generated to remove entries from sqlite_schema and/or
3387 ** sqlite_temp_schema if required.
drhfaacf172011-08-12 01:51:453388 */
3389 pTrigger = sqlite3TriggerList(pParse, pTab);
3390 while( pTrigger ){
larrybrbc917382023-06-07 08:40:313391 assert( pTrigger->pSchema==pTab->pSchema ||
drhfaacf172011-08-12 01:51:453392 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",
drh69c33822016-08-18 14:33:113406 pDb->zDbSName, pTab->zName
drhfaacf172011-08-12 01:51:453407 );
3408 }
3409#endif
3410
drh346a70c2020-06-15 20:27:353411 /* Drop all entries in the schema table that refer to the
drh067b92b2020-06-19 15:24:123412 ** table. The program name loops through the schema table and deletes
drhfaacf172011-08-12 01:51:453413 ** every row that refers to a table of the same name as the one being
mistachkin48864df2013-03-21 21:20:323414 ** dropped. Triggers are handled separately because a trigger can be
drhfaacf172011-08-12 01:51:453415 ** created in the temp database that refers to a table in another
3416 ** database.
3417 */
larrybrbc917382023-06-07 08:40:313418 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:203419 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE
drh346a70c2020-06-15 20:27:353420 " WHERE tbl_name=%Q and type!='trigger'",
3421 pDb->zDbSName, pTab->zName);
drhfaacf172011-08-12 01:51:453422 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);
dan1d4b1642018-12-28 17:45:083431 sqlite3MayAbort(pParse);
drhfaacf172011-08-12 01:51:453432 }
3433 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
3434 sqlite3ChangeCookie(pParse, iDb);
3435 sqliteViewResetAll(db, iDb);
drhfaacf172011-08-12 01:51:453436}
3437
3438/*
drh070ae3b2019-11-16 13:51:313439** Return TRUE if shadow tables should be read-only in the current
3440** context.
3441*/
3442int sqlite3ReadOnlyShadowTables(sqlite3 *db){
3443#ifndef SQLITE_OMIT_VIRTUALTABLE
3444 if( (db->flags & SQLITE_Defensive)!=0
3445 && db->pVtabCtx==0
3446 && db->nVdbeExec==0
dan73983652021-07-19 14:00:293447 && !sqlite3VtabInSync(db)
drh070ae3b2019-11-16 13:51:313448 ){
3449 return 1;
3450 }
3451#endif
3452 return 0;
3453}
3454
3455/*
drhd0c51d12019-11-16 12:04:383456** Return true if it is not allowed to drop the given table
3457*/
drh070ae3b2019-11-16 13:51:313458static int tableMayNotBeDropped(sqlite3 *db, Table *pTab){
drhd0c51d12019-11-16 12:04:383459 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 }
drh070ae3b2019-11-16 13:51:313464 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
3465 return 1;
drhd0c51d12019-11-16 12:04:383466 }
dan35c73122021-11-06 18:22:503467 if( pTab->tabFlags & TF_Eponymous ){
3468 return 1;
3469 }
drhd0c51d12019-11-16 12:04:383470 return 0;
3471}
3472
3473/*
drh75897232000-05-29 14:26:003474** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:093475** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:003476*/
drha0733842005-12-29 01:11:363477void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
danielk1977a8858102004-05-28 12:11:213478 Table *pTab;
drh75897232000-05-29 14:26:003479 Vdbe *v;
drh9bb575f2004-09-06 17:24:113480 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:243481 int iDb;
drh75897232000-05-29 14:26:003482
drh8af73d42009-05-13 22:58:283483 if( db->mallocFailed ){
drh6f7adc82006-01-11 21:41:203484 goto exit_drop_table;
3485 }
drh8af73d42009-05-13 22:58:283486 assert( pParse->nErr==0 );
danielk1977a8858102004-05-28 12:11:213487 assert( pName->nSrc==1 );
drh8797bd62024-08-17 19:46:493488 assert( pName->a[0].fg.fixedSchema==0 );
drh692c1602024-08-20 19:09:593489 assert( pName->a[0].fg.isSubquery==0 );
drh75209962015-04-19 22:31:453490 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
drha7564662010-02-22 19:32:313491 if( noErr ) db->suppressErr++;
drh4d249e62016-06-10 22:49:013492 assert( isView==0 || isView==LOCATE_VIEW );
dan41fb5cd2012-10-04 19:33:003493 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
drha7564662010-02-22 19:32:313494 if( noErr ) db->suppressErr--;
danielk1977a8858102004-05-28 12:11:213495
drha0733842005-12-29 01:11:363496 if( pTab==0 ){
drh31da7be2021-05-13 18:24:223497 if( noErr ){
drh8797bd62024-08-17 19:46:493498 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
drh31da7be2021-05-13 18:24:223499 sqlite3ForceNotReadOnly(pParse);
3500 }
drha0733842005-12-29 01:11:363501 goto exit_drop_table;
3502 }
danielk1977da184232006-01-05 11:34:323503 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drhe22a3342003-04-22 20:30:373504 assert( iDb>=0 && iDb<db->nDb );
danielk1977b5258c32007-10-04 18:11:153505
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 }
drhe5f9c642003-01-13 23:27:313512#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:313513 {
3514 int code;
danielk1977da184232006-01-05 11:34:323515 const char *zTab = SCHEMA_TABLE(iDb);
drh69c33822016-08-18 14:33:113516 const char *zDb = db->aDb[iDb].zDbSName;
danielk1977f1a381e2006-06-16 08:01:023517 const char *zArg2 = 0;
danielk19774adee202004-05-08 08:23:193518 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:213519 goto exit_drop_table;
drhe22a3342003-04-22 20:30:373520 }
drhe5f9c642003-01-13 23:27:313521 if( isView ){
danielk197753c0f742005-03-29 03:10:593522 if( !OMIT_TEMPDB && iDb==1 ){
drhe5f9c642003-01-13 23:27:313523 code = SQLITE_DROP_TEMP_VIEW;
3524 }else{
3525 code = SQLITE_DROP_VIEW;
3526 }
danielk19774b2688a2006-06-20 11:01:073527#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977f1a381e2006-06-16 08:01:023528 }else if( IsVirtual(pTab) ){
3529 code = SQLITE_DROP_VTABLE;
danielk1977595a5232009-07-24 17:58:533530 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
danielk19774b2688a2006-06-20 11:01:073531#endif
drhe5f9c642003-01-13 23:27:313532 }else{
danielk197753c0f742005-03-29 03:10:593533 if( !OMIT_TEMPDB && iDb==1 ){
drhe5f9c642003-01-13 23:27:313534 code = SQLITE_DROP_TEMP_TABLE;
3535 }else{
3536 code = SQLITE_DROP_TABLE;
3537 }
3538 }
danielk1977f1a381e2006-06-16 08:01:023539 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
danielk1977a8858102004-05-28 12:11:213540 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:313541 }
danielk1977a8858102004-05-28 12:11:213542 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
3543 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:273544 }
drhe5f9c642003-01-13 23:27:313545 }
3546#endif
drh070ae3b2019-11-16 13:51:313547 if( tableMayNotBeDropped(db, pTab) ){
danielk1977a8858102004-05-28 12:11:213548 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
danielk1977a8858102004-05-28 12:11:213549 goto exit_drop_table;
drh75897232000-05-29 14:26:003550 }
danielk1977576ec6b2005-01-21 11:55:253551
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 */
drhf38524d2021-08-02 16:41:573556 if( isView && !IsView(pTab) ){
danielk1977a8858102004-05-28 12:11:213557 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
3558 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:003559 }
drhf38524d2021-08-02 16:41:573560 if( !isView && IsView(pTab) ){
danielk1977a8858102004-05-28 12:11:213561 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
3562 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:003563 }
danielk1977576ec6b2005-01-21 11:55:253564#endif
drh75897232000-05-29 14:26:003565
drh067b92b2020-06-19 15:24:123566 /* Generate code to remove the table from the schema table
drh1ccde152000-06-17 13:12:393567 ** on disk.
3568 */
danielk19774adee202004-05-08 08:23:193569 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:003570 if( v ){
drh77658e22007-12-04 16:54:523571 sqlite3BeginWriteOperation(pParse, 1, iDb);
mistachkin0fc2da32018-07-20 20:56:223572 if( !isView ){
3573 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
3574 sqlite3FkDropTable(pParse, pName, pTab);
3575 }
drhfaacf172011-08-12 01:51:453576 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
drh75897232000-05-29 14:26:003577 }
danielk1977a8858102004-05-28 12:11:213578
3579exit_drop_table:
drh633e6d52008-07-28 19:34:533580 sqlite3SrcListDelete(db, pName);
drh75897232000-05-29 14:26:003581}
3582
3583/*
drhc2eef3b2002-08-31 18:53:063584** 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
drhbd50a922013-11-03 02:27:583588** 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
drhc2eef3b2002-08-31 18:53:063590** 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
drhe61922a2009-05-02 13:29:373594** under construction in the pParse->pNewTable field.
drhc2eef3b2002-08-31 18:53:063595**
3596** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:193597** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:063598*/
danielk19774adee202004-05-08 08:23:193599void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:063600 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:163601 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:063602 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:163603 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:063604 int flags /* Conflict resolution algorithms. */
3605){
danielk197718576932008-08-06 13:47:403606 sqlite3 *db = pParse->db;
drhb7f91642004-10-31 02:22:473607#ifndef SQLITE_OMIT_FOREIGN_KEY
drh40e016e2004-11-04 14:47:113608 FKey *pFKey = 0;
dan1da40a32009-09-19 17:00:313609 FKey *pNextTo;
drhc2eef3b2002-08-31 18:53:063610 Table *p = pParse->pNewTable;
drh913306a2021-11-26 17:10:183611 i64 nByte;
drhc2eef3b2002-08-31 18:53:063612 int i;
3613 int nCol;
3614 char *z;
drhc2eef3b2002-08-31 18:53:063615
3616 assert( pTo!=0 );
drh8af73d42009-05-13 22:58:283617 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
drhc2eef3b2002-08-31 18:53:063618 if( pFromCol==0 ){
3619 int iCol = p->nCol-1;
drhd3001712009-05-12 17:46:533620 if( NEVER(iCol<0) ) goto fk_end;
danielk19770202b292004-06-09 09:55:163621 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:193622 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:563623 " should reference only one column of table %T",
drhcf9d36d2021-08-02 18:03:433624 p->aCol[iCol].zCnName, pTo);
drhc2eef3b2002-08-31 18:53:063625 goto fk_end;
3626 }
3627 nCol = 1;
danielk19770202b292004-06-09 09:55:163628 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:193629 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:063630 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:563631 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:063632 goto fk_end;
3633 }else{
danielk19770202b292004-06-09 09:55:163634 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:063635 }
drhcebf06c2025-03-14 18:10:023636 nByte = SZ_FKEY(nCol) + pTo->n + 1;
drhc2eef3b2002-08-31 18:53:063637 if( pToCol ){
danielk19770202b292004-06-09 09:55:163638 for(i=0; i<pToCol->nExpr; i++){
drh41cee662019-12-12 20:22:343639 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
drhc2eef3b2002-08-31 18:53:063640 }
3641 }
drh633e6d52008-07-28 19:34:533642 pFKey = sqlite3DbMallocZero(db, nByte );
drh17435752007-08-16 04:30:383643 if( pFKey==0 ){
3644 goto fk_end;
3645 }
drhc2eef3b2002-08-31 18:53:063646 pFKey->pFrom = p;
drh78b2fa82021-10-07 12:11:203647 assert( IsOrdinaryTable(p) );
drhf38524d2021-08-02 16:41:573648 pFKey->pNextFrom = p->u.tab.pFKey;
drhe61922a2009-05-02 13:29:373649 z = (char*)&pFKey->aCol[nCol];
drhdf68f6b2002-09-21 15:57:573650 pFKey->zTo = z;
danc9461ec2018-08-29 21:00:163651 if( IN_RENAME_OBJECT ){
3652 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
3653 }
drhc2eef3b2002-08-31 18:53:063654 memcpy(z, pTo->z, pTo->n);
3655 z[pTo->n] = 0;
danielk197770d9e9c2009-04-24 18:06:093656 sqlite3Dequote(z);
drhc2eef3b2002-08-31 18:53:063657 z += pTo->n+1;
drhc2eef3b2002-08-31 18:53:063658 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:063659 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++){
drhcf9d36d2021-08-02 18:03:433665 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
drhc2eef3b2002-08-31 18:53:063666 pFKey->aCol[i].iFrom = j;
3667 break;
3668 }
3669 }
3670 if( j>=p->nCol ){
larrybrbc917382023-06-07 08:40:313671 sqlite3ErrorMsg(pParse,
3672 "unknown column \"%s\" in foreign key definition",
drh41cee662019-12-12 20:22:343673 pFromCol->a[i].zEName);
drhc2eef3b2002-08-31 18:53:063674 goto fk_end;
3675 }
danc9461ec2018-08-29 21:00:163676 if( IN_RENAME_OBJECT ){
drh41cee662019-12-12 20:22:343677 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
dancf8f2892018-08-09 20:47:013678 }
drhc2eef3b2002-08-31 18:53:063679 }
3680 }
3681 if( pToCol ){
3682 for(i=0; i<nCol; i++){
drh41cee662019-12-12 20:22:343683 int n = sqlite3Strlen30(pToCol->a[i].zEName);
drhc2eef3b2002-08-31 18:53:063684 pFKey->aCol[i].zCol = z;
danc9461ec2018-08-29 21:00:163685 if( IN_RENAME_OBJECT ){
drh41cee662019-12-12 20:22:343686 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
dan6fe7f232018-08-10 19:19:333687 }
drh41cee662019-12-12 20:22:343688 memcpy(z, pToCol->a[i].zEName, n);
drhc2eef3b2002-08-31 18:53:063689 z[n] = 0;
3690 z += n+1;
3691 }
3692 }
3693 pFKey->isDeferred = 0;
dan8099ce62009-09-23 08:43:353694 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
3695 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
drhc2eef3b2002-08-31 18:53:063696
drh21206082011-04-04 18:22:023697 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
larrybrbc917382023-06-07 08:40:313698 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
drhacbcb7e2014-08-21 20:26:373699 pFKey->zTo, (void *)pFKey
dan1da40a32009-09-19 17:00:313700 );
danf59c5ca2009-09-22 16:55:383701 if( pNextTo==pFKey ){
drh4a642b62016-02-05 01:55:273702 sqlite3OomFault(db);
danf59c5ca2009-09-22 16:55:383703 goto fk_end;
3704 }
dan1da40a32009-09-19 17:00:313705 if( pNextTo ){
3706 assert( pNextTo->pPrevTo==0 );
3707 pFKey->pNextTo = pNextTo;
3708 pNextTo->pPrevTo = pFKey;
3709 }
3710
drhc2eef3b2002-08-31 18:53:063711 /* Link the foreign key to the table as the last step.
3712 */
drh78b2fa82021-10-07 12:11:203713 assert( IsOrdinaryTable(p) );
drhf38524d2021-08-02 16:41:573714 p->u.tab.pFKey = pFKey;
drhc2eef3b2002-08-31 18:53:063715 pFKey = 0;
3716
3717fk_end:
drh633e6d52008-07-28 19:34:533718 sqlite3DbFree(db, pFKey);
drhb7f91642004-10-31 02:22:473719#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
drh633e6d52008-07-28 19:34:533720 sqlite3ExprListDelete(db, pFromCol);
3721 sqlite3ExprListDelete(db, pToCol);
drhc2eef3b2002-08-31 18:53:063722}
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*/
danielk19774adee202004-05-08 08:23:193731void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhb7f91642004-10-31 02:22:473732#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:063733 Table *pTab;
3734 FKey *pFKey;
drhf38524d2021-08-02 16:41:573735 if( (pTab = pParse->pNewTable)==0 ) return;
drh78b2fa82021-10-07 12:11:203736 if( NEVER(!IsOrdinaryTable(pTab)) ) return;
drhf38524d2021-08-02 16:41:573737 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
drh4c429832009-10-12 22:30:493738 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
drh1bd10f82008-12-10 21:19:563739 pFKey->isDeferred = (u8)isDeferred;
drhb7f91642004-10-31 02:22:473740#endif
drhc2eef3b2002-08-31 18:53:063741}
3742
3743/*
drh063336a2004-11-05 20:58:393744** 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
drh1db639c2008-01-17 02:36:283749** created. The register specified by memRootPage contains the
drh063336a2004-11-05 20:58:393750** 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*/
3754static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
3755 Table *pTab = pIndex->pTable; /* The table that is indexed */
danielk19776ab3a2e2009-02-19 14:39:253756 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
3757 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
drhb07028f2011-10-14 21:49:183758 int iSorter; /* Cursor opened by OpenSorter (if in use) */
drh063336a2004-11-05 20:58:393759 int addr1; /* Address of top of loop */
dan5134d132011-09-02 10:31:113760 int addr2; /* Address to jump to for next iteration */
drhabc38152020-07-22 13:38:043761 Pgno tnum; /* Root page of index */
drhb2b9d3d2013-08-01 01:14:433762 int iPartIdxLabel; /* Jump to this label to skip a row */
drh063336a2004-11-05 20:58:393763 Vdbe *v; /* Generate code into this virtual machine */
danielk1977b3bf5562006-01-10 17:58:233764 KeyInfo *pKey; /* KeyInfo for index */
peter.d.reid60ec9142014-09-06 16:39:463765 int regRecord; /* Register holding assembled index record */
drh17435752007-08-16 04:30:383766 sqlite3 *db = pParse->db; /* The database connection */
3767 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
drh063336a2004-11-05 20:58:393768
danielk19771d54df82004-11-23 15:41:163769#ifndef SQLITE_OMIT_AUTHORIZATION
3770 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
drh69c33822016-08-18 14:33:113771 db->aDb[iDb].zDbSName ) ){
danielk19771d54df82004-11-23 15:41:163772 return;
3773 }
3774#endif
3775
danielk1977c00da102006-01-07 13:21:043776 /* Require a write-lock on the table to perform this operation */
3777 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
3778
drh063336a2004-11-05 20:58:393779 v = sqlite3GetVdbe(pParse);
3780 if( v==0 ) return;
3781 if( memRootPage>=0 ){
drhabc38152020-07-22 13:38:043782 tnum = (Pgno)memRootPage;
drh063336a2004-11-05 20:58:393783 }else{
3784 tnum = pIndex->tnum;
drh063336a2004-11-05 20:58:393785 }
drh2ec2fb22013-11-06 19:59:233786 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
drh0c7d3d32022-01-24 16:47:123787 assert( pKey!=0 || pParse->nErr );
dana20fde62011-07-12 14:28:053788
dan689ab892011-08-12 15:02:003789 /* Open the sorter cursor if we are to use one. */
drhca892a72011-09-03 00:17:513790 iSorter = pParse->nTab++;
danfad9f9a2014-04-01 18:41:513791 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
drh2ec2fb22013-11-06 19:59:233792 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
dana20fde62011-07-12 14:28:053793
3794 /* Open the table. Loop through all rows of the table, inserting index
3795 ** records into the sorter. */
drhdd9930e2013-10-23 23:37:023796 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
drh688852a2014-02-17 22:40:433797 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
drh2d401ab2008-01-10 23:50:113798 regRecord = sqlite3GetTempReg(pParse);
drh4031baf2018-05-28 17:31:203799 sqlite3MultiWrite(pParse);
dana20fde62011-07-12 14:28:053800
drh1c2c0b72014-01-04 19:27:053801 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
drhca892a72011-09-03 00:17:513802 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
drh87744512014-04-13 19:15:493803 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
drh688852a2014-02-17 22:40:433804 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
drhca892a72011-09-03 00:17:513805 sqlite3VdbeJumpHere(v, addr1);
drh44156282013-10-23 22:23:033806 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
larrybrbc917382023-06-07 08:40:313807 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, (int)tnum, iDb,
drh2ec2fb22013-11-06 19:59:233808 (char *)pKey, P4_KEYINFO);
drh44156282013-10-23 22:23:033809 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
3810
drh688852a2014-02-17 22:40:433811 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
drh60de73e2016-04-05 15:59:233812 if( IsUniqueIndex(pIndex) ){
drh4031baf2018-05-28 17:31:203813 int j2 = sqlite3VdbeGoto(v, 1);
drhca892a72011-09-03 00:17:513814 addr2 = sqlite3VdbeCurrentAddr(v);
drh4031baf2018-05-28 17:31:203815 sqlite3VdbeVerifyAbortable(v, OE_Abort);
drh1153c7b2013-11-01 22:02:563816 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
drhac502322014-07-30 13:56:483817 pIndex->nKeyCol); VdbeCoverage(v);
drhf9c8ce32013-11-05 13:33:553818 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
drh4031baf2018-05-28 17:31:203819 sqlite3VdbeJumpHere(v, j2);
drhca892a72011-09-03 00:17:513820 }else{
dan7ed6c062019-05-21 16:32:413821 /* 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
larrybrbc917382023-06-07 08:40:313826 ** needs to be restored if the statement aborts), so we call
dan7ed6c062019-05-21 16:32:413827 ** sqlite3MayAbort() for all CREATE INDEX statements. */
danef14abb2019-05-21 14:42:243828 sqlite3MayAbort(pParse);
drhca892a72011-09-03 00:17:513829 addr2 = sqlite3VdbeCurrentAddr(v);
dan689ab892011-08-12 15:02:003830 }
drh6cf4a7d2014-10-13 13:00:583831 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
drhbf9ff252019-05-14 00:43:133832 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.
drh8a6f89c2025-04-10 10:18:073838 ** See ticket: https://sqlite.org/src/info/bba7b69f9849b5bf
drhbf9ff252019-05-14 00:43:133839 */
3840 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
3841 }
drh9b4eaeb2016-11-09 00:10:333842 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
drhca892a72011-09-03 00:17:513843 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
drh2d401ab2008-01-10 23:50:113844 sqlite3ReleaseTempReg(pParse, regRecord);
drh688852a2014-02-17 22:40:433845 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
drhd654be82005-09-20 17:42:233846 sqlite3VdbeJumpHere(v, addr1);
dana20fde62011-07-12 14:28:053847
drh66a51672008-01-03 00:01:233848 sqlite3VdbeAddOp1(v, OP_Close, iTab);
3849 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
dan689ab892011-08-12 15:02:003850 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
drh063336a2004-11-05 20:58:393851}
3852
3853/*
drh77e57df2013-10-22 14:28:023854** 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*/
3860Index *sqlite3AllocateIndexObject(
3861 sqlite3 *db, /* Database connection */
drhcc803b22025-02-21 20:35:373862 int nCol, /* Total number of columns in the index */
drh77e57df2013-10-22 14:28:023863 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 */
drhef86b942025-02-17 17:33:143867 i64 nByte; /* Bytes of space for Index object + arrays */
drh77e57df2013-10-22 14:28:023868
drhcc803b22025-02-21 20:35:373869 assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] );
drh77e57df2013-10-22 14:28:023870 nByte = ROUND8(sizeof(Index)) + /* Index structure */
3871 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
dancfc9df72014-04-25 15:01:013872 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
drhbbbdc832013-10-22 18:01:403873 sizeof(i16)*nCol + /* Index.aiColumn */
drh77e57df2013-10-22 14:28:023874 sizeof(u8)*nCol); /* Index.aSortOrder */
3875 p = sqlite3DbMallocZero(db, nByte + nExtra);
3876 if( p ){
3877 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
drhf19aa5f2015-12-30 16:51:203878 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
dancfc9df72014-04-25 15:01:013879 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
3880 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
drh77e57df2013-10-22 14:28:023881 p->aSortOrder = (u8*)pExtra;
drhcc803b22025-02-21 20:35:373882 assert( nCol>0 );
3883 p->nColumn = (u16)nCol;
3884 p->nKeyCol = (u16)(nCol - 1);
drh77e57df2013-10-22 14:28:023885 *ppExtra = ((char*)p) + nByte;
3886 }
3887 return p;
3888}
3889
3890/*
dan9105fd52019-08-19 17:26:323891** 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*/
3895int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
3896 if( pList ){
3897 int i;
3898 for(i=0; i<pList->nExpr; i++){
drhd88fd532022-05-02 20:49:303899 if( pList->a[i].fg.bNulls ){
3900 u8 sf = pList->a[i].fg.sortFlags;
larrybrbc917382023-06-07 08:40:313901 sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s",
dan9105fd52019-08-19 17:26:323902 (sf==0 || sf==3) ? "FIRST" : "LAST"
3903 );
3904 return 1;
3905 }
3906 }
3907 }
3908 return 0;
3909}
3910
3911/*
larrybrbc917382023-06-07 08:40:313912** 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
drhadbca9c2001-09-27 15:11:533914** 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
drh382c0242001-10-06 16:33:023916** as the table to be indexed. pParse->pNewTable is a table that is
3917** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:003918**
drh382c0242001-10-06 16:33:023919** 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
larrybrbc917382023-06-07 08:40:313921** to the table currently under construction.
drh75897232000-05-29 14:26:003922*/
drh62340f82016-05-31 21:18:153923void sqlite3CreateIndex(
drh23bf66d2004-12-14 03:34:343924 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 */
danielk19770202b292004-06-09 09:55:163928 ExprList *pList, /* A list of columns to be indexed */
drh23bf66d2004-12-14 03:34:343929 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh1c55ba02007-07-02 19:31:273930 Token *pStart, /* The CREATE token that begins this statement */
drh1fe05372013-07-31 18:12:263931 Expr *pPIWhere, /* WHERE clause for partial indices */
drh4d91a702006-01-04 15:54:363932 int sortOrder, /* Sort order of primary key when pList==NULL */
drh62340f82016-05-31 21:18:153933 int ifNotExist, /* Omit error if index already exists */
3934 u8 idxType /* The index type */
drh75897232000-05-29 14:26:003935){
drhfdd6e852005-12-16 01:06:163936 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 */
drhbeae3192001-09-22 18:12:083940 int i, j;
drhfdd6e852005-12-16 01:06:163941 DbFixer sFix; /* For assigning database names to pTable */
3942 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
drh9bb575f2004-09-06 17:24:113943 sqlite3 *db = pParse->db;
drhfdd6e852005-12-16 01:06:163944 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 */
drhc28c4e52013-10-03 19:21:413948 int nExtra = 0; /* Space allocated for zExtra[] */
drh44156282013-10-23 22:23:033949 int nExtraCol; /* Number of extra columns needed */
drh47b927d2013-12-03 00:11:403950 char *zExtra = 0; /* Extra space after the Index object */
drh44156282013-10-23 22:23:033951 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
danielk1977cbb18d22004-05-28 11:37:273952
drh0c7d3d32022-01-24 16:47:123953 assert( db->pParse==pParse );
3954 if( pParse->nErr ){
drh62340f82016-05-31 21:18:153955 goto exit_create_index;
3956 }
drh0c7d3d32022-01-24 16:47:123957 assert( db->mallocFailed==0 );
drh62340f82016-05-31 21:18:153958 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
drhd3001712009-05-12 17:46:533959 goto exit_create_index;
3960 }
3961 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977e501b892006-01-09 06:29:473962 goto exit_create_index;
3963 }
dan9105fd52019-08-19 17:26:323964 if( sqlite3HasExplicitNulls(pParse, pList) ){
3965 goto exit_create_index;
3966 }
drhdaffd0e2001-04-11 14:28:423967
drh75897232000-05-29 14:26:003968 /*
3969 ** Find the table that is to be indexed. Return early if not found.
3970 */
danielk1977cbb18d22004-05-28 11:37:273971 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:273972
larrybrbc917382023-06-07 08:40:313973 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:193974 ** to search for the table. 'Fix' the table name to this db
3975 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:273976 */
3977 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:193978 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:273979 if( iDb<0 ) goto exit_create_index;
drhb07028f2011-10-14 21:49:183980 assert( pName && pName->z );
danielk1977cbb18d22004-05-28 11:37:273981
danielk197753c0f742005-03-29 03:10:593982#ifndef SQLITE_OMIT_TEMPDB
mistachkind5578432012-08-25 10:01:293983 /* If the index name was unqualified, check if the table
danielk1977fe910332007-12-02 11:46:343984 ** is a temp table. If so, set the database to 1. Do not do this
larrybrbc917382023-06-07 08:40:313985 ** if initializing a database schema.
danielk1977cbb18d22004-05-28 11:37:273986 */
danielk1977fe910332007-12-02 11:46:343987 if( !db->init.busy ){
3988 pTab = sqlite3SrcListLookup(pParse, pTblName);
drhd3001712009-05-12 17:46:533989 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
danielk1977fe910332007-12-02 11:46:343990 iDb = 1;
3991 }
danielk1977ef2cb632004-05-29 02:37:193992 }
danielk197753c0f742005-03-29 03:10:593993#endif
danielk1977ef2cb632004-05-29 02:37:193994
drhd100f692013-10-03 15:39:443995 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
3996 if( sqlite3FixSrcList(&sFix, pTblName) ){
drh85c23c62005-08-20 03:03:043997 /* Because the parser constructs pTblName from a single identifier,
3998 ** sqlite3FixSrcList can never fail. */
3999 assert(0);
danielk1977cbb18d22004-05-28 11:37:274000 }
dan41fb5cd2012-10-04 19:33:004001 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
drhc31c7c12012-10-08 23:25:074002 assert( db->mallocFailed==0 || pTab==0 );
4003 if( pTab==0 ) goto exit_create_index;
drh989b1162013-08-01 22:27:264004 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
larrybrbc917382023-06-07 08:40:314005 sqlite3ErrorMsg(pParse,
drh989b1162013-08-01 22:27:264006 "cannot create a TEMP index on non-TEMP table \"%s\"",
4007 pTab->zName);
4008 goto exit_create_index;
4009 }
drh44156282013-10-23 22:23:034010 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
drh75897232000-05-29 14:26:004011 }else{
drhe3c41372001-09-17 20:25:584012 assert( pName==0 );
drhb07028f2011-10-14 21:49:184013 assert( pStart==0 );
danielk1977da184232006-01-05 11:34:324014 pTab = pParse->pNewTable;
drha6370df2006-01-04 21:40:064015 if( !pTab ) goto exit_create_index;
danielk1977da184232006-01-05 11:34:324016 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
drh75897232000-05-29 14:26:004017 }
drhfdd6e852005-12-16 01:06:164018 pDb = &db->aDb[iDb];
danielk1977cbb18d22004-05-28 11:37:274019
drhd3001712009-05-12 17:46:534020 assert( pTab!=0 );
larrybrbc917382023-06-07 08:40:314021 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
drh3a3a03f2014-09-11 16:36:434022 && db->init.busy==0
drh346f4e22019-03-25 21:35:414023 && pTblName!=0
drh067b92b2020-06-19 15:24:124024 ){
danielk19774adee202004-05-08 08:23:194025 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:494026 goto exit_create_index;
4027 }
danielk1977576ec6b2005-01-21 11:55:254028#ifndef SQLITE_OMIT_VIEW
drhf38524d2021-08-02 16:41:574029 if( IsView(pTab) ){
danielk19774adee202004-05-08 08:23:194030 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:104031 goto exit_create_index;
4032 }
danielk1977576ec6b2005-01-21 11:55:254033#endif
danielk19775ee9d692006-06-21 12:36:254034#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
drh75897232000-05-29 14:26:004040
4041 /*
4042 ** Find the name of the index. Make sure there is not already another
larrybrbc917382023-06-07 08:40:314043 ** index or table with the same name.
drhf57b3392001-10-08 13:22:324044 **
4045 ** Exception: If we are reading the names of permanent indices from the
drh1e32bed2020-06-19 13:33:534046 ** sqlite_schema table (because some other process changed the schema) and
drhf57b3392001-10-08 13:22:324047 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:244048 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:324049 **
4050 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:534051 ** dealing with a primary key or UNIQUE constraint. We have to invent our
4052 ** own name.
drh75897232000-05-29 14:26:004053 */
danielk1977d8123362004-06-12 09:25:124054 if( pName ){
drh17435752007-08-16 04:30:384055 zName = sqlite3NameFromToken(db, pName);
drhe3c41372001-09-17 20:25:584056 if( zName==0 ) goto exit_create_index;
drhb07028f2011-10-14 21:49:184057 assert( pName->z!=0 );
drhc5a93d42019-08-12 00:08:074058 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
drhd24cc422003-03-27 12:51:244059 goto exit_create_index;
drhe3c41372001-09-17 20:25:584060 }
danc9461ec2018-08-29 21:00:164061 if( !IN_RENAME_OBJECT ){
dancf8f2892018-08-09 20:47:014062 if( !db->init.busy ){
drh626bcc82022-08-09 16:13:214063 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
dancf8f2892018-08-09 20:47:014064 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);
drh31da7be2021-05-13 18:24:224074 sqlite3ForceNotReadOnly(pParse);
dancf8f2892018-08-09 20:47:014075 }
danielk1977d45a0312007-03-13 16:32:254076 goto exit_create_index;
4077 }
4078 }
danielk1977a21c6b62005-01-24 10:25:594079 }else{
drhadbca9c2001-09-27 15:11:534080 int n;
4081 Index *pLoop;
4082 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
drhf089aa42008-07-08 19:34:064083 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
danielk1977a1644fd2007-08-29 12:31:254084 if( zName==0 ){
danielk1977a1644fd2007-08-29 12:31:254085 goto exit_create_index;
4086 }
drh0aafa9c2016-08-05 14:35:474087
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. */
dancf8f2892018-08-09 20:47:014093 if( IN_SPECIAL_PARSE ) zName[7]++;
drh75897232000-05-29 14:26:004094 }
4095
drhe5f9c642003-01-13 23:27:314096 /* Check for authorization to create an index.
4097 */
4098#ifndef SQLITE_OMIT_AUTHORIZATION
danc9461ec2018-08-29 21:00:164099 if( !IN_RENAME_OBJECT ){
drh69c33822016-08-18 14:33:114100 const char *zDb = pDb->zDbSName;
danielk197753c0f742005-03-29 03:10:594101 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
drhe22a3342003-04-22 20:30:374102 goto exit_create_index;
4103 }
4104 i = SQLITE_CREATE_INDEX;
danielk197753c0f742005-03-29 03:10:594105 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:194106 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:374107 goto exit_create_index;
4108 }
drhe5f9c642003-01-13 23:27:314109 }
4110#endif
4111
drh75897232000-05-29 14:26:004112 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:394113 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:004114 ** So create a fake list to simulate this.
4115 */
4116 if( pList==0 ){
drh108aa002015-08-24 20:21:204117 Token prevCol;
dan26e731c2018-01-29 16:22:394118 Column *pCol = &pTab->aCol[pTab->nCol-1];
4119 pCol->colFlags |= COLFLAG_UNIQUE;
drhcf9d36d2021-08-02 18:03:434120 sqlite3TokenInit(&prevCol, pCol->zCnName);
drh108aa002015-08-24 20:21:204121 pList = sqlite3ExprListAppend(pParse, 0,
4122 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
drh75897232000-05-29 14:26:004123 if( pList==0 ) goto exit_create_index;
drhbc622bc2015-08-24 15:39:424124 assert( pList->nExpr==1 );
dan5b32bdf2019-08-17 15:47:324125 sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED);
drh108aa002015-08-24 20:21:204126 }else{
4127 sqlite3ExprListCheckLength(pParse, pList, "index");
drh8fe25c62019-03-31 21:09:334128 if( pParse->nErr ) goto exit_create_index;
drh75897232000-05-29 14:26:004129 }
4130
danielk1977b3bf5562006-01-10 17:58:234131 /* 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++){
drhd3001712009-05-12 17:46:534135 Expr *pExpr = pList->a[i].pExpr;
drh7d3d9da2015-09-01 00:42:524136 assert( pExpr!=0 );
4137 if( pExpr->op==TK_COLLATE ){
drhf9751072021-10-07 13:40:294138 assert( !ExprHasProperty(pExpr, EP_IntValue) );
dan911ce412013-05-15 15:16:504139 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
danielk1977b3bf5562006-01-10 17:58:234140 }
4141 }
4142
larrybrbc917382023-06-07 08:40:314143 /*
4144 ** Allocate the index structure.
drh75897232000-05-29 14:26:004145 */
drhea678832008-12-10 19:26:224146 nName = sqlite3Strlen30(zName);
drh44156282013-10-23 22:23:034147 nExtraCol = pPk ? pPk->nKeyCol : 1;
drh8fe25c62019-03-31 21:09:334148 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
drh44156282013-10-23 22:23:034149 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
drh77e57df2013-10-22 14:28:024150 nName + nExtra + 1, &zExtra);
drh17435752007-08-16 04:30:384151 if( db->mallocFailed ){
4152 goto exit_create_index;
4153 }
dancfc9df72014-04-25 15:01:014154 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
drhe09b84c2011-11-14 02:53:544155 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
drh77e57df2013-10-22 14:28:024156 pIndex->zName = zExtra;
4157 zExtra += nName + 1;
drh5bb3eb92007-05-04 13:15:554158 memcpy(pIndex->zName, zName, nName+1);
drh75897232000-05-29 14:26:004159 pIndex->pTable = pTab;
drh1bd10f82008-12-10 21:19:564160 pIndex->onError = (u8)onError;
drh9eade082013-10-24 14:16:104161 pIndex->uniqNotNull = onError!=OE_None;
drh62340f82016-05-31 21:18:154162 pIndex->idxType = idxType;
danielk1977da184232006-01-05 11:34:324163 pIndex->pSchema = db->aDb[iDb].pSchema;
drh72ffd092013-10-30 15:52:324164 pIndex->nKeyCol = pList->nExpr;
drh3780be12013-07-31 19:05:224165 if( pPIWhere ){
4166 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
4167 pIndex->pPartIdxWhere = pPIWhere;
4168 pPIWhere = 0;
4169 }
drh21206082011-04-04 18:22:024170 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
drh75897232000-05-29 14:26:004171
drhfdd6e852005-12-16 01:06:164172 /* Check to see if we should honor DESC requests on index columns
4173 */
danielk1977da184232006-01-05 11:34:324174 if( pDb->pSchema->file_format>=4 ){
drhfdd6e852005-12-16 01:06:164175 sortOrderMask = -1; /* Honor DESC */
drhfdd6e852005-12-16 01:06:164176 }else{
4177 sortOrderMask = 0; /* Ignore DESC */
4178 }
4179
drh1f9ca2c2015-08-25 16:57:524180 /* 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,
drh4b92f982015-09-29 17:20:144183 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
drhd3001712009-05-12 17:46:534184 **
drh1f9ca2c2015-08-25 16:57:524185 ** 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.
drh75897232000-05-29 14:26:004188 */
dancf8f2892018-08-09 20:47:014189 pListItem = pList->a;
danc9461ec2018-08-29 21:00:164190 if( IN_RENAME_OBJECT ){
dancf8f2892018-08-09 20:47:014191 pIndex->aColExpr = pList;
4192 pList = 0;
4193 }
4194 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
drh1f9ca2c2015-08-25 16:57:524195 Expr *pCExpr; /* The i-th index expression */
4196 int requestedSortOrder; /* ASC or DESC on the i-th expression */
drhf19aa5f2015-12-30 16:51:204197 const char *zColl; /* Collation sequence name */
danielk1977b3bf5562006-01-10 17:58:234198
drhedb04ed2015-09-04 12:54:014199 sqlite3StringToId(pListItem->pExpr);
drha514b8e2015-08-25 00:27:064200 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
4201 if( pParse->nErr ) goto exit_create_index;
drh108aa002015-08-24 20:21:204202 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
drha514b8e2015-08-25 00:27:064203 if( pCExpr->op!=TK_COLUMN ){
drh1f9ca2c2015-08-25 16:57:524204 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 ){
dancf8f2892018-08-09 20:47:014210 pIndex->aColExpr = pList;
4211 pList = 0;
drh1f9ca2c2015-08-25 16:57:524212 }
drh4b92f982015-09-29 17:20:144213 j = XN_EXPR;
4214 pIndex->aiColumn[i] = XN_EXPR;
drh84926532015-08-31 19:38:424215 pIndex->uniqNotNull = 0;
drh4bc1cc12022-10-13 21:08:344216 pIndex->bHasExpr = 1;
drh1f9ca2c2015-08-25 16:57:524217 }else{
4218 j = pCExpr->iColumn;
4219 assert( j<=0x7fff );
4220 if( j<0 ){
4221 j = pTab->iPKey;
drhc7476732019-10-24 20:29:254222 }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;
drh08535842022-10-17 14:30:014228 pIndex->bHasExpr = 1;
drhc7476732019-10-24 20:29:254229 }
drh1f9ca2c2015-08-25 16:57:524230 }
4231 pIndex->aiColumn[i] = (i16)j;
drh108aa002015-08-24 20:21:204232 }
drha514b8e2015-08-25 00:27:064233 zColl = 0;
drh108aa002015-08-24 20:21:204234 if( pListItem->pExpr->op==TK_COLLATE ){
drhd3001712009-05-12 17:46:534235 int nColl;
drhf9751072021-10-07 13:40:294236 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
dan911ce412013-05-15 15:16:504237 zColl = pListItem->pExpr->u.zToken;
drhd3001712009-05-12 17:46:534238 nColl = sqlite3Strlen30(zColl) + 1;
4239 assert( nExtra>=nColl );
4240 memcpy(zExtra, zColl, nColl);
danielk1977b3bf5562006-01-10 17:58:234241 zColl = zExtra;
drhd3001712009-05-12 17:46:534242 zExtra += nColl;
4243 nExtra -= nColl;
drha514b8e2015-08-25 00:27:064244 }else if( j>=0 ){
drh65b40092021-08-05 15:27:194245 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
danielk19770202b292004-06-09 09:55:164246 }
drhf19aa5f2015-12-30 16:51:204247 if( !zColl ) zColl = sqlite3StrBINARY;
drhb7f24de2009-05-13 17:35:234248 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
danielk19777cedc8d2004-06-10 10:50:084249 goto exit_create_index;
4250 }
danielk1977b3bf5562006-01-10 17:58:234251 pIndex->azColl[i] = zColl;
drhd88fd532022-05-02 20:49:304252 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
drh1bd10f82008-12-10 21:19:564253 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
drh75897232000-05-29 14:26:004254 }
drh1f9ca2c2015-08-25 16:57:524255
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 */
drh44156282013-10-23 22:23:034260 if( pPk ){
drh7913e412013-11-01 20:30:364261 for(j=0; j<pPk->nKeyCol; j++){
4262 int x = pPk->aiColumn[j];
drh1f9ca2c2015-08-25 16:57:524263 assert( x>=0 );
drhf78d0f42019-04-28 19:27:024264 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
larrybrbc917382023-06-07 08:40:314265 pIndex->nColumn--;
drh7913e412013-11-01 20:30:364266 }else{
drhf78d0f42019-04-28 19:27:024267 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
drh7913e412013-11-01 20:30:364268 pIndex->aiColumn[i] = x;
4269 pIndex->azColl[i] = pPk->azColl[j];
4270 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
4271 i++;
4272 }
drh44156282013-10-23 22:23:034273 }
drh7913e412013-11-01 20:30:364274 assert( i==pIndex->nColumn );
drh44156282013-10-23 22:23:034275 }else{
drh4b92f982015-09-29 17:20:144276 pIndex->aiColumn[i] = XN_ROWID;
drhf19aa5f2015-12-30 16:51:204277 pIndex->azColl[i] = sqlite3StrBINARY;
drh44156282013-10-23 22:23:034278 }
drh51147ba2005-07-23 22:59:554279 sqlite3DefaultRowEst(pIndex);
drhe13e9f52013-10-05 19:18:004280 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
drh75897232000-05-29 14:26:004281
danf769cd62016-02-24 20:16:284282 /* If this index contains every column of its table, then mark
4283 ** it as a covering index */
larrybrbc917382023-06-07 08:40:314284 assert( HasRowid(pTab)
drhb9bcf7c2019-10-19 13:29:104285 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
drh00eee7a2023-10-06 12:55:534286 recomputeColumnsNotIndexed(pIndex);
danf769cd62016-02-24 20:16:284287 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;
drhb9bcf7c2019-10-19 13:29:104291 if( sqlite3TableColumnToIndex(pIndex,j)>=0 ) continue;
danf769cd62016-02-24 20:16:284292 pIndex->isCovering = 0;
4293 break;
4294 }
4295 }
4296
danielk1977d8123362004-06-12 09:25:124297 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.
drhd3001712009-05-12 17:46:534310 **
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.
danielk1977d8123362004-06-12 09:25:124318 */
4319 Index *pIdx;
4320 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
4321 int k;
drh5f1d1d92014-07-31 22:59:044322 assert( IsUniqueIndex(pIdx) );
drh48dd1d82014-05-27 18:18:584323 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
drh5f1d1d92014-07-31 22:59:044324 assert( IsUniqueIndex(pIndex) );
danielk1977d8123362004-06-12 09:25:124325
drhbbbdc832013-10-22 18:01:404326 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
4327 for(k=0; k<pIdx->nKeyCol; k++){
drhd3001712009-05-12 17:46:534328 const char *z1;
4329 const char *z2;
drh1f9ca2c2015-08-25 16:57:524330 assert( pIdx->aiColumn[k]>=0 );
danielk1977d8123362004-06-12 09:25:124331 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
drhd3001712009-05-12 17:46:534332 z1 = pIdx->azColl[k];
4333 z2 = pIndex->azColl[k];
drhc41c1322016-02-11 13:30:364334 if( sqlite3StrICmp(z1, z2) ) break;
danielk1977d8123362004-06-12 09:25:124335 }
drhbbbdc832013-10-22 18:01:404336 if( k==pIdx->nKeyCol ){
danielk1977f736b772004-06-17 06:13:344337 if( pIdx->onError!=pIndex->onError ){
4338 /* This constraint creates the same index as a previous
4339 ** constraint specified somewhere in the CREATE TABLE statement.
larrybrbc917382023-06-07 08:40:314340 ** However the ON CONFLICT clauses are different. If both this
danielk1977f736b772004-06-17 06:13:344341 ** constraint and the previous equivalent constraint have explicit
4342 ** ON CONFLICT clauses this is an error. Otherwise, use the
mistachkin48864df2013-03-21 21:20:324343 ** explicitly specified behavior for the index.
danielk1977f736b772004-06-17 06:13:344344 */
4345 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
larrybrbc917382023-06-07 08:40:314346 sqlite3ErrorMsg(pParse,
danielk1977f736b772004-06-17 06:13:344347 "conflicting ON CONFLICT clauses specified", 0);
4348 }
4349 if( pIdx->onError==OE_Default ){
4350 pIdx->onError = pIndex->onError;
4351 }
4352 }
drh273bfe92016-06-02 16:22:534353 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
drh885eeb62019-01-09 02:02:244354 if( IN_RENAME_OBJECT ){
4355 pIndex->pNext = pParse->pNewIndex;
4356 pParse->pNewIndex = pIndex;
4357 pIndex = 0;
4358 }
danielk1977d8123362004-06-12 09:25:124359 goto exit_create_index;
4360 }
4361 }
4362 }
4363
danc9461ec2018-08-29 21:00:164364 if( !IN_RENAME_OBJECT ){
drhd78eeee2001-09-13 16:18:534365
dancf8f2892018-08-09 20:47:014366 /* Link the new Index structure to its table and to the other
larrybrbc917382023-06-07 08:40:314367 ** in-memory database structures.
drh063336a2004-11-05 20:58:394368 */
dancf8f2892018-08-09 20:47:014369 assert( pParse->nErr==0 );
4370 if( db->init.busy ){
4371 Index *p;
4372 assert( !IN_SPECIAL_PARSE );
4373 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
dancf8f2892018-08-09 20:47:014374 if( pTblName!=0 ){
4375 pIndex->tnum = db->init.newTnum;
drh8d406732019-01-30 18:33:334376 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
drh8bf41262019-01-30 19:50:074377 sqlite3ErrorMsg(pParse, "invalid rootpage");
drh8d406732019-01-30 18:33:334378 pParse->rc = SQLITE_CORRUPT_BKPT;
4379 goto exit_create_index;
4380 }
dancf8f2892018-08-09 20:47:014381 }
larrybrbc917382023-06-07 08:40:314382 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
danda7a4c02019-01-30 19:12:134383 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;
drh75897232000-05-29 14:26:004390 }
drh063336a2004-11-05 20:58:394391
dancf8f2892018-08-09 20:47:014392 /* 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
drh1e32bed2020-06-19 13:33:534395 ** 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
dancf8f2892018-08-09 20:47:014397 ** 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.
drh063336a2004-11-05 20:58:394404 */
dancf8f2892018-08-09 20:47:014405 else if( HasRowid(pTab) || pTblName!=0 ){
4406 Vdbe *v;
4407 char *zStmt;
4408 int iMem = ++pParse->nMem;
drh063336a2004-11-05 20:58:394409
dancf8f2892018-08-09 20:47:014410 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
larrybrbc917382023-06-07 08:40:314416 ** 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
dancf8f2892018-08-09 20:47:014419 ** that case the convertToWithoutRowidTable() routine will replace
4420 ** the Noop with a Goto to jump over the VDBE code generated below. */
drhabc38152020-07-22 13:38:044421 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
dancf8f2892018-08-09 20:47:014422 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 */
drh55f66b32019-07-16 19:44:324427 assert( pName!=0 || pStart==0 );
dancf8f2892018-08-09 20:47:014428 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
drh1e32bed2020-06-19 13:33:534440 /* Add an entry in sqlite_schema for this index
dancf8f2892018-08-09 20:47:014441 */
larrybrbc917382023-06-07 08:40:314442 sqlite3NestedParse(pParse,
drhfd4bf772021-12-03 14:43:494443 "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 );
dancf8f2892018-08-09 20:47:014450 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,
dan6a5a13d2021-02-17 20:08:224459 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
dancf8f2892018-08-09 20:47:014460 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
4461 }
4462
drhabc38152020-07-22 13:38:044463 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
drh5e00f6c2001-09-13 13:46:564464 }
drh75897232000-05-29 14:26:004465 }
drh234c39d2004-07-24 03:30:474466 if( db->init.busy || pTblName==0 ){
drhd35bdd62019-12-15 02:49:324467 pIndex->pNext = pTab->pIndex;
4468 pTab->pIndex = pIndex;
drh234c39d2004-07-24 03:30:474469 pIndex = 0;
danielk1977d8123362004-06-12 09:25:124470 }
danc9461ec2018-08-29 21:00:164471 else if( IN_RENAME_OBJECT ){
dancf8f2892018-08-09 20:47:014472 assert( pParse->pNewIndex==0 );
4473 pParse->pNewIndex = pIndex;
4474 pIndex = 0;
4475 }
danielk1977d8123362004-06-12 09:25:124476
drh75897232000-05-29 14:26:004477 /* Clean up before exiting */
4478exit_create_index:
dancf8f2892018-08-09 20:47:014479 if( pIndex ) sqlite3FreeIndex(db, pIndex);
drh97060e52021-03-21 17:52:474480 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. */
drhe85e1da2021-10-01 21:01:074485 Index **ppFrom;
drhd35bdd62019-12-15 02:49:324486 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 }
drh97060e52021-03-21 17:52:474498#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
drhd35bdd62019-12-15 02:49:324508 }
drh1fe05372013-07-31 18:12:264509 sqlite3ExprDelete(db, pPIWhere);
drh633e6d52008-07-28 19:34:534510 sqlite3ExprListDelete(db, pList);
4511 sqlite3SrcListDelete(db, pTblName);
4512 sqlite3DbFree(db, zName);
drh75897232000-05-29 14:26:004513}
4514
4515/*
drh51147ba2005-07-23 22:59:554516** Fill the Index.aiRowEst[] array with default information - information
drh91124b32005-08-18 18:15:054517** to be used when we have not run the ANALYZE command.
drh28c4cf42005-07-27 20:41:434518**
peter.d.reid60ec9142014-09-06 16:39:464519** aiRowEst[0] is supposed to contain the number of elements in the index.
drh28c4cf42005-07-27 20:41:434520** 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
dancfc9df72014-04-25 15:01:014523** of rows that match any particular combination of the first 2 columns
drh28c4cf42005-07-27 20:41:434524** 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.
drh51147ba2005-07-23 22:59:554532*/
4533void sqlite3DefaultRowEst(Index *pIdx){
drh56c65c92020-05-28 00:45:164534 /* 10, 9, 8, 7, 6 */
4535 static const LogEst aVal[] = { 33, 32, 30, 28, 26 };
dancfc9df72014-04-25 15:01:014536 LogEst *a = pIdx->aiRowLogEst;
drh56c65c92020-05-28 00:45:164537 LogEst x;
dancfc9df72014-04-25 15:01:014538 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
drh51147ba2005-07-23 22:59:554539 int i;
dancfc9df72014-04-25 15:01:014540
drh33bec3f2017-02-17 13:38:154541 /* Indexes with default row estimates should not have stat1 data */
4542 assert( !pIdx->hasStat1 );
4543
larrybrbc917382023-06-07 08:40:314544 /* Set the first entry (number of rows in the index) to the estimated
drh8dc570b2016-06-08 18:07:214545 ** number of rows in the table, or half the number of rows in the table
drh56c65c92020-05-28 00:45:164546 ** 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
drh8c1fbe82020-08-11 17:20:024552 ** stat1 data to be ignored by the query planner.
drh56c65c92020-05-28 00:45:164553 */
4554 x = pIdx->pTable->nRowLogEst;
4555 assert( 99==sqlite3LogEst(1000) );
4556 if( x<99 ){
4557 pIdx->pTable->nRowLogEst = x = 99;
4558 }
drh5f086dd2021-04-29 13:37:364559 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
drh56c65c92020-05-28 00:45:164560 a[0] = x;
dan264d2b92014-04-29 19:01:574561
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. */
dancfc9df72014-04-25 15:01:014564 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
dan264d2b92014-04-29 19:01:574565 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
4566 a[i] = 23; assert( 23==sqlite3LogEst(5) );
drh28c4cf42005-07-27 20:41:434567 }
dan264d2b92014-04-29 19:01:574568
4569 assert( 0==sqlite3LogEst(1) );
drh5f1d1d92014-07-31 22:59:044570 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
drh51147ba2005-07-23 22:59:554571}
4572
4573/*
drh74e24cd2002-01-09 03:19:594574** This routine will drop an existing named index. This routine
4575** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:004576*/
drh4d91a702006-01-04 15:54:364577void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
drh75897232000-05-29 14:26:004578 Index *pIndex;
drh75897232000-05-29 14:26:004579 Vdbe *v;
drh9bb575f2004-09-06 17:24:114580 sqlite3 *db = pParse->db;
danielk1977da184232006-01-05 11:34:324581 int iDb;
drh75897232000-05-29 14:26:004582
drh8af73d42009-05-13 22:58:284583 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:204584 goto exit_drop_index;
4585 }
drh3cdb1392022-01-24 12:48:544586 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
drhd24cc422003-03-27 12:51:244587 assert( pName->nSrc==1 );
drh8797bd62024-08-17 19:46:494588 assert( pName->a[0].fg.fixedSchema==0 );
drh692c1602024-08-20 19:09:594589 assert( pName->a[0].fg.isSubquery==0 );
danielk1977d5d56522005-03-16 12:15:204590 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
4591 goto exit_drop_index;
4592 }
drh8797bd62024-08-17 19:46:494593 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].u4.zDatabase);
drh75897232000-05-29 14:26:004594 if( pIndex==0 ){
drh4d91a702006-01-04 15:54:364595 if( !ifExists ){
drha9799932021-03-19 13:00:284596 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
dan57966752011-04-09 17:32:584597 }else{
drh8797bd62024-08-17 19:46:494598 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
drh31da7be2021-05-13 18:24:224599 sqlite3ForceNotReadOnly(pParse);
drh4d91a702006-01-04 15:54:364600 }
drha6ecd332004-06-10 00:29:094601 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:244602 goto exit_drop_index;
drh75897232000-05-29 14:26:004603 }
drh48dd1d82014-05-27 18:18:584604 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
danielk19774adee202004-05-08 08:23:194605 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:524606 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:244607 goto exit_drop_index;
4608 }
danielk1977da184232006-01-05 11:34:324609 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
drhe5f9c642003-01-13 23:27:314610#ifndef SQLITE_OMIT_AUTHORIZATION
4611 {
4612 int code = SQLITE_DROP_INDEX;
4613 Table *pTab = pIndex->pTable;
drh69c33822016-08-18 14:33:114614 const char *zDb = db->aDb[iDb].zDbSName;
danielk1977da184232006-01-05 11:34:324615 const char *zTab = SCHEMA_TABLE(iDb);
danielk19774adee202004-05-08 08:23:194616 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:244617 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:314618 }
drh93fd5422021-06-14 20:41:204619 if( !OMIT_TEMPDB && iDb==1 ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:194620 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:244621 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:314622 }
drhed6c8672003-01-12 18:02:164623 }
drhe5f9c642003-01-13 23:27:314624#endif
drh75897232000-05-29 14:26:004625
drh067b92b2020-06-19 15:24:124626 /* Generate code to remove the index and from the schema table */
danielk19774adee202004-05-08 08:23:194627 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:004628 if( v ){
drh77658e22007-12-04 16:54:524629 sqlite3BeginWriteOperation(pParse, 1, iDb);
drhb17131a2004-11-05 22:18:494630 sqlite3NestedParse(pParse,
drha4a871c2021-11-04 14:04:204631 "DELETE FROM %Q." LEGACY_SCHEMA_TABLE " WHERE name=%Q AND type='index'",
drh346a70c2020-06-15 20:27:354632 db->aDb[iDb].zDbSName, pIndex->zName
drhb17131a2004-11-05 22:18:494633 );
drha5ae4c32011-08-07 01:31:524634 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
drh9cbf3422008-01-17 16:22:134635 sqlite3ChangeCookie(pParse, iDb);
drhb17131a2004-11-05 22:18:494636 destroyRootPage(pParse, pIndex->tnum, iDb);
drh66a51672008-01-03 00:01:234637 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
drh75897232000-05-29 14:26:004638 }
4639
drhd24cc422003-03-27 12:51:244640exit_drop_index:
drh633e6d52008-07-28 19:34:534641 sqlite3SrcListDelete(db, pName);
drh75897232000-05-29 14:26:004642}
4643
4644/*
dan9ace1122012-03-29 07:51:454645** 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.
drh13449892005-09-07 21:22:454648**
dan9ace1122012-03-29 07:51:454649** 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).
drh13449892005-09-07 21:22:454652**
dan9ace1122012-03-29 07:51:454653** 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.
drh13449892005-09-07 21:22:454657**
dan9ace1122012-03-29 07:51:454658** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
4659** unchanged and a copy of pArray returned.
drh13449892005-09-07 21:22:454660*/
drhcf643722007-03-27 13:36:374661void *sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:384662 sqlite3 *db, /* Connection to notify of malloc failures */
drhcf643722007-03-27 13:36:374663 void *pArray, /* Array of objects. Might be reallocated */
4664 int szEntry, /* Size of each object in the array */
drhcf643722007-03-27 13:36:374665 int *pnEntry, /* Number of objects currently in use */
drhcf643722007-03-27 13:36:374666 int *pIdx /* Write the index of a new slot here */
4667){
4668 char *z;
drhf6ad2012019-04-13 14:07:574669 sqlite3_int64 n = *pIdx = *pnEntry;
drh6c535152012-02-02 03:38:304670 if( (n & (n-1))==0 ){
drh0aa32312019-04-13 04:01:124671 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
drh6c535152012-02-02 03:38:304672 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
drh13449892005-09-07 21:22:454673 if( pNew==0 ){
drhcf643722007-03-27 13:36:374674 *pIdx = -1;
4675 return pArray;
drh13449892005-09-07 21:22:454676 }
drhcf643722007-03-27 13:36:374677 pArray = pNew;
drh13449892005-09-07 21:22:454678 }
drhcf643722007-03-27 13:36:374679 z = (char*)pArray;
drh6c535152012-02-02 03:38:304680 memset(&z[n * szEntry], 0, szEntry);
drhcf643722007-03-27 13:36:374681 ++*pnEntry;
4682 return pArray;
drh13449892005-09-07 21:22:454683}
4684
4685/*
drh75897232000-05-29 14:26:004686** Append a new element to the given IdList. Create a new IdList if
4687** need be.
drhdaffd0e2001-04-11 14:28:424688**
4689** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:004690*/
dan5496d6a2018-08-13 17:14:264691IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
4692 sqlite3 *db = pParse->db;
drh13449892005-09-07 21:22:454693 int i;
drh75897232000-05-29 14:26:004694 if( pList==0 ){
drhcebf06c2025-03-14 18:10:024695 pList = sqlite3DbMallocZero(db, SZ_IDLIST(1));
drh75897232000-05-29 14:26:004696 if( pList==0 ) return 0;
drha99e3252022-04-15 15:47:144697 }else{
4698 IdList *pNew;
drhcebf06c2025-03-14 18:10:024699 pNew = sqlite3DbRealloc(db, pList, SZ_IDLIST(pList->nId+1));
drha99e3252022-04-15 15:47:144700 if( pNew==0 ){
4701 sqlite3IdListDelete(db, pList);
4702 return 0;
4703 }
4704 pList = pNew;
drh75897232000-05-29 14:26:004705 }
drha99e3252022-04-15 15:47:144706 i = pList->nId++;
drh17435752007-08-16 04:30:384707 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
danc9461ec2018-08-29 21:00:164708 if( IN_RENAME_OBJECT && pList->a[i].zName ){
dan07e95232018-08-21 16:32:534709 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
dan5496d6a2018-08-13 17:14:264710 }
drh75897232000-05-29 14:26:004711 return pList;
4712}
4713
4714/*
drhfe05af82005-07-21 03:14:594715** Delete an IdList.
4716*/
drh633e6d52008-07-28 19:34:534717void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
drhfe05af82005-07-21 03:14:594718 int i;
drh41ce47c2022-08-22 02:00:264719 assert( db!=0 );
drhfe05af82005-07-21 03:14:594720 if( pList==0 ) return;
drhfe05af82005-07-21 03:14:594721 for(i=0; i<pList->nId; i++){
drh633e6d52008-07-28 19:34:534722 sqlite3DbFree(db, pList->a[i].zName);
drhfe05af82005-07-21 03:14:594723 }
drh41ce47c2022-08-22 02:00:264724 sqlite3DbNNFreeNN(db, pList);
drhfe05af82005-07-21 03:14:594725}
4726
4727/*
4728** Return the index in pList of the identifier named zId. Return -1
4729** if not found.
4730*/
4731int sqlite3IdListIndex(IdList *pList, const char *zName){
4732 int i;
drhd44f8b22022-04-07 01:11:134733 assert( pList!=0 );
drhfe05af82005-07-21 03:14:594734 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/*
drh0ad7aa82019-01-17 14:34:464741** 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/*
drha78c22c2008-11-11 18:28:584753** 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**
drh29c992c2019-01-17 15:40:414768** 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.
drha78c22c2008-11-11 18:28:584771*/
4772SrcList *sqlite3SrcListEnlarge(
drh29c992c2019-01-17 15:40:414773 Parse *pParse, /* Parsing context into which errors are reported */
drha78c22c2008-11-11 18:28:584774 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 );
drh8af73d42009-05-13 22:58:284783 assert( pSrc!=0 );
4784 assert( iStart<=pSrc->nSrc );
drha78c22c2008-11-11 18:28:584785
4786 /* Allocate additional space if needed */
drhfc5717c2014-03-05 19:04:464787 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
drha78c22c2008-11-11 18:28:584788 SrcList *pNew;
drh0aa32312019-04-13 04:01:124789 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
drh29c992c2019-01-17 15:40:414790 sqlite3 *db = pParse->db;
drh0ad7aa82019-01-17 14:34:464791
4792 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
drh29c992c2019-01-17 15:40:414793 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
4794 SQLITE_MAX_SRCLIST);
4795 return 0;
drh0ad7aa82019-01-17 14:34:464796 }
4797 if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
drhcebf06c2025-03-14 18:10:024798 pNew = sqlite3DbRealloc(db, pSrc, SZ_SRCLIST(nAlloc));
drha78c22c2008-11-11 18:28:584799 if( pNew==0 ){
4800 assert( db->mallocFailed );
drh29c992c2019-01-17 15:40:414801 return 0;
drha78c22c2008-11-11 18:28:584802 }
4803 pSrc = pNew;
drhd0ee3a12019-02-06 01:18:364804 pSrc->nAlloc = nAlloc;
drha78c22c2008-11-11 18:28:584805 }
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 }
drh6d1626e2014-03-05 15:52:434812 pSrc->nSrc += nExtra;
drha78c22c2008-11-11 18:28:584813
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/*
drhad3cab52002-05-24 02:04:324826** Append a new table name to the given SrcList. Create a new SrcList if
drhb7916a72009-05-27 10:31:294827** need be. A new entry is created in the SrcList even if pTable is NULL.
drhad3cab52002-05-24 02:04:324828**
drh29c992c2019-01-17 15:40:414829** A SrcList is returned, or NULL if there is an OOM error or if the
4830** SrcList grows to large. The returned
drha78c22c2008-11-11 18:28:584831** 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.
drh113088e2003-03-20 01:16:584834**
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
larrybrbc917382023-06-07 08:40:314839** come from pTable (if pDatabase is NULL) or from pDatabase.
drh113088e2003-03-20 01:16:584840** 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**
drh17435752007-08-16 04:30:384845** sqlite3SrcListAppend(D,A,B,0);
drh113088e2003-03-20 01:16:584846**
4847** Then B is a table name and the database name is unspecified. If called
4848** like this:
4849**
drh17435752007-08-16 04:30:384850** sqlite3SrcListAppend(D,A,B,C);
drh113088e2003-03-20 01:16:584851**
drhd3001712009-05-12 17:46:534852** 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);
drhb7916a72009-05-27 10:31:294856**
4857** Both pTable and pDatabase are assumed to be quoted. They are dequoted
4858** before being added to the SrcList.
drhad3cab52002-05-24 02:04:324859*/
drh17435752007-08-16 04:30:384860SrcList *sqlite3SrcListAppend(
drh29c992c2019-01-17 15:40:414861 Parse *pParse, /* Parsing context, in which errors are reported */
drh17435752007-08-16 04:30:384862 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){
drh76012942021-02-21 21:04:544866 SrcItem *pItem;
drh29c992c2019-01-17 15:40:414867 sqlite3 *db;
drhd3001712009-05-12 17:46:534868 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
drh29c992c2019-01-17 15:40:414869 assert( pParse!=0 );
4870 assert( pParse->db!=0 );
4871 db = pParse->db;
drhad3cab52002-05-24 02:04:324872 if( pList==0 ){
drhcebf06c2025-03-14 18:10:024873 pList = sqlite3DbMallocRawNN(pParse->db, SZ_SRCLIST(1));
drhad3cab52002-05-24 02:04:324874 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:124875 pList->nAlloc = 1;
drhac178b32016-12-14 11:14:134876 pList->nSrc = 1;
4877 memset(&pList->a[0], 0, sizeof(pList->a[0]));
4878 pList->a[0].iCursor = -1;
4879 }else{
drh29c992c2019-01-17 15:40:414880 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 }
drhad3cab52002-05-24 02:04:324887 }
drha78c22c2008-11-11 18:28:584888 pItem = &pList->a[pList->nSrc-1];
drh113088e2003-03-20 01:16:584889 if( pDatabase && pDatabase->z==0 ){
4890 pDatabase = 0;
4891 }
drh8797bd62024-08-17 19:46:494892 assert( pItem->fg.fixedSchema==0 );
drh692c1602024-08-20 19:09:594893 assert( pItem->fg.isSubquery==0 );
drhd3001712009-05-12 17:46:534894 if( pDatabase ){
drh169a6892017-07-06 01:02:094895 pItem->zName = sqlite3NameFromToken(db, pDatabase);
drh8797bd62024-08-17 19:46:494896 pItem->u4.zDatabase = sqlite3NameFromToken(db, pTable);
drh169a6892017-07-06 01:02:094897 }else{
4898 pItem->zName = sqlite3NameFromToken(db, pTable);
drh8797bd62024-08-17 19:46:494899 pItem->u4.zDatabase = 0;
drh113088e2003-03-20 01:16:584900 }
drhad3cab52002-05-24 02:04:324901 return pList;
4902}
4903
4904/*
drhdfe88ec2008-11-03 20:55:064905** Assign VdbeCursor index numbers to all tables in a SrcList
drh63eb5f22003-04-29 16:20:444906*/
danielk19774adee202004-05-08 08:23:194907void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:444908 int i;
drh76012942021-02-21 21:04:544909 SrcItem *pItem;
drh9da977f2021-04-20 12:14:124910 assert( pList || pParse->db->mallocFailed );
4911 if( ALWAYS(pList) ){
danielk1977261919c2005-12-06 12:52:594912 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
drh34055852020-10-19 01:23:484913 if( pItem->iCursor>=0 ) continue;
danielk1977261919c2005-12-06 12:52:594914 pItem->iCursor = pParse->nTab++;
drh1521ca42024-08-19 22:48:304915 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);
danielk1977261919c2005-12-06 12:52:594920 }
drh63eb5f22003-04-29 16:20:444921 }
4922 }
4923}
4924
4925/*
drh1521ca42024-08-19 22:48:304926** Delete a Subquery object and its substructure.
4927*/
4928void 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*/
4938Select *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/*
drhad3cab52002-05-24 02:04:324950** Delete an entire SrcList including all its substructure.
4951*/
drh633e6d52008-07-28 19:34:534952void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
drhad3cab52002-05-24 02:04:324953 int i;
drh76012942021-02-21 21:04:544954 SrcItem *pItem;
drh41ce47c2022-08-22 02:00:264955 assert( db!=0 );
drhad3cab52002-05-24 02:04:324956 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:094957 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
drhb204b6a2024-08-17 23:23:234958
4959 /* Check invariants on SrcItem */
drh692c1602024-08-20 19:09:594960 assert( !pItem->fg.isIndexedBy || !pItem->fg.isTabFunc );
4961 assert( !pItem->fg.isCte || !pItem->fg.isIndexedBy );
drh692c1602024-08-20 19:09:594962 assert( !pItem->fg.fixedSchema || !pItem->fg.isSubquery );
4963 assert( !pItem->fg.isSubquery || (pItem->u4.pSubq!=0 &&
4964 pItem->u4.pSubq->pSelect!=0) );
drhb204b6a2024-08-17 23:23:234965
drh41ce47c2022-08-22 02:00:264966 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
4967 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
drh1521ca42024-08-19 22:48:304968 if( pItem->fg.isSubquery ){
4969 sqlite3SubqueryDelete(db, pItem->u4.pSubq);
4970 }else if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
drh8797bd62024-08-17 19:46:494971 sqlite3DbNNFreeNN(db, pItem->u4.zDatabase);
4972 }
drh8a48b9c2015-08-19 15:20:004973 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
4974 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
drhb204b6a2024-08-17 23:23:234975 sqlite3DeleteTable(db, pItem->pSTab);
drhd44f8b22022-04-07 01:11:134976 if( pItem->fg.isUsing ){
4977 sqlite3IdListDelete(db, pItem->u3.pUsing);
4978 }else if( pItem->u3.pOn ){
4979 sqlite3ExprDelete(db, pItem->u3.pOn);
4980 }
drh75897232000-05-29 14:26:004981 }
drh41ce47c2022-08-22 02:00:264982 sqlite3DbNNFreeNN(db, pList);
drh75897232000-05-29 14:26:004983}
4984
drh982cef72000-05-30 16:27:034985/*
drh1521ca42024-08-19 22:48:304986** 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*/
5000int 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;
drh0766cbf2024-08-20 20:01:215007 assert( pSelect!=0 );
drh1521ca42024-08-19 22:48:305008 assert( pItem->fg.isSubquery==0 );
drhff4ad292024-08-20 16:50:215009 if( pItem->fg.fixedSchema ){
5010 pItem->u4.pSchema = 0;
5011 pItem->fg.fixedSchema = 0;
5012 }else if( pItem->u4.zDatabase!=0 ){
drh1521ca42024-08-19 22:48:305013 sqlite3DbFree(pParse->db, pItem->u4.zDatabase);
5014 pItem->u4.zDatabase = 0;
5015 }
drh1521ca42024-08-19 22:48:305016 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/*
drh61dfc312006-12-16 16:25:155034** 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.reid60ec9142014-09-06 16:39:465040** usual case. If the term has an alias, then pAlias points to the
drh61dfc312006-12-16 16:25:155041** 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*/
5049SrcList *sqlite3SrcListAppendFromTerm(
drh17435752007-08-16 04:30:385050 Parse *pParse, /* Parsing context */
drh61dfc312006-12-16 16:25:155051 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 */
drhd44f8b22022-04-07 01:11:135056 OnOrUsing *pOnUsing /* Either the ON clause or the USING clause */
drh61dfc312006-12-16 16:25:155057){
drh76012942021-02-21 21:04:545058 SrcItem *pItem;
drh17435752007-08-16 04:30:385059 sqlite3 *db = pParse->db;
drhd44f8b22022-04-07 01:11:135060 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
larrybrbc917382023-06-07 08:40:315061 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
drhd44f8b22022-04-07 01:11:135062 (pOnUsing->pOn ? "ON" : "USING")
danielk1977bd1a0a42009-07-01 16:12:075063 );
5064 goto append_from_error;
5065 }
drh29c992c2019-01-17 15:40:415066 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
drh9d9c41e2017-10-31 03:40:155067 if( p==0 ){
danielk1977bd1a0a42009-07-01 16:12:075068 goto append_from_error;
drh61dfc312006-12-16 16:25:155069 }
drh9d9c41e2017-10-31 03:40:155070 assert( p->nSrc>0 );
drh61dfc312006-12-16 16:25:155071 pItem = &p->a[p->nSrc-1];
drha488ec92018-09-07 18:52:255072 assert( (pTable==0)==(pDatabase==0) );
5073 assert( pItem->zName==0 || pDatabase!=0 );
danc9461ec2018-08-29 21:00:165074 if( IN_RENAME_OBJECT && pItem->zName ){
drha488ec92018-09-07 18:52:255075 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
danc9461ec2018-08-29 21:00:165076 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
5077 }
drh8af73d42009-05-13 22:58:285078 assert( pAlias!=0 );
5079 if( pAlias->n ){
drh17435752007-08-16 04:30:385080 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
drh61dfc312006-12-16 16:25:155081 }
drh1521ca42024-08-19 22:48:305082 assert( pSubquery==0 || pDatabase==0 );
drh815b7822022-04-20 15:07:395083 if( pSubquery ){
drh1521ca42024-08-19 22:48:305084 if( sqlite3SrcItemAttachSubquery(pParse, pItem, pSubquery, 0) ){
5085 if( pSubquery->selFlags & SF_NestedFrom ){
5086 pItem->fg.isNestedFrom = 1;
5087 }
drh815b7822022-04-20 15:07:395088 }
5089 }
drhd44f8b22022-04-07 01:11:135090 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 }
drh61dfc312006-12-16 16:25:155100 return p;
danielk1977bd1a0a42009-07-01 16:12:075101
drh46658d72021-12-08 18:50:305102append_from_error:
danielk1977bd1a0a42009-07-01 16:12:075103 assert( p==0 );
drhd44f8b22022-04-07 01:11:135104 sqlite3ClearOnOrUsing(db, pOnUsing);
danielk1977bd1a0a42009-07-01 16:12:075105 sqlite3SelectDelete(db, pSubquery);
5106 return 0;
drh61dfc312006-12-16 16:25:155107}
5108
5109/*
larrybrbc917382023-06-07 08:40:315110** Add an INDEXED BY or NOT INDEXED clause to the most recently added
danielk1977b1c685b2008-10-06 16:18:395111** element of the source-list passed as the second argument.
5112*/
5113void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
drh8af73d42009-05-13 22:58:285114 assert( pIndexedBy!=0 );
drh8abc80b2017-08-12 01:09:065115 if( p && pIndexedBy->n>0 ){
drh76012942021-02-21 21:04:545116 SrcItem *pItem;
drh8abc80b2017-08-12 01:09:065117 assert( p->nSrc>0 );
5118 pItem = &p->a[p->nSrc-1];
drh8a48b9c2015-08-19 15:20:005119 assert( pItem->fg.notIndexed==0 );
5120 assert( pItem->fg.isIndexedBy==0 );
5121 assert( pItem->fg.isTabFunc==0 );
danielk1977b1c685b2008-10-06 16:18:395122 if( pIndexedBy->n==1 && !pIndexedBy->z ){
larrybrbc917382023-06-07 08:40:315123 /* A "NOT INDEXED" clause was supplied. See parse.y
danielk1977b1c685b2008-10-06 16:18:395124 ** construct "indexed_opt" for details. */
drh8a48b9c2015-08-19 15:20:005125 pItem->fg.notIndexed = 1;
danielk1977b1c685b2008-10-06 16:18:395126 }else{
drh8a48b9c2015-08-19 15:20:005127 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
drh8abc80b2017-08-12 01:09:065128 pItem->fg.isIndexedBy = 1;
drhdbfbb5a2021-10-07 23:04:505129 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
danielk1977b1c685b2008-10-06 16:18:395130 }
5131 }
5132}
5133
5134/*
dan69887c92020-04-27 20:55:335135** 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.
larrybrbc917382023-06-07 08:40:315138*/
dan69887c92020-04-27 20:55:335139SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
dan5525ac12024-06-07 21:00:425140 assert( p1 );
drh449b3452025-07-08 17:28:095141 assert( p2 || pParse->nErr );
5142 assert( p2==0 || p2->nSrc>=1 );
5143 testcase( p1->nSrc==0 );
dan8b023cf2020-04-30 18:28:405144 if( p2 ){
dan5525ac12024-06-07 21:00:425145 int nOld = p1->nSrc;
5146 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, nOld);
dan8b023cf2020-04-30 18:28:405147 if( pNew==0 ){
5148 sqlite3SrcListDelete(pParse->db, p2);
5149 }else{
5150 p1 = pNew;
dan5525ac12024-06-07 21:00:425151 memcpy(&p1->a[nOld], p2->a, p2->nSrc*sizeof(SrcItem));
drh449b3452025-07-08 17:28:095152 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);
drh525326e2020-07-15 21:53:535155 sqlite3DbFree(pParse->db, p2);
dan69887c92020-04-27 20:55:335156 }
5157 }
5158 return p1;
5159}
5160
5161/*
drh01d230c2015-08-19 17:11:375162** Add the list of function arguments to the SrcList entry for a
5163** table-valued-function.
5164*/
5165void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
drh20292312015-11-21 13:24:465166 if( p ){
drh76012942021-02-21 21:04:545167 SrcItem *pItem = &p->a[p->nSrc-1];
drh01d230c2015-08-19 17:11:375168 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;
drhd8b1bfc2015-08-20 23:21:345173 }else{
5174 sqlite3ExprListDelete(pParse->db, pList);
drh01d230c2015-08-19 17:11:375175 }
5176}
5177
5178/*
drh61dfc312006-12-16 16:25:155179** 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.
drh62ed36b2022-04-10 20:28:415192**
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.
drh61dfc312006-12-16 16:25:155199*/
drhfdc621a2022-04-16 19:13:165200void sqlite3SrcListShiftJoinType(Parse *pParse, SrcList *p){
drh6dab33b2022-04-21 19:25:515201 (void)pParse;
drh62ed36b2022-04-10 20:28:415202 if( p && p->nSrc>1 ){
5203 int i = p->nSrc-1;
drha76ac882022-04-08 19:20:125204 u8 allFlags = 0;
drh62ed36b2022-04-10 20:28:415205 do{
drha76ac882022-04-08 19:20:125206 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
drh62ed36b2022-04-10 20:28:415207 }while( (--i)>0 );
drh8a48b9c2015-08-19 15:20:005208 p->a[0].fg.jointype = 0;
drha76ac882022-04-08 19:20:125209
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{
drhfdc621a2022-04-16 19:13:165217 p->a[i].fg.jointype |= JT_LTORJ;
5218 }while( (--i)>=0 );
drha76ac882022-04-08 19:20:125219 }
drh61dfc312006-12-16 16:25:155220 }
5221}
5222
5223/*
drhb0c88652016-02-01 13:21:135224** Generate VDBE code for a BEGIN statement.
drhc4a3c772001-04-04 11:48:575225*/
drh684917c2004-10-05 02:41:425226void sqlite3BeginTransaction(Parse *pParse, int type){
drh9bb575f2004-09-06 17:24:115227 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:495228 Vdbe *v;
drh684917c2004-10-05 02:41:425229 int i;
drh5e00f6c2001-09-13 13:46:565230
drhd3001712009-05-12 17:46:535231 assert( pParse!=0 );
5232 db = pParse->db;
5233 assert( db!=0 );
drhd3001712009-05-12 17:46:535234 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
5235 return;
5236 }
danielk19771d850a72004-05-31 08:26:495237 v = sqlite3GetVdbe(pParse);
5238 if( !v ) return;
drh684917c2004-10-05 02:41:425239 if( type!=TK_DEFERRED ){
5240 for(i=0; i<db->nDb; i++){
drh1ca037f2020-10-12 13:24:005241 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);
drhfb982642007-08-30 01:19:595251 sqlite3VdbeUsesBtree(v, i);
drh684917c2004-10-05 02:41:425252 }
5253 }
drhb0c88652016-02-01 13:21:135254 sqlite3VdbeAddOp0(v, OP_AutoCommit);
drhc4a3c772001-04-04 11:48:575255}
5256
5257/*
drh07a3b112017-07-06 01:28:025258** 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.
drhc4a3c772001-04-04 11:48:575261*/
drh07a3b112017-07-06 01:28:025262void sqlite3EndTransaction(Parse *pParse, int eType){
danielk19771d850a72004-05-31 08:26:495263 Vdbe *v;
drh07a3b112017-07-06 01:28:025264 int isRollback;
drh5e00f6c2001-09-13 13:46:565265
drhd3001712009-05-12 17:46:535266 assert( pParse!=0 );
drhb07028f2011-10-14 21:49:185267 assert( pParse->db!=0 );
drh07a3b112017-07-06 01:28:025268 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
5269 isRollback = eType==TK_ROLLBACK;
larrybrbc917382023-06-07 08:40:315270 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
drh07a3b112017-07-06 01:28:025271 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
drhd3001712009-05-12 17:46:535272 return;
5273 }
danielk19771d850a72004-05-31 08:26:495274 v = sqlite3GetVdbe(pParse);
5275 if( v ){
drh07a3b112017-07-06 01:28:025276 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
drh02f75f12004-02-24 01:04:115277 }
drhc4a3c772001-04-04 11:48:575278}
drhf57b14a2001-09-14 18:54:085279
5280/*
danielk1977fd7f0452008-12-17 17:30:265281** This function is called by the parser when it parses a command to create,
larrybrbc917382023-06-07 08:40:315282** release or rollback an SQL savepoint.
danielk1977fd7f0452008-12-17 17:30:265283*/
5284void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
danielk1977ab9b7032008-12-30 06:24:585285 char *zName = sqlite3NameFromToken(pParse->db, pName);
5286 if( zName ){
5287 Vdbe *v = sqlite3GetVdbe(pParse);
5288#ifndef SQLITE_OMIT_AUTHORIZATION
dan558814f2010-06-02 05:53:535289 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
danielk1977ab9b7032008-12-30 06:24:585290 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);
danielk1977fd7f0452008-12-17 17:30:265297 }
5298}
5299
5300/*
drhdc3ff9c2004-08-18 02:10:155301** 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*/
danielk1977ddfb2f02006-02-17 12:25:145304int sqlite3OpenTempDatabase(Parse *pParse){
drhdc3ff9c2004-08-18 02:10:155305 sqlite3 *db = pParse->db;
5306 if( db->aDb[1].pBt==0 && !pParse->explain ){
drh33f4e022007-09-03 15:19:345307 int rc;
drh10a76c92010-01-26 01:25:265308 Btree *pBt;
larrybrbc917382023-06-07 08:40:315309 static const int flags =
drh33f4e022007-09-03 15:19:345310 SQLITE_OPEN_READWRITE |
5311 SQLITE_OPEN_CREATE |
5312 SQLITE_OPEN_EXCLUSIVE |
5313 SQLITE_OPEN_DELETEONCLOSE |
5314 SQLITE_OPEN_TEMP_DB;
5315
dan3a6d8ae2011-04-23 15:54:545316 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
drhdc3ff9c2004-08-18 02:10:155317 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 }
drh10a76c92010-01-26 01:25:265323 db->aDb[1].pBt = pBt;
danielk197714db2662006-01-09 16:12:045324 assert( db->aDb[1].pSchema );
drhe937df82020-05-07 01:56:575325 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
drh4a642b62016-02-05 01:55:275326 sqlite3OomFault(db);
drh7c9c9862010-01-31 14:18:215327 return 1;
drh10a76c92010-01-26 01:25:265328 }
drhdc3ff9c2004-08-18 02:10:155329 }
5330 return 0;
5331}
5332
5333/*
drhaceb31b2014-02-08 01:40:275334** 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().
drh001bbcb2003-03-19 03:14:005338*/
drh1d8f8922020-08-16 00:30:445339static void sqlite3CodeVerifySchemaAtToplevel(Parse *pToplevel, int iDb){
5340 assert( iDb>=0 && iDb<pToplevel->db->nDb );
5341 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
drh099b3852021-03-10 16:35:375342 assert( iDb<SQLITE_MAX_DB );
drh1d8f8922020-08-16 00:30:445343 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
drha7ab6d82014-07-21 15:44:395344 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
5345 DbMaskSet(pToplevel->cookieMask, iDb);
drhaceb31b2014-02-08 01:40:275346 if( !OMIT_TEMPDB && iDb==1 ){
5347 sqlite3OpenTempDatabase(pToplevel);
5348 }
drh001bbcb2003-03-19 03:14:005349 }
drh001bbcb2003-03-19 03:14:005350}
drh1d8f8922020-08-16 00:30:445351void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
5352 sqlite3CodeVerifySchemaAtToplevel(sqlite3ParseToplevel(pParse), iDb);
5353}
5354
drh001bbcb2003-03-19 03:14:005355
5356/*
larrybrbc917382023-06-07 08:40:315357** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
dan57966752011-04-09 17:32:585358** attached database. Otherwise, invoke it for the database named zDb only.
5359*/
5360void 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];
drh69c33822016-08-18 14:33:115365 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
dan57966752011-04-09 17:32:585366 sqlite3CodeVerifySchema(pParse, i);
5367 }
5368 }
5369}
5370
5371/*
drh1c928532002-01-31 15:54:215372** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:115373** 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
drh7f0f12e2004-05-21 13:39:505377** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:115378** 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.
drh1c928532002-01-31 15:54:215383*/
drh7f0f12e2004-05-21 13:39:505384void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
dan65a7cd12009-09-01 12:16:015385 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh1d8f8922020-08-16 00:30:445386 sqlite3CodeVerifySchemaAtToplevel(pToplevel, iDb);
drha7ab6d82014-07-21 15:44:395387 DbMaskSet(pToplevel->writeMask, iDb);
dane0af83a2009-09-08 19:15:015388 pToplevel->isMultiWrite |= setStatement;
5389}
5390
drhff738bc2009-09-24 00:09:585391/*
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*/
5398void sqlite3MultiWrite(Parse *pParse){
5399 Parse *pToplevel = sqlite3ParseToplevel(pParse);
5400 pToplevel->isMultiWrite = 1;
5401}
5402
larrybrbc917382023-06-07 08:40:315403/*
drhff738bc2009-09-24 00:09:585404** The code generator calls this routine if is discovers that it is
larrybrbc917382023-06-07 08:40:315405** possible to abort a statement prior to completion. In order to
drhff738bc2009-09-24 00:09:585406** 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
larrybrbc917382023-06-07 08:40:315414** makes it more difficult to prove that the code is correct (in
drhff738bc2009-09-24 00:09:585415** 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.
dane0af83a2009-09-08 19:15:015418*/
5419void 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*/
drhd91c1a12013-02-09 13:58:255429void sqlite3HaltConstraint(
5430 Parse *pParse, /* Parsing context */
5431 int errCode, /* extended error code */
5432 int onError, /* Constraint type */
5433 char *p4, /* Error message */
drhf9c8ce32013-11-05 13:33:555434 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
5435 u8 p5Errmsg /* P5_ErrMsg type */
drhd91c1a12013-02-09 13:58:255436){
drh289a0c82020-08-15 22:23:005437 Vdbe *v;
5438 assert( pParse->pVdbe!=0 );
5439 v = sqlite3GetVdbe(pParse);
drh9e5fdc42020-05-08 19:02:215440 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
dane0af83a2009-09-08 19:15:015441 if( onError==OE_Abort ){
5442 sqlite3MayAbort(pParse);
danielk19771d850a72004-05-31 08:26:495443 }
drhd91c1a12013-02-09 13:58:255444 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
drh9b34abe2016-01-16 15:12:355445 sqlite3VdbeChangeP5(v, p5Errmsg);
drhf9c8ce32013-11-05 13:33:555446}
5447
5448/*
5449** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
5450*/
5451void 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
larrybrbc917382023-06-07 08:40:315461 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
drh86ec1ed2019-04-10 00:58:075462 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
drh8b576422015-08-31 23:09:425463 if( pIdx->aColExpr ){
drh0cdbe1a2018-05-09 13:46:265464 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
drh8b576422015-08-31 23:09:425465 }else{
5466 for(j=0; j<pIdx->nKeyCol; j++){
5467 char *zCol;
5468 assert( pIdx->aiColumn[j]>=0 );
drhcf9d36d2021-08-02 18:03:435469 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
drh0cdbe1a2018-05-09 13:46:265470 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);
drh8b576422015-08-31 23:09:425474 }
drhf9c8ce32013-11-05 13:33:555475 }
5476 zErr = sqlite3StrAccumFinish(&errMsg);
larrybrbc917382023-06-07 08:40:315477 sqlite3HaltConstraint(pParse,
5478 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
drh48dd1d82014-05-27 18:18:585479 : SQLITE_CONSTRAINT_UNIQUE,
dan93889d92013-11-06 16:28:595480 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
drhf9c8ce32013-11-05 13:33:555481}
5482
5483
5484/*
5485** Code an OP_Halt due to non-unique rowid.
5486*/
5487void sqlite3RowidConstraint(
5488 Parse *pParse, /* Parsing context */
5489 int onError, /* Conflict resolution algorithm */
larrybrbc917382023-06-07 08:40:315490 Table *pTab /* The table with the non-unique rowid */
drhf9c8ce32013-11-05 13:33:555491){
5492 char *zMsg;
5493 int rc;
5494 if( pTab->iPKey>=0 ){
5495 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
drhcf9d36d2021-08-02 18:03:435496 pTab->aCol[pTab->iPKey].zCnName);
drhf9c8ce32013-11-05 13:33:555497 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);
drh663fc632002-02-02 18:49:195504}
5505
drh4343fea2004-11-05 23:46:155506/*
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
danielk1977b3bf5562006-01-10 17:58:235511static int collationMatch(const char *zColl, Index *pIndex){
5512 int i;
drh04491712009-05-13 17:21:135513 assert( zColl!=0 );
danielk1977b3bf5562006-01-10 17:58:235514 for(i=0; i<pIndex->nColumn; i++){
5515 const char *z = pIndex->azColl[i];
drhbbbdc832013-10-22 18:01:405516 assert( z!=0 || pIndex->aiColumn[i]<0 );
5517 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
danielk1977b3bf5562006-01-10 17:58:235518 return 1;
5519 }
drh4343fea2004-11-05 23:46:155520 }
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
danielk1977b3bf5562006-01-10 17:58:235530static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
dane6370e92019-01-11 17:41:235531 if( !IsVirtual(pTab) ){
5532 Index *pIndex; /* An index associated with pTab */
drh4343fea2004-11-05 23:46:155533
dane6370e92019-01-11 17:41:235534 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 }
drh4343fea2004-11-05 23:46:155540 }
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
danielk1977b3bf5562006-01-10 17:58:235551static void reindexDatabases(Parse *pParse, char const *zColl){
drh4343fea2004-11-05 23:46:155552 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
drh21206082011-04-04 18:22:025558 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
drh4343fea2004-11-05 23:46:155559 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
drh43617e92006-03-06 20:55:465560 assert( pDb!=0 );
danielk1977da184232006-01-05 11:34:325561 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
drh4343fea2004-11-05 23:46:155562 pTab = (Table*)sqliteHashData(k);
danielk1977b3bf5562006-01-10 17:58:235563 reindexTable(pParse, pTab, zColl);
drh4343fea2004-11-05 23:46:155564 }
5565 }
5566}
5567#endif
5568
5569/*
drheee46cf2004-11-06 00:02:485570** 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.
drh4343fea2004-11-05 23:46:155581*/
5582#ifndef SQLITE_OMIT_REINDEX
5583void 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
danielk197733a5edc2005-01-27 00:22:025593 /* 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) ){
danielk1977e63739a2005-01-27 00:33:375596 return;
danielk197733a5edc2005-01-27 00:22:025597 }
5598
drh8af73d42009-05-13 22:58:285599 if( pName1==0 ){
drh4343fea2004-11-05 23:46:155600 reindexDatabases(pParse, 0);
5601 return;
drhd3001712009-05-12 17:46:535602 }else if( NEVER(pName2==0) || pName2->z==0 ){
danielk197739002502007-11-12 09:50:265603 char *zColl;
danielk1977b3bf5562006-01-10 17:58:235604 assert( pName1->z );
danielk197739002502007-11-12 09:50:265605 zColl = sqlite3NameFromToken(pParse->db, pName1);
5606 if( !zColl ) return;
drhc4a64fa2009-05-11 20:53:285607 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
drh4343fea2004-11-05 23:46:155608 if( pColl ){
drhd3001712009-05-12 17:46:535609 reindexDatabases(pParse, zColl);
5610 sqlite3DbFree(db, zColl);
drh4343fea2004-11-05 23:46:155611 return;
5612 }
drh633e6d52008-07-28 19:34:535613 sqlite3DbFree(db, zColl);
drh4343fea2004-11-05 23:46:155614 }
5615 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
5616 if( iDb<0 ) return;
drh17435752007-08-16 04:30:385617 z = sqlite3NameFromToken(db, pObjName);
drh84f31122007-05-12 15:00:145618 if( z==0 ) return;
drhff6905a2024-01-09 12:28:515619 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
drh4343fea2004-11-05 23:46:155620 pTab = sqlite3FindTable(db, z, zDb);
5621 if( pTab ){
5622 reindexTable(pParse, pTab, 0);
drh633e6d52008-07-28 19:34:535623 sqlite3DbFree(db, z);
drh4343fea2004-11-05 23:46:155624 return;
5625 }
5626 pIndex = sqlite3FindIndex(db, z, zDb);
drh633e6d52008-07-28 19:34:535627 sqlite3DbFree(db, z);
drh4343fea2004-11-05 23:46:155628 if( pIndex ){
drhff6905a2024-01-09 12:28:515629 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema);
drh4343fea2004-11-05 23:46:155630 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
danielk1977b3bf5562006-01-10 17:58:235637
5638/*
drh2ec2fb22013-11-06 19:59:235639** Return a KeyInfo structure that is appropriate for the given Index.
danielk1977b3bf5562006-01-10 17:58:235640**
drh2ec2fb22013-11-06 19:59:235641** The caller should invoke sqlite3KeyInfoUnref() on the returned object
5642** when it has finished using it.
danielk1977b3bf5562006-01-10 17:58:235643*/
drh2ec2fb22013-11-06 19:59:235644KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
drh18b67f32014-12-12 00:20:375645 int i;
5646 int nCol = pIdx->nColumn;
5647 int nKey = pIdx->nKeyCol;
5648 KeyInfo *pKey;
drh2ec2fb22013-11-06 19:59:235649 if( pParse->nErr ) return 0;
drh18b67f32014-12-12 00:20:375650 if( pIdx->uniqNotNull ){
5651 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
5652 }else{
5653 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
drh41e13e12013-11-07 14:09:395654 }
drh18b67f32014-12-12 00:20:375655 if( pKey ){
5656 assert( sqlite3KeyInfoIsWriteable(pKey) );
5657 for(i=0; i<nCol; i++){
drhf19aa5f2015-12-30 16:51:205658 const char *zColl = pIdx->azColl[i];
5659 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
drh18b67f32014-12-12 00:20:375660 sqlite3LocateCollSeq(pParse, zColl);
dan6e118922019-08-12 16:36:385661 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
5662 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
drh2ec2fb22013-11-06 19:59:235663 }
drh18b67f32014-12-12 00:20:375664 if( pParse->nErr ){
drh7e8515d2017-12-08 19:37:045665 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 }
drh18b67f32014-12-12 00:20:375677 sqlite3KeyInfoUnref(pKey);
5678 pKey = 0;
danielk1977b3bf5562006-01-10 17:58:235679 }
danielk1977b3bf5562006-01-10 17:58:235680 }
drh18b67f32014-12-12 00:20:375681 return pKey;
danielk1977b3bf5562006-01-10 17:58:235682}
drh8b471862014-01-11 13:22:175683
5684#ifndef SQLITE_OMIT_CTE
drhf824b412021-02-20 14:57:165685/*
5686** Create a new CTE object
5687*/
5688Cte *sqlite3CteNew(
5689 Parse *pParse, /* Parsing context */
5690 Token *pName, /* Name of the common-table */
5691 ExprList *pArglist, /* Optional column name list for the table */
drh745912e2021-02-22 03:04:255692 Select *pQuery, /* Query used to initialize the table */
5693 u8 eM10d /* The MATERIALIZED flag */
drhf824b412021-02-20 14:57:165694){
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);
drh745912e2021-02-22 03:04:255708 pNew->eM10d = eM10d;
drhf824b412021-02-20 14:57:165709 }
5710 return pNew;
5711}
5712
5713/*
5714** Clear information from a Cte object, but do not deallocate storage
5715** for the object itself.
5716*/
5717static 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*/
5727void sqlite3CteDelete(sqlite3 *db, Cte *pCte){
5728 assert( pCte!=0 );
5729 cteClear(db, pCte);
5730 sqlite3DbFree(db, pCte);
5731}
5732
larrybrbc917382023-06-07 08:40:315733/*
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
drhf824b412021-02-20 14:57:165736** the WITH clause of the second argument. If the second argument is
5737** NULL, then a new WITH argument is created.
drh8b471862014-01-11 13:22:175738*/
dan7d562db2014-01-11 19:19:365739With *sqlite3WithAdd(
drh8b471862014-01-11 13:22:175740 Parse *pParse, /* Parsing context */
dan7d562db2014-01-11 19:19:365741 With *pWith, /* Existing WITH clause, or NULL */
drhf824b412021-02-20 14:57:165742 Cte *pCte /* CTE to add to the WITH clause */
drh8b471862014-01-11 13:22:175743){
dan4e9119d2014-01-13 15:12:235744 sqlite3 *db = pParse->db;
5745 With *pNew;
5746 char *zName;
5747
drhf824b412021-02-20 14:57:165748 if( pCte==0 ){
5749 return pWith;
5750 }
5751
dan4e9119d2014-01-13 15:12:235752 /* Check that the CTE name is unique within this WITH clause. If
5753 ** not, store an error in the Parse structure. */
drhf824b412021-02-20 14:57:165754 zName = pCte->zName;
dan4e9119d2014-01-13 15:12:235755 if( zName && pWith ){
5756 int i;
5757 for(i=0; i<pWith->nCte; i++){
5758 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
drh727a99f2014-01-16 21:59:515759 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
dan4e9119d2014-01-13 15:12:235760 }
5761 }
5762 }
5763
5764 if( pWith ){
drhcebf06c2025-03-14 18:10:025765 pNew = sqlite3DbRealloc(db, pWith, SZ_WITH(pWith->nCte+1));
dan4e9119d2014-01-13 15:12:235766 }else{
drhcebf06c2025-03-14 18:10:025767 pNew = sqlite3DbMallocZero(db, SZ_WITH(1));
dan4e9119d2014-01-13 15:12:235768 }
drhb84e5742016-02-05 02:42:545769 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
dan4e9119d2014-01-13 15:12:235770
drhb84e5742016-02-05 02:42:545771 if( db->mallocFailed ){
drhf824b412021-02-20 14:57:165772 sqlite3CteDelete(db, pCte);
dana9f5c132014-01-13 16:36:405773 pNew = pWith;
dan4e9119d2014-01-13 15:12:235774 }else{
drhf824b412021-02-20 14:57:165775 pNew->a[pNew->nCte++] = *pCte;
5776 sqlite3DbFree(db, pCte);
dan4e9119d2014-01-13 15:12:235777 }
5778
5779 return pNew;
drh8b471862014-01-11 13:22:175780}
5781
dan7d562db2014-01-11 19:19:365782/*
5783** Free the contents of the With object passed as the second argument.
drh8b471862014-01-11 13:22:175784*/
dan7d562db2014-01-11 19:19:365785void sqlite3WithDelete(sqlite3 *db, With *pWith){
dan4e9119d2014-01-13 15:12:235786 if( pWith ){
5787 int i;
5788 for(i=0; i<pWith->nCte; i++){
drhf824b412021-02-20 14:57:165789 cteClear(db, &pWith->a[i]);
dan4e9119d2014-01-13 15:12:235790 }
5791 sqlite3DbFree(db, pWith);
5792 }
drh8b471862014-01-11 13:22:175793}
drh82fc1b62023-12-06 18:25:415794void sqlite3WithDeleteGeneric(sqlite3 *db, void *pWith){
5795 sqlite3WithDelete(db, (With*)pWith);
5796}
drh8b471862014-01-11 13:22:175797#endif /* !defined(SQLITE_OMIT_CTE) */