blob: c146c2ff913178fbb92cb1a6b66eb49cb6e46d76 [file] [log] [blame]
drhd0e4a6c2005-02-15 20:47:57 +00001/*
2** 2005 February 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains C code routines that used to generate VDBE code
13** that implements the ALTER TABLE command.
drhd0e4a6c2005-02-15 20:47:57 +000014*/
15#include "sqliteInt.h"
16
drh1f01ec12005-02-15 21:36:18 +000017/*
18** The code in this file only exists if we are not omitting the
19** ALTER TABLE logic from the build.
20*/
drhd0e4a6c2005-02-15 20:47:57 +000021#ifndef SQLITE_OMIT_ALTERTABLE
drh1f01ec12005-02-15 21:36:18 +000022
drh1f01ec12005-02-15 21:36:18 +000023/*
danbe535002011-04-01 15:15:58 +000024** Parameter zName is the name of a table that is about to be altered
25** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
26** If the table is a system table, this function leaves an error message
27** in pParse->zErr (system tables may not be altered) and returns non-zero.
28**
29** Or, if zName is not a system table, zero is returned.
30*/
dan397a78d2018-12-18 20:31:14 +000031static int isAlterableTable(Parse *pParse, Table *pTab){
32 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
33#ifndef SQLITE_OMIT_VIRTUALTABLE
drh070ae3b2019-11-16 13:51:31 +000034 || ( (pTab->tabFlags & TF_Shadow)!=0
35 && sqlite3ReadOnlyShadowTables(pParse->db)
dan397a78d2018-12-18 20:31:14 +000036 )
37#endif
38 ){
39 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
danbe535002011-04-01 15:15:58 +000040 return 1;
41 }
42 return 0;
43}
44
dan09236502018-09-01 16:05:50 +000045/*
46** Generate code to verify that the schemas of database zDb and, if
47** bTemp is not true, database "temp", can still be parsed. This is
48** called at the end of the generation of an ALTER TABLE ... RENAME ...
49** statement to ensure that the operation has not rendered any schema
50** objects unusable.
51*/
drh16b870d2018-09-12 15:51:56 +000052static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
dan9d324822018-08-30 20:03:44 +000053 sqlite3NestedParse(pParse,
54 "SELECT 1 "
drh346a70c2020-06-15 20:27:35 +000055 "FROM \"%w\"." DFLT_SCHEMA_TABLE " "
dan65455fc2019-04-19 16:34:22 +000056 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
dan9d324822018-08-30 20:03:44 +000057 " AND sql NOT LIKE 'create virtual%%'"
dan1d85c6b2018-09-06 16:01:37 +000058 " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
drh346a70c2020-06-15 20:27:35 +000059 zDb,
dan9d324822018-08-30 20:03:44 +000060 zDb, bTemp
61 );
62
63 if( bTemp==0 ){
64 sqlite3NestedParse(pParse,
65 "SELECT 1 "
drh346a70c2020-06-15 20:27:35 +000066 "FROM temp." DFLT_SCHEMA_TABLE " "
dan65455fc2019-04-19 16:34:22 +000067 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
dan9d324822018-08-30 20:03:44 +000068 " AND sql NOT LIKE 'create virtual%%'"
dan1d85c6b2018-09-06 16:01:37 +000069 " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
drh346a70c2020-06-15 20:27:35 +000070 zDb
dan9d324822018-08-30 20:03:44 +000071 );
72 }
73}
74
danbe535002011-04-01 15:15:58 +000075/*
dan09236502018-09-01 16:05:50 +000076** Generate code to reload the schema for database iDb. And, if iDb!=1, for
77** the temp database as well.
78*/
drh16b870d2018-09-12 15:51:56 +000079static void renameReloadSchema(Parse *pParse, int iDb){
dan09236502018-09-01 16:05:50 +000080 Vdbe *v = pParse->pVdbe;
81 if( v ){
82 sqlite3ChangeCookie(pParse, iDb);
83 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
84 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
85 }
86}
87
88/*
drhd0e4a6c2005-02-15 20:47:57 +000089** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
90** command.
91*/
92void sqlite3AlterRenameTable(
93 Parse *pParse, /* Parser context. */
94 SrcList *pSrc, /* The table to rename. */
95 Token *pName /* The new table name. */
96){
97 int iDb; /* Database that contains the table */
98 char *zDb; /* Name of database iDb */
99 Table *pTab; /* Table being renamed */
100 char *zName = 0; /* NULL-terminated version of pName */
drhd0e4a6c2005-02-15 20:47:57 +0000101 sqlite3 *db = pParse->db; /* Database connection */
drh4e5dd852007-05-15 03:56:49 +0000102 int nTabName; /* Number of UTF-8 characters in zTabName */
103 const char *zTabName; /* Original name of the table */
drhd0e4a6c2005-02-15 20:47:57 +0000104 Vdbe *v;
danielk1977595a5232009-07-24 17:58:53 +0000105 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
drh8257aa82017-07-26 19:59:13 +0000106 u32 savedDbFlags; /* Saved value of db->mDbFlags */
drh545f5872010-04-24 14:02:59 +0000107
drh8257aa82017-07-26 19:59:13 +0000108 savedDbFlags = db->mDbFlags;
drh56d56f72009-04-16 16:30:17 +0000109 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000110 assert( pSrc->nSrc==1 );
drh1fee73e2007-08-29 04:00:57 +0000111 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhd0e4a6c2005-02-15 20:47:57 +0000112
dan41fb5cd2012-10-04 19:33:00 +0000113 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
drhd0e4a6c2005-02-15 20:47:57 +0000114 if( !pTab ) goto exit_rename_table;
danielk1977da184232006-01-05 11:34:32 +0000115 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drh69c33822016-08-18 14:33:11 +0000116 zDb = db->aDb[iDb].zDbSName;
drh8257aa82017-07-26 19:59:13 +0000117 db->mDbFlags |= DBFLAG_PreferBuiltin;
drhd0e4a6c2005-02-15 20:47:57 +0000118
119 /* Get a NULL terminated version of the new table name. */
drh17435752007-08-16 04:30:38 +0000120 zName = sqlite3NameFromToken(db, pName);
drhd0e4a6c2005-02-15 20:47:57 +0000121 if( !zName ) goto exit_rename_table;
122
123 /* Check that a table or index named 'zName' does not already exist
124 ** in database iDb. If so, this is an error.
125 */
drh3d863b52020-05-14 21:16:52 +0000126 if( sqlite3FindTable(db, zName, zDb)
127 || sqlite3FindIndex(db, zName, zDb)
128 || sqlite3IsShadowTableOf(db, pTab, zName)
129 ){
drhd0e4a6c2005-02-15 20:47:57 +0000130 sqlite3ErrorMsg(pParse,
131 "there is already another table or index with this name: %s", zName);
132 goto exit_rename_table;
133 }
134
135 /* Make sure it is not a system table being altered, or a reserved name
136 ** that the table is being renamed to.
137 */
dan397a78d2018-12-18 20:31:14 +0000138 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
drhd0e4a6c2005-02-15 20:47:57 +0000139 goto exit_rename_table;
140 }
drhc5a93d42019-08-12 00:08:07 +0000141 if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
142 goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000143 }
144
danielk197761116ae2007-12-13 08:15:30 +0000145#ifndef SQLITE_OMIT_VIEW
146 if( pTab->pSelect ){
147 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
148 goto exit_rename_table;
149 }
150#endif
151
drhd0e4a6c2005-02-15 20:47:57 +0000152#ifndef SQLITE_OMIT_AUTHORIZATION
153 /* Invoke the authorization callback. */
154 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
155 goto exit_rename_table;
156 }
157#endif
158
danielk1977182c4ba2007-06-27 15:53:34 +0000159#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19775c558862007-06-27 17:09:24 +0000160 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
161 goto exit_rename_table;
162 }
danielk1977595a5232009-07-24 17:58:53 +0000163 if( IsVirtual(pTab) ){
164 pVTab = sqlite3GetVTable(db, pTab);
165 if( pVTab->pVtab->pModule->xRename==0 ){
166 pVTab = 0;
167 }
danielk1977182c4ba2007-06-27 15:53:34 +0000168 }
169#endif
170
dan1f3b2842019-03-15 16:17:32 +0000171 /* Begin a transaction for database iDb. Then modify the schema cookie
172 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
173 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
174 ** nested SQL may raise an exception. */
drhd0e4a6c2005-02-15 20:47:57 +0000175 v = sqlite3GetVdbe(pParse);
176 if( v==0 ){
177 goto exit_rename_table;
178 }
dan1f3b2842019-03-15 16:17:32 +0000179 sqlite3MayAbort(pParse);
drhd0e4a6c2005-02-15 20:47:57 +0000180
drh4e5dd852007-05-15 03:56:49 +0000181 /* figure out how many UTF-8 characters are in zName */
182 zTabName = pTab->zName;
drh9a087a92007-05-15 14:34:32 +0000183 nTabName = sqlite3Utf8CharLen(zTabName, -1);
drh4e5dd852007-05-15 03:56:49 +0000184
danc9461ec2018-08-29 21:00:16 +0000185 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
186 ** the schema to use the new table name. */
187 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000188 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
dan65372fa2018-09-03 20:05:15 +0000189 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
danc9461ec2018-08-29 21:00:16 +0000190 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
dan65455fc2019-04-19 16:34:22 +0000191 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
drh346a70c2020-06-15 20:27:35 +0000192 , zDb, zDb, zTabName, zName, (iDb==1), zTabName
danc9461ec2018-08-29 21:00:16 +0000193 );
dan432cc5b2009-09-26 17:51:48 +0000194
drh1e32bed2020-06-19 13:33:53 +0000195 /* Update the tbl_name and name columns of the sqlite_schema table
danc9461ec2018-08-29 21:00:16 +0000196 ** as required. */
drhd0e4a6c2005-02-15 20:47:57 +0000197 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000198 "UPDATE %Q." DFLT_SCHEMA_TABLE " SET "
drhd0e4a6c2005-02-15 20:47:57 +0000199 "tbl_name = %Q, "
200 "name = CASE "
201 "WHEN type='table' THEN %Q "
dan65455fc2019-04-19 16:34:22 +0000202 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
203 " AND type='index' THEN "
drha21a9292007-10-20 20:58:57 +0000204 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
drhd0e4a6c2005-02-15 20:47:57 +0000205 "ELSE name END "
drh01522682012-02-01 01:13:10 +0000206 "WHERE tbl_name=%Q COLLATE nocase AND "
drhd0e4a6c2005-02-15 20:47:57 +0000207 "(type='table' OR type='index' OR type='trigger');",
drh346a70c2020-06-15 20:27:35 +0000208 zDb,
danc9461ec2018-08-29 21:00:16 +0000209 zName, zName, zName,
210 nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000211 );
212
213#ifndef SQLITE_OMIT_AUTOINCREMENT
214 /* If the sqlite_sequence table exists in this database, then update
215 ** it with the new table name.
216 */
217 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
218 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000219 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
drhd0e4a6c2005-02-15 20:47:57 +0000220 zDb, zName, pTab->zName);
221 }
222#endif
223
danc9461ec2018-08-29 21:00:16 +0000224 /* If the table being renamed is not itself part of the temp database,
225 ** edit view and trigger definitions within the temp database
226 ** as required. */
227 if( iDb!=1 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000228 sqlite3NestedParse(pParse,
drh1e32bed2020-06-19 13:33:53 +0000229 "UPDATE sqlite_temp_schema SET "
dan65372fa2018-09-03 20:05:15 +0000230 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
danc9461ec2018-08-29 21:00:16 +0000231 "tbl_name = "
dan1d85c6b2018-09-06 16:01:37 +0000232 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
233 " sqlite_rename_test(%Q, sql, type, name, 1) "
234 "THEN %Q ELSE tbl_name END "
danc9461ec2018-08-29 21:00:16 +0000235 "WHERE type IN ('view', 'trigger')"
dan1d85c6b2018-09-06 16:01:37 +0000236 , zDb, zTabName, zName, zTabName, zDb, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000237 }
drhd0e4a6c2005-02-15 20:47:57 +0000238
dan34566c42018-09-20 17:21:21 +0000239 /* If this is a virtual table, invoke the xRename() function if
240 ** one is defined. The xRename() callback will modify the names
241 ** of any resources used by the v-table implementation (including other
242 ** SQLite tables) that are identified by the name of the virtual table.
243 */
244#ifndef SQLITE_OMIT_VIRTUALTABLE
245 if( pVTab ){
246 int i = ++pParse->nMem;
247 sqlite3VdbeLoadString(v, i, zName);
248 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
dan34566c42018-09-20 17:21:21 +0000249 }
250#endif
251
dan09236502018-09-01 16:05:50 +0000252 renameReloadSchema(pParse, iDb);
dan9d324822018-08-30 20:03:44 +0000253 renameTestSchema(pParse, zDb, iDb==1);
254
drhd0e4a6c2005-02-15 20:47:57 +0000255exit_rename_table:
drh633e6d52008-07-28 19:34:53 +0000256 sqlite3SrcListDelete(db, pSrc);
257 sqlite3DbFree(db, zName);
drh8257aa82017-07-26 19:59:13 +0000258 db->mDbFlags = savedDbFlags;
drhd0e4a6c2005-02-15 20:47:57 +0000259}
danielk197719a8e7e2005-03-17 05:03:38 +0000260
drhd3001712009-05-12 17:46:53 +0000261/*
drh9e5fdc42020-05-08 19:02:21 +0000262** Write code that will raise an error if the table described by
263** zDb and zTab is not empty.
264*/
265static void sqlite3ErrorIfNotEmpty(
266 Parse *pParse, /* Parsing context */
267 const char *zDb, /* Schema holding the table */
268 const char *zTab, /* Table to check for empty */
269 const char *zErr /* Error message text */
270){
271 sqlite3NestedParse(pParse,
272 "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
273 zErr, zDb, zTab
274 );
275}
276
277/*
danielk197719a8e7e2005-03-17 05:03:38 +0000278** This function is called after an "ALTER TABLE ... ADD" statement
279** has been parsed. Argument pColDef contains the text of the new
280** column definition.
281**
282** The Table structure pParse->pNewTable was extended to include
283** the new column during parsing.
284*/
285void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
286 Table *pNew; /* Copy of pParse->pNewTable */
287 Table *pTab; /* Table being altered */
288 int iDb; /* Database number */
289 const char *zDb; /* Database name */
290 const char *zTab; /* Table name */
291 char *zCol; /* Null-terminated column definition */
292 Column *pCol; /* The new column */
293 Expr *pDflt; /* Default value for the new column */
drh17435752007-08-16 04:30:38 +0000294 sqlite3 *db; /* The database connection; */
dan09236502018-09-01 16:05:50 +0000295 Vdbe *v; /* The prepared statement under construction */
drh86396212016-07-14 19:13:11 +0000296 int r1; /* Temporary registers */
danielk197719a8e7e2005-03-17 05:03:38 +0000297
danielk1977f150c9d2008-10-30 17:21:12 +0000298 db = pParse->db;
299 if( pParse->nErr || db->mallocFailed ) return;
danielk197719a8e7e2005-03-17 05:03:38 +0000300 pNew = pParse->pNewTable;
301 assert( pNew );
302
drh1fee73e2007-08-29 04:00:57 +0000303 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000304 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
drh69c33822016-08-18 14:33:11 +0000305 zDb = db->aDb[iDb].zDbSName;
drh03881232009-02-13 03:43:31 +0000306 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
danielk197719a8e7e2005-03-17 05:03:38 +0000307 pCol = &pNew->aCol[pNew->nCol-1];
308 pDflt = pCol->pDflt;
drh17435752007-08-16 04:30:38 +0000309 pTab = sqlite3FindTable(db, zTab, zDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000310 assert( pTab );
311
drh81f2ccd2006-01-31 14:28:44 +0000312#ifndef SQLITE_OMIT_AUTHORIZATION
313 /* Invoke the authorization callback. */
314 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
315 return;
316 }
317#endif
318
danielk197719a8e7e2005-03-17 05:03:38 +0000319
320 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
321 ** If there is a NOT NULL constraint, then the default value for the
322 ** column must not be NULL.
323 */
drha371ace2012-09-13 14:22:47 +0000324 if( pCol->colFlags & COLFLAG_PRIMKEY ){
danielk197719a8e7e2005-03-17 05:03:38 +0000325 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
326 return;
327 }
328 if( pNew->pIndex ){
drh9e5fdc42020-05-08 19:02:21 +0000329 sqlite3ErrorMsg(pParse,
330 "Cannot add a UNIQUE column");
danielk197719a8e7e2005-03-17 05:03:38 +0000331 return;
332 }
drhc27ea2a2019-10-16 20:05:56 +0000333 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
334 /* If the default value for the new column was specified with a
335 ** literal NULL, then set pDflt to 0. This simplifies checking
336 ** for an SQL NULL default below.
337 */
338 assert( pDflt==0 || pDflt->op==TK_SPAN );
339 if( pDflt && pDflt->pLeft->op==TK_NULL ){
340 pDflt = 0;
341 }
342 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
drh9e5fdc42020-05-08 19:02:21 +0000343 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
drhc27ea2a2019-10-16 20:05:56 +0000344 "Cannot add a REFERENCES column with non-NULL default value");
drhc27ea2a2019-10-16 20:05:56 +0000345 }
346 if( pCol->notNull && !pDflt ){
drh9e5fdc42020-05-08 19:02:21 +0000347 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
drhc27ea2a2019-10-16 20:05:56 +0000348 "Cannot add a NOT NULL column with default value NULL");
drhc27ea2a2019-10-16 20:05:56 +0000349 }
350
drh9e5fdc42020-05-08 19:02:21 +0000351
drhc27ea2a2019-10-16 20:05:56 +0000352 /* Ensure the default expression is something that sqlite3ValueFromExpr()
353 ** can handle (i.e. not CURRENT_TIME etc.)
354 */
355 if( pDflt ){
356 sqlite3_value *pVal = 0;
357 int rc;
358 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
359 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
360 if( rc!=SQLITE_OK ){
361 assert( db->mallocFailed == 1 );
362 return;
363 }
364 if( !pVal ){
drh9e5fdc42020-05-08 19:02:21 +0000365 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
366 "Cannot add a column with non-constant default");
drhc27ea2a2019-10-16 20:05:56 +0000367 }
368 sqlite3ValueFree(pVal);
369 }
drh035f6d92019-10-24 01:04:10 +0000370 }else if( pCol->colFlags & COLFLAG_STORED ){
drh9e5fdc42020-05-08 19:02:21 +0000371 sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
danielk197719a8e7e2005-03-17 05:03:38 +0000372 }
373
danielk197719a8e7e2005-03-17 05:03:38 +0000374
375 /* Modify the CREATE TABLE statement. */
drh17435752007-08-16 04:30:38 +0000376 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000377 if( zCol ){
378 char *zEnd = &zCol[pColDef->n-1];
drh8257aa82017-07-26 19:59:13 +0000379 u32 savedDbFlags = db->mDbFlags;
drh56d56f72009-04-16 16:30:17 +0000380 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000381 *zEnd-- = '\0';
382 }
drh8257aa82017-07-26 19:59:13 +0000383 db->mDbFlags |= DBFLAG_PreferBuiltin;
danielk197719a8e7e2005-03-17 05:03:38 +0000384 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000385 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
drha21a9292007-10-20 20:58:57 +0000386 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
danielk197719a8e7e2005-03-17 05:03:38 +0000387 "WHERE type = 'table' AND name = %Q",
drh346a70c2020-06-15 20:27:35 +0000388 zDb, pNew->addColOffset, zCol, pNew->addColOffset+1,
danielk197719a8e7e2005-03-17 05:03:38 +0000389 zTab
390 );
drh633e6d52008-07-28 19:34:53 +0000391 sqlite3DbFree(db, zCol);
drh8257aa82017-07-26 19:59:13 +0000392 db->mDbFlags = savedDbFlags;
danielk197719a8e7e2005-03-17 05:03:38 +0000393 }
394
drh86396212016-07-14 19:13:11 +0000395 /* Make sure the schema version is at least 3. But do not upgrade
396 ** from less than 3 to 4, as that will corrupt any preexisting DESC
397 ** index.
danielk197719a8e7e2005-03-17 05:03:38 +0000398 */
dan09236502018-09-01 16:05:50 +0000399 v = sqlite3GetVdbe(pParse);
400 if( v ){
401 r1 = sqlite3GetTempReg(pParse);
402 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
403 sqlite3VdbeUsesBtree(v, iDb);
404 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
405 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
406 VdbeCoverage(v);
407 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
408 sqlite3ReleaseTempReg(pParse, r1);
409 }
danielk197719a8e7e2005-03-17 05:03:38 +0000410
dan09236502018-09-01 16:05:50 +0000411 /* Reload the table definition */
412 renameReloadSchema(pParse, iDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000413}
414
drhfdd6e852005-12-16 01:06:16 +0000415/*
danielk197719a8e7e2005-03-17 05:03:38 +0000416** This function is called by the parser after the table-name in
417** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
418** pSrc is the full-name of the table being altered.
419**
420** This routine makes a (partial) copy of the Table structure
421** for the table being altered and sets Parse.pNewTable to point
422** to it. Routines called by the parser as the column definition
423** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
424** the copy. The copy of the Table structure is deleted by tokenize.c
425** after parsing is finished.
426**
427** Routine sqlite3AlterFinishAddColumn() will be called to complete
428** coding the "ALTER TABLE ... ADD" statement.
429*/
430void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
431 Table *pNew;
432 Table *pTab;
danielk197719a8e7e2005-03-17 05:03:38 +0000433 int iDb;
434 int i;
435 int nAlloc;
drh17435752007-08-16 04:30:38 +0000436 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000437
438 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000439 assert( pParse->pNewTable==0 );
drh1fee73e2007-08-29 04:00:57 +0000440 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000441 if( db->mallocFailed ) goto exit_begin_add_column;
dan41fb5cd2012-10-04 19:33:00 +0000442 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
danielk197719a8e7e2005-03-17 05:03:38 +0000443 if( !pTab ) goto exit_begin_add_column;
444
danielk19775ee9d692006-06-21 12:36:25 +0000445#ifndef SQLITE_OMIT_VIRTUALTABLE
446 if( IsVirtual(pTab) ){
447 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
448 goto exit_begin_add_column;
449 }
450#endif
451
danielk197719a8e7e2005-03-17 05:03:38 +0000452 /* Make sure this is not an attempt to ALTER a view. */
453 if( pTab->pSelect ){
454 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
455 goto exit_begin_add_column;
456 }
dan397a78d2018-12-18 20:31:14 +0000457 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
danbe535002011-04-01 15:15:58 +0000458 goto exit_begin_add_column;
459 }
danielk197719a8e7e2005-03-17 05:03:38 +0000460
dan03e025e2019-10-07 18:43:21 +0000461 sqlite3MayAbort(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +0000462 assert( pTab->addColOffset>0 );
drh17435752007-08-16 04:30:38 +0000463 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000464
465 /* Put a copy of the Table struct in Parse.pNewTable for the
drh03881232009-02-13 03:43:31 +0000466 ** sqlite3AddColumn() function and friends to modify. But modify
467 ** the name by adding an "sqlite_altertab_" prefix. By adding this
468 ** prefix, we insure that the name will not collide with an existing
469 ** table because user table are not allowed to have the "sqlite_"
470 ** prefix on their name.
danielk197719a8e7e2005-03-17 05:03:38 +0000471 */
drh17435752007-08-16 04:30:38 +0000472 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
danielk197719a8e7e2005-03-17 05:03:38 +0000473 if( !pNew ) goto exit_begin_add_column;
474 pParse->pNewTable = pNew;
drh79df7782016-12-14 14:07:35 +0000475 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000476 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000477 assert( pNew->nCol>0 );
478 nAlloc = (((pNew->nCol-1)/8)*8)+8;
479 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197726783a52007-08-29 14:06:22 +0000480 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
drh03881232009-02-13 03:43:31 +0000481 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000482 if( !pNew->aCol || !pNew->zName ){
drh4df86af2016-02-04 11:48:00 +0000483 assert( db->mallocFailed );
danielk197719a8e7e2005-03-17 05:03:38 +0000484 goto exit_begin_add_column;
485 }
486 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
487 for(i=0; i<pNew->nCol; i++){
488 Column *pCol = &pNew->aCol[i];
drh17435752007-08-16 04:30:38 +0000489 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
drhd44390c2020-04-06 18:16:31 +0000490 pCol->hName = sqlite3StrIHash(pCol->zName);
drhff22e182006-02-09 02:56:02 +0000491 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000492 pCol->pDflt = 0;
493 }
drh17435752007-08-16 04:30:38 +0000494 pNew->pSchema = db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000495 pNew->addColOffset = pTab->addColOffset;
drh79df7782016-12-14 14:07:35 +0000496 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000497
danielk197719a8e7e2005-03-17 05:03:38 +0000498exit_begin_add_column:
drh633e6d52008-07-28 19:34:53 +0000499 sqlite3SrcListDelete(db, pSrc);
danielk197719a8e7e2005-03-17 05:03:38 +0000500 return;
501}
dancf8f2892018-08-09 20:47:01 +0000502
drh4a2c7472018-08-13 15:09:48 +0000503/*
dan9d705572018-08-20 16:16:05 +0000504** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
505** command. This function checks if the table is a view or virtual
506** table (columns of views or virtual tables may not be renamed). If so,
507** it loads an error message into pParse and returns non-zero.
508**
509** Or, if pTab is not a view or virtual table, zero is returned.
510*/
511#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
512static int isRealTable(Parse *pParse, Table *pTab){
513 const char *zType = 0;
514#ifndef SQLITE_OMIT_VIEW
515 if( pTab->pSelect ){
516 zType = "view";
dan1041a6a2018-09-06 17:47:09 +0000517 }
dan9d705572018-08-20 16:16:05 +0000518#endif
519#ifndef SQLITE_OMIT_VIRTUALTABLE
520 if( IsVirtual(pTab) ){
521 zType = "virtual table";
522 }
523#endif
524 if( zType ){
525 sqlite3ErrorMsg(
drh79a5ee92018-08-23 19:32:04 +0000526 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
dan9d705572018-08-20 16:16:05 +0000527 );
528 return 1;
529 }
530 return 0;
531}
532#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
533# define isRealTable(x,y) (0)
534#endif
535
536/*
drh4a2c7472018-08-13 15:09:48 +0000537** Handles the following parser reduction:
538**
539** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
540*/
dancf8f2892018-08-09 20:47:01 +0000541void sqlite3AlterRenameColumn(
drh4a2c7472018-08-13 15:09:48 +0000542 Parse *pParse, /* Parsing context */
543 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
544 Token *pOld, /* Name of column being changed */
545 Token *pNew /* New column name */
dancf8f2892018-08-09 20:47:01 +0000546){
drh4a2c7472018-08-13 15:09:48 +0000547 sqlite3 *db = pParse->db; /* Database connection */
dancf8f2892018-08-09 20:47:01 +0000548 Table *pTab; /* Table being updated */
549 int iCol; /* Index of column being renamed */
drh4a2c7472018-08-13 15:09:48 +0000550 char *zOld = 0; /* Old column name */
551 char *zNew = 0; /* New column name */
552 const char *zDb; /* Name of schema containing the table */
553 int iSchema; /* Index of the schema */
554 int bQuote; /* True to quote the new name */
dancf8f2892018-08-09 20:47:01 +0000555
drh4a2c7472018-08-13 15:09:48 +0000556 /* Locate the table to be altered */
dancf8f2892018-08-09 20:47:01 +0000557 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
558 if( !pTab ) goto exit_rename_column;
drh4a2c7472018-08-13 15:09:48 +0000559
560 /* Cannot alter a system table */
dan397a78d2018-12-18 20:31:14 +0000561 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
dan9d705572018-08-20 16:16:05 +0000562 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
drh4a2c7472018-08-13 15:09:48 +0000563
564 /* Which schema holds the table to be altered */
dancf8f2892018-08-09 20:47:01 +0000565 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
566 assert( iSchema>=0 );
567 zDb = db->aDb[iSchema].zDbSName;
568
drh0d019b92018-08-25 16:14:46 +0000569#ifndef SQLITE_OMIT_AUTHORIZATION
570 /* Invoke the authorization callback. */
571 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
572 goto exit_rename_column;
573 }
574#endif
575
drh4a2c7472018-08-13 15:09:48 +0000576 /* Make sure the old name really is a column name in the table to be
577 ** altered. Set iCol to be the index of the column being renamed */
dancf8f2892018-08-09 20:47:01 +0000578 zOld = sqlite3NameFromToken(db, pOld);
579 if( !zOld ) goto exit_rename_column;
580 for(iCol=0; iCol<pTab->nCol; iCol++){
581 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
582 }
583 if( iCol==pTab->nCol ){
584 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
585 goto exit_rename_column;
586 }
587
drh4a2c7472018-08-13 15:09:48 +0000588 /* Do the rename operation using a recursive UPDATE statement that
589 ** uses the sqlite_rename_column() SQL function to compute the new
drh1e32bed2020-06-19 13:33:53 +0000590 ** CREATE statement text for the sqlite_schema table.
drh4a2c7472018-08-13 15:09:48 +0000591 */
dan1f3b2842019-03-15 16:17:32 +0000592 sqlite3MayAbort(pParse);
dancf8f2892018-08-09 20:47:01 +0000593 zNew = sqlite3NameFromToken(db, pNew);
594 if( !zNew ) goto exit_rename_column;
dan404c3ba2018-08-11 20:38:33 +0000595 assert( pNew->n>0 );
596 bQuote = sqlite3Isquote(pNew->z[0]);
dancf8f2892018-08-09 20:47:01 +0000597 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000598 "UPDATE \"%w\"." DFLT_SCHEMA_TABLE " SET "
danb87a9a82018-09-01 20:23:28 +0000599 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
dan65455fc2019-04-19 16:34:22 +0000600 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
601 " AND (type != 'index' OR tbl_name = %Q)"
dan499b8252018-08-17 18:08:28 +0000602 " AND sql NOT LIKE 'create virtual%%'",
drh346a70c2020-06-15 20:27:35 +0000603 zDb,
danb87a9a82018-09-01 20:23:28 +0000604 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
dan987db762018-08-14 20:18:50 +0000605 pTab->zName
dancf8f2892018-08-09 20:47:01 +0000606 );
607
dan9d324822018-08-30 20:03:44 +0000608 sqlite3NestedParse(pParse,
drh346a70c2020-06-15 20:27:35 +0000609 "UPDATE temp." DFLT_SCHEMA_TABLE " SET "
danb87a9a82018-09-01 20:23:28 +0000610 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
dan9d324822018-08-30 20:03:44 +0000611 "WHERE type IN ('trigger', 'view')",
dan9d324822018-08-30 20:03:44 +0000612 zDb, pTab->zName, iCol, zNew, bQuote
613 );
614
dane325ffe2018-08-11 13:40:20 +0000615 /* Drop and reload the database schema. */
dan09236502018-09-01 16:05:50 +0000616 renameReloadSchema(pParse, iSchema);
dan9d324822018-08-30 20:03:44 +0000617 renameTestSchema(pParse, zDb, iSchema==1);
dan0d5fa6b2018-08-24 17:55:49 +0000618
dancf8f2892018-08-09 20:47:01 +0000619 exit_rename_column:
620 sqlite3SrcListDelete(db, pSrc);
621 sqlite3DbFree(db, zOld);
622 sqlite3DbFree(db, zNew);
623 return;
624}
625
drh4a2c7472018-08-13 15:09:48 +0000626/*
627** Each RenameToken object maps an element of the parse tree into
628** the token that generated that element. The parse tree element
629** might be one of:
630**
631** * A pointer to an Expr that represents an ID
632** * The name of a table column in Column.zName
633**
634** A list of RenameToken objects can be constructed during parsing.
dan07e95232018-08-21 16:32:53 +0000635** Each new object is created by sqlite3RenameTokenMap().
636** As the parse tree is transformed, the sqlite3RenameTokenRemap()
drh4a2c7472018-08-13 15:09:48 +0000637** routine is used to keep the mapping current.
638**
639** After the parse finishes, renameTokenFind() routine can be used
640** to look up the actual token value that created some element in
641** the parse tree.
642*/
dancf8f2892018-08-09 20:47:01 +0000643struct RenameToken {
drh4a2c7472018-08-13 15:09:48 +0000644 void *p; /* Parse tree element created by token t */
645 Token t; /* The token that created parse tree element p */
646 RenameToken *pNext; /* Next is a list of all RenameToken objects */
dancf8f2892018-08-09 20:47:01 +0000647};
648
drh4a2c7472018-08-13 15:09:48 +0000649/*
650** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
651** down into the Walker.
652*/
dan5496d6a2018-08-13 17:14:26 +0000653typedef struct RenameCtx RenameCtx;
dancf8f2892018-08-09 20:47:01 +0000654struct RenameCtx {
655 RenameToken *pList; /* List of tokens to overwrite */
656 int nList; /* Number of tokens in pList */
657 int iCol; /* Index of column being renamed */
dan987db762018-08-14 20:18:50 +0000658 Table *pTab; /* Table being ALTERed */
dan5496d6a2018-08-13 17:14:26 +0000659 const char *zOld; /* Old column name */
dancf8f2892018-08-09 20:47:01 +0000660};
661
dan8900a482018-09-05 14:36:05 +0000662#ifdef SQLITE_DEBUG
663/*
664** This function is only for debugging. It performs two tasks:
665**
666** 1. Checks that pointer pPtr does not already appear in the
667** rename-token list.
668**
669** 2. Dereferences each pointer in the rename-token list.
670**
671** The second is most effective when debugging under valgrind or
672** address-sanitizer or similar. If any of these pointers no longer
673** point to valid objects, an exception is raised by the memory-checking
674** tool.
675**
676** The point of this is to prevent comparisons of invalid pointer values.
677** Even though this always seems to work, it is undefined according to the
678** C standard. Example of undefined comparison:
679**
680** sqlite3_free(x);
681** if( x==y ) ...
682**
683** Technically, as x no longer points into a valid object or to the byte
684** following a valid object, it may not be used in comparison operations.
685*/
drh16b870d2018-09-12 15:51:56 +0000686static void renameTokenCheckAll(Parse *pParse, void *pPtr){
dan8900a482018-09-05 14:36:05 +0000687 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
688 RenameToken *p;
689 u8 i = 0;
690 for(p=pParse->pRename; p; p=p->pNext){
691 if( p->p ){
692 assert( p->p!=pPtr );
693 i += *(u8*)(p->p);
694 }
danc9461ec2018-08-29 21:00:16 +0000695 }
696 }
697}
dan8900a482018-09-05 14:36:05 +0000698#else
699# define renameTokenCheckAll(x,y)
700#endif
danc9461ec2018-08-29 21:00:16 +0000701
drh4a2c7472018-08-13 15:09:48 +0000702/*
drh49b269e2018-11-26 15:00:25 +0000703** Remember that the parser tree element pPtr was created using
704** the token pToken.
dan07e95232018-08-21 16:32:53 +0000705**
drh49b269e2018-11-26 15:00:25 +0000706** In other words, construct a new RenameToken object and add it
707** to the list of RenameToken objects currently being built up
708** in pParse->pRename.
709**
710** The pPtr argument is returned so that this routine can be used
711** with tail recursion in tokenExpr() routine, for a small performance
712** improvement.
drh4a2c7472018-08-13 15:09:48 +0000713*/
dan07e95232018-08-21 16:32:53 +0000714void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
dancf8f2892018-08-09 20:47:01 +0000715 RenameToken *pNew;
danc9461ec2018-08-29 21:00:16 +0000716 assert( pPtr || pParse->db->mallocFailed );
dan8900a482018-09-05 14:36:05 +0000717 renameTokenCheckAll(pParse, pPtr);
drh2d99f952020-04-07 01:18:23 +0000718 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
dane6dc1e52019-12-05 14:31:43 +0000719 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
720 if( pNew ){
721 pNew->p = pPtr;
722 pNew->t = *pToken;
723 pNew->pNext = pParse->pRename;
724 pParse->pRename = pNew;
725 }
dancf8f2892018-08-09 20:47:01 +0000726 }
danc9461ec2018-08-29 21:00:16 +0000727
dand145e5f2018-08-21 08:29:48 +0000728 return pPtr;
dancf8f2892018-08-09 20:47:01 +0000729}
730
drh4a2c7472018-08-13 15:09:48 +0000731/*
dan07e95232018-08-21 16:32:53 +0000732** It is assumed that there is already a RenameToken object associated
733** with parse tree element pFrom. This function remaps the associated token
734** to parse tree element pTo.
drh4a2c7472018-08-13 15:09:48 +0000735*/
dan07e95232018-08-21 16:32:53 +0000736void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
dancf8f2892018-08-09 20:47:01 +0000737 RenameToken *p;
dan8900a482018-09-05 14:36:05 +0000738 renameTokenCheckAll(pParse, pTo);
danc9461ec2018-08-29 21:00:16 +0000739 for(p=pParse->pRename; p; p=p->pNext){
dancf8f2892018-08-09 20:47:01 +0000740 if( p->p==pFrom ){
741 p->p = pTo;
742 break;
743 }
744 }
dancf8f2892018-08-09 20:47:01 +0000745}
746
drh4a2c7472018-08-13 15:09:48 +0000747/*
dan8900a482018-09-05 14:36:05 +0000748** Walker callback used by sqlite3RenameExprUnmap().
749*/
750static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
751 Parse *pParse = pWalker->pParse;
752 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
753 return WRC_Continue;
754}
755
756/*
dan8f407622019-12-04 14:26:38 +0000757** Iterate through the Select objects that are part of WITH clauses attached
758** to select statement pSelect.
759*/
760static void renameWalkWith(Walker *pWalker, Select *pSelect){
drh646975c2019-12-17 12:03:30 +0000761 With *pWith = pSelect->pWith;
762 if( pWith ){
dan8f407622019-12-04 14:26:38 +0000763 int i;
drh646975c2019-12-17 12:03:30 +0000764 for(i=0; i<pWith->nCte; i++){
765 Select *p = pWith->a[i].pSelect;
dan8f407622019-12-04 14:26:38 +0000766 NameContext sNC;
767 memset(&sNC, 0, sizeof(sNC));
768 sNC.pParse = pWalker->pParse;
769 sqlite3SelectPrep(sNC.pParse, p, &sNC);
770 sqlite3WalkSelect(pWalker, p);
drh646975c2019-12-17 12:03:30 +0000771 sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols);
dan8f407622019-12-04 14:26:38 +0000772 }
773 }
774}
775
776/*
danfb99e382020-04-03 11:20:40 +0000777** Unmap all tokens in the IdList object passed as the second argument.
778*/
779static void unmapColumnIdlistNames(
780 Parse *pParse,
781 IdList *pIdList
782){
783 if( pIdList ){
784 int ii;
785 for(ii=0; ii<pIdList->nId; ii++){
786 sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName);
787 }
788 }
789}
790
791/*
dan0b277a92019-06-11 12:03:10 +0000792** Walker callback used by sqlite3RenameExprUnmap().
793*/
794static int renameUnmapSelectCb(Walker *pWalker, Select *p){
drhbdf4cf02019-06-15 15:21:49 +0000795 Parse *pParse = pWalker->pParse;
796 int i;
drhd63b69b2019-12-04 15:08:58 +0000797 if( pParse->nErr ) return WRC_Abort;
drh2bc5cf92019-12-09 19:29:10 +0000798 if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
drhbdf4cf02019-06-15 15:21:49 +0000799 if( ALWAYS(p->pEList) ){
800 ExprList *pList = p->pEList;
801 for(i=0; i<pList->nExpr; i++){
drhc4938ea2019-12-13 00:49:42 +0000802 if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
drh41cee662019-12-12 20:22:34 +0000803 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
drhbdf4cf02019-06-15 15:21:49 +0000804 }
805 }
806 }
drh2c3f4652019-06-11 16:43:58 +0000807 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
drhbdf4cf02019-06-15 15:21:49 +0000808 SrcList *pSrc = p->pSrc;
809 for(i=0; i<pSrc->nSrc; i++){
810 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
dan394aa712019-12-20 14:18:29 +0000811 if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort;
danfb99e382020-04-03 11:20:40 +0000812 unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing);
dan0b277a92019-06-11 12:03:10 +0000813 }
814 }
dan8f407622019-12-04 14:26:38 +0000815
816 renameWalkWith(pWalker, p);
dan0b277a92019-06-11 12:03:10 +0000817 return WRC_Continue;
818}
819
820/*
dan8900a482018-09-05 14:36:05 +0000821** Remove all nodes that are part of expression pExpr from the rename list.
822*/
823void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
dane6dc1e52019-12-05 14:31:43 +0000824 u8 eMode = pParse->eParseMode;
dan8900a482018-09-05 14:36:05 +0000825 Walker sWalker;
826 memset(&sWalker, 0, sizeof(Walker));
827 sWalker.pParse = pParse;
828 sWalker.xExprCallback = renameUnmapExprCb;
dan0b277a92019-06-11 12:03:10 +0000829 sWalker.xSelectCallback = renameUnmapSelectCb;
dane6dc1e52019-12-05 14:31:43 +0000830 pParse->eParseMode = PARSE_MODE_UNMAP;
dan8900a482018-09-05 14:36:05 +0000831 sqlite3WalkExpr(&sWalker, pExpr);
dane6dc1e52019-12-05 14:31:43 +0000832 pParse->eParseMode = eMode;
dan8900a482018-09-05 14:36:05 +0000833}
834
835/*
dane8ab40d2018-09-12 08:51:48 +0000836** Remove all nodes that are part of expression-list pEList from the
837** rename list.
838*/
839void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
840 if( pEList ){
841 int i;
842 Walker sWalker;
843 memset(&sWalker, 0, sizeof(Walker));
844 sWalker.pParse = pParse;
845 sWalker.xExprCallback = renameUnmapExprCb;
846 sqlite3WalkExprList(&sWalker, pEList);
847 for(i=0; i<pEList->nExpr; i++){
drh9fc1b9a2020-01-02 22:23:01 +0000848 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
drhc4938ea2019-12-13 00:49:42 +0000849 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
850 }
dane8ab40d2018-09-12 08:51:48 +0000851 }
852 }
853}
854
855/*
drh4a2c7472018-08-13 15:09:48 +0000856** Free the list of RenameToken objects given in the second argument
857*/
dancf8f2892018-08-09 20:47:01 +0000858static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
859 RenameToken *pNext;
860 RenameToken *p;
861 for(p=pToken; p; p=pNext){
862 pNext = p->pNext;
863 sqlite3DbFree(db, p);
864 }
865}
866
dan987db762018-08-14 20:18:50 +0000867/*
868** Search the Parse object passed as the first argument for a RenameToken
869** object associated with parse tree element pPtr. If found, remove it
870** from the Parse object and add it to the list maintained by the
871** RenameCtx object passed as the second argument.
872*/
873static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
dancf8f2892018-08-09 20:47:01 +0000874 RenameToken **pp;
dan1b0c5de2018-08-24 16:04:26 +0000875 assert( pPtr!=0 );
dancf8f2892018-08-09 20:47:01 +0000876 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
877 if( (*pp)->p==pPtr ){
878 RenameToken *pToken = *pp;
879 *pp = pToken->pNext;
dan5496d6a2018-08-13 17:14:26 +0000880 pToken->pNext = pCtx->pList;
881 pCtx->pList = pToken;
882 pCtx->nList++;
883 break;
dancf8f2892018-08-09 20:47:01 +0000884 }
885 }
dancf8f2892018-08-09 20:47:01 +0000886}
887
dan24fedb92018-08-18 17:35:38 +0000888/*
889** This is a Walker select callback. It does nothing. It is only required
890** because without a dummy callback, sqlite3WalkExpr() and similar do not
891** descend into sub-select statements.
892*/
dan987db762018-08-14 20:18:50 +0000893static int renameColumnSelectCb(Walker *pWalker, Select *p){
dan38096962019-12-09 08:13:43 +0000894 if( p->selFlags & SF_View ) return WRC_Prune;
danea412512018-12-05 13:49:04 +0000895 renameWalkWith(pWalker, p);
dan987db762018-08-14 20:18:50 +0000896 return WRC_Continue;
897}
898
dan987db762018-08-14 20:18:50 +0000899/*
900** This is a Walker expression callback.
901**
902** For every TK_COLUMN node in the expression tree, search to see
903** if the column being references is the column being renamed by an
904** ALTER TABLE statement. If it is, then attach its associated
905** RenameToken object to the list of RenameToken objects being
906** constructed in RenameCtx object at pWalker->u.pRename.
907*/
dancf8f2892018-08-09 20:47:01 +0000908static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
dan5496d6a2018-08-13 17:14:26 +0000909 RenameCtx *p = pWalker->u.pRename;
dan0cbb0b12018-08-16 19:49:16 +0000910 if( pExpr->op==TK_TRIGGER
911 && pExpr->iColumn==p->iCol
912 && pWalker->pParse->pTriggerTab==p->pTab
913 ){
dan5be60c52018-08-15 20:28:39 +0000914 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
dan0cbb0b12018-08-16 19:49:16 +0000915 }else if( pExpr->op==TK_COLUMN
916 && pExpr->iColumn==p->iCol
drheda079c2018-09-20 19:02:15 +0000917 && p->pTab==pExpr->y.pTab
dan987db762018-08-14 20:18:50 +0000918 ){
dan5496d6a2018-08-13 17:14:26 +0000919 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
dancf8f2892018-08-09 20:47:01 +0000920 }
921 return WRC_Continue;
922}
923
dan987db762018-08-14 20:18:50 +0000924/*
925** The RenameCtx contains a list of tokens that reference a column that
dan24fedb92018-08-18 17:35:38 +0000926** is being renamed by an ALTER TABLE statement. Return the "last"
dan987db762018-08-14 20:18:50 +0000927** RenameToken in the RenameCtx and remove that RenameToken from the
dan24fedb92018-08-18 17:35:38 +0000928** RenameContext. "Last" means the last RenameToken encountered when
929** the input SQL is parsed from left to right. Repeated calls to this routine
dan987db762018-08-14 20:18:50 +0000930** return all column name tokens in the order that they are encountered
931** in the SQL statement.
932*/
dan5496d6a2018-08-13 17:14:26 +0000933static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
dancf8f2892018-08-09 20:47:01 +0000934 RenameToken *pBest = pCtx->pList;
935 RenameToken *pToken;
936 RenameToken **pp;
937
938 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
939 if( pToken->t.z>pBest->t.z ) pBest = pToken;
940 }
941 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
942 *pp = pBest->pNext;
943
944 return pBest;
945}
946
dan6fe7f232018-08-10 19:19:33 +0000947/*
dan24fedb92018-08-18 17:35:38 +0000948** An error occured while parsing or otherwise processing a database
949** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
950** ALTER TABLE RENAME COLUMN program. The error message emitted by the
951** sub-routine is currently stored in pParse->zErrMsg. This function
952** adds context to the error message and then stores it in pCtx.
953*/
danb0137382018-08-20 20:01:01 +0000954static void renameColumnParseError(
955 sqlite3_context *pCtx,
dan0d5fa6b2018-08-24 17:55:49 +0000956 int bPost,
danb0137382018-08-20 20:01:01 +0000957 sqlite3_value *pType,
958 sqlite3_value *pObject,
959 Parse *pParse
960){
drh79a5ee92018-08-23 19:32:04 +0000961 const char *zT = (const char*)sqlite3_value_text(pType);
962 const char *zN = (const char*)sqlite3_value_text(pObject);
dan24fedb92018-08-18 17:35:38 +0000963 char *zErr;
danb0137382018-08-20 20:01:01 +0000964
dan0d5fa6b2018-08-24 17:55:49 +0000965 zErr = sqlite3_mprintf("error in %s %s%s: %s",
966 zT, zN, (bPost ? " after rename" : ""),
967 pParse->zErrMsg
968 );
dan24fedb92018-08-18 17:35:38 +0000969 sqlite3_result_error(pCtx, zErr, -1);
970 sqlite3_free(zErr);
971}
972
973/*
dan06249392018-08-21 15:06:59 +0000974** For each name in the the expression-list pEList (i.e. each
975** pEList->a[i].zName) that matches the string in zOld, extract the
976** corresponding rename-token from Parse object pParse and add it
977** to the RenameCtx pCtx.
978*/
979static void renameColumnElistNames(
980 Parse *pParse,
981 RenameCtx *pCtx,
982 ExprList *pEList,
983 const char *zOld
984){
985 if( pEList ){
986 int i;
987 for(i=0; i<pEList->nExpr; i++){
drh41cee662019-12-12 20:22:34 +0000988 char *zName = pEList->a[i].zEName;
drh9fc1b9a2020-01-02 22:23:01 +0000989 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
990 && ALWAYS(zName!=0)
drhc4938ea2019-12-13 00:49:42 +0000991 && 0==sqlite3_stricmp(zName, zOld)
992 ){
dan06249392018-08-21 15:06:59 +0000993 renameTokenFind(pParse, pCtx, (void*)zName);
994 }
995 }
996 }
997}
998
999/*
1000** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
1001** that matches the string in zOld, extract the corresponding rename-token
1002** from Parse object pParse and add it to the RenameCtx pCtx.
1003*/
1004static void renameColumnIdlistNames(
1005 Parse *pParse,
1006 RenameCtx *pCtx,
1007 IdList *pIdList,
1008 const char *zOld
1009){
1010 if( pIdList ){
1011 int i;
1012 for(i=0; i<pIdList->nId; i++){
1013 char *zName = pIdList->a[i].zName;
1014 if( 0==sqlite3_stricmp(zName, zOld) ){
1015 renameTokenFind(pParse, pCtx, (void*)zName);
1016 }
1017 }
1018 }
1019}
1020
danfb99e382020-04-03 11:20:40 +00001021
dandd1a9c82018-09-05 08:28:30 +00001022/*
1023** Parse the SQL statement zSql using Parse object (*p). The Parse object
1024** is initialized by this function before it is used.
1025*/
danc9461ec2018-08-29 21:00:16 +00001026static int renameParseSql(
dandd1a9c82018-09-05 08:28:30 +00001027 Parse *p, /* Memory to use for Parse object */
1028 const char *zDb, /* Name of schema SQL belongs to */
dandd1a9c82018-09-05 08:28:30 +00001029 sqlite3 *db, /* Database handle */
1030 const char *zSql, /* SQL to parse */
1031 int bTemp /* True if SQL is from temp schema */
danc9461ec2018-08-29 21:00:16 +00001032){
1033 int rc;
1034 char *zErr = 0;
1035
1036 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
1037
1038 /* Parse the SQL statement passed as the first argument. If no error
1039 ** occurs and the parse does not result in a new table, index or
1040 ** trigger object, the database must be corrupt. */
1041 memset(p, 0, sizeof(Parse));
dane6dc1e52019-12-05 14:31:43 +00001042 p->eParseMode = PARSE_MODE_RENAME;
danc9461ec2018-08-29 21:00:16 +00001043 p->db = db;
1044 p->nQueryLoop = 1;
1045 rc = sqlite3RunParser(p, zSql, &zErr);
1046 assert( p->zErrMsg==0 );
1047 assert( rc!=SQLITE_OK || zErr==0 );
danc9461ec2018-08-29 21:00:16 +00001048 p->zErrMsg = zErr;
1049 if( db->mallocFailed ) rc = SQLITE_NOMEM;
1050 if( rc==SQLITE_OK
1051 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
1052 ){
1053 rc = SQLITE_CORRUPT_BKPT;
1054 }
1055
1056#ifdef SQLITE_DEBUG
1057 /* Ensure that all mappings in the Parse.pRename list really do map to
1058 ** a part of the input string. */
1059 if( rc==SQLITE_OK ){
1060 int nSql = sqlite3Strlen30(zSql);
1061 RenameToken *pToken;
1062 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
1063 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
1064 }
1065 }
1066#endif
1067
1068 db->init.iDb = 0;
1069 return rc;
1070}
1071
dandd1a9c82018-09-05 08:28:30 +00001072/*
1073** This function edits SQL statement zSql, replacing each token identified
1074** by the linked list pRename with the text of zNew. If argument bQuote is
1075** true, then zNew is always quoted first. If no error occurs, the result
1076** is loaded into context object pCtx as the result.
1077**
1078** Or, if an error occurs (i.e. an OOM condition), an error is left in
1079** pCtx and an SQLite error code returned.
1080*/
danc9461ec2018-08-29 21:00:16 +00001081static int renameEditSql(
1082 sqlite3_context *pCtx, /* Return result here */
1083 RenameCtx *pRename, /* Rename context */
1084 const char *zSql, /* SQL statement to edit */
1085 const char *zNew, /* New token text */
1086 int bQuote /* True to always quote token */
1087){
1088 int nNew = sqlite3Strlen30(zNew);
1089 int nSql = sqlite3Strlen30(zSql);
1090 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1091 int rc = SQLITE_OK;
1092 char *zQuot;
1093 char *zOut;
1094 int nQuot;
1095
1096 /* Set zQuot to point to a buffer containing a quoted copy of the
1097 ** identifier zNew. If the corresponding identifier in the original
1098 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
1099 ** point to zQuot so that all substitutions are made using the
1100 ** quoted version of the new column name. */
drhc753c212018-09-01 16:55:36 +00001101 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
danc9461ec2018-08-29 21:00:16 +00001102 if( zQuot==0 ){
1103 return SQLITE_NOMEM;
1104 }else{
1105 nQuot = sqlite3Strlen30(zQuot);
1106 }
1107 if( bQuote ){
1108 zNew = zQuot;
1109 nNew = nQuot;
1110 }
1111
1112 /* At this point pRename->pList contains a list of RenameToken objects
1113 ** corresponding to all tokens in the input SQL that must be replaced
1114 ** with the new column name. All that remains is to construct and
1115 ** return the edited SQL string. */
1116 assert( nQuot>=nNew );
1117 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
1118 if( zOut ){
1119 int nOut = nSql;
1120 memcpy(zOut, zSql, nSql);
1121 while( pRename->pList ){
1122 int iOff; /* Offset of token to replace in zOut */
1123 RenameToken *pBest = renameColumnTokenNext(pRename);
1124
1125 u32 nReplace;
1126 const char *zReplace;
1127 if( sqlite3IsIdChar(*pBest->t.z) ){
1128 nReplace = nNew;
1129 zReplace = zNew;
1130 }else{
1131 nReplace = nQuot;
1132 zReplace = zQuot;
1133 }
1134
1135 iOff = pBest->t.z - zSql;
1136 if( pBest->t.n!=nReplace ){
1137 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
1138 nOut - (iOff + pBest->t.n)
1139 );
1140 nOut += nReplace - pBest->t.n;
1141 zOut[nOut] = '\0';
1142 }
1143 memcpy(&zOut[iOff], zReplace, nReplace);
1144 sqlite3DbFree(db, pBest);
1145 }
1146
1147 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
1148 sqlite3DbFree(db, zOut);
1149 }else{
1150 rc = SQLITE_NOMEM;
1151 }
1152
1153 sqlite3_free(zQuot);
1154 return rc;
1155}
1156
dandd1a9c82018-09-05 08:28:30 +00001157/*
1158** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
1159** it was read from the schema of database zDb. Return SQLITE_OK if
1160** successful. Otherwise, return an SQLite error code and leave an error
1161** message in the Parse object.
1162*/
drha7c74002020-07-18 18:44:59 +00001163static int renameResolveTrigger(Parse *pParse){
danc9461ec2018-08-29 21:00:16 +00001164 sqlite3 *db = pParse->db;
dand5e6fef2018-09-07 15:50:31 +00001165 Trigger *pNew = pParse->pNewTrigger;
danc9461ec2018-08-29 21:00:16 +00001166 TriggerStep *pStep;
1167 NameContext sNC;
1168 int rc = SQLITE_OK;
1169
1170 memset(&sNC, 0, sizeof(sNC));
1171 sNC.pParse = pParse;
dand5e6fef2018-09-07 15:50:31 +00001172 assert( pNew->pTabSchema );
1173 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
1174 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
1175 );
1176 pParse->eTriggerOp = pNew->op;
drhf470c372018-10-03 18:05:36 +00001177 /* ALWAYS() because if the table of the trigger does not exist, the
1178 ** error would have been hit before this point */
1179 if( ALWAYS(pParse->pTriggerTab) ){
dan5351e882018-10-01 07:04:12 +00001180 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
1181 }
danc9461ec2018-08-29 21:00:16 +00001182
1183 /* Resolve symbols in WHEN clause */
dan5351e882018-10-01 07:04:12 +00001184 if( rc==SQLITE_OK && pNew->pWhen ){
dand5e6fef2018-09-07 15:50:31 +00001185 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
danc9461ec2018-08-29 21:00:16 +00001186 }
1187
dand5e6fef2018-09-07 15:50:31 +00001188 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
danc9461ec2018-08-29 21:00:16 +00001189 if( pStep->pSelect ){
1190 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
1191 if( pParse->nErr ) rc = pParse->rc;
1192 }
dand5e6fef2018-09-07 15:50:31 +00001193 if( rc==SQLITE_OK && pStep->zTarget ){
dane7877b22020-07-14 19:51:01 +00001194 SrcList *pSrc = sqlite3TriggerStepSrc(pParse, pStep);
1195 if( pSrc ){
1196 int i;
drha3e64952020-08-12 16:19:12 +00001197 for(i=0; i<pSrc->nSrc && rc==SQLITE_OK; i++){
dane7877b22020-07-14 19:51:01 +00001198 struct SrcList_item *p = &pSrc->a[i];
1199 p->pTab = sqlite3LocateTableItem(pParse, 0, p);
1200 p->iCursor = pParse->nTab++;
1201 if( p->pTab==0 ){
1202 rc = SQLITE_ERROR;
1203 }else{
1204 p->pTab->nTabRef++;
1205 rc = sqlite3ViewGetColumnNames(pParse, p->pTab);
1206 }
1207 }
1208 sNC.pSrcList = pSrc;
1209 if( rc==SQLITE_OK && pStep->pWhere ){
danc9461ec2018-08-29 21:00:16 +00001210 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
1211 }
1212 if( rc==SQLITE_OK ){
1213 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
1214 }
1215 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
1216 if( pStep->pUpsert ){
1217 Upsert *pUpsert = pStep->pUpsert;
1218 assert( rc==SQLITE_OK );
dane7877b22020-07-14 19:51:01 +00001219 pUpsert->pUpsertSrc = pSrc;
danc9461ec2018-08-29 21:00:16 +00001220 sNC.uNC.pUpsert = pUpsert;
1221 sNC.ncFlags = NC_UUpsert;
1222 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
1223 if( rc==SQLITE_OK ){
1224 ExprList *pUpsertSet = pUpsert->pUpsertSet;
1225 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
1226 }
1227 if( rc==SQLITE_OK ){
1228 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
1229 }
1230 if( rc==SQLITE_OK ){
1231 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
1232 }
1233 sNC.ncFlags = 0;
1234 }
dan0e14e982019-01-18 16:06:18 +00001235 sNC.pSrcList = 0;
dane7877b22020-07-14 19:51:01 +00001236 sqlite3SrcListDelete(db, pSrc);
1237 }else{
1238 rc = SQLITE_NOMEM;
danc9461ec2018-08-29 21:00:16 +00001239 }
1240 }
1241 }
1242 return rc;
1243}
1244
dandd1a9c82018-09-05 08:28:30 +00001245/*
1246** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
1247** objects that are part of the trigger passed as the second argument.
1248*/
danc9461ec2018-08-29 21:00:16 +00001249static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
1250 TriggerStep *pStep;
1251
1252 /* Find tokens to edit in WHEN clause */
1253 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
1254
1255 /* Find tokens to edit in trigger steps */
1256 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1257 sqlite3WalkSelect(pWalker, pStep->pSelect);
1258 sqlite3WalkExpr(pWalker, pStep->pWhere);
1259 sqlite3WalkExprList(pWalker, pStep->pExprList);
1260 if( pStep->pUpsert ){
1261 Upsert *pUpsert = pStep->pUpsert;
1262 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
1263 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
1264 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
1265 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
1266 }
1267 }
1268}
1269
dandd1a9c82018-09-05 08:28:30 +00001270/*
1271** Free the contents of Parse object (*pParse). Do not free the memory
1272** occupied by the Parse object itself.
1273*/
dan141e1192018-08-31 18:23:53 +00001274static void renameParseCleanup(Parse *pParse){
1275 sqlite3 *db = pParse->db;
drh885eeb62019-01-09 02:02:24 +00001276 Index *pIdx;
dan141e1192018-08-31 18:23:53 +00001277 if( pParse->pVdbe ){
1278 sqlite3VdbeFinalize(pParse->pVdbe);
1279 }
1280 sqlite3DeleteTable(db, pParse->pNewTable);
drh885eeb62019-01-09 02:02:24 +00001281 while( (pIdx = pParse->pNewIndex)!=0 ){
1282 pParse->pNewIndex = pIdx->pNext;
1283 sqlite3FreeIndex(db, pIdx);
1284 }
dan141e1192018-08-31 18:23:53 +00001285 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
1286 sqlite3DbFree(db, pParse->zErrMsg);
1287 renameTokenFree(db, pParse->pRename);
1288 sqlite3ParserReset(pParse);
1289}
1290
dan06249392018-08-21 15:06:59 +00001291/*
dan987db762018-08-14 20:18:50 +00001292** SQL function:
1293**
1294** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
1295**
1296** 0. zSql: SQL statement to rewrite
danb0137382018-08-20 20:01:01 +00001297** 1. type: Type of object ("table", "view" etc.)
1298** 2. object: Name of object
1299** 3. Database: Database name (e.g. "main")
1300** 4. Table: Table name
1301** 5. iCol: Index of column to rename
1302** 6. zNew: New column name
dan9d324822018-08-30 20:03:44 +00001303** 7. bQuote: Non-zero if the new column name should be quoted.
danb87a9a82018-09-01 20:23:28 +00001304** 8. bTemp: True if zSql comes from temp schema
dan987db762018-08-14 20:18:50 +00001305**
1306** Do a column rename operation on the CREATE statement given in zSql.
1307** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
1308** into zNew. The name should be quoted if bQuote is true.
1309**
1310** This function is used internally by the ALTER TABLE RENAME COLUMN command.
drheea8eb62018-11-26 18:09:15 +00001311** It is only accessible to SQL created using sqlite3NestedParse(). It is
1312** not reachable from ordinary SQL passed into sqlite3_prepare().
dan6fe7f232018-08-10 19:19:33 +00001313*/
dancf8f2892018-08-09 20:47:01 +00001314static void renameColumnFunc(
1315 sqlite3_context *context,
1316 int NotUsed,
1317 sqlite3_value **argv
1318){
1319 sqlite3 *db = sqlite3_context_db_handle(context);
dan5496d6a2018-08-13 17:14:26 +00001320 RenameCtx sCtx;
drhad866a12018-08-10 19:33:09 +00001321 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
danb0137382018-08-20 20:01:01 +00001322 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
1323 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
1324 int iCol = sqlite3_value_int(argv[5]);
1325 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
danb0137382018-08-20 20:01:01 +00001326 int bQuote = sqlite3_value_int(argv[7]);
danb87a9a82018-09-01 20:23:28 +00001327 int bTemp = sqlite3_value_int(argv[8]);
dan987db762018-08-14 20:18:50 +00001328 const char *zOld;
dancf8f2892018-08-09 20:47:01 +00001329 int rc;
dancf8f2892018-08-09 20:47:01 +00001330 Parse sParse;
1331 Walker sWalker;
dancf8f2892018-08-09 20:47:01 +00001332 Index *pIdx;
dancf8f2892018-08-09 20:47:01 +00001333 int i;
dan987db762018-08-14 20:18:50 +00001334 Table *pTab;
dan141e1192018-08-31 18:23:53 +00001335#ifndef SQLITE_OMIT_AUTHORIZATION
1336 sqlite3_xauth xAuth = db->xAuth;
1337#endif
dancf8f2892018-08-09 20:47:01 +00001338
drh38d99642018-08-18 18:27:18 +00001339 UNUSED_PARAMETER(NotUsed);
dan987db762018-08-14 20:18:50 +00001340 if( zSql==0 ) return;
dan987db762018-08-14 20:18:50 +00001341 if( zTable==0 ) return;
danb0137382018-08-20 20:01:01 +00001342 if( zNew==0 ) return;
dan987db762018-08-14 20:18:50 +00001343 if( iCol<0 ) return;
drhda76adc2018-08-25 02:04:05 +00001344 sqlite3BtreeEnterAll(db);
dan987db762018-08-14 20:18:50 +00001345 pTab = sqlite3FindTable(db, zTable, zDb);
drhda76adc2018-08-25 02:04:05 +00001346 if( pTab==0 || iCol>=pTab->nCol ){
1347 sqlite3BtreeLeaveAll(db);
1348 return;
1349 }
dan987db762018-08-14 20:18:50 +00001350 zOld = pTab->aCol[iCol].zName;
dancf8f2892018-08-09 20:47:01 +00001351 memset(&sCtx, 0, sizeof(sCtx));
dan987db762018-08-14 20:18:50 +00001352 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
dancf8f2892018-08-09 20:47:01 +00001353
dan141e1192018-08-31 18:23:53 +00001354#ifndef SQLITE_OMIT_AUTHORIZATION
1355 db->xAuth = 0;
1356#endif
drhb2ab3dc2019-12-20 14:08:34 +00001357 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
dan24fedb92018-08-18 17:35:38 +00001358
dancf8f2892018-08-09 20:47:01 +00001359 /* Find tokens that need to be replaced. */
1360 memset(&sWalker, 0, sizeof(Walker));
1361 sWalker.pParse = &sParse;
1362 sWalker.xExprCallback = renameColumnExprCb;
dan987db762018-08-14 20:18:50 +00001363 sWalker.xSelectCallback = renameColumnSelectCb;
dancf8f2892018-08-09 20:47:01 +00001364 sWalker.u.pRename = &sCtx;
1365
dan0cbb0b12018-08-16 19:49:16 +00001366 sCtx.pTab = pTab;
dan987db762018-08-14 20:18:50 +00001367 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
dancf8f2892018-08-09 20:47:01 +00001368 if( sParse.pNewTable ){
dan987db762018-08-14 20:18:50 +00001369 Select *pSelect = sParse.pNewTable->pSelect;
1370 if( pSelect ){
dan38096962019-12-09 08:13:43 +00001371 pSelect->selFlags &= ~SF_View;
dan987db762018-08-14 20:18:50 +00001372 sParse.rc = SQLITE_OK;
dan38096962019-12-09 08:13:43 +00001373 sqlite3SelectPrep(&sParse, pSelect, 0);
dan987db762018-08-14 20:18:50 +00001374 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
1375 if( rc==SQLITE_OK ){
1376 sqlite3WalkSelect(&sWalker, pSelect);
dana8762ae2018-08-11 17:49:23 +00001377 }
dan987db762018-08-14 20:18:50 +00001378 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
1379 }else{
1380 /* A regular table */
1381 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
1382 FKey *pFKey;
1383 assert( sParse.pNewTable->pSelect==0 );
dan0cbb0b12018-08-16 19:49:16 +00001384 sCtx.pTab = sParse.pNewTable;
dan987db762018-08-14 20:18:50 +00001385 if( bFKOnly==0 ){
1386 renameTokenFind(
1387 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
1388 );
1389 if( sCtx.iCol<0 ){
1390 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
dancf8f2892018-08-09 20:47:01 +00001391 }
dan987db762018-08-14 20:18:50 +00001392 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
1393 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
1394 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1395 }
drh885eeb62019-01-09 02:02:24 +00001396 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
1397 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1398 }
dan987db762018-08-14 20:18:50 +00001399 }
drhb9bcf7c2019-10-19 13:29:10 +00001400#ifndef SQLITE_OMIT_GENERATED_COLUMNS
1401 for(i=0; i<sParse.pNewTable->nCol; i++){
1402 sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
1403 }
1404#endif
dan987db762018-08-14 20:18:50 +00001405
1406 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1407 for(i=0; i<pFKey->nCol; i++){
dan356afab2018-08-14 21:05:35 +00001408 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
dan987db762018-08-14 20:18:50 +00001409 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
1410 }
1411 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
1412 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
1413 ){
1414 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
1415 }
dan6fe7f232018-08-10 19:19:33 +00001416 }
dancf8f2892018-08-09 20:47:01 +00001417 }
1418 }
dan5496d6a2018-08-13 17:14:26 +00001419 }else if( sParse.pNewIndex ){
dancf8f2892018-08-09 20:47:01 +00001420 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
1421 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
dan5496d6a2018-08-13 17:14:26 +00001422 }else{
dan5be60c52018-08-15 20:28:39 +00001423 /* A trigger */
1424 TriggerStep *pStep;
drha7c74002020-07-18 18:44:59 +00001425 rc = renameResolveTrigger(&sParse);
danc9461ec2018-08-29 21:00:16 +00001426 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
dan5be60c52018-08-15 20:28:39 +00001427
danc9461ec2018-08-29 21:00:16 +00001428 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
1429 if( pStep->zTarget ){
dan06249392018-08-21 15:06:59 +00001430 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
danc9461ec2018-08-29 21:00:16 +00001431 if( pTarget==pTab ){
dan0cbb0b12018-08-16 19:49:16 +00001432 if( pStep->pUpsert ){
danc9461ec2018-08-29 21:00:16 +00001433 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
1434 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
dan0cbb0b12018-08-16 19:49:16 +00001435 }
danc9461ec2018-08-29 21:00:16 +00001436 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
1437 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
dan5be60c52018-08-15 20:28:39 +00001438 }
1439 }
1440 }
1441
dan5be60c52018-08-15 20:28:39 +00001442
1443 /* Find tokens to edit in UPDATE OF clause */
dan06249392018-08-21 15:06:59 +00001444 if( sParse.pTriggerTab==pTab ){
1445 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
dan5496d6a2018-08-13 17:14:26 +00001446 }
dan5be60c52018-08-15 20:28:39 +00001447
danc9461ec2018-08-29 21:00:16 +00001448 /* Find tokens to edit in various expressions and selects */
1449 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
dancf8f2892018-08-09 20:47:01 +00001450 }
1451
dan987db762018-08-14 20:18:50 +00001452 assert( rc==SQLITE_OK );
danc9461ec2018-08-29 21:00:16 +00001453 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
dancf8f2892018-08-09 20:47:01 +00001454
drh5fc22cd2018-08-13 13:43:11 +00001455renameColumnFunc_done:
dan987db762018-08-14 20:18:50 +00001456 if( rc!=SQLITE_OK ){
dan24fedb92018-08-18 17:35:38 +00001457 if( sParse.zErrMsg ){
dan9d324822018-08-30 20:03:44 +00001458 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
dan987db762018-08-14 20:18:50 +00001459 }else{
1460 sqlite3_result_error_code(context, rc);
1461 }
1462 }
1463
dan141e1192018-08-31 18:23:53 +00001464 renameParseCleanup(&sParse);
drh4a2c7472018-08-13 15:09:48 +00001465 renameTokenFree(db, sCtx.pList);
dan141e1192018-08-31 18:23:53 +00001466#ifndef SQLITE_OMIT_AUTHORIZATION
1467 db->xAuth = xAuth;
1468#endif
drhda76adc2018-08-25 02:04:05 +00001469 sqlite3BtreeLeaveAll(db);
dancf8f2892018-08-09 20:47:01 +00001470}
1471
dandd1a9c82018-09-05 08:28:30 +00001472/*
1473** Walker expression callback used by "RENAME TABLE".
1474*/
danc9461ec2018-08-29 21:00:16 +00001475static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
1476 RenameCtx *p = pWalker->u.pRename;
drheda079c2018-09-20 19:02:15 +00001477 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
1478 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
danc9461ec2018-08-29 21:00:16 +00001479 }
1480 return WRC_Continue;
1481}
1482
1483/*
dandd1a9c82018-09-05 08:28:30 +00001484** Walker select callback used by "RENAME TABLE".
danc9461ec2018-08-29 21:00:16 +00001485*/
1486static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
1487 int i;
1488 RenameCtx *p = pWalker->u.pRename;
1489 SrcList *pSrc = pSelect->pSrc;
dan38096962019-12-09 08:13:43 +00001490 if( pSelect->selFlags & SF_View ) return WRC_Prune;
drh92a28242019-10-09 15:37:58 +00001491 if( pSrc==0 ){
1492 assert( pWalker->pParse->db->mallocFailed );
drh974b2482018-12-06 01:53:12 +00001493 return WRC_Abort;
1494 }
danc9461ec2018-08-29 21:00:16 +00001495 for(i=0; i<pSrc->nSrc; i++){
1496 struct SrcList_item *pItem = &pSrc->a[i];
1497 if( pItem->pTab==p->pTab ){
1498 renameTokenFind(pWalker->pParse, p, pItem->zName);
1499 }
1500 }
danea412512018-12-05 13:49:04 +00001501 renameWalkWith(pWalker, pSelect);
danc9461ec2018-08-29 21:00:16 +00001502
1503 return WRC_Continue;
1504}
1505
1506
1507/*
1508** This C function implements an SQL user function that is used by SQL code
1509** generated by the ALTER TABLE ... RENAME command to modify the definition
1510** of any foreign key constraints that use the table being renamed as the
1511** parent table. It is passed three arguments:
1512**
dan141e1192018-08-31 18:23:53 +00001513** 0: The database containing the table being renamed.
dan65372fa2018-09-03 20:05:15 +00001514** 1. type: Type of object ("table", "view" etc.)
1515** 2. object: Name of object
1516** 3: The complete text of the schema statement being modified,
1517** 4: The old name of the table being renamed, and
1518** 5: The new name of the table being renamed.
1519** 6: True if the schema statement comes from the temp db.
danc9461ec2018-08-29 21:00:16 +00001520**
dan141e1192018-08-31 18:23:53 +00001521** It returns the new schema statement. For example:
danc9461ec2018-08-29 21:00:16 +00001522**
dan141e1192018-08-31 18:23:53 +00001523** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
danc9461ec2018-08-29 21:00:16 +00001524** -> 'CREATE TABLE t1(a REFERENCES t3)'
1525*/
1526static void renameTableFunc(
1527 sqlite3_context *context,
1528 int NotUsed,
1529 sqlite3_value **argv
1530){
1531 sqlite3 *db = sqlite3_context_db_handle(context);
drh5b1da302018-09-01 20:02:07 +00001532 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
dan65372fa2018-09-03 20:05:15 +00001533 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
1534 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
1535 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
1536 int bTemp = sqlite3_value_int(argv[6]);
drh5b1da302018-09-01 20:02:07 +00001537 UNUSED_PARAMETER(NotUsed);
danc9461ec2018-08-29 21:00:16 +00001538
dan141e1192018-08-31 18:23:53 +00001539 if( zInput && zOld && zNew ){
dan141e1192018-08-31 18:23:53 +00001540 Parse sParse;
1541 int rc;
1542 int bQuote = 1;
1543 RenameCtx sCtx;
1544 Walker sWalker;
danc9461ec2018-08-29 21:00:16 +00001545
dan141e1192018-08-31 18:23:53 +00001546#ifndef SQLITE_OMIT_AUTHORIZATION
1547 sqlite3_xauth xAuth = db->xAuth;
1548 db->xAuth = 0;
danc9461ec2018-08-29 21:00:16 +00001549#endif
1550
dan141e1192018-08-31 18:23:53 +00001551 sqlite3BtreeEnterAll(db);
1552
1553 memset(&sCtx, 0, sizeof(RenameCtx));
1554 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
1555 memset(&sWalker, 0, sizeof(Walker));
1556 sWalker.pParse = &sParse;
1557 sWalker.xExprCallback = renameTableExprCb;
1558 sWalker.xSelectCallback = renameTableSelectCb;
1559 sWalker.u.pRename = &sCtx;
1560
drhb2ab3dc2019-12-20 14:08:34 +00001561 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
dan141e1192018-08-31 18:23:53 +00001562
1563 if( rc==SQLITE_OK ){
dan674b8942018-09-20 08:28:01 +00001564 int isLegacy = (db->flags & SQLITE_LegacyAlter);
dan141e1192018-08-31 18:23:53 +00001565 if( sParse.pNewTable ){
1566 Table *pTab = sParse.pNewTable;
1567
1568 if( pTab->pSelect ){
dan674b8942018-09-20 08:28:01 +00001569 if( isLegacy==0 ){
dan38096962019-12-09 08:13:43 +00001570 Select *pSelect = pTab->pSelect;
dan674b8942018-09-20 08:28:01 +00001571 NameContext sNC;
1572 memset(&sNC, 0, sizeof(sNC));
1573 sNC.pParse = &sParse;
dan141e1192018-08-31 18:23:53 +00001574
dan38096962019-12-09 08:13:43 +00001575 assert( pSelect->selFlags & SF_View );
1576 pSelect->selFlags &= ~SF_View;
dan674b8942018-09-20 08:28:01 +00001577 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
drh1548d522019-12-20 12:55:21 +00001578 if( sParse.nErr ){
1579 rc = sParse.rc;
1580 }else{
1581 sqlite3WalkSelect(&sWalker, pTab->pSelect);
1582 }
dan674b8942018-09-20 08:28:01 +00001583 }
dan141e1192018-08-31 18:23:53 +00001584 }else{
1585 /* Modify any FK definitions to point to the new table. */
1586#ifndef SQLITE_OMIT_FOREIGN_KEY
danb4307012018-11-09 20:04:05 +00001587 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
dan202a0272018-09-07 11:51:21 +00001588 FKey *pFKey;
1589 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1590 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
1591 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
1592 }
dan141e1192018-08-31 18:23:53 +00001593 }
1594 }
1595#endif
1596
1597 /* If this is the table being altered, fix any table refs in CHECK
1598 ** expressions. Also update the name that appears right after the
1599 ** "CREATE [VIRTUAL] TABLE" bit. */
1600 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
1601 sCtx.pTab = pTab;
dan674b8942018-09-20 08:28:01 +00001602 if( isLegacy==0 ){
1603 sqlite3WalkExprList(&sWalker, pTab->pCheck);
1604 }
dan141e1192018-08-31 18:23:53 +00001605 renameTokenFind(&sParse, &sCtx, pTab->zName);
1606 }
danc9461ec2018-08-29 21:00:16 +00001607 }
1608 }
danc9461ec2018-08-29 21:00:16 +00001609
dan141e1192018-08-31 18:23:53 +00001610 else if( sParse.pNewIndex ){
1611 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
dan674b8942018-09-20 08:28:01 +00001612 if( isLegacy==0 ){
1613 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
1614 }
dan141e1192018-08-31 18:23:53 +00001615 }
danc9461ec2018-08-29 21:00:16 +00001616
1617#ifndef SQLITE_OMIT_TRIGGER
danb87a9a82018-09-01 20:23:28 +00001618 else{
dan141e1192018-08-31 18:23:53 +00001619 Trigger *pTrigger = sParse.pNewTrigger;
1620 TriggerStep *pStep;
1621 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
1622 && sCtx.pTab->pSchema==pTrigger->pTabSchema
1623 ){
1624 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
1625 }
danc9461ec2018-08-29 21:00:16 +00001626
dan674b8942018-09-20 08:28:01 +00001627 if( isLegacy==0 ){
drha7c74002020-07-18 18:44:59 +00001628 rc = renameResolveTrigger(&sParse);
dan674b8942018-09-20 08:28:01 +00001629 if( rc==SQLITE_OK ){
1630 renameWalkTrigger(&sWalker, pTrigger);
1631 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1632 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
1633 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
1634 }
dan141e1192018-08-31 18:23:53 +00001635 }
dan0ccda962018-08-30 16:26:48 +00001636 }
danc9461ec2018-08-29 21:00:16 +00001637 }
1638 }
dan141e1192018-08-31 18:23:53 +00001639#endif
danc9461ec2018-08-29 21:00:16 +00001640 }
dan141e1192018-08-31 18:23:53 +00001641
1642 if( rc==SQLITE_OK ){
1643 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
1644 }
1645 if( rc!=SQLITE_OK ){
dan65372fa2018-09-03 20:05:15 +00001646 if( sParse.zErrMsg ){
1647 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
1648 }else{
1649 sqlite3_result_error_code(context, rc);
1650 }
dan141e1192018-08-31 18:23:53 +00001651 }
1652
1653 renameParseCleanup(&sParse);
1654 renameTokenFree(db, sCtx.pList);
1655 sqlite3BtreeLeaveAll(db);
1656#ifndef SQLITE_OMIT_AUTHORIZATION
1657 db->xAuth = xAuth;
danc9461ec2018-08-29 21:00:16 +00001658#endif
1659 }
1660
danc9461ec2018-08-29 21:00:16 +00001661 return;
1662}
1663
dandd1a9c82018-09-05 08:28:30 +00001664/*
1665** An SQL user function that checks that there are no parse or symbol
1666** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
1667** After an ALTER TABLE .. RENAME operation is performed and the schema
1668** reloaded, this function is called on each SQL statement in the schema
dan1d85c6b2018-09-06 16:01:37 +00001669** to ensure that it is still usable.
dandd1a9c82018-09-05 08:28:30 +00001670**
1671** 0: Database name ("main", "temp" etc.).
1672** 1: SQL statement.
1673** 2: Object type ("view", "table", "trigger" or "index").
1674** 3: Object name.
1675** 4: True if object is from temp schema.
dan1d85c6b2018-09-06 16:01:37 +00001676**
1677** Unless it finds an error, this function normally returns NULL. However, it
1678** returns integer value 1 if:
1679**
1680** * the SQL argument creates a trigger, and
1681** * the table that the trigger is attached to is in database zDb.
dandd1a9c82018-09-05 08:28:30 +00001682*/
dan9d324822018-08-30 20:03:44 +00001683static void renameTableTest(
1684 sqlite3_context *context,
1685 int NotUsed,
1686 sqlite3_value **argv
1687){
1688 sqlite3 *db = sqlite3_context_db_handle(context);
drh5b1da302018-09-01 20:02:07 +00001689 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
1690 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
dan9d324822018-08-30 20:03:44 +00001691 int bTemp = sqlite3_value_int(argv[4]);
dan674b8942018-09-20 08:28:01 +00001692 int isLegacy = (db->flags & SQLITE_LegacyAlter);
dan9d324822018-08-30 20:03:44 +00001693
dan141e1192018-08-31 18:23:53 +00001694#ifndef SQLITE_OMIT_AUTHORIZATION
1695 sqlite3_xauth xAuth = db->xAuth;
1696 db->xAuth = 0;
1697#endif
1698
drh5b1da302018-09-01 20:02:07 +00001699 UNUSED_PARAMETER(NotUsed);
dan09236502018-09-01 16:05:50 +00001700 if( zDb && zInput ){
1701 int rc;
1702 Parse sParse;
drhb2ab3dc2019-12-20 14:08:34 +00001703 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
dan09236502018-09-01 16:05:50 +00001704 if( rc==SQLITE_OK ){
dan674b8942018-09-20 08:28:01 +00001705 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
dan09236502018-09-01 16:05:50 +00001706 NameContext sNC;
1707 memset(&sNC, 0, sizeof(sNC));
1708 sNC.pParse = &sParse;
1709 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
1710 if( sParse.nErr ) rc = sParse.rc;
1711 }
1712
1713 else if( sParse.pNewTrigger ){
dan674b8942018-09-20 08:28:01 +00001714 if( isLegacy==0 ){
drha7c74002020-07-18 18:44:59 +00001715 rc = renameResolveTrigger(&sParse);
dan674b8942018-09-20 08:28:01 +00001716 }
drh3b700452018-09-07 19:12:08 +00001717 if( rc==SQLITE_OK ){
dan1d85c6b2018-09-06 16:01:37 +00001718 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
1719 int i2 = sqlite3FindDbName(db, zDb);
1720 if( i1==i2 ) sqlite3_result_int(context, 1);
1721 }
dan09236502018-09-01 16:05:50 +00001722 }
dan9d324822018-08-30 20:03:44 +00001723 }
1724
dan09236502018-09-01 16:05:50 +00001725 if( rc!=SQLITE_OK ){
1726 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
dan9d324822018-08-30 20:03:44 +00001727 }
dan09236502018-09-01 16:05:50 +00001728 renameParseCleanup(&sParse);
dan9d324822018-08-30 20:03:44 +00001729 }
1730
dan141e1192018-08-31 18:23:53 +00001731#ifndef SQLITE_OMIT_AUTHORIZATION
1732 db->xAuth = xAuth;
1733#endif
dan9d324822018-08-30 20:03:44 +00001734}
1735
dancf8f2892018-08-09 20:47:01 +00001736/*
1737** Register built-in functions used to help implement ALTER TABLE
1738*/
1739void sqlite3AlterFunctions(void){
1740 static FuncDef aAlterTableFuncs[] = {
drheea8eb62018-11-26 18:09:15 +00001741 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
1742 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
1743 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest),
dancf8f2892018-08-09 20:47:01 +00001744 };
1745 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
1746}
drhd0e4a6c2005-02-15 20:47:57 +00001747#endif /* SQLITE_ALTER_TABLE */