blob: c357f220744fe7c303d0da0e077c1f3ab2fa99da [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 "
55 "FROM \"%w\".%s "
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 ",
dan9d324822018-08-30 20:03:44 +000059 zDb, MASTER_NAME,
60 zDb, bTemp
61 );
62
63 if( bTemp==0 ){
64 sqlite3NestedParse(pParse,
65 "SELECT 1 "
66 "FROM temp.%s "
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 ",
dan9d324822018-08-30 20:03:44 +000070 MASTER_NAME, zDb
71 );
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 */
126 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
127 sqlite3ErrorMsg(pParse,
128 "there is already another table or index with this name: %s", zName);
129 goto exit_rename_table;
130 }
131
132 /* Make sure it is not a system table being altered, or a reserved name
133 ** that the table is being renamed to.
134 */
dan397a78d2018-12-18 20:31:14 +0000135 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
drhd0e4a6c2005-02-15 20:47:57 +0000136 goto exit_rename_table;
137 }
drhc5a93d42019-08-12 00:08:07 +0000138 if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
139 goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000140 }
141
danielk197761116ae2007-12-13 08:15:30 +0000142#ifndef SQLITE_OMIT_VIEW
143 if( pTab->pSelect ){
144 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
145 goto exit_rename_table;
146 }
147#endif
148
drhd0e4a6c2005-02-15 20:47:57 +0000149#ifndef SQLITE_OMIT_AUTHORIZATION
150 /* Invoke the authorization callback. */
151 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
152 goto exit_rename_table;
153 }
154#endif
155
danielk1977182c4ba2007-06-27 15:53:34 +0000156#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19775c558862007-06-27 17:09:24 +0000157 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
158 goto exit_rename_table;
159 }
danielk1977595a5232009-07-24 17:58:53 +0000160 if( IsVirtual(pTab) ){
161 pVTab = sqlite3GetVTable(db, pTab);
162 if( pVTab->pVtab->pModule->xRename==0 ){
163 pVTab = 0;
164 }
danielk1977182c4ba2007-06-27 15:53:34 +0000165 }
166#endif
167
dan1f3b2842019-03-15 16:17:32 +0000168 /* Begin a transaction for database iDb. Then modify the schema cookie
169 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
170 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
171 ** nested SQL may raise an exception. */
drhd0e4a6c2005-02-15 20:47:57 +0000172 v = sqlite3GetVdbe(pParse);
173 if( v==0 ){
174 goto exit_rename_table;
175 }
dan1f3b2842019-03-15 16:17:32 +0000176 sqlite3MayAbort(pParse);
drhd0e4a6c2005-02-15 20:47:57 +0000177
drh4e5dd852007-05-15 03:56:49 +0000178 /* figure out how many UTF-8 characters are in zName */
179 zTabName = pTab->zName;
drh9a087a92007-05-15 14:34:32 +0000180 nTabName = sqlite3Utf8CharLen(zTabName, -1);
drh4e5dd852007-05-15 03:56:49 +0000181
danc9461ec2018-08-29 21:00:16 +0000182 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
183 ** the schema to use the new table name. */
184 sqlite3NestedParse(pParse,
185 "UPDATE \"%w\".%s SET "
dan65372fa2018-09-03 20:05:15 +0000186 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
danc9461ec2018-08-29 21:00:16 +0000187 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
dan65455fc2019-04-19 16:34:22 +0000188 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
dan0ccda962018-08-30 16:26:48 +0000189 , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
danc9461ec2018-08-29 21:00:16 +0000190 );
dan432cc5b2009-09-26 17:51:48 +0000191
danc9461ec2018-08-29 21:00:16 +0000192 /* Update the tbl_name and name columns of the sqlite_master table
193 ** as required. */
drhd0e4a6c2005-02-15 20:47:57 +0000194 sqlite3NestedParse(pParse,
195 "UPDATE %Q.%s SET "
drhd0e4a6c2005-02-15 20:47:57 +0000196 "tbl_name = %Q, "
197 "name = CASE "
198 "WHEN type='table' THEN %Q "
dan65455fc2019-04-19 16:34:22 +0000199 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
200 " AND type='index' THEN "
drha21a9292007-10-20 20:58:57 +0000201 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
drhd0e4a6c2005-02-15 20:47:57 +0000202 "ELSE name END "
drh01522682012-02-01 01:13:10 +0000203 "WHERE tbl_name=%Q COLLATE nocase AND "
drhd0e4a6c2005-02-15 20:47:57 +0000204 "(type='table' OR type='index' OR type='trigger');",
danc9461ec2018-08-29 21:00:16 +0000205 zDb, MASTER_NAME,
206 zName, zName, zName,
207 nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000208 );
209
210#ifndef SQLITE_OMIT_AUTOINCREMENT
211 /* If the sqlite_sequence table exists in this database, then update
212 ** it with the new table name.
213 */
214 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
215 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000216 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
drhd0e4a6c2005-02-15 20:47:57 +0000217 zDb, zName, pTab->zName);
218 }
219#endif
220
danc9461ec2018-08-29 21:00:16 +0000221 /* If the table being renamed is not itself part of the temp database,
222 ** edit view and trigger definitions within the temp database
223 ** as required. */
224 if( iDb!=1 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000225 sqlite3NestedParse(pParse,
226 "UPDATE sqlite_temp_master SET "
dan65372fa2018-09-03 20:05:15 +0000227 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
danc9461ec2018-08-29 21:00:16 +0000228 "tbl_name = "
dan1d85c6b2018-09-06 16:01:37 +0000229 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
230 " sqlite_rename_test(%Q, sql, type, name, 1) "
231 "THEN %Q ELSE tbl_name END "
danc9461ec2018-08-29 21:00:16 +0000232 "WHERE type IN ('view', 'trigger')"
dan1d85c6b2018-09-06 16:01:37 +0000233 , zDb, zTabName, zName, zTabName, zDb, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000234 }
drhd0e4a6c2005-02-15 20:47:57 +0000235
dan34566c42018-09-20 17:21:21 +0000236 /* If this is a virtual table, invoke the xRename() function if
237 ** one is defined. The xRename() callback will modify the names
238 ** of any resources used by the v-table implementation (including other
239 ** SQLite tables) that are identified by the name of the virtual table.
240 */
241#ifndef SQLITE_OMIT_VIRTUALTABLE
242 if( pVTab ){
243 int i = ++pParse->nMem;
244 sqlite3VdbeLoadString(v, i, zName);
245 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
dan34566c42018-09-20 17:21:21 +0000246 }
247#endif
248
dan09236502018-09-01 16:05:50 +0000249 renameReloadSchema(pParse, iDb);
dan9d324822018-08-30 20:03:44 +0000250 renameTestSchema(pParse, zDb, iDb==1);
251
drhd0e4a6c2005-02-15 20:47:57 +0000252exit_rename_table:
drh633e6d52008-07-28 19:34:53 +0000253 sqlite3SrcListDelete(db, pSrc);
254 sqlite3DbFree(db, zName);
drh8257aa82017-07-26 19:59:13 +0000255 db->mDbFlags = savedDbFlags;
drhd0e4a6c2005-02-15 20:47:57 +0000256}
danielk197719a8e7e2005-03-17 05:03:38 +0000257
drhd3001712009-05-12 17:46:53 +0000258/*
danielk197719a8e7e2005-03-17 05:03:38 +0000259** This function is called after an "ALTER TABLE ... ADD" statement
260** has been parsed. Argument pColDef contains the text of the new
261** column definition.
262**
263** The Table structure pParse->pNewTable was extended to include
264** the new column during parsing.
265*/
266void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
267 Table *pNew; /* Copy of pParse->pNewTable */
268 Table *pTab; /* Table being altered */
269 int iDb; /* Database number */
270 const char *zDb; /* Database name */
271 const char *zTab; /* Table name */
272 char *zCol; /* Null-terminated column definition */
273 Column *pCol; /* The new column */
274 Expr *pDflt; /* Default value for the new column */
drh17435752007-08-16 04:30:38 +0000275 sqlite3 *db; /* The database connection; */
dan09236502018-09-01 16:05:50 +0000276 Vdbe *v; /* The prepared statement under construction */
drh86396212016-07-14 19:13:11 +0000277 int r1; /* Temporary registers */
danielk197719a8e7e2005-03-17 05:03:38 +0000278
danielk1977f150c9d2008-10-30 17:21:12 +0000279 db = pParse->db;
280 if( pParse->nErr || db->mallocFailed ) return;
danielk197719a8e7e2005-03-17 05:03:38 +0000281 pNew = pParse->pNewTable;
282 assert( pNew );
283
drh1fee73e2007-08-29 04:00:57 +0000284 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000285 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
drh69c33822016-08-18 14:33:11 +0000286 zDb = db->aDb[iDb].zDbSName;
drh03881232009-02-13 03:43:31 +0000287 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
danielk197719a8e7e2005-03-17 05:03:38 +0000288 pCol = &pNew->aCol[pNew->nCol-1];
289 pDflt = pCol->pDflt;
drh17435752007-08-16 04:30:38 +0000290 pTab = sqlite3FindTable(db, zTab, zDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000291 assert( pTab );
292
drh81f2ccd2006-01-31 14:28:44 +0000293#ifndef SQLITE_OMIT_AUTHORIZATION
294 /* Invoke the authorization callback. */
295 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
296 return;
297 }
298#endif
299
danielk197719a8e7e2005-03-17 05:03:38 +0000300
301 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
302 ** If there is a NOT NULL constraint, then the default value for the
303 ** column must not be NULL.
304 */
drha371ace2012-09-13 14:22:47 +0000305 if( pCol->colFlags & COLFLAG_PRIMKEY ){
danielk197719a8e7e2005-03-17 05:03:38 +0000306 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
307 return;
308 }
309 if( pNew->pIndex ){
310 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
311 return;
312 }
drhc27ea2a2019-10-16 20:05:56 +0000313 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
314 /* If the default value for the new column was specified with a
315 ** literal NULL, then set pDflt to 0. This simplifies checking
316 ** for an SQL NULL default below.
317 */
318 assert( pDflt==0 || pDflt->op==TK_SPAN );
319 if( pDflt && pDflt->pLeft->op==TK_NULL ){
320 pDflt = 0;
321 }
322 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
323 sqlite3ErrorMsg(pParse,
324 "Cannot add a REFERENCES column with non-NULL default value");
325 return;
326 }
327 if( pCol->notNull && !pDflt ){
328 sqlite3ErrorMsg(pParse,
329 "Cannot add a NOT NULL column with default value NULL");
330 return;
331 }
332
333 /* Ensure the default expression is something that sqlite3ValueFromExpr()
334 ** can handle (i.e. not CURRENT_TIME etc.)
335 */
336 if( pDflt ){
337 sqlite3_value *pVal = 0;
338 int rc;
339 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
340 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
341 if( rc!=SQLITE_OK ){
342 assert( db->mallocFailed == 1 );
343 return;
344 }
345 if( !pVal ){
346 sqlite3ErrorMsg(pParse,"Cannot add a column with non-constant default");
347 return;
348 }
349 sqlite3ValueFree(pVal);
350 }
drh035f6d92019-10-24 01:04:10 +0000351 }else if( pCol->colFlags & COLFLAG_STORED ){
352 sqlite3ErrorMsg(pParse, "cannot add a STORED column");
353 return;
danielk197719a8e7e2005-03-17 05:03:38 +0000354 }
355
danielk197719a8e7e2005-03-17 05:03:38 +0000356
357 /* Modify the CREATE TABLE statement. */
drh17435752007-08-16 04:30:38 +0000358 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000359 if( zCol ){
360 char *zEnd = &zCol[pColDef->n-1];
drh8257aa82017-07-26 19:59:13 +0000361 u32 savedDbFlags = db->mDbFlags;
drh56d56f72009-04-16 16:30:17 +0000362 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000363 *zEnd-- = '\0';
364 }
drh8257aa82017-07-26 19:59:13 +0000365 db->mDbFlags |= DBFLAG_PreferBuiltin;
danielk197719a8e7e2005-03-17 05:03:38 +0000366 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000367 "UPDATE \"%w\".%s SET "
drha21a9292007-10-20 20:58:57 +0000368 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
danielk197719a8e7e2005-03-17 05:03:38 +0000369 "WHERE type = 'table' AND name = %Q",
drhe0a04a32016-12-16 01:00:21 +0000370 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
danielk197719a8e7e2005-03-17 05:03:38 +0000371 zTab
372 );
drh633e6d52008-07-28 19:34:53 +0000373 sqlite3DbFree(db, zCol);
drh8257aa82017-07-26 19:59:13 +0000374 db->mDbFlags = savedDbFlags;
danielk197719a8e7e2005-03-17 05:03:38 +0000375 }
376
drh86396212016-07-14 19:13:11 +0000377 /* Make sure the schema version is at least 3. But do not upgrade
378 ** from less than 3 to 4, as that will corrupt any preexisting DESC
379 ** index.
danielk197719a8e7e2005-03-17 05:03:38 +0000380 */
dan09236502018-09-01 16:05:50 +0000381 v = sqlite3GetVdbe(pParse);
382 if( v ){
383 r1 = sqlite3GetTempReg(pParse);
384 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
385 sqlite3VdbeUsesBtree(v, iDb);
386 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
387 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
388 VdbeCoverage(v);
389 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
390 sqlite3ReleaseTempReg(pParse, r1);
391 }
danielk197719a8e7e2005-03-17 05:03:38 +0000392
dan09236502018-09-01 16:05:50 +0000393 /* Reload the table definition */
394 renameReloadSchema(pParse, iDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000395}
396
drhfdd6e852005-12-16 01:06:16 +0000397/*
danielk197719a8e7e2005-03-17 05:03:38 +0000398** This function is called by the parser after the table-name in
399** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
400** pSrc is the full-name of the table being altered.
401**
402** This routine makes a (partial) copy of the Table structure
403** for the table being altered and sets Parse.pNewTable to point
404** to it. Routines called by the parser as the column definition
405** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
406** the copy. The copy of the Table structure is deleted by tokenize.c
407** after parsing is finished.
408**
409** Routine sqlite3AlterFinishAddColumn() will be called to complete
410** coding the "ALTER TABLE ... ADD" statement.
411*/
412void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
413 Table *pNew;
414 Table *pTab;
danielk197719a8e7e2005-03-17 05:03:38 +0000415 int iDb;
416 int i;
417 int nAlloc;
drh17435752007-08-16 04:30:38 +0000418 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000419
420 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000421 assert( pParse->pNewTable==0 );
drh1fee73e2007-08-29 04:00:57 +0000422 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000423 if( db->mallocFailed ) goto exit_begin_add_column;
dan41fb5cd2012-10-04 19:33:00 +0000424 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
danielk197719a8e7e2005-03-17 05:03:38 +0000425 if( !pTab ) goto exit_begin_add_column;
426
danielk19775ee9d692006-06-21 12:36:25 +0000427#ifndef SQLITE_OMIT_VIRTUALTABLE
428 if( IsVirtual(pTab) ){
429 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
430 goto exit_begin_add_column;
431 }
432#endif
433
danielk197719a8e7e2005-03-17 05:03:38 +0000434 /* Make sure this is not an attempt to ALTER a view. */
435 if( pTab->pSelect ){
436 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
437 goto exit_begin_add_column;
438 }
dan397a78d2018-12-18 20:31:14 +0000439 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
danbe535002011-04-01 15:15:58 +0000440 goto exit_begin_add_column;
441 }
danielk197719a8e7e2005-03-17 05:03:38 +0000442
dan03e025e2019-10-07 18:43:21 +0000443 sqlite3MayAbort(pParse);
danielk197719a8e7e2005-03-17 05:03:38 +0000444 assert( pTab->addColOffset>0 );
drh17435752007-08-16 04:30:38 +0000445 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000446
447 /* Put a copy of the Table struct in Parse.pNewTable for the
drh03881232009-02-13 03:43:31 +0000448 ** sqlite3AddColumn() function and friends to modify. But modify
449 ** the name by adding an "sqlite_altertab_" prefix. By adding this
450 ** prefix, we insure that the name will not collide with an existing
451 ** table because user table are not allowed to have the "sqlite_"
452 ** prefix on their name.
danielk197719a8e7e2005-03-17 05:03:38 +0000453 */
drh17435752007-08-16 04:30:38 +0000454 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
danielk197719a8e7e2005-03-17 05:03:38 +0000455 if( !pNew ) goto exit_begin_add_column;
456 pParse->pNewTable = pNew;
drh79df7782016-12-14 14:07:35 +0000457 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000458 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000459 assert( pNew->nCol>0 );
460 nAlloc = (((pNew->nCol-1)/8)*8)+8;
461 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197726783a52007-08-29 14:06:22 +0000462 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
drh03881232009-02-13 03:43:31 +0000463 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000464 if( !pNew->aCol || !pNew->zName ){
drh4df86af2016-02-04 11:48:00 +0000465 assert( db->mallocFailed );
danielk197719a8e7e2005-03-17 05:03:38 +0000466 goto exit_begin_add_column;
467 }
468 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
469 for(i=0; i<pNew->nCol; i++){
470 Column *pCol = &pNew->aCol[i];
drh17435752007-08-16 04:30:38 +0000471 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
drhd44390c2020-04-06 18:16:31 +0000472 pCol->hName = sqlite3StrIHash(pCol->zName);
drhff22e182006-02-09 02:56:02 +0000473 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000474 pCol->pDflt = 0;
475 }
drh17435752007-08-16 04:30:38 +0000476 pNew->pSchema = db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000477 pNew->addColOffset = pTab->addColOffset;
drh79df7782016-12-14 14:07:35 +0000478 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000479
danielk197719a8e7e2005-03-17 05:03:38 +0000480exit_begin_add_column:
drh633e6d52008-07-28 19:34:53 +0000481 sqlite3SrcListDelete(db, pSrc);
danielk197719a8e7e2005-03-17 05:03:38 +0000482 return;
483}
dancf8f2892018-08-09 20:47:01 +0000484
drh4a2c7472018-08-13 15:09:48 +0000485/*
dan9d705572018-08-20 16:16:05 +0000486** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
487** command. This function checks if the table is a view or virtual
488** table (columns of views or virtual tables may not be renamed). If so,
489** it loads an error message into pParse and returns non-zero.
490**
491** Or, if pTab is not a view or virtual table, zero is returned.
492*/
493#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
494static int isRealTable(Parse *pParse, Table *pTab){
495 const char *zType = 0;
496#ifndef SQLITE_OMIT_VIEW
497 if( pTab->pSelect ){
498 zType = "view";
dan1041a6a2018-09-06 17:47:09 +0000499 }
dan9d705572018-08-20 16:16:05 +0000500#endif
501#ifndef SQLITE_OMIT_VIRTUALTABLE
502 if( IsVirtual(pTab) ){
503 zType = "virtual table";
504 }
505#endif
506 if( zType ){
507 sqlite3ErrorMsg(
drh79a5ee92018-08-23 19:32:04 +0000508 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
dan9d705572018-08-20 16:16:05 +0000509 );
510 return 1;
511 }
512 return 0;
513}
514#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
515# define isRealTable(x,y) (0)
516#endif
517
518/*
drh4a2c7472018-08-13 15:09:48 +0000519** Handles the following parser reduction:
520**
521** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
522*/
dancf8f2892018-08-09 20:47:01 +0000523void sqlite3AlterRenameColumn(
drh4a2c7472018-08-13 15:09:48 +0000524 Parse *pParse, /* Parsing context */
525 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
526 Token *pOld, /* Name of column being changed */
527 Token *pNew /* New column name */
dancf8f2892018-08-09 20:47:01 +0000528){
drh4a2c7472018-08-13 15:09:48 +0000529 sqlite3 *db = pParse->db; /* Database connection */
dancf8f2892018-08-09 20:47:01 +0000530 Table *pTab; /* Table being updated */
531 int iCol; /* Index of column being renamed */
drh4a2c7472018-08-13 15:09:48 +0000532 char *zOld = 0; /* Old column name */
533 char *zNew = 0; /* New column name */
534 const char *zDb; /* Name of schema containing the table */
535 int iSchema; /* Index of the schema */
536 int bQuote; /* True to quote the new name */
dancf8f2892018-08-09 20:47:01 +0000537
drh4a2c7472018-08-13 15:09:48 +0000538 /* Locate the table to be altered */
dancf8f2892018-08-09 20:47:01 +0000539 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
540 if( !pTab ) goto exit_rename_column;
drh4a2c7472018-08-13 15:09:48 +0000541
542 /* Cannot alter a system table */
dan397a78d2018-12-18 20:31:14 +0000543 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
dan9d705572018-08-20 16:16:05 +0000544 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
drh4a2c7472018-08-13 15:09:48 +0000545
546 /* Which schema holds the table to be altered */
dancf8f2892018-08-09 20:47:01 +0000547 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
548 assert( iSchema>=0 );
549 zDb = db->aDb[iSchema].zDbSName;
550
drh0d019b92018-08-25 16:14:46 +0000551#ifndef SQLITE_OMIT_AUTHORIZATION
552 /* Invoke the authorization callback. */
553 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
554 goto exit_rename_column;
555 }
556#endif
557
drh4a2c7472018-08-13 15:09:48 +0000558 /* Make sure the old name really is a column name in the table to be
559 ** altered. Set iCol to be the index of the column being renamed */
dancf8f2892018-08-09 20:47:01 +0000560 zOld = sqlite3NameFromToken(db, pOld);
561 if( !zOld ) goto exit_rename_column;
562 for(iCol=0; iCol<pTab->nCol; iCol++){
563 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
564 }
565 if( iCol==pTab->nCol ){
566 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
567 goto exit_rename_column;
568 }
569
drh4a2c7472018-08-13 15:09:48 +0000570 /* Do the rename operation using a recursive UPDATE statement that
571 ** uses the sqlite_rename_column() SQL function to compute the new
572 ** CREATE statement text for the sqlite_master table.
573 */
dan1f3b2842019-03-15 16:17:32 +0000574 sqlite3MayAbort(pParse);
dancf8f2892018-08-09 20:47:01 +0000575 zNew = sqlite3NameFromToken(db, pNew);
576 if( !zNew ) goto exit_rename_column;
dan404c3ba2018-08-11 20:38:33 +0000577 assert( pNew->n>0 );
578 bQuote = sqlite3Isquote(pNew->z[0]);
dancf8f2892018-08-09 20:47:01 +0000579 sqlite3NestedParse(pParse,
580 "UPDATE \"%w\".%s SET "
danb87a9a82018-09-01 20:23:28 +0000581 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
dan65455fc2019-04-19 16:34:22 +0000582 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
583 " AND (type != 'index' OR tbl_name = %Q)"
dan499b8252018-08-17 18:08:28 +0000584 " AND sql NOT LIKE 'create virtual%%'",
dan987db762018-08-14 20:18:50 +0000585 zDb, MASTER_NAME,
danb87a9a82018-09-01 20:23:28 +0000586 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
dan987db762018-08-14 20:18:50 +0000587 pTab->zName
dancf8f2892018-08-09 20:47:01 +0000588 );
589
dan9d324822018-08-30 20:03:44 +0000590 sqlite3NestedParse(pParse,
591 "UPDATE temp.%s SET "
danb87a9a82018-09-01 20:23:28 +0000592 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
dan9d324822018-08-30 20:03:44 +0000593 "WHERE type IN ('trigger', 'view')",
594 MASTER_NAME,
595 zDb, pTab->zName, iCol, zNew, bQuote
596 );
597
dane325ffe2018-08-11 13:40:20 +0000598 /* Drop and reload the database schema. */
dan09236502018-09-01 16:05:50 +0000599 renameReloadSchema(pParse, iSchema);
dan9d324822018-08-30 20:03:44 +0000600 renameTestSchema(pParse, zDb, iSchema==1);
dan0d5fa6b2018-08-24 17:55:49 +0000601
dancf8f2892018-08-09 20:47:01 +0000602 exit_rename_column:
603 sqlite3SrcListDelete(db, pSrc);
604 sqlite3DbFree(db, zOld);
605 sqlite3DbFree(db, zNew);
606 return;
607}
608
drh4a2c7472018-08-13 15:09:48 +0000609/*
610** Each RenameToken object maps an element of the parse tree into
611** the token that generated that element. The parse tree element
612** might be one of:
613**
614** * A pointer to an Expr that represents an ID
615** * The name of a table column in Column.zName
616**
617** A list of RenameToken objects can be constructed during parsing.
dan07e95232018-08-21 16:32:53 +0000618** Each new object is created by sqlite3RenameTokenMap().
619** As the parse tree is transformed, the sqlite3RenameTokenRemap()
drh4a2c7472018-08-13 15:09:48 +0000620** routine is used to keep the mapping current.
621**
622** After the parse finishes, renameTokenFind() routine can be used
623** to look up the actual token value that created some element in
624** the parse tree.
625*/
dancf8f2892018-08-09 20:47:01 +0000626struct RenameToken {
drh4a2c7472018-08-13 15:09:48 +0000627 void *p; /* Parse tree element created by token t */
628 Token t; /* The token that created parse tree element p */
629 RenameToken *pNext; /* Next is a list of all RenameToken objects */
dancf8f2892018-08-09 20:47:01 +0000630};
631
drh4a2c7472018-08-13 15:09:48 +0000632/*
633** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
634** down into the Walker.
635*/
dan5496d6a2018-08-13 17:14:26 +0000636typedef struct RenameCtx RenameCtx;
dancf8f2892018-08-09 20:47:01 +0000637struct RenameCtx {
638 RenameToken *pList; /* List of tokens to overwrite */
639 int nList; /* Number of tokens in pList */
640 int iCol; /* Index of column being renamed */
dan987db762018-08-14 20:18:50 +0000641 Table *pTab; /* Table being ALTERed */
dan5496d6a2018-08-13 17:14:26 +0000642 const char *zOld; /* Old column name */
dancf8f2892018-08-09 20:47:01 +0000643};
644
dan8900a482018-09-05 14:36:05 +0000645#ifdef SQLITE_DEBUG
646/*
647** This function is only for debugging. It performs two tasks:
648**
649** 1. Checks that pointer pPtr does not already appear in the
650** rename-token list.
651**
652** 2. Dereferences each pointer in the rename-token list.
653**
654** The second is most effective when debugging under valgrind or
655** address-sanitizer or similar. If any of these pointers no longer
656** point to valid objects, an exception is raised by the memory-checking
657** tool.
658**
659** The point of this is to prevent comparisons of invalid pointer values.
660** Even though this always seems to work, it is undefined according to the
661** C standard. Example of undefined comparison:
662**
663** sqlite3_free(x);
664** if( x==y ) ...
665**
666** Technically, as x no longer points into a valid object or to the byte
667** following a valid object, it may not be used in comparison operations.
668*/
drh16b870d2018-09-12 15:51:56 +0000669static void renameTokenCheckAll(Parse *pParse, void *pPtr){
dan8900a482018-09-05 14:36:05 +0000670 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
671 RenameToken *p;
672 u8 i = 0;
673 for(p=pParse->pRename; p; p=p->pNext){
674 if( p->p ){
675 assert( p->p!=pPtr );
676 i += *(u8*)(p->p);
677 }
danc9461ec2018-08-29 21:00:16 +0000678 }
679 }
680}
dan8900a482018-09-05 14:36:05 +0000681#else
682# define renameTokenCheckAll(x,y)
683#endif
danc9461ec2018-08-29 21:00:16 +0000684
drh4a2c7472018-08-13 15:09:48 +0000685/*
drh49b269e2018-11-26 15:00:25 +0000686** Remember that the parser tree element pPtr was created using
687** the token pToken.
dan07e95232018-08-21 16:32:53 +0000688**
drh49b269e2018-11-26 15:00:25 +0000689** In other words, construct a new RenameToken object and add it
690** to the list of RenameToken objects currently being built up
691** in pParse->pRename.
692**
693** The pPtr argument is returned so that this routine can be used
694** with tail recursion in tokenExpr() routine, for a small performance
695** improvement.
drh4a2c7472018-08-13 15:09:48 +0000696*/
dan07e95232018-08-21 16:32:53 +0000697void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
dancf8f2892018-08-09 20:47:01 +0000698 RenameToken *pNew;
danc9461ec2018-08-29 21:00:16 +0000699 assert( pPtr || pParse->db->mallocFailed );
dan8900a482018-09-05 14:36:05 +0000700 renameTokenCheckAll(pParse, pPtr);
drh2d99f952020-04-07 01:18:23 +0000701 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
dane6dc1e52019-12-05 14:31:43 +0000702 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
703 if( pNew ){
704 pNew->p = pPtr;
705 pNew->t = *pToken;
706 pNew->pNext = pParse->pRename;
707 pParse->pRename = pNew;
708 }
dancf8f2892018-08-09 20:47:01 +0000709 }
danc9461ec2018-08-29 21:00:16 +0000710
dand145e5f2018-08-21 08:29:48 +0000711 return pPtr;
dancf8f2892018-08-09 20:47:01 +0000712}
713
drh4a2c7472018-08-13 15:09:48 +0000714/*
dan07e95232018-08-21 16:32:53 +0000715** It is assumed that there is already a RenameToken object associated
716** with parse tree element pFrom. This function remaps the associated token
717** to parse tree element pTo.
drh4a2c7472018-08-13 15:09:48 +0000718*/
dan07e95232018-08-21 16:32:53 +0000719void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
dancf8f2892018-08-09 20:47:01 +0000720 RenameToken *p;
dan8900a482018-09-05 14:36:05 +0000721 renameTokenCheckAll(pParse, pTo);
danc9461ec2018-08-29 21:00:16 +0000722 for(p=pParse->pRename; p; p=p->pNext){
dancf8f2892018-08-09 20:47:01 +0000723 if( p->p==pFrom ){
724 p->p = pTo;
725 break;
726 }
727 }
dancf8f2892018-08-09 20:47:01 +0000728}
729
drh4a2c7472018-08-13 15:09:48 +0000730/*
dan8900a482018-09-05 14:36:05 +0000731** Walker callback used by sqlite3RenameExprUnmap().
732*/
733static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
734 Parse *pParse = pWalker->pParse;
735 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
736 return WRC_Continue;
737}
738
739/*
dan8f407622019-12-04 14:26:38 +0000740** Iterate through the Select objects that are part of WITH clauses attached
741** to select statement pSelect.
742*/
743static void renameWalkWith(Walker *pWalker, Select *pSelect){
drh646975c2019-12-17 12:03:30 +0000744 With *pWith = pSelect->pWith;
745 if( pWith ){
dan8f407622019-12-04 14:26:38 +0000746 int i;
drh646975c2019-12-17 12:03:30 +0000747 for(i=0; i<pWith->nCte; i++){
748 Select *p = pWith->a[i].pSelect;
dan8f407622019-12-04 14:26:38 +0000749 NameContext sNC;
750 memset(&sNC, 0, sizeof(sNC));
751 sNC.pParse = pWalker->pParse;
752 sqlite3SelectPrep(sNC.pParse, p, &sNC);
753 sqlite3WalkSelect(pWalker, p);
drh646975c2019-12-17 12:03:30 +0000754 sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols);
dan8f407622019-12-04 14:26:38 +0000755 }
756 }
757}
758
759/*
danfb99e382020-04-03 11:20:40 +0000760** Unmap all tokens in the IdList object passed as the second argument.
761*/
762static void unmapColumnIdlistNames(
763 Parse *pParse,
764 IdList *pIdList
765){
766 if( pIdList ){
767 int ii;
768 for(ii=0; ii<pIdList->nId; ii++){
769 sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName);
770 }
771 }
772}
773
774/*
dan0b277a92019-06-11 12:03:10 +0000775** Walker callback used by sqlite3RenameExprUnmap().
776*/
777static int renameUnmapSelectCb(Walker *pWalker, Select *p){
drhbdf4cf02019-06-15 15:21:49 +0000778 Parse *pParse = pWalker->pParse;
779 int i;
drhd63b69b2019-12-04 15:08:58 +0000780 if( pParse->nErr ) return WRC_Abort;
drh2bc5cf92019-12-09 19:29:10 +0000781 if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
drhbdf4cf02019-06-15 15:21:49 +0000782 if( ALWAYS(p->pEList) ){
783 ExprList *pList = p->pEList;
784 for(i=0; i<pList->nExpr; i++){
drhc4938ea2019-12-13 00:49:42 +0000785 if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
drh41cee662019-12-12 20:22:34 +0000786 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
drhbdf4cf02019-06-15 15:21:49 +0000787 }
788 }
789 }
drh2c3f4652019-06-11 16:43:58 +0000790 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
drhbdf4cf02019-06-15 15:21:49 +0000791 SrcList *pSrc = p->pSrc;
792 for(i=0; i<pSrc->nSrc; i++){
793 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
dan394aa712019-12-20 14:18:29 +0000794 if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort;
danfb99e382020-04-03 11:20:40 +0000795 unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing);
dan0b277a92019-06-11 12:03:10 +0000796 }
797 }
dan8f407622019-12-04 14:26:38 +0000798
799 renameWalkWith(pWalker, p);
dan0b277a92019-06-11 12:03:10 +0000800 return WRC_Continue;
801}
802
803/*
dan8900a482018-09-05 14:36:05 +0000804** Remove all nodes that are part of expression pExpr from the rename list.
805*/
806void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
dane6dc1e52019-12-05 14:31:43 +0000807 u8 eMode = pParse->eParseMode;
dan8900a482018-09-05 14:36:05 +0000808 Walker sWalker;
809 memset(&sWalker, 0, sizeof(Walker));
810 sWalker.pParse = pParse;
811 sWalker.xExprCallback = renameUnmapExprCb;
dan0b277a92019-06-11 12:03:10 +0000812 sWalker.xSelectCallback = renameUnmapSelectCb;
dane6dc1e52019-12-05 14:31:43 +0000813 pParse->eParseMode = PARSE_MODE_UNMAP;
dan8900a482018-09-05 14:36:05 +0000814 sqlite3WalkExpr(&sWalker, pExpr);
dane6dc1e52019-12-05 14:31:43 +0000815 pParse->eParseMode = eMode;
dan8900a482018-09-05 14:36:05 +0000816}
817
818/*
dane8ab40d2018-09-12 08:51:48 +0000819** Remove all nodes that are part of expression-list pEList from the
820** rename list.
821*/
822void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
823 if( pEList ){
824 int i;
825 Walker sWalker;
826 memset(&sWalker, 0, sizeof(Walker));
827 sWalker.pParse = pParse;
828 sWalker.xExprCallback = renameUnmapExprCb;
829 sqlite3WalkExprList(&sWalker, pEList);
830 for(i=0; i<pEList->nExpr; i++){
drh9fc1b9a2020-01-02 22:23:01 +0000831 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
drhc4938ea2019-12-13 00:49:42 +0000832 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
833 }
dane8ab40d2018-09-12 08:51:48 +0000834 }
835 }
836}
837
838/*
drh4a2c7472018-08-13 15:09:48 +0000839** Free the list of RenameToken objects given in the second argument
840*/
dancf8f2892018-08-09 20:47:01 +0000841static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
842 RenameToken *pNext;
843 RenameToken *p;
844 for(p=pToken; p; p=pNext){
845 pNext = p->pNext;
846 sqlite3DbFree(db, p);
847 }
848}
849
dan987db762018-08-14 20:18:50 +0000850/*
851** Search the Parse object passed as the first argument for a RenameToken
852** object associated with parse tree element pPtr. If found, remove it
853** from the Parse object and add it to the list maintained by the
854** RenameCtx object passed as the second argument.
855*/
856static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
dancf8f2892018-08-09 20:47:01 +0000857 RenameToken **pp;
dan1b0c5de2018-08-24 16:04:26 +0000858 assert( pPtr!=0 );
dancf8f2892018-08-09 20:47:01 +0000859 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
860 if( (*pp)->p==pPtr ){
861 RenameToken *pToken = *pp;
862 *pp = pToken->pNext;
dan5496d6a2018-08-13 17:14:26 +0000863 pToken->pNext = pCtx->pList;
864 pCtx->pList = pToken;
865 pCtx->nList++;
866 break;
dancf8f2892018-08-09 20:47:01 +0000867 }
868 }
dancf8f2892018-08-09 20:47:01 +0000869}
870
dan24fedb92018-08-18 17:35:38 +0000871/*
872** This is a Walker select callback. It does nothing. It is only required
873** because without a dummy callback, sqlite3WalkExpr() and similar do not
874** descend into sub-select statements.
875*/
dan987db762018-08-14 20:18:50 +0000876static int renameColumnSelectCb(Walker *pWalker, Select *p){
dan38096962019-12-09 08:13:43 +0000877 if( p->selFlags & SF_View ) return WRC_Prune;
danea412512018-12-05 13:49:04 +0000878 renameWalkWith(pWalker, p);
dan987db762018-08-14 20:18:50 +0000879 return WRC_Continue;
880}
881
dan987db762018-08-14 20:18:50 +0000882/*
883** This is a Walker expression callback.
884**
885** For every TK_COLUMN node in the expression tree, search to see
886** if the column being references is the column being renamed by an
887** ALTER TABLE statement. If it is, then attach its associated
888** RenameToken object to the list of RenameToken objects being
889** constructed in RenameCtx object at pWalker->u.pRename.
890*/
dancf8f2892018-08-09 20:47:01 +0000891static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
dan5496d6a2018-08-13 17:14:26 +0000892 RenameCtx *p = pWalker->u.pRename;
dan0cbb0b12018-08-16 19:49:16 +0000893 if( pExpr->op==TK_TRIGGER
894 && pExpr->iColumn==p->iCol
895 && pWalker->pParse->pTriggerTab==p->pTab
896 ){
dan5be60c52018-08-15 20:28:39 +0000897 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
dan0cbb0b12018-08-16 19:49:16 +0000898 }else if( pExpr->op==TK_COLUMN
899 && pExpr->iColumn==p->iCol
drheda079c2018-09-20 19:02:15 +0000900 && p->pTab==pExpr->y.pTab
dan987db762018-08-14 20:18:50 +0000901 ){
dan5496d6a2018-08-13 17:14:26 +0000902 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
dancf8f2892018-08-09 20:47:01 +0000903 }
904 return WRC_Continue;
905}
906
dan987db762018-08-14 20:18:50 +0000907/*
908** The RenameCtx contains a list of tokens that reference a column that
dan24fedb92018-08-18 17:35:38 +0000909** is being renamed by an ALTER TABLE statement. Return the "last"
dan987db762018-08-14 20:18:50 +0000910** RenameToken in the RenameCtx and remove that RenameToken from the
dan24fedb92018-08-18 17:35:38 +0000911** RenameContext. "Last" means the last RenameToken encountered when
912** the input SQL is parsed from left to right. Repeated calls to this routine
dan987db762018-08-14 20:18:50 +0000913** return all column name tokens in the order that they are encountered
914** in the SQL statement.
915*/
dan5496d6a2018-08-13 17:14:26 +0000916static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
dancf8f2892018-08-09 20:47:01 +0000917 RenameToken *pBest = pCtx->pList;
918 RenameToken *pToken;
919 RenameToken **pp;
920
921 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
922 if( pToken->t.z>pBest->t.z ) pBest = pToken;
923 }
924 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
925 *pp = pBest->pNext;
926
927 return pBest;
928}
929
dan6fe7f232018-08-10 19:19:33 +0000930/*
dan24fedb92018-08-18 17:35:38 +0000931** An error occured while parsing or otherwise processing a database
932** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
933** ALTER TABLE RENAME COLUMN program. The error message emitted by the
934** sub-routine is currently stored in pParse->zErrMsg. This function
935** adds context to the error message and then stores it in pCtx.
936*/
danb0137382018-08-20 20:01:01 +0000937static void renameColumnParseError(
938 sqlite3_context *pCtx,
dan0d5fa6b2018-08-24 17:55:49 +0000939 int bPost,
danb0137382018-08-20 20:01:01 +0000940 sqlite3_value *pType,
941 sqlite3_value *pObject,
942 Parse *pParse
943){
drh79a5ee92018-08-23 19:32:04 +0000944 const char *zT = (const char*)sqlite3_value_text(pType);
945 const char *zN = (const char*)sqlite3_value_text(pObject);
dan24fedb92018-08-18 17:35:38 +0000946 char *zErr;
danb0137382018-08-20 20:01:01 +0000947
dan0d5fa6b2018-08-24 17:55:49 +0000948 zErr = sqlite3_mprintf("error in %s %s%s: %s",
949 zT, zN, (bPost ? " after rename" : ""),
950 pParse->zErrMsg
951 );
dan24fedb92018-08-18 17:35:38 +0000952 sqlite3_result_error(pCtx, zErr, -1);
953 sqlite3_free(zErr);
954}
955
956/*
dan06249392018-08-21 15:06:59 +0000957** For each name in the the expression-list pEList (i.e. each
958** pEList->a[i].zName) that matches the string in zOld, extract the
959** corresponding rename-token from Parse object pParse and add it
960** to the RenameCtx pCtx.
961*/
962static void renameColumnElistNames(
963 Parse *pParse,
964 RenameCtx *pCtx,
965 ExprList *pEList,
966 const char *zOld
967){
968 if( pEList ){
969 int i;
970 for(i=0; i<pEList->nExpr; i++){
drh41cee662019-12-12 20:22:34 +0000971 char *zName = pEList->a[i].zEName;
drh9fc1b9a2020-01-02 22:23:01 +0000972 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
973 && ALWAYS(zName!=0)
drhc4938ea2019-12-13 00:49:42 +0000974 && 0==sqlite3_stricmp(zName, zOld)
975 ){
dan06249392018-08-21 15:06:59 +0000976 renameTokenFind(pParse, pCtx, (void*)zName);
977 }
978 }
979 }
980}
981
982/*
983** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
984** that matches the string in zOld, extract the corresponding rename-token
985** from Parse object pParse and add it to the RenameCtx pCtx.
986*/
987static void renameColumnIdlistNames(
988 Parse *pParse,
989 RenameCtx *pCtx,
990 IdList *pIdList,
991 const char *zOld
992){
993 if( pIdList ){
994 int i;
995 for(i=0; i<pIdList->nId; i++){
996 char *zName = pIdList->a[i].zName;
997 if( 0==sqlite3_stricmp(zName, zOld) ){
998 renameTokenFind(pParse, pCtx, (void*)zName);
999 }
1000 }
1001 }
1002}
1003
danfb99e382020-04-03 11:20:40 +00001004
dandd1a9c82018-09-05 08:28:30 +00001005/*
1006** Parse the SQL statement zSql using Parse object (*p). The Parse object
1007** is initialized by this function before it is used.
1008*/
danc9461ec2018-08-29 21:00:16 +00001009static int renameParseSql(
dandd1a9c82018-09-05 08:28:30 +00001010 Parse *p, /* Memory to use for Parse object */
1011 const char *zDb, /* Name of schema SQL belongs to */
dandd1a9c82018-09-05 08:28:30 +00001012 sqlite3 *db, /* Database handle */
1013 const char *zSql, /* SQL to parse */
1014 int bTemp /* True if SQL is from temp schema */
danc9461ec2018-08-29 21:00:16 +00001015){
1016 int rc;
1017 char *zErr = 0;
1018
1019 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
1020
1021 /* Parse the SQL statement passed as the first argument. If no error
1022 ** occurs and the parse does not result in a new table, index or
1023 ** trigger object, the database must be corrupt. */
1024 memset(p, 0, sizeof(Parse));
dane6dc1e52019-12-05 14:31:43 +00001025 p->eParseMode = PARSE_MODE_RENAME;
danc9461ec2018-08-29 21:00:16 +00001026 p->db = db;
1027 p->nQueryLoop = 1;
1028 rc = sqlite3RunParser(p, zSql, &zErr);
1029 assert( p->zErrMsg==0 );
1030 assert( rc!=SQLITE_OK || zErr==0 );
danc9461ec2018-08-29 21:00:16 +00001031 p->zErrMsg = zErr;
1032 if( db->mallocFailed ) rc = SQLITE_NOMEM;
1033 if( rc==SQLITE_OK
1034 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
1035 ){
1036 rc = SQLITE_CORRUPT_BKPT;
1037 }
1038
1039#ifdef SQLITE_DEBUG
1040 /* Ensure that all mappings in the Parse.pRename list really do map to
1041 ** a part of the input string. */
1042 if( rc==SQLITE_OK ){
1043 int nSql = sqlite3Strlen30(zSql);
1044 RenameToken *pToken;
1045 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
1046 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
1047 }
1048 }
1049#endif
1050
1051 db->init.iDb = 0;
1052 return rc;
1053}
1054
dandd1a9c82018-09-05 08:28:30 +00001055/*
1056** This function edits SQL statement zSql, replacing each token identified
1057** by the linked list pRename with the text of zNew. If argument bQuote is
1058** true, then zNew is always quoted first. If no error occurs, the result
1059** is loaded into context object pCtx as the result.
1060**
1061** Or, if an error occurs (i.e. an OOM condition), an error is left in
1062** pCtx and an SQLite error code returned.
1063*/
danc9461ec2018-08-29 21:00:16 +00001064static int renameEditSql(
1065 sqlite3_context *pCtx, /* Return result here */
1066 RenameCtx *pRename, /* Rename context */
1067 const char *zSql, /* SQL statement to edit */
1068 const char *zNew, /* New token text */
1069 int bQuote /* True to always quote token */
1070){
1071 int nNew = sqlite3Strlen30(zNew);
1072 int nSql = sqlite3Strlen30(zSql);
1073 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1074 int rc = SQLITE_OK;
1075 char *zQuot;
1076 char *zOut;
1077 int nQuot;
1078
1079 /* Set zQuot to point to a buffer containing a quoted copy of the
1080 ** identifier zNew. If the corresponding identifier in the original
1081 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
1082 ** point to zQuot so that all substitutions are made using the
1083 ** quoted version of the new column name. */
drhc753c212018-09-01 16:55:36 +00001084 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
danc9461ec2018-08-29 21:00:16 +00001085 if( zQuot==0 ){
1086 return SQLITE_NOMEM;
1087 }else{
1088 nQuot = sqlite3Strlen30(zQuot);
1089 }
1090 if( bQuote ){
1091 zNew = zQuot;
1092 nNew = nQuot;
1093 }
1094
1095 /* At this point pRename->pList contains a list of RenameToken objects
1096 ** corresponding to all tokens in the input SQL that must be replaced
1097 ** with the new column name. All that remains is to construct and
1098 ** return the edited SQL string. */
1099 assert( nQuot>=nNew );
1100 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
1101 if( zOut ){
1102 int nOut = nSql;
1103 memcpy(zOut, zSql, nSql);
1104 while( pRename->pList ){
1105 int iOff; /* Offset of token to replace in zOut */
1106 RenameToken *pBest = renameColumnTokenNext(pRename);
1107
1108 u32 nReplace;
1109 const char *zReplace;
1110 if( sqlite3IsIdChar(*pBest->t.z) ){
1111 nReplace = nNew;
1112 zReplace = zNew;
1113 }else{
1114 nReplace = nQuot;
1115 zReplace = zQuot;
1116 }
1117
1118 iOff = pBest->t.z - zSql;
1119 if( pBest->t.n!=nReplace ){
1120 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
1121 nOut - (iOff + pBest->t.n)
1122 );
1123 nOut += nReplace - pBest->t.n;
1124 zOut[nOut] = '\0';
1125 }
1126 memcpy(&zOut[iOff], zReplace, nReplace);
1127 sqlite3DbFree(db, pBest);
1128 }
1129
1130 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
1131 sqlite3DbFree(db, zOut);
1132 }else{
1133 rc = SQLITE_NOMEM;
1134 }
1135
1136 sqlite3_free(zQuot);
1137 return rc;
1138}
1139
dandd1a9c82018-09-05 08:28:30 +00001140/*
1141** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
1142** it was read from the schema of database zDb. Return SQLITE_OK if
1143** successful. Otherwise, return an SQLite error code and leave an error
1144** message in the Parse object.
1145*/
1146static int renameResolveTrigger(Parse *pParse, const char *zDb){
danc9461ec2018-08-29 21:00:16 +00001147 sqlite3 *db = pParse->db;
dand5e6fef2018-09-07 15:50:31 +00001148 Trigger *pNew = pParse->pNewTrigger;
danc9461ec2018-08-29 21:00:16 +00001149 TriggerStep *pStep;
1150 NameContext sNC;
1151 int rc = SQLITE_OK;
1152
1153 memset(&sNC, 0, sizeof(sNC));
1154 sNC.pParse = pParse;
dand5e6fef2018-09-07 15:50:31 +00001155 assert( pNew->pTabSchema );
1156 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
1157 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
1158 );
1159 pParse->eTriggerOp = pNew->op;
drhf470c372018-10-03 18:05:36 +00001160 /* ALWAYS() because if the table of the trigger does not exist, the
1161 ** error would have been hit before this point */
1162 if( ALWAYS(pParse->pTriggerTab) ){
dan5351e882018-10-01 07:04:12 +00001163 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
1164 }
danc9461ec2018-08-29 21:00:16 +00001165
1166 /* Resolve symbols in WHEN clause */
dan5351e882018-10-01 07:04:12 +00001167 if( rc==SQLITE_OK && pNew->pWhen ){
dand5e6fef2018-09-07 15:50:31 +00001168 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
danc9461ec2018-08-29 21:00:16 +00001169 }
1170
dand5e6fef2018-09-07 15:50:31 +00001171 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
danc9461ec2018-08-29 21:00:16 +00001172 if( pStep->pSelect ){
1173 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
1174 if( pParse->nErr ) rc = pParse->rc;
1175 }
dand5e6fef2018-09-07 15:50:31 +00001176 if( rc==SQLITE_OK && pStep->zTarget ){
danc9461ec2018-08-29 21:00:16 +00001177 Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
1178 if( pTarget==0 ){
1179 rc = SQLITE_ERROR;
dande79e092018-09-17 13:38:45 +00001180 }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
dan0e14e982019-01-18 16:06:18 +00001181 SrcList sSrc;
danc9461ec2018-08-29 21:00:16 +00001182 memset(&sSrc, 0, sizeof(sSrc));
1183 sSrc.nSrc = 1;
1184 sSrc.a[0].zName = pStep->zTarget;
1185 sSrc.a[0].pTab = pTarget;
1186 sNC.pSrcList = &sSrc;
1187 if( pStep->pWhere ){
1188 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
1189 }
1190 if( rc==SQLITE_OK ){
1191 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
1192 }
1193 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
1194 if( pStep->pUpsert ){
1195 Upsert *pUpsert = pStep->pUpsert;
1196 assert( rc==SQLITE_OK );
1197 pUpsert->pUpsertSrc = &sSrc;
1198 sNC.uNC.pUpsert = pUpsert;
1199 sNC.ncFlags = NC_UUpsert;
1200 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
1201 if( rc==SQLITE_OK ){
1202 ExprList *pUpsertSet = pUpsert->pUpsertSet;
1203 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
1204 }
1205 if( rc==SQLITE_OK ){
1206 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
1207 }
1208 if( rc==SQLITE_OK ){
1209 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
1210 }
1211 sNC.ncFlags = 0;
1212 }
dan0e14e982019-01-18 16:06:18 +00001213 sNC.pSrcList = 0;
danc9461ec2018-08-29 21:00:16 +00001214 }
1215 }
1216 }
1217 return rc;
1218}
1219
dandd1a9c82018-09-05 08:28:30 +00001220/*
1221** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
1222** objects that are part of the trigger passed as the second argument.
1223*/
danc9461ec2018-08-29 21:00:16 +00001224static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
1225 TriggerStep *pStep;
1226
1227 /* Find tokens to edit in WHEN clause */
1228 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
1229
1230 /* Find tokens to edit in trigger steps */
1231 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1232 sqlite3WalkSelect(pWalker, pStep->pSelect);
1233 sqlite3WalkExpr(pWalker, pStep->pWhere);
1234 sqlite3WalkExprList(pWalker, pStep->pExprList);
1235 if( pStep->pUpsert ){
1236 Upsert *pUpsert = pStep->pUpsert;
1237 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
1238 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
1239 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
1240 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
1241 }
1242 }
1243}
1244
dandd1a9c82018-09-05 08:28:30 +00001245/*
1246** Free the contents of Parse object (*pParse). Do not free the memory
1247** occupied by the Parse object itself.
1248*/
dan141e1192018-08-31 18:23:53 +00001249static void renameParseCleanup(Parse *pParse){
1250 sqlite3 *db = pParse->db;
drh885eeb62019-01-09 02:02:24 +00001251 Index *pIdx;
dan141e1192018-08-31 18:23:53 +00001252 if( pParse->pVdbe ){
1253 sqlite3VdbeFinalize(pParse->pVdbe);
1254 }
1255 sqlite3DeleteTable(db, pParse->pNewTable);
drh885eeb62019-01-09 02:02:24 +00001256 while( (pIdx = pParse->pNewIndex)!=0 ){
1257 pParse->pNewIndex = pIdx->pNext;
1258 sqlite3FreeIndex(db, pIdx);
1259 }
dan141e1192018-08-31 18:23:53 +00001260 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
1261 sqlite3DbFree(db, pParse->zErrMsg);
1262 renameTokenFree(db, pParse->pRename);
1263 sqlite3ParserReset(pParse);
1264}
1265
dan06249392018-08-21 15:06:59 +00001266/*
dan987db762018-08-14 20:18:50 +00001267** SQL function:
1268**
1269** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
1270**
1271** 0. zSql: SQL statement to rewrite
danb0137382018-08-20 20:01:01 +00001272** 1. type: Type of object ("table", "view" etc.)
1273** 2. object: Name of object
1274** 3. Database: Database name (e.g. "main")
1275** 4. Table: Table name
1276** 5. iCol: Index of column to rename
1277** 6. zNew: New column name
dan9d324822018-08-30 20:03:44 +00001278** 7. bQuote: Non-zero if the new column name should be quoted.
danb87a9a82018-09-01 20:23:28 +00001279** 8. bTemp: True if zSql comes from temp schema
dan987db762018-08-14 20:18:50 +00001280**
1281** Do a column rename operation on the CREATE statement given in zSql.
1282** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
1283** into zNew. The name should be quoted if bQuote is true.
1284**
1285** This function is used internally by the ALTER TABLE RENAME COLUMN command.
drheea8eb62018-11-26 18:09:15 +00001286** It is only accessible to SQL created using sqlite3NestedParse(). It is
1287** not reachable from ordinary SQL passed into sqlite3_prepare().
dan6fe7f232018-08-10 19:19:33 +00001288*/
dancf8f2892018-08-09 20:47:01 +00001289static void renameColumnFunc(
1290 sqlite3_context *context,
1291 int NotUsed,
1292 sqlite3_value **argv
1293){
1294 sqlite3 *db = sqlite3_context_db_handle(context);
dan5496d6a2018-08-13 17:14:26 +00001295 RenameCtx sCtx;
drhad866a12018-08-10 19:33:09 +00001296 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
danb0137382018-08-20 20:01:01 +00001297 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
1298 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
1299 int iCol = sqlite3_value_int(argv[5]);
1300 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
danb0137382018-08-20 20:01:01 +00001301 int bQuote = sqlite3_value_int(argv[7]);
danb87a9a82018-09-01 20:23:28 +00001302 int bTemp = sqlite3_value_int(argv[8]);
dan987db762018-08-14 20:18:50 +00001303 const char *zOld;
dancf8f2892018-08-09 20:47:01 +00001304 int rc;
dancf8f2892018-08-09 20:47:01 +00001305 Parse sParse;
1306 Walker sWalker;
dancf8f2892018-08-09 20:47:01 +00001307 Index *pIdx;
dancf8f2892018-08-09 20:47:01 +00001308 int i;
dan987db762018-08-14 20:18:50 +00001309 Table *pTab;
dan141e1192018-08-31 18:23:53 +00001310#ifndef SQLITE_OMIT_AUTHORIZATION
1311 sqlite3_xauth xAuth = db->xAuth;
1312#endif
dancf8f2892018-08-09 20:47:01 +00001313
drh38d99642018-08-18 18:27:18 +00001314 UNUSED_PARAMETER(NotUsed);
dan987db762018-08-14 20:18:50 +00001315 if( zSql==0 ) return;
dan987db762018-08-14 20:18:50 +00001316 if( zTable==0 ) return;
danb0137382018-08-20 20:01:01 +00001317 if( zNew==0 ) return;
dan987db762018-08-14 20:18:50 +00001318 if( iCol<0 ) return;
drhda76adc2018-08-25 02:04:05 +00001319 sqlite3BtreeEnterAll(db);
dan987db762018-08-14 20:18:50 +00001320 pTab = sqlite3FindTable(db, zTable, zDb);
drhda76adc2018-08-25 02:04:05 +00001321 if( pTab==0 || iCol>=pTab->nCol ){
1322 sqlite3BtreeLeaveAll(db);
1323 return;
1324 }
dan987db762018-08-14 20:18:50 +00001325 zOld = pTab->aCol[iCol].zName;
dancf8f2892018-08-09 20:47:01 +00001326 memset(&sCtx, 0, sizeof(sCtx));
dan987db762018-08-14 20:18:50 +00001327 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
dancf8f2892018-08-09 20:47:01 +00001328
dan141e1192018-08-31 18:23:53 +00001329#ifndef SQLITE_OMIT_AUTHORIZATION
1330 db->xAuth = 0;
1331#endif
drhb2ab3dc2019-12-20 14:08:34 +00001332 rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
dan24fedb92018-08-18 17:35:38 +00001333
dancf8f2892018-08-09 20:47:01 +00001334 /* Find tokens that need to be replaced. */
1335 memset(&sWalker, 0, sizeof(Walker));
1336 sWalker.pParse = &sParse;
1337 sWalker.xExprCallback = renameColumnExprCb;
dan987db762018-08-14 20:18:50 +00001338 sWalker.xSelectCallback = renameColumnSelectCb;
dancf8f2892018-08-09 20:47:01 +00001339 sWalker.u.pRename = &sCtx;
1340
dan0cbb0b12018-08-16 19:49:16 +00001341 sCtx.pTab = pTab;
dan987db762018-08-14 20:18:50 +00001342 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
dancf8f2892018-08-09 20:47:01 +00001343 if( sParse.pNewTable ){
dan987db762018-08-14 20:18:50 +00001344 Select *pSelect = sParse.pNewTable->pSelect;
1345 if( pSelect ){
dan38096962019-12-09 08:13:43 +00001346 pSelect->selFlags &= ~SF_View;
dan987db762018-08-14 20:18:50 +00001347 sParse.rc = SQLITE_OK;
dan38096962019-12-09 08:13:43 +00001348 sqlite3SelectPrep(&sParse, pSelect, 0);
dan987db762018-08-14 20:18:50 +00001349 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
1350 if( rc==SQLITE_OK ){
1351 sqlite3WalkSelect(&sWalker, pSelect);
dana8762ae2018-08-11 17:49:23 +00001352 }
dan987db762018-08-14 20:18:50 +00001353 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
1354 }else{
1355 /* A regular table */
1356 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
1357 FKey *pFKey;
1358 assert( sParse.pNewTable->pSelect==0 );
dan0cbb0b12018-08-16 19:49:16 +00001359 sCtx.pTab = sParse.pNewTable;
dan987db762018-08-14 20:18:50 +00001360 if( bFKOnly==0 ){
1361 renameTokenFind(
1362 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
1363 );
1364 if( sCtx.iCol<0 ){
1365 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
dancf8f2892018-08-09 20:47:01 +00001366 }
dan987db762018-08-14 20:18:50 +00001367 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
1368 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
1369 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1370 }
drh885eeb62019-01-09 02:02:24 +00001371 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
1372 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1373 }
dan987db762018-08-14 20:18:50 +00001374 }
drhb9bcf7c2019-10-19 13:29:10 +00001375#ifndef SQLITE_OMIT_GENERATED_COLUMNS
1376 for(i=0; i<sParse.pNewTable->nCol; i++){
1377 sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
1378 }
1379#endif
dan987db762018-08-14 20:18:50 +00001380
1381 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1382 for(i=0; i<pFKey->nCol; i++){
dan356afab2018-08-14 21:05:35 +00001383 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
dan987db762018-08-14 20:18:50 +00001384 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
1385 }
1386 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
1387 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
1388 ){
1389 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
1390 }
dan6fe7f232018-08-10 19:19:33 +00001391 }
dancf8f2892018-08-09 20:47:01 +00001392 }
1393 }
dan5496d6a2018-08-13 17:14:26 +00001394 }else if( sParse.pNewIndex ){
dancf8f2892018-08-09 20:47:01 +00001395 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
1396 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
dan5496d6a2018-08-13 17:14:26 +00001397 }else{
dan5be60c52018-08-15 20:28:39 +00001398 /* A trigger */
1399 TriggerStep *pStep;
dan0ccda962018-08-30 16:26:48 +00001400 rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
danc9461ec2018-08-29 21:00:16 +00001401 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
dan5be60c52018-08-15 20:28:39 +00001402
danc9461ec2018-08-29 21:00:16 +00001403 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
1404 if( pStep->zTarget ){
dan06249392018-08-21 15:06:59 +00001405 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
danc9461ec2018-08-29 21:00:16 +00001406 if( pTarget==pTab ){
dan0cbb0b12018-08-16 19:49:16 +00001407 if( pStep->pUpsert ){
danc9461ec2018-08-29 21:00:16 +00001408 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
1409 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
dan0cbb0b12018-08-16 19:49:16 +00001410 }
danc9461ec2018-08-29 21:00:16 +00001411 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
1412 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
dan5be60c52018-08-15 20:28:39 +00001413 }
1414 }
1415 }
1416
dan5be60c52018-08-15 20:28:39 +00001417
1418 /* Find tokens to edit in UPDATE OF clause */
dan06249392018-08-21 15:06:59 +00001419 if( sParse.pTriggerTab==pTab ){
1420 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
dan5496d6a2018-08-13 17:14:26 +00001421 }
dan5be60c52018-08-15 20:28:39 +00001422
danc9461ec2018-08-29 21:00:16 +00001423 /* Find tokens to edit in various expressions and selects */
1424 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
dancf8f2892018-08-09 20:47:01 +00001425 }
1426
dan987db762018-08-14 20:18:50 +00001427 assert( rc==SQLITE_OK );
danc9461ec2018-08-29 21:00:16 +00001428 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
dancf8f2892018-08-09 20:47:01 +00001429
drh5fc22cd2018-08-13 13:43:11 +00001430renameColumnFunc_done:
dan987db762018-08-14 20:18:50 +00001431 if( rc!=SQLITE_OK ){
dan24fedb92018-08-18 17:35:38 +00001432 if( sParse.zErrMsg ){
dan9d324822018-08-30 20:03:44 +00001433 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
dan987db762018-08-14 20:18:50 +00001434 }else{
1435 sqlite3_result_error_code(context, rc);
1436 }
1437 }
1438
dan141e1192018-08-31 18:23:53 +00001439 renameParseCleanup(&sParse);
drh4a2c7472018-08-13 15:09:48 +00001440 renameTokenFree(db, sCtx.pList);
dan141e1192018-08-31 18:23:53 +00001441#ifndef SQLITE_OMIT_AUTHORIZATION
1442 db->xAuth = xAuth;
1443#endif
drhda76adc2018-08-25 02:04:05 +00001444 sqlite3BtreeLeaveAll(db);
dancf8f2892018-08-09 20:47:01 +00001445}
1446
dandd1a9c82018-09-05 08:28:30 +00001447/*
1448** Walker expression callback used by "RENAME TABLE".
1449*/
danc9461ec2018-08-29 21:00:16 +00001450static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
1451 RenameCtx *p = pWalker->u.pRename;
drheda079c2018-09-20 19:02:15 +00001452 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
1453 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
danc9461ec2018-08-29 21:00:16 +00001454 }
1455 return WRC_Continue;
1456}
1457
1458/*
dandd1a9c82018-09-05 08:28:30 +00001459** Walker select callback used by "RENAME TABLE".
danc9461ec2018-08-29 21:00:16 +00001460*/
1461static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
1462 int i;
1463 RenameCtx *p = pWalker->u.pRename;
1464 SrcList *pSrc = pSelect->pSrc;
dan38096962019-12-09 08:13:43 +00001465 if( pSelect->selFlags & SF_View ) return WRC_Prune;
drh92a28242019-10-09 15:37:58 +00001466 if( pSrc==0 ){
1467 assert( pWalker->pParse->db->mallocFailed );
drh974b2482018-12-06 01:53:12 +00001468 return WRC_Abort;
1469 }
danc9461ec2018-08-29 21:00:16 +00001470 for(i=0; i<pSrc->nSrc; i++){
1471 struct SrcList_item *pItem = &pSrc->a[i];
1472 if( pItem->pTab==p->pTab ){
1473 renameTokenFind(pWalker->pParse, p, pItem->zName);
1474 }
1475 }
danea412512018-12-05 13:49:04 +00001476 renameWalkWith(pWalker, pSelect);
danc9461ec2018-08-29 21:00:16 +00001477
1478 return WRC_Continue;
1479}
1480
1481
1482/*
1483** This C function implements an SQL user function that is used by SQL code
1484** generated by the ALTER TABLE ... RENAME command to modify the definition
1485** of any foreign key constraints that use the table being renamed as the
1486** parent table. It is passed three arguments:
1487**
dan141e1192018-08-31 18:23:53 +00001488** 0: The database containing the table being renamed.
dan65372fa2018-09-03 20:05:15 +00001489** 1. type: Type of object ("table", "view" etc.)
1490** 2. object: Name of object
1491** 3: The complete text of the schema statement being modified,
1492** 4: The old name of the table being renamed, and
1493** 5: The new name of the table being renamed.
1494** 6: True if the schema statement comes from the temp db.
danc9461ec2018-08-29 21:00:16 +00001495**
dan141e1192018-08-31 18:23:53 +00001496** It returns the new schema statement. For example:
danc9461ec2018-08-29 21:00:16 +00001497**
dan141e1192018-08-31 18:23:53 +00001498** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
danc9461ec2018-08-29 21:00:16 +00001499** -> 'CREATE TABLE t1(a REFERENCES t3)'
1500*/
1501static void renameTableFunc(
1502 sqlite3_context *context,
1503 int NotUsed,
1504 sqlite3_value **argv
1505){
1506 sqlite3 *db = sqlite3_context_db_handle(context);
drh5b1da302018-09-01 20:02:07 +00001507 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
dan65372fa2018-09-03 20:05:15 +00001508 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
1509 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
1510 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
1511 int bTemp = sqlite3_value_int(argv[6]);
drh5b1da302018-09-01 20:02:07 +00001512 UNUSED_PARAMETER(NotUsed);
danc9461ec2018-08-29 21:00:16 +00001513
dan141e1192018-08-31 18:23:53 +00001514 if( zInput && zOld && zNew ){
dan141e1192018-08-31 18:23:53 +00001515 Parse sParse;
1516 int rc;
1517 int bQuote = 1;
1518 RenameCtx sCtx;
1519 Walker sWalker;
danc9461ec2018-08-29 21:00:16 +00001520
dan141e1192018-08-31 18:23:53 +00001521#ifndef SQLITE_OMIT_AUTHORIZATION
1522 sqlite3_xauth xAuth = db->xAuth;
1523 db->xAuth = 0;
danc9461ec2018-08-29 21:00:16 +00001524#endif
1525
dan141e1192018-08-31 18:23:53 +00001526 sqlite3BtreeEnterAll(db);
1527
1528 memset(&sCtx, 0, sizeof(RenameCtx));
1529 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
1530 memset(&sWalker, 0, sizeof(Walker));
1531 sWalker.pParse = &sParse;
1532 sWalker.xExprCallback = renameTableExprCb;
1533 sWalker.xSelectCallback = renameTableSelectCb;
1534 sWalker.u.pRename = &sCtx;
1535
drhb2ab3dc2019-12-20 14:08:34 +00001536 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
dan141e1192018-08-31 18:23:53 +00001537
1538 if( rc==SQLITE_OK ){
dan674b8942018-09-20 08:28:01 +00001539 int isLegacy = (db->flags & SQLITE_LegacyAlter);
dan141e1192018-08-31 18:23:53 +00001540 if( sParse.pNewTable ){
1541 Table *pTab = sParse.pNewTable;
1542
1543 if( pTab->pSelect ){
dan674b8942018-09-20 08:28:01 +00001544 if( isLegacy==0 ){
dan38096962019-12-09 08:13:43 +00001545 Select *pSelect = pTab->pSelect;
dan674b8942018-09-20 08:28:01 +00001546 NameContext sNC;
1547 memset(&sNC, 0, sizeof(sNC));
1548 sNC.pParse = &sParse;
dan141e1192018-08-31 18:23:53 +00001549
dan38096962019-12-09 08:13:43 +00001550 assert( pSelect->selFlags & SF_View );
1551 pSelect->selFlags &= ~SF_View;
dan674b8942018-09-20 08:28:01 +00001552 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
drh1548d522019-12-20 12:55:21 +00001553 if( sParse.nErr ){
1554 rc = sParse.rc;
1555 }else{
1556 sqlite3WalkSelect(&sWalker, pTab->pSelect);
1557 }
dan674b8942018-09-20 08:28:01 +00001558 }
dan141e1192018-08-31 18:23:53 +00001559 }else{
1560 /* Modify any FK definitions to point to the new table. */
1561#ifndef SQLITE_OMIT_FOREIGN_KEY
danb4307012018-11-09 20:04:05 +00001562 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
dan202a0272018-09-07 11:51:21 +00001563 FKey *pFKey;
1564 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1565 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
1566 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
1567 }
dan141e1192018-08-31 18:23:53 +00001568 }
1569 }
1570#endif
1571
1572 /* If this is the table being altered, fix any table refs in CHECK
1573 ** expressions. Also update the name that appears right after the
1574 ** "CREATE [VIRTUAL] TABLE" bit. */
1575 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
1576 sCtx.pTab = pTab;
dan674b8942018-09-20 08:28:01 +00001577 if( isLegacy==0 ){
1578 sqlite3WalkExprList(&sWalker, pTab->pCheck);
1579 }
dan141e1192018-08-31 18:23:53 +00001580 renameTokenFind(&sParse, &sCtx, pTab->zName);
1581 }
danc9461ec2018-08-29 21:00:16 +00001582 }
1583 }
danc9461ec2018-08-29 21:00:16 +00001584
dan141e1192018-08-31 18:23:53 +00001585 else if( sParse.pNewIndex ){
1586 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
dan674b8942018-09-20 08:28:01 +00001587 if( isLegacy==0 ){
1588 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
1589 }
dan141e1192018-08-31 18:23:53 +00001590 }
danc9461ec2018-08-29 21:00:16 +00001591
1592#ifndef SQLITE_OMIT_TRIGGER
danb87a9a82018-09-01 20:23:28 +00001593 else{
dan141e1192018-08-31 18:23:53 +00001594 Trigger *pTrigger = sParse.pNewTrigger;
1595 TriggerStep *pStep;
1596 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
1597 && sCtx.pTab->pSchema==pTrigger->pTabSchema
1598 ){
1599 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
1600 }
danc9461ec2018-08-29 21:00:16 +00001601
dan674b8942018-09-20 08:28:01 +00001602 if( isLegacy==0 ){
1603 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
1604 if( rc==SQLITE_OK ){
1605 renameWalkTrigger(&sWalker, pTrigger);
1606 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1607 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
1608 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
1609 }
dan141e1192018-08-31 18:23:53 +00001610 }
dan0ccda962018-08-30 16:26:48 +00001611 }
danc9461ec2018-08-29 21:00:16 +00001612 }
1613 }
dan141e1192018-08-31 18:23:53 +00001614#endif
danc9461ec2018-08-29 21:00:16 +00001615 }
dan141e1192018-08-31 18:23:53 +00001616
1617 if( rc==SQLITE_OK ){
1618 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
1619 }
1620 if( rc!=SQLITE_OK ){
dan65372fa2018-09-03 20:05:15 +00001621 if( sParse.zErrMsg ){
1622 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
1623 }else{
1624 sqlite3_result_error_code(context, rc);
1625 }
dan141e1192018-08-31 18:23:53 +00001626 }
1627
1628 renameParseCleanup(&sParse);
1629 renameTokenFree(db, sCtx.pList);
1630 sqlite3BtreeLeaveAll(db);
1631#ifndef SQLITE_OMIT_AUTHORIZATION
1632 db->xAuth = xAuth;
danc9461ec2018-08-29 21:00:16 +00001633#endif
1634 }
1635
danc9461ec2018-08-29 21:00:16 +00001636 return;
1637}
1638
dandd1a9c82018-09-05 08:28:30 +00001639/*
1640** An SQL user function that checks that there are no parse or symbol
1641** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
1642** After an ALTER TABLE .. RENAME operation is performed and the schema
1643** reloaded, this function is called on each SQL statement in the schema
dan1d85c6b2018-09-06 16:01:37 +00001644** to ensure that it is still usable.
dandd1a9c82018-09-05 08:28:30 +00001645**
1646** 0: Database name ("main", "temp" etc.).
1647** 1: SQL statement.
1648** 2: Object type ("view", "table", "trigger" or "index").
1649** 3: Object name.
1650** 4: True if object is from temp schema.
dan1d85c6b2018-09-06 16:01:37 +00001651**
1652** Unless it finds an error, this function normally returns NULL. However, it
1653** returns integer value 1 if:
1654**
1655** * the SQL argument creates a trigger, and
1656** * the table that the trigger is attached to is in database zDb.
dandd1a9c82018-09-05 08:28:30 +00001657*/
dan9d324822018-08-30 20:03:44 +00001658static void renameTableTest(
1659 sqlite3_context *context,
1660 int NotUsed,
1661 sqlite3_value **argv
1662){
1663 sqlite3 *db = sqlite3_context_db_handle(context);
drh5b1da302018-09-01 20:02:07 +00001664 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
1665 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
dan9d324822018-08-30 20:03:44 +00001666 int bTemp = sqlite3_value_int(argv[4]);
dan674b8942018-09-20 08:28:01 +00001667 int isLegacy = (db->flags & SQLITE_LegacyAlter);
dan9d324822018-08-30 20:03:44 +00001668
dan141e1192018-08-31 18:23:53 +00001669#ifndef SQLITE_OMIT_AUTHORIZATION
1670 sqlite3_xauth xAuth = db->xAuth;
1671 db->xAuth = 0;
1672#endif
1673
drh5b1da302018-09-01 20:02:07 +00001674 UNUSED_PARAMETER(NotUsed);
dan09236502018-09-01 16:05:50 +00001675 if( zDb && zInput ){
1676 int rc;
1677 Parse sParse;
drhb2ab3dc2019-12-20 14:08:34 +00001678 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
dan09236502018-09-01 16:05:50 +00001679 if( rc==SQLITE_OK ){
dan674b8942018-09-20 08:28:01 +00001680 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
dan09236502018-09-01 16:05:50 +00001681 NameContext sNC;
1682 memset(&sNC, 0, sizeof(sNC));
1683 sNC.pParse = &sParse;
1684 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
1685 if( sParse.nErr ) rc = sParse.rc;
1686 }
1687
1688 else if( sParse.pNewTrigger ){
dan674b8942018-09-20 08:28:01 +00001689 if( isLegacy==0 ){
1690 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
1691 }
drh3b700452018-09-07 19:12:08 +00001692 if( rc==SQLITE_OK ){
dan1d85c6b2018-09-06 16:01:37 +00001693 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
1694 int i2 = sqlite3FindDbName(db, zDb);
1695 if( i1==i2 ) sqlite3_result_int(context, 1);
1696 }
dan09236502018-09-01 16:05:50 +00001697 }
dan9d324822018-08-30 20:03:44 +00001698 }
1699
dan09236502018-09-01 16:05:50 +00001700 if( rc!=SQLITE_OK ){
1701 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
dan9d324822018-08-30 20:03:44 +00001702 }
dan09236502018-09-01 16:05:50 +00001703 renameParseCleanup(&sParse);
dan9d324822018-08-30 20:03:44 +00001704 }
1705
dan141e1192018-08-31 18:23:53 +00001706#ifndef SQLITE_OMIT_AUTHORIZATION
1707 db->xAuth = xAuth;
1708#endif
dan9d324822018-08-30 20:03:44 +00001709}
1710
dancf8f2892018-08-09 20:47:01 +00001711/*
1712** Register built-in functions used to help implement ALTER TABLE
1713*/
1714void sqlite3AlterFunctions(void){
1715 static FuncDef aAlterTableFuncs[] = {
drheea8eb62018-11-26 18:09:15 +00001716 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
1717 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
1718 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest),
dancf8f2892018-08-09 20:47:01 +00001719 };
1720 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
1721}
drhd0e4a6c2005-02-15 20:47:57 +00001722#endif /* SQLITE_ALTER_TABLE */