blob: f3467fa09eb92c4b3bbefe446d9b0f898219e95b [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);
drhff22e182006-02-09 02:56:02 +0000472 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000473 pCol->pDflt = 0;
474 }
drh17435752007-08-16 04:30:38 +0000475 pNew->pSchema = db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000476 pNew->addColOffset = pTab->addColOffset;
drh79df7782016-12-14 14:07:35 +0000477 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000478
danielk197719a8e7e2005-03-17 05:03:38 +0000479exit_begin_add_column:
drh633e6d52008-07-28 19:34:53 +0000480 sqlite3SrcListDelete(db, pSrc);
danielk197719a8e7e2005-03-17 05:03:38 +0000481 return;
482}
dancf8f2892018-08-09 20:47:01 +0000483
drh4a2c7472018-08-13 15:09:48 +0000484/*
dan9d705572018-08-20 16:16:05 +0000485** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
486** command. This function checks if the table is a view or virtual
487** table (columns of views or virtual tables may not be renamed). If so,
488** it loads an error message into pParse and returns non-zero.
489**
490** Or, if pTab is not a view or virtual table, zero is returned.
491*/
492#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
493static int isRealTable(Parse *pParse, Table *pTab){
494 const char *zType = 0;
495#ifndef SQLITE_OMIT_VIEW
496 if( pTab->pSelect ){
497 zType = "view";
dan1041a6a2018-09-06 17:47:09 +0000498 }
dan9d705572018-08-20 16:16:05 +0000499#endif
500#ifndef SQLITE_OMIT_VIRTUALTABLE
501 if( IsVirtual(pTab) ){
502 zType = "virtual table";
503 }
504#endif
505 if( zType ){
506 sqlite3ErrorMsg(
drh79a5ee92018-08-23 19:32:04 +0000507 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
dan9d705572018-08-20 16:16:05 +0000508 );
509 return 1;
510 }
511 return 0;
512}
513#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
514# define isRealTable(x,y) (0)
515#endif
516
517/*
drh4a2c7472018-08-13 15:09:48 +0000518** Handles the following parser reduction:
519**
520** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
521*/
dancf8f2892018-08-09 20:47:01 +0000522void sqlite3AlterRenameColumn(
drh4a2c7472018-08-13 15:09:48 +0000523 Parse *pParse, /* Parsing context */
524 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
525 Token *pOld, /* Name of column being changed */
526 Token *pNew /* New column name */
dancf8f2892018-08-09 20:47:01 +0000527){
drh4a2c7472018-08-13 15:09:48 +0000528 sqlite3 *db = pParse->db; /* Database connection */
dancf8f2892018-08-09 20:47:01 +0000529 Table *pTab; /* Table being updated */
530 int iCol; /* Index of column being renamed */
drh4a2c7472018-08-13 15:09:48 +0000531 char *zOld = 0; /* Old column name */
532 char *zNew = 0; /* New column name */
533 const char *zDb; /* Name of schema containing the table */
534 int iSchema; /* Index of the schema */
535 int bQuote; /* True to quote the new name */
dancf8f2892018-08-09 20:47:01 +0000536
drh4a2c7472018-08-13 15:09:48 +0000537 /* Locate the table to be altered */
dancf8f2892018-08-09 20:47:01 +0000538 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
539 if( !pTab ) goto exit_rename_column;
drh4a2c7472018-08-13 15:09:48 +0000540
541 /* Cannot alter a system table */
dan397a78d2018-12-18 20:31:14 +0000542 if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
dan9d705572018-08-20 16:16:05 +0000543 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
drh4a2c7472018-08-13 15:09:48 +0000544
545 /* Which schema holds the table to be altered */
dancf8f2892018-08-09 20:47:01 +0000546 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
547 assert( iSchema>=0 );
548 zDb = db->aDb[iSchema].zDbSName;
549
drh0d019b92018-08-25 16:14:46 +0000550#ifndef SQLITE_OMIT_AUTHORIZATION
551 /* Invoke the authorization callback. */
552 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
553 goto exit_rename_column;
554 }
555#endif
556
drh4a2c7472018-08-13 15:09:48 +0000557 /* Make sure the old name really is a column name in the table to be
558 ** altered. Set iCol to be the index of the column being renamed */
dancf8f2892018-08-09 20:47:01 +0000559 zOld = sqlite3NameFromToken(db, pOld);
560 if( !zOld ) goto exit_rename_column;
561 for(iCol=0; iCol<pTab->nCol; iCol++){
562 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
563 }
564 if( iCol==pTab->nCol ){
565 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
566 goto exit_rename_column;
567 }
568
drh4a2c7472018-08-13 15:09:48 +0000569 /* Do the rename operation using a recursive UPDATE statement that
570 ** uses the sqlite_rename_column() SQL function to compute the new
571 ** CREATE statement text for the sqlite_master table.
572 */
dan1f3b2842019-03-15 16:17:32 +0000573 sqlite3MayAbort(pParse);
dancf8f2892018-08-09 20:47:01 +0000574 zNew = sqlite3NameFromToken(db, pNew);
575 if( !zNew ) goto exit_rename_column;
dan404c3ba2018-08-11 20:38:33 +0000576 assert( pNew->n>0 );
577 bQuote = sqlite3Isquote(pNew->z[0]);
dancf8f2892018-08-09 20:47:01 +0000578 sqlite3NestedParse(pParse,
579 "UPDATE \"%w\".%s SET "
danb87a9a82018-09-01 20:23:28 +0000580 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
dan65455fc2019-04-19 16:34:22 +0000581 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
582 " AND (type != 'index' OR tbl_name = %Q)"
dan499b8252018-08-17 18:08:28 +0000583 " AND sql NOT LIKE 'create virtual%%'",
dan987db762018-08-14 20:18:50 +0000584 zDb, MASTER_NAME,
danb87a9a82018-09-01 20:23:28 +0000585 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
dan987db762018-08-14 20:18:50 +0000586 pTab->zName
dancf8f2892018-08-09 20:47:01 +0000587 );
588
dan9d324822018-08-30 20:03:44 +0000589 sqlite3NestedParse(pParse,
590 "UPDATE temp.%s SET "
danb87a9a82018-09-01 20:23:28 +0000591 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
dan9d324822018-08-30 20:03:44 +0000592 "WHERE type IN ('trigger', 'view')",
593 MASTER_NAME,
594 zDb, pTab->zName, iCol, zNew, bQuote
595 );
596
dane325ffe2018-08-11 13:40:20 +0000597 /* Drop and reload the database schema. */
dan09236502018-09-01 16:05:50 +0000598 renameReloadSchema(pParse, iSchema);
dan9d324822018-08-30 20:03:44 +0000599 renameTestSchema(pParse, zDb, iSchema==1);
dan0d5fa6b2018-08-24 17:55:49 +0000600
dancf8f2892018-08-09 20:47:01 +0000601 exit_rename_column:
602 sqlite3SrcListDelete(db, pSrc);
603 sqlite3DbFree(db, zOld);
604 sqlite3DbFree(db, zNew);
605 return;
606}
607
drh4a2c7472018-08-13 15:09:48 +0000608/*
609** Each RenameToken object maps an element of the parse tree into
610** the token that generated that element. The parse tree element
611** might be one of:
612**
613** * A pointer to an Expr that represents an ID
614** * The name of a table column in Column.zName
615**
616** A list of RenameToken objects can be constructed during parsing.
dan07e95232018-08-21 16:32:53 +0000617** Each new object is created by sqlite3RenameTokenMap().
618** As the parse tree is transformed, the sqlite3RenameTokenRemap()
drh4a2c7472018-08-13 15:09:48 +0000619** routine is used to keep the mapping current.
620**
621** After the parse finishes, renameTokenFind() routine can be used
622** to look up the actual token value that created some element in
623** the parse tree.
624*/
dancf8f2892018-08-09 20:47:01 +0000625struct RenameToken {
drh4a2c7472018-08-13 15:09:48 +0000626 void *p; /* Parse tree element created by token t */
627 Token t; /* The token that created parse tree element p */
628 RenameToken *pNext; /* Next is a list of all RenameToken objects */
dancf8f2892018-08-09 20:47:01 +0000629};
630
drh4a2c7472018-08-13 15:09:48 +0000631/*
632** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
633** down into the Walker.
634*/
dan5496d6a2018-08-13 17:14:26 +0000635typedef struct RenameCtx RenameCtx;
dancf8f2892018-08-09 20:47:01 +0000636struct RenameCtx {
637 RenameToken *pList; /* List of tokens to overwrite */
638 int nList; /* Number of tokens in pList */
639 int iCol; /* Index of column being renamed */
dan987db762018-08-14 20:18:50 +0000640 Table *pTab; /* Table being ALTERed */
dan5496d6a2018-08-13 17:14:26 +0000641 const char *zOld; /* Old column name */
dancf8f2892018-08-09 20:47:01 +0000642};
643
dan8900a482018-09-05 14:36:05 +0000644#ifdef SQLITE_DEBUG
645/*
646** This function is only for debugging. It performs two tasks:
647**
648** 1. Checks that pointer pPtr does not already appear in the
649** rename-token list.
650**
651** 2. Dereferences each pointer in the rename-token list.
652**
653** The second is most effective when debugging under valgrind or
654** address-sanitizer or similar. If any of these pointers no longer
655** point to valid objects, an exception is raised by the memory-checking
656** tool.
657**
658** The point of this is to prevent comparisons of invalid pointer values.
659** Even though this always seems to work, it is undefined according to the
660** C standard. Example of undefined comparison:
661**
662** sqlite3_free(x);
663** if( x==y ) ...
664**
665** Technically, as x no longer points into a valid object or to the byte
666** following a valid object, it may not be used in comparison operations.
667*/
drh16b870d2018-09-12 15:51:56 +0000668static void renameTokenCheckAll(Parse *pParse, void *pPtr){
dan8900a482018-09-05 14:36:05 +0000669 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
670 RenameToken *p;
671 u8 i = 0;
672 for(p=pParse->pRename; p; p=p->pNext){
673 if( p->p ){
674 assert( p->p!=pPtr );
675 i += *(u8*)(p->p);
676 }
danc9461ec2018-08-29 21:00:16 +0000677 }
678 }
679}
dan8900a482018-09-05 14:36:05 +0000680#else
681# define renameTokenCheckAll(x,y)
682#endif
danc9461ec2018-08-29 21:00:16 +0000683
drh4a2c7472018-08-13 15:09:48 +0000684/*
drh49b269e2018-11-26 15:00:25 +0000685** Remember that the parser tree element pPtr was created using
686** the token pToken.
dan07e95232018-08-21 16:32:53 +0000687**
drh49b269e2018-11-26 15:00:25 +0000688** In other words, construct a new RenameToken object and add it
689** to the list of RenameToken objects currently being built up
690** in pParse->pRename.
691**
692** The pPtr argument is returned so that this routine can be used
693** with tail recursion in tokenExpr() routine, for a small performance
694** improvement.
drh4a2c7472018-08-13 15:09:48 +0000695*/
dan07e95232018-08-21 16:32:53 +0000696void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
dancf8f2892018-08-09 20:47:01 +0000697 RenameToken *pNew;
danc9461ec2018-08-29 21:00:16 +0000698 assert( pPtr || pParse->db->mallocFailed );
dan8900a482018-09-05 14:36:05 +0000699 renameTokenCheckAll(pParse, pPtr);
dane6dc1e52019-12-05 14:31:43 +0000700 if( pParse->eParseMode!=PARSE_MODE_UNMAP ){
701 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
702 if( pNew ){
703 pNew->p = pPtr;
704 pNew->t = *pToken;
705 pNew->pNext = pParse->pRename;
706 pParse->pRename = pNew;
707 }
dancf8f2892018-08-09 20:47:01 +0000708 }
danc9461ec2018-08-29 21:00:16 +0000709
dand145e5f2018-08-21 08:29:48 +0000710 return pPtr;
dancf8f2892018-08-09 20:47:01 +0000711}
712
drh4a2c7472018-08-13 15:09:48 +0000713/*
dan07e95232018-08-21 16:32:53 +0000714** It is assumed that there is already a RenameToken object associated
715** with parse tree element pFrom. This function remaps the associated token
716** to parse tree element pTo.
drh4a2c7472018-08-13 15:09:48 +0000717*/
dan07e95232018-08-21 16:32:53 +0000718void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
dancf8f2892018-08-09 20:47:01 +0000719 RenameToken *p;
dan8900a482018-09-05 14:36:05 +0000720 renameTokenCheckAll(pParse, pTo);
danc9461ec2018-08-29 21:00:16 +0000721 for(p=pParse->pRename; p; p=p->pNext){
dancf8f2892018-08-09 20:47:01 +0000722 if( p->p==pFrom ){
723 p->p = pTo;
724 break;
725 }
726 }
dancf8f2892018-08-09 20:47:01 +0000727}
728
drh4a2c7472018-08-13 15:09:48 +0000729/*
dan8900a482018-09-05 14:36:05 +0000730** Walker callback used by sqlite3RenameExprUnmap().
731*/
732static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
733 Parse *pParse = pWalker->pParse;
734 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
735 return WRC_Continue;
736}
737
738/*
dan8f407622019-12-04 14:26:38 +0000739** Iterate through the Select objects that are part of WITH clauses attached
740** to select statement pSelect.
741*/
742static void renameWalkWith(Walker *pWalker, Select *pSelect){
743 if( pSelect->pWith ){
744 int i;
745 for(i=0; i<pSelect->pWith->nCte; i++){
746 Select *p = pSelect->pWith->a[i].pSelect;
747 NameContext sNC;
748 memset(&sNC, 0, sizeof(sNC));
749 sNC.pParse = pWalker->pParse;
750 sqlite3SelectPrep(sNC.pParse, p, &sNC);
751 sqlite3WalkSelect(pWalker, p);
752 }
753 }
754}
755
756/*
dan0b277a92019-06-11 12:03:10 +0000757** Walker callback used by sqlite3RenameExprUnmap().
758*/
759static int renameUnmapSelectCb(Walker *pWalker, Select *p){
drhbdf4cf02019-06-15 15:21:49 +0000760 Parse *pParse = pWalker->pParse;
761 int i;
drhd63b69b2019-12-04 15:08:58 +0000762 if( pParse->nErr ) return WRC_Abort;
drh2bc5cf92019-12-09 19:29:10 +0000763 if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
drhbdf4cf02019-06-15 15:21:49 +0000764 if( ALWAYS(p->pEList) ){
765 ExprList *pList = p->pEList;
766 for(i=0; i<pList->nExpr; i++){
drh41cee662019-12-12 20:22:34 +0000767 if( pList->a[i].zEName ){
768 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
drhbdf4cf02019-06-15 15:21:49 +0000769 }
770 }
771 }
drh2c3f4652019-06-11 16:43:58 +0000772 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
drhbdf4cf02019-06-15 15:21:49 +0000773 SrcList *pSrc = p->pSrc;
774 for(i=0; i<pSrc->nSrc; i++){
775 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
dan0b277a92019-06-11 12:03:10 +0000776 }
777 }
dan8f407622019-12-04 14:26:38 +0000778
779 renameWalkWith(pWalker, p);
dan0b277a92019-06-11 12:03:10 +0000780 return WRC_Continue;
781}
782
783/*
dan8900a482018-09-05 14:36:05 +0000784** Remove all nodes that are part of expression pExpr from the rename list.
785*/
786void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
dane6dc1e52019-12-05 14:31:43 +0000787 u8 eMode = pParse->eParseMode;
dan8900a482018-09-05 14:36:05 +0000788 Walker sWalker;
789 memset(&sWalker, 0, sizeof(Walker));
790 sWalker.pParse = pParse;
791 sWalker.xExprCallback = renameUnmapExprCb;
dan0b277a92019-06-11 12:03:10 +0000792 sWalker.xSelectCallback = renameUnmapSelectCb;
dane6dc1e52019-12-05 14:31:43 +0000793 pParse->eParseMode = PARSE_MODE_UNMAP;
dan8900a482018-09-05 14:36:05 +0000794 sqlite3WalkExpr(&sWalker, pExpr);
dane6dc1e52019-12-05 14:31:43 +0000795 pParse->eParseMode = eMode;
dan8900a482018-09-05 14:36:05 +0000796}
797
798/*
dane8ab40d2018-09-12 08:51:48 +0000799** Remove all nodes that are part of expression-list pEList from the
800** rename list.
801*/
802void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
803 if( pEList ){
804 int i;
805 Walker sWalker;
806 memset(&sWalker, 0, sizeof(Walker));
807 sWalker.pParse = pParse;
808 sWalker.xExprCallback = renameUnmapExprCb;
809 sqlite3WalkExprList(&sWalker, pEList);
810 for(i=0; i<pEList->nExpr; i++){
drh41cee662019-12-12 20:22:34 +0000811 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
dane8ab40d2018-09-12 08:51:48 +0000812 }
813 }
814}
815
816/*
drh4a2c7472018-08-13 15:09:48 +0000817** Free the list of RenameToken objects given in the second argument
818*/
dancf8f2892018-08-09 20:47:01 +0000819static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
820 RenameToken *pNext;
821 RenameToken *p;
822 for(p=pToken; p; p=pNext){
823 pNext = p->pNext;
824 sqlite3DbFree(db, p);
825 }
826}
827
dan987db762018-08-14 20:18:50 +0000828/*
829** Search the Parse object passed as the first argument for a RenameToken
830** object associated with parse tree element pPtr. If found, remove it
831** from the Parse object and add it to the list maintained by the
832** RenameCtx object passed as the second argument.
833*/
834static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
dancf8f2892018-08-09 20:47:01 +0000835 RenameToken **pp;
dan1b0c5de2018-08-24 16:04:26 +0000836 assert( pPtr!=0 );
dancf8f2892018-08-09 20:47:01 +0000837 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
838 if( (*pp)->p==pPtr ){
839 RenameToken *pToken = *pp;
840 *pp = pToken->pNext;
dan5496d6a2018-08-13 17:14:26 +0000841 pToken->pNext = pCtx->pList;
842 pCtx->pList = pToken;
843 pCtx->nList++;
844 break;
dancf8f2892018-08-09 20:47:01 +0000845 }
846 }
dancf8f2892018-08-09 20:47:01 +0000847}
848
dan24fedb92018-08-18 17:35:38 +0000849/*
850** This is a Walker select callback. It does nothing. It is only required
851** because without a dummy callback, sqlite3WalkExpr() and similar do not
852** descend into sub-select statements.
853*/
dan987db762018-08-14 20:18:50 +0000854static int renameColumnSelectCb(Walker *pWalker, Select *p){
dan38096962019-12-09 08:13:43 +0000855 if( p->selFlags & SF_View ) return WRC_Prune;
danea412512018-12-05 13:49:04 +0000856 renameWalkWith(pWalker, p);
dan987db762018-08-14 20:18:50 +0000857 return WRC_Continue;
858}
859
dan987db762018-08-14 20:18:50 +0000860/*
861** This is a Walker expression callback.
862**
863** For every TK_COLUMN node in the expression tree, search to see
864** if the column being references is the column being renamed by an
865** ALTER TABLE statement. If it is, then attach its associated
866** RenameToken object to the list of RenameToken objects being
867** constructed in RenameCtx object at pWalker->u.pRename.
868*/
dancf8f2892018-08-09 20:47:01 +0000869static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
dan5496d6a2018-08-13 17:14:26 +0000870 RenameCtx *p = pWalker->u.pRename;
dan0cbb0b12018-08-16 19:49:16 +0000871 if( pExpr->op==TK_TRIGGER
872 && pExpr->iColumn==p->iCol
873 && pWalker->pParse->pTriggerTab==p->pTab
874 ){
dan5be60c52018-08-15 20:28:39 +0000875 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
dan0cbb0b12018-08-16 19:49:16 +0000876 }else if( pExpr->op==TK_COLUMN
877 && pExpr->iColumn==p->iCol
drheda079c2018-09-20 19:02:15 +0000878 && p->pTab==pExpr->y.pTab
dan987db762018-08-14 20:18:50 +0000879 ){
dan5496d6a2018-08-13 17:14:26 +0000880 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
dancf8f2892018-08-09 20:47:01 +0000881 }
882 return WRC_Continue;
883}
884
dan987db762018-08-14 20:18:50 +0000885/*
886** The RenameCtx contains a list of tokens that reference a column that
dan24fedb92018-08-18 17:35:38 +0000887** is being renamed by an ALTER TABLE statement. Return the "last"
dan987db762018-08-14 20:18:50 +0000888** RenameToken in the RenameCtx and remove that RenameToken from the
dan24fedb92018-08-18 17:35:38 +0000889** RenameContext. "Last" means the last RenameToken encountered when
890** the input SQL is parsed from left to right. Repeated calls to this routine
dan987db762018-08-14 20:18:50 +0000891** return all column name tokens in the order that they are encountered
892** in the SQL statement.
893*/
dan5496d6a2018-08-13 17:14:26 +0000894static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
dancf8f2892018-08-09 20:47:01 +0000895 RenameToken *pBest = pCtx->pList;
896 RenameToken *pToken;
897 RenameToken **pp;
898
899 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
900 if( pToken->t.z>pBest->t.z ) pBest = pToken;
901 }
902 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
903 *pp = pBest->pNext;
904
905 return pBest;
906}
907
dan6fe7f232018-08-10 19:19:33 +0000908/*
dan24fedb92018-08-18 17:35:38 +0000909** An error occured while parsing or otherwise processing a database
910** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
911** ALTER TABLE RENAME COLUMN program. The error message emitted by the
912** sub-routine is currently stored in pParse->zErrMsg. This function
913** adds context to the error message and then stores it in pCtx.
914*/
danb0137382018-08-20 20:01:01 +0000915static void renameColumnParseError(
916 sqlite3_context *pCtx,
dan0d5fa6b2018-08-24 17:55:49 +0000917 int bPost,
danb0137382018-08-20 20:01:01 +0000918 sqlite3_value *pType,
919 sqlite3_value *pObject,
920 Parse *pParse
921){
drh79a5ee92018-08-23 19:32:04 +0000922 const char *zT = (const char*)sqlite3_value_text(pType);
923 const char *zN = (const char*)sqlite3_value_text(pObject);
dan24fedb92018-08-18 17:35:38 +0000924 char *zErr;
danb0137382018-08-20 20:01:01 +0000925
dan0d5fa6b2018-08-24 17:55:49 +0000926 zErr = sqlite3_mprintf("error in %s %s%s: %s",
927 zT, zN, (bPost ? " after rename" : ""),
928 pParse->zErrMsg
929 );
dan24fedb92018-08-18 17:35:38 +0000930 sqlite3_result_error(pCtx, zErr, -1);
931 sqlite3_free(zErr);
932}
933
934/*
dan06249392018-08-21 15:06:59 +0000935** For each name in the the expression-list pEList (i.e. each
936** pEList->a[i].zName) that matches the string in zOld, extract the
937** corresponding rename-token from Parse object pParse and add it
938** to the RenameCtx pCtx.
939*/
940static void renameColumnElistNames(
941 Parse *pParse,
942 RenameCtx *pCtx,
943 ExprList *pEList,
944 const char *zOld
945){
946 if( pEList ){
947 int i;
948 for(i=0; i<pEList->nExpr; i++){
drh41cee662019-12-12 20:22:34 +0000949 char *zName = pEList->a[i].zEName;
dan06249392018-08-21 15:06:59 +0000950 if( 0==sqlite3_stricmp(zName, zOld) ){
951 renameTokenFind(pParse, pCtx, (void*)zName);
952 }
953 }
954 }
955}
956
957/*
958** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
959** that matches the string in zOld, extract the corresponding rename-token
960** from Parse object pParse and add it to the RenameCtx pCtx.
961*/
962static void renameColumnIdlistNames(
963 Parse *pParse,
964 RenameCtx *pCtx,
965 IdList *pIdList,
966 const char *zOld
967){
968 if( pIdList ){
969 int i;
970 for(i=0; i<pIdList->nId; i++){
971 char *zName = pIdList->a[i].zName;
972 if( 0==sqlite3_stricmp(zName, zOld) ){
973 renameTokenFind(pParse, pCtx, (void*)zName);
974 }
975 }
976 }
977}
978
dandd1a9c82018-09-05 08:28:30 +0000979/*
980** Parse the SQL statement zSql using Parse object (*p). The Parse object
981** is initialized by this function before it is used.
982*/
danc9461ec2018-08-29 21:00:16 +0000983static int renameParseSql(
dandd1a9c82018-09-05 08:28:30 +0000984 Parse *p, /* Memory to use for Parse object */
985 const char *zDb, /* Name of schema SQL belongs to */
986 int bTable, /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
987 sqlite3 *db, /* Database handle */
988 const char *zSql, /* SQL to parse */
989 int bTemp /* True if SQL is from temp schema */
danc9461ec2018-08-29 21:00:16 +0000990){
991 int rc;
992 char *zErr = 0;
993
994 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
995
996 /* Parse the SQL statement passed as the first argument. If no error
997 ** occurs and the parse does not result in a new table, index or
998 ** trigger object, the database must be corrupt. */
999 memset(p, 0, sizeof(Parse));
dane6dc1e52019-12-05 14:31:43 +00001000 p->eParseMode = PARSE_MODE_RENAME;
danc9461ec2018-08-29 21:00:16 +00001001 p->db = db;
1002 p->nQueryLoop = 1;
1003 rc = sqlite3RunParser(p, zSql, &zErr);
1004 assert( p->zErrMsg==0 );
1005 assert( rc!=SQLITE_OK || zErr==0 );
danc9461ec2018-08-29 21:00:16 +00001006 p->zErrMsg = zErr;
1007 if( db->mallocFailed ) rc = SQLITE_NOMEM;
1008 if( rc==SQLITE_OK
1009 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
1010 ){
1011 rc = SQLITE_CORRUPT_BKPT;
1012 }
1013
1014#ifdef SQLITE_DEBUG
1015 /* Ensure that all mappings in the Parse.pRename list really do map to
1016 ** a part of the input string. */
1017 if( rc==SQLITE_OK ){
1018 int nSql = sqlite3Strlen30(zSql);
1019 RenameToken *pToken;
1020 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
1021 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
1022 }
1023 }
1024#endif
1025
1026 db->init.iDb = 0;
1027 return rc;
1028}
1029
dandd1a9c82018-09-05 08:28:30 +00001030/*
1031** This function edits SQL statement zSql, replacing each token identified
1032** by the linked list pRename with the text of zNew. If argument bQuote is
1033** true, then zNew is always quoted first. If no error occurs, the result
1034** is loaded into context object pCtx as the result.
1035**
1036** Or, if an error occurs (i.e. an OOM condition), an error is left in
1037** pCtx and an SQLite error code returned.
1038*/
danc9461ec2018-08-29 21:00:16 +00001039static int renameEditSql(
1040 sqlite3_context *pCtx, /* Return result here */
1041 RenameCtx *pRename, /* Rename context */
1042 const char *zSql, /* SQL statement to edit */
1043 const char *zNew, /* New token text */
1044 int bQuote /* True to always quote token */
1045){
1046 int nNew = sqlite3Strlen30(zNew);
1047 int nSql = sqlite3Strlen30(zSql);
1048 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1049 int rc = SQLITE_OK;
1050 char *zQuot;
1051 char *zOut;
1052 int nQuot;
1053
1054 /* Set zQuot to point to a buffer containing a quoted copy of the
1055 ** identifier zNew. If the corresponding identifier in the original
1056 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
1057 ** point to zQuot so that all substitutions are made using the
1058 ** quoted version of the new column name. */
drhc753c212018-09-01 16:55:36 +00001059 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
danc9461ec2018-08-29 21:00:16 +00001060 if( zQuot==0 ){
1061 return SQLITE_NOMEM;
1062 }else{
1063 nQuot = sqlite3Strlen30(zQuot);
1064 }
1065 if( bQuote ){
1066 zNew = zQuot;
1067 nNew = nQuot;
1068 }
1069
1070 /* At this point pRename->pList contains a list of RenameToken objects
1071 ** corresponding to all tokens in the input SQL that must be replaced
1072 ** with the new column name. All that remains is to construct and
1073 ** return the edited SQL string. */
1074 assert( nQuot>=nNew );
1075 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
1076 if( zOut ){
1077 int nOut = nSql;
1078 memcpy(zOut, zSql, nSql);
1079 while( pRename->pList ){
1080 int iOff; /* Offset of token to replace in zOut */
1081 RenameToken *pBest = renameColumnTokenNext(pRename);
1082
1083 u32 nReplace;
1084 const char *zReplace;
1085 if( sqlite3IsIdChar(*pBest->t.z) ){
1086 nReplace = nNew;
1087 zReplace = zNew;
1088 }else{
1089 nReplace = nQuot;
1090 zReplace = zQuot;
1091 }
1092
1093 iOff = pBest->t.z - zSql;
1094 if( pBest->t.n!=nReplace ){
1095 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
1096 nOut - (iOff + pBest->t.n)
1097 );
1098 nOut += nReplace - pBest->t.n;
1099 zOut[nOut] = '\0';
1100 }
1101 memcpy(&zOut[iOff], zReplace, nReplace);
1102 sqlite3DbFree(db, pBest);
1103 }
1104
1105 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
1106 sqlite3DbFree(db, zOut);
1107 }else{
1108 rc = SQLITE_NOMEM;
1109 }
1110
1111 sqlite3_free(zQuot);
1112 return rc;
1113}
1114
dandd1a9c82018-09-05 08:28:30 +00001115/*
1116** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
1117** it was read from the schema of database zDb. Return SQLITE_OK if
1118** successful. Otherwise, return an SQLite error code and leave an error
1119** message in the Parse object.
1120*/
1121static int renameResolveTrigger(Parse *pParse, const char *zDb){
danc9461ec2018-08-29 21:00:16 +00001122 sqlite3 *db = pParse->db;
dand5e6fef2018-09-07 15:50:31 +00001123 Trigger *pNew = pParse->pNewTrigger;
danc9461ec2018-08-29 21:00:16 +00001124 TriggerStep *pStep;
1125 NameContext sNC;
1126 int rc = SQLITE_OK;
1127
1128 memset(&sNC, 0, sizeof(sNC));
1129 sNC.pParse = pParse;
dand5e6fef2018-09-07 15:50:31 +00001130 assert( pNew->pTabSchema );
1131 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
1132 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
1133 );
1134 pParse->eTriggerOp = pNew->op;
drhf470c372018-10-03 18:05:36 +00001135 /* ALWAYS() because if the table of the trigger does not exist, the
1136 ** error would have been hit before this point */
1137 if( ALWAYS(pParse->pTriggerTab) ){
dan5351e882018-10-01 07:04:12 +00001138 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
1139 }
danc9461ec2018-08-29 21:00:16 +00001140
1141 /* Resolve symbols in WHEN clause */
dan5351e882018-10-01 07:04:12 +00001142 if( rc==SQLITE_OK && pNew->pWhen ){
dand5e6fef2018-09-07 15:50:31 +00001143 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
danc9461ec2018-08-29 21:00:16 +00001144 }
1145
dand5e6fef2018-09-07 15:50:31 +00001146 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
danc9461ec2018-08-29 21:00:16 +00001147 if( pStep->pSelect ){
1148 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
1149 if( pParse->nErr ) rc = pParse->rc;
1150 }
dand5e6fef2018-09-07 15:50:31 +00001151 if( rc==SQLITE_OK && pStep->zTarget ){
danc9461ec2018-08-29 21:00:16 +00001152 Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
1153 if( pTarget==0 ){
1154 rc = SQLITE_ERROR;
dande79e092018-09-17 13:38:45 +00001155 }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
dan0e14e982019-01-18 16:06:18 +00001156 SrcList sSrc;
danc9461ec2018-08-29 21:00:16 +00001157 memset(&sSrc, 0, sizeof(sSrc));
1158 sSrc.nSrc = 1;
1159 sSrc.a[0].zName = pStep->zTarget;
1160 sSrc.a[0].pTab = pTarget;
1161 sNC.pSrcList = &sSrc;
1162 if( pStep->pWhere ){
1163 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
1164 }
1165 if( rc==SQLITE_OK ){
1166 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
1167 }
1168 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
1169 if( pStep->pUpsert ){
1170 Upsert *pUpsert = pStep->pUpsert;
1171 assert( rc==SQLITE_OK );
1172 pUpsert->pUpsertSrc = &sSrc;
1173 sNC.uNC.pUpsert = pUpsert;
1174 sNC.ncFlags = NC_UUpsert;
1175 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
1176 if( rc==SQLITE_OK ){
1177 ExprList *pUpsertSet = pUpsert->pUpsertSet;
1178 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
1179 }
1180 if( rc==SQLITE_OK ){
1181 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
1182 }
1183 if( rc==SQLITE_OK ){
1184 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
1185 }
1186 sNC.ncFlags = 0;
1187 }
dan0e14e982019-01-18 16:06:18 +00001188 sNC.pSrcList = 0;
danc9461ec2018-08-29 21:00:16 +00001189 }
1190 }
1191 }
1192 return rc;
1193}
1194
dandd1a9c82018-09-05 08:28:30 +00001195/*
1196** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
1197** objects that are part of the trigger passed as the second argument.
1198*/
danc9461ec2018-08-29 21:00:16 +00001199static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
1200 TriggerStep *pStep;
1201
1202 /* Find tokens to edit in WHEN clause */
1203 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
1204
1205 /* Find tokens to edit in trigger steps */
1206 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1207 sqlite3WalkSelect(pWalker, pStep->pSelect);
1208 sqlite3WalkExpr(pWalker, pStep->pWhere);
1209 sqlite3WalkExprList(pWalker, pStep->pExprList);
1210 if( pStep->pUpsert ){
1211 Upsert *pUpsert = pStep->pUpsert;
1212 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
1213 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
1214 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
1215 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
1216 }
1217 }
1218}
1219
dandd1a9c82018-09-05 08:28:30 +00001220/*
1221** Free the contents of Parse object (*pParse). Do not free the memory
1222** occupied by the Parse object itself.
1223*/
dan141e1192018-08-31 18:23:53 +00001224static void renameParseCleanup(Parse *pParse){
1225 sqlite3 *db = pParse->db;
drh885eeb62019-01-09 02:02:24 +00001226 Index *pIdx;
dan141e1192018-08-31 18:23:53 +00001227 if( pParse->pVdbe ){
1228 sqlite3VdbeFinalize(pParse->pVdbe);
1229 }
1230 sqlite3DeleteTable(db, pParse->pNewTable);
drh885eeb62019-01-09 02:02:24 +00001231 while( (pIdx = pParse->pNewIndex)!=0 ){
1232 pParse->pNewIndex = pIdx->pNext;
1233 sqlite3FreeIndex(db, pIdx);
1234 }
dan141e1192018-08-31 18:23:53 +00001235 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
1236 sqlite3DbFree(db, pParse->zErrMsg);
1237 renameTokenFree(db, pParse->pRename);
1238 sqlite3ParserReset(pParse);
1239}
1240
dan06249392018-08-21 15:06:59 +00001241/*
dan987db762018-08-14 20:18:50 +00001242** SQL function:
1243**
1244** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
1245**
1246** 0. zSql: SQL statement to rewrite
danb0137382018-08-20 20:01:01 +00001247** 1. type: Type of object ("table", "view" etc.)
1248** 2. object: Name of object
1249** 3. Database: Database name (e.g. "main")
1250** 4. Table: Table name
1251** 5. iCol: Index of column to rename
1252** 6. zNew: New column name
dan9d324822018-08-30 20:03:44 +00001253** 7. bQuote: Non-zero if the new column name should be quoted.
danb87a9a82018-09-01 20:23:28 +00001254** 8. bTemp: True if zSql comes from temp schema
dan987db762018-08-14 20:18:50 +00001255**
1256** Do a column rename operation on the CREATE statement given in zSql.
1257** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
1258** into zNew. The name should be quoted if bQuote is true.
1259**
1260** This function is used internally by the ALTER TABLE RENAME COLUMN command.
drheea8eb62018-11-26 18:09:15 +00001261** It is only accessible to SQL created using sqlite3NestedParse(). It is
1262** not reachable from ordinary SQL passed into sqlite3_prepare().
dan6fe7f232018-08-10 19:19:33 +00001263*/
dancf8f2892018-08-09 20:47:01 +00001264static void renameColumnFunc(
1265 sqlite3_context *context,
1266 int NotUsed,
1267 sqlite3_value **argv
1268){
1269 sqlite3 *db = sqlite3_context_db_handle(context);
dan5496d6a2018-08-13 17:14:26 +00001270 RenameCtx sCtx;
drhad866a12018-08-10 19:33:09 +00001271 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
danb0137382018-08-20 20:01:01 +00001272 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
1273 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
1274 int iCol = sqlite3_value_int(argv[5]);
1275 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
danb0137382018-08-20 20:01:01 +00001276 int bQuote = sqlite3_value_int(argv[7]);
danb87a9a82018-09-01 20:23:28 +00001277 int bTemp = sqlite3_value_int(argv[8]);
dan987db762018-08-14 20:18:50 +00001278 const char *zOld;
dancf8f2892018-08-09 20:47:01 +00001279 int rc;
dancf8f2892018-08-09 20:47:01 +00001280 Parse sParse;
1281 Walker sWalker;
dancf8f2892018-08-09 20:47:01 +00001282 Index *pIdx;
dancf8f2892018-08-09 20:47:01 +00001283 int i;
dan987db762018-08-14 20:18:50 +00001284 Table *pTab;
dan141e1192018-08-31 18:23:53 +00001285#ifndef SQLITE_OMIT_AUTHORIZATION
1286 sqlite3_xauth xAuth = db->xAuth;
1287#endif
dancf8f2892018-08-09 20:47:01 +00001288
drh38d99642018-08-18 18:27:18 +00001289 UNUSED_PARAMETER(NotUsed);
dan987db762018-08-14 20:18:50 +00001290 if( zSql==0 ) return;
dan987db762018-08-14 20:18:50 +00001291 if( zTable==0 ) return;
danb0137382018-08-20 20:01:01 +00001292 if( zNew==0 ) return;
dan987db762018-08-14 20:18:50 +00001293 if( iCol<0 ) return;
drhda76adc2018-08-25 02:04:05 +00001294 sqlite3BtreeEnterAll(db);
dan987db762018-08-14 20:18:50 +00001295 pTab = sqlite3FindTable(db, zTable, zDb);
drhda76adc2018-08-25 02:04:05 +00001296 if( pTab==0 || iCol>=pTab->nCol ){
1297 sqlite3BtreeLeaveAll(db);
1298 return;
1299 }
dan987db762018-08-14 20:18:50 +00001300 zOld = pTab->aCol[iCol].zName;
dancf8f2892018-08-09 20:47:01 +00001301 memset(&sCtx, 0, sizeof(sCtx));
dan987db762018-08-14 20:18:50 +00001302 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
dancf8f2892018-08-09 20:47:01 +00001303
dan141e1192018-08-31 18:23:53 +00001304#ifndef SQLITE_OMIT_AUTHORIZATION
1305 db->xAuth = 0;
1306#endif
danc9461ec2018-08-29 21:00:16 +00001307 rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);
dan24fedb92018-08-18 17:35:38 +00001308
dancf8f2892018-08-09 20:47:01 +00001309 /* Find tokens that need to be replaced. */
1310 memset(&sWalker, 0, sizeof(Walker));
1311 sWalker.pParse = &sParse;
1312 sWalker.xExprCallback = renameColumnExprCb;
dan987db762018-08-14 20:18:50 +00001313 sWalker.xSelectCallback = renameColumnSelectCb;
dancf8f2892018-08-09 20:47:01 +00001314 sWalker.u.pRename = &sCtx;
1315
dan0cbb0b12018-08-16 19:49:16 +00001316 sCtx.pTab = pTab;
dan987db762018-08-14 20:18:50 +00001317 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
dancf8f2892018-08-09 20:47:01 +00001318 if( sParse.pNewTable ){
dan987db762018-08-14 20:18:50 +00001319 Select *pSelect = sParse.pNewTable->pSelect;
1320 if( pSelect ){
dan38096962019-12-09 08:13:43 +00001321 pSelect->selFlags &= ~SF_View;
dan987db762018-08-14 20:18:50 +00001322 sParse.rc = SQLITE_OK;
dan38096962019-12-09 08:13:43 +00001323 sqlite3SelectPrep(&sParse, pSelect, 0);
dan987db762018-08-14 20:18:50 +00001324 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
1325 if( rc==SQLITE_OK ){
1326 sqlite3WalkSelect(&sWalker, pSelect);
dana8762ae2018-08-11 17:49:23 +00001327 }
dan987db762018-08-14 20:18:50 +00001328 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
1329 }else{
1330 /* A regular table */
1331 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
1332 FKey *pFKey;
1333 assert( sParse.pNewTable->pSelect==0 );
dan0cbb0b12018-08-16 19:49:16 +00001334 sCtx.pTab = sParse.pNewTable;
dan987db762018-08-14 20:18:50 +00001335 if( bFKOnly==0 ){
1336 renameTokenFind(
1337 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
1338 );
1339 if( sCtx.iCol<0 ){
1340 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
dancf8f2892018-08-09 20:47:01 +00001341 }
dan987db762018-08-14 20:18:50 +00001342 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
1343 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
1344 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1345 }
drh885eeb62019-01-09 02:02:24 +00001346 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
1347 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1348 }
dan987db762018-08-14 20:18:50 +00001349 }
drhb9bcf7c2019-10-19 13:29:10 +00001350#ifndef SQLITE_OMIT_GENERATED_COLUMNS
1351 for(i=0; i<sParse.pNewTable->nCol; i++){
1352 sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
1353 }
1354#endif
dan987db762018-08-14 20:18:50 +00001355
1356 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1357 for(i=0; i<pFKey->nCol; i++){
dan356afab2018-08-14 21:05:35 +00001358 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
dan987db762018-08-14 20:18:50 +00001359 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
1360 }
1361 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
1362 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
1363 ){
1364 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
1365 }
dan6fe7f232018-08-10 19:19:33 +00001366 }
dancf8f2892018-08-09 20:47:01 +00001367 }
1368 }
dan5496d6a2018-08-13 17:14:26 +00001369 }else if( sParse.pNewIndex ){
dancf8f2892018-08-09 20:47:01 +00001370 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
1371 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
dan5496d6a2018-08-13 17:14:26 +00001372 }else{
dan5be60c52018-08-15 20:28:39 +00001373 /* A trigger */
1374 TriggerStep *pStep;
dan0ccda962018-08-30 16:26:48 +00001375 rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
danc9461ec2018-08-29 21:00:16 +00001376 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
dan5be60c52018-08-15 20:28:39 +00001377
danc9461ec2018-08-29 21:00:16 +00001378 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
1379 if( pStep->zTarget ){
dan06249392018-08-21 15:06:59 +00001380 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
danc9461ec2018-08-29 21:00:16 +00001381 if( pTarget==pTab ){
dan0cbb0b12018-08-16 19:49:16 +00001382 if( pStep->pUpsert ){
danc9461ec2018-08-29 21:00:16 +00001383 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
1384 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
dan0cbb0b12018-08-16 19:49:16 +00001385 }
danc9461ec2018-08-29 21:00:16 +00001386 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
1387 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
dan5be60c52018-08-15 20:28:39 +00001388 }
1389 }
1390 }
1391
dan5be60c52018-08-15 20:28:39 +00001392
1393 /* Find tokens to edit in UPDATE OF clause */
dan06249392018-08-21 15:06:59 +00001394 if( sParse.pTriggerTab==pTab ){
1395 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
dan5496d6a2018-08-13 17:14:26 +00001396 }
dan5be60c52018-08-15 20:28:39 +00001397
danc9461ec2018-08-29 21:00:16 +00001398 /* Find tokens to edit in various expressions and selects */
1399 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
dancf8f2892018-08-09 20:47:01 +00001400 }
1401
dan987db762018-08-14 20:18:50 +00001402 assert( rc==SQLITE_OK );
danc9461ec2018-08-29 21:00:16 +00001403 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
dancf8f2892018-08-09 20:47:01 +00001404
drh5fc22cd2018-08-13 13:43:11 +00001405renameColumnFunc_done:
dan987db762018-08-14 20:18:50 +00001406 if( rc!=SQLITE_OK ){
dan24fedb92018-08-18 17:35:38 +00001407 if( sParse.zErrMsg ){
dan9d324822018-08-30 20:03:44 +00001408 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
dan987db762018-08-14 20:18:50 +00001409 }else{
1410 sqlite3_result_error_code(context, rc);
1411 }
1412 }
1413
dan141e1192018-08-31 18:23:53 +00001414 renameParseCleanup(&sParse);
drh4a2c7472018-08-13 15:09:48 +00001415 renameTokenFree(db, sCtx.pList);
dan141e1192018-08-31 18:23:53 +00001416#ifndef SQLITE_OMIT_AUTHORIZATION
1417 db->xAuth = xAuth;
1418#endif
drhda76adc2018-08-25 02:04:05 +00001419 sqlite3BtreeLeaveAll(db);
dancf8f2892018-08-09 20:47:01 +00001420}
1421
dandd1a9c82018-09-05 08:28:30 +00001422/*
1423** Walker expression callback used by "RENAME TABLE".
1424*/
danc9461ec2018-08-29 21:00:16 +00001425static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
1426 RenameCtx *p = pWalker->u.pRename;
drheda079c2018-09-20 19:02:15 +00001427 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
1428 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
danc9461ec2018-08-29 21:00:16 +00001429 }
1430 return WRC_Continue;
1431}
1432
1433/*
dandd1a9c82018-09-05 08:28:30 +00001434** Walker select callback used by "RENAME TABLE".
danc9461ec2018-08-29 21:00:16 +00001435*/
1436static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
1437 int i;
1438 RenameCtx *p = pWalker->u.pRename;
1439 SrcList *pSrc = pSelect->pSrc;
dan38096962019-12-09 08:13:43 +00001440 if( pSelect->selFlags & SF_View ) return WRC_Prune;
drh92a28242019-10-09 15:37:58 +00001441 if( pSrc==0 ){
1442 assert( pWalker->pParse->db->mallocFailed );
drh974b2482018-12-06 01:53:12 +00001443 return WRC_Abort;
1444 }
danc9461ec2018-08-29 21:00:16 +00001445 for(i=0; i<pSrc->nSrc; i++){
1446 struct SrcList_item *pItem = &pSrc->a[i];
1447 if( pItem->pTab==p->pTab ){
1448 renameTokenFind(pWalker->pParse, p, pItem->zName);
1449 }
1450 }
danea412512018-12-05 13:49:04 +00001451 renameWalkWith(pWalker, pSelect);
danc9461ec2018-08-29 21:00:16 +00001452
1453 return WRC_Continue;
1454}
1455
1456
1457/*
1458** This C function implements an SQL user function that is used by SQL code
1459** generated by the ALTER TABLE ... RENAME command to modify the definition
1460** of any foreign key constraints that use the table being renamed as the
1461** parent table. It is passed three arguments:
1462**
dan141e1192018-08-31 18:23:53 +00001463** 0: The database containing the table being renamed.
dan65372fa2018-09-03 20:05:15 +00001464** 1. type: Type of object ("table", "view" etc.)
1465** 2. object: Name of object
1466** 3: The complete text of the schema statement being modified,
1467** 4: The old name of the table being renamed, and
1468** 5: The new name of the table being renamed.
1469** 6: True if the schema statement comes from the temp db.
danc9461ec2018-08-29 21:00:16 +00001470**
dan141e1192018-08-31 18:23:53 +00001471** It returns the new schema statement. For example:
danc9461ec2018-08-29 21:00:16 +00001472**
dan141e1192018-08-31 18:23:53 +00001473** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
danc9461ec2018-08-29 21:00:16 +00001474** -> 'CREATE TABLE t1(a REFERENCES t3)'
1475*/
1476static void renameTableFunc(
1477 sqlite3_context *context,
1478 int NotUsed,
1479 sqlite3_value **argv
1480){
1481 sqlite3 *db = sqlite3_context_db_handle(context);
drh5b1da302018-09-01 20:02:07 +00001482 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
dan65372fa2018-09-03 20:05:15 +00001483 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
1484 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
1485 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
1486 int bTemp = sqlite3_value_int(argv[6]);
drh5b1da302018-09-01 20:02:07 +00001487 UNUSED_PARAMETER(NotUsed);
danc9461ec2018-08-29 21:00:16 +00001488
dan141e1192018-08-31 18:23:53 +00001489 if( zInput && zOld && zNew ){
dan141e1192018-08-31 18:23:53 +00001490 Parse sParse;
1491 int rc;
1492 int bQuote = 1;
1493 RenameCtx sCtx;
1494 Walker sWalker;
danc9461ec2018-08-29 21:00:16 +00001495
dan141e1192018-08-31 18:23:53 +00001496#ifndef SQLITE_OMIT_AUTHORIZATION
1497 sqlite3_xauth xAuth = db->xAuth;
1498 db->xAuth = 0;
danc9461ec2018-08-29 21:00:16 +00001499#endif
1500
dan141e1192018-08-31 18:23:53 +00001501 sqlite3BtreeEnterAll(db);
1502
1503 memset(&sCtx, 0, sizeof(RenameCtx));
1504 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
1505 memset(&sWalker, 0, sizeof(Walker));
1506 sWalker.pParse = &sParse;
1507 sWalker.xExprCallback = renameTableExprCb;
1508 sWalker.xSelectCallback = renameTableSelectCb;
1509 sWalker.u.pRename = &sCtx;
1510
1511 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
1512
1513 if( rc==SQLITE_OK ){
dan674b8942018-09-20 08:28:01 +00001514 int isLegacy = (db->flags & SQLITE_LegacyAlter);
dan141e1192018-08-31 18:23:53 +00001515 if( sParse.pNewTable ){
1516 Table *pTab = sParse.pNewTable;
1517
1518 if( pTab->pSelect ){
dan674b8942018-09-20 08:28:01 +00001519 if( isLegacy==0 ){
dan38096962019-12-09 08:13:43 +00001520 Select *pSelect = pTab->pSelect;
dan674b8942018-09-20 08:28:01 +00001521 NameContext sNC;
1522 memset(&sNC, 0, sizeof(sNC));
1523 sNC.pParse = &sParse;
dan141e1192018-08-31 18:23:53 +00001524
dan38096962019-12-09 08:13:43 +00001525 assert( pSelect->selFlags & SF_View );
1526 pSelect->selFlags &= ~SF_View;
dan674b8942018-09-20 08:28:01 +00001527 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
1528 if( sParse.nErr ) rc = sParse.rc;
1529 sqlite3WalkSelect(&sWalker, pTab->pSelect);
1530 }
dan141e1192018-08-31 18:23:53 +00001531 }else{
1532 /* Modify any FK definitions to point to the new table. */
1533#ifndef SQLITE_OMIT_FOREIGN_KEY
danb4307012018-11-09 20:04:05 +00001534 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
dan202a0272018-09-07 11:51:21 +00001535 FKey *pFKey;
1536 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1537 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
1538 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
1539 }
dan141e1192018-08-31 18:23:53 +00001540 }
1541 }
1542#endif
1543
1544 /* If this is the table being altered, fix any table refs in CHECK
1545 ** expressions. Also update the name that appears right after the
1546 ** "CREATE [VIRTUAL] TABLE" bit. */
1547 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
1548 sCtx.pTab = pTab;
dan674b8942018-09-20 08:28:01 +00001549 if( isLegacy==0 ){
1550 sqlite3WalkExprList(&sWalker, pTab->pCheck);
1551 }
dan141e1192018-08-31 18:23:53 +00001552 renameTokenFind(&sParse, &sCtx, pTab->zName);
1553 }
danc9461ec2018-08-29 21:00:16 +00001554 }
1555 }
danc9461ec2018-08-29 21:00:16 +00001556
dan141e1192018-08-31 18:23:53 +00001557 else if( sParse.pNewIndex ){
1558 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
dan674b8942018-09-20 08:28:01 +00001559 if( isLegacy==0 ){
1560 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
1561 }
dan141e1192018-08-31 18:23:53 +00001562 }
danc9461ec2018-08-29 21:00:16 +00001563
1564#ifndef SQLITE_OMIT_TRIGGER
danb87a9a82018-09-01 20:23:28 +00001565 else{
dan141e1192018-08-31 18:23:53 +00001566 Trigger *pTrigger = sParse.pNewTrigger;
1567 TriggerStep *pStep;
1568 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
1569 && sCtx.pTab->pSchema==pTrigger->pTabSchema
1570 ){
1571 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
1572 }
danc9461ec2018-08-29 21:00:16 +00001573
dan674b8942018-09-20 08:28:01 +00001574 if( isLegacy==0 ){
1575 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
1576 if( rc==SQLITE_OK ){
1577 renameWalkTrigger(&sWalker, pTrigger);
1578 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1579 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
1580 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
1581 }
dan141e1192018-08-31 18:23:53 +00001582 }
dan0ccda962018-08-30 16:26:48 +00001583 }
danc9461ec2018-08-29 21:00:16 +00001584 }
1585 }
dan141e1192018-08-31 18:23:53 +00001586#endif
danc9461ec2018-08-29 21:00:16 +00001587 }
dan141e1192018-08-31 18:23:53 +00001588
1589 if( rc==SQLITE_OK ){
1590 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
1591 }
1592 if( rc!=SQLITE_OK ){
dan65372fa2018-09-03 20:05:15 +00001593 if( sParse.zErrMsg ){
1594 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
1595 }else{
1596 sqlite3_result_error_code(context, rc);
1597 }
dan141e1192018-08-31 18:23:53 +00001598 }
1599
1600 renameParseCleanup(&sParse);
1601 renameTokenFree(db, sCtx.pList);
1602 sqlite3BtreeLeaveAll(db);
1603#ifndef SQLITE_OMIT_AUTHORIZATION
1604 db->xAuth = xAuth;
danc9461ec2018-08-29 21:00:16 +00001605#endif
1606 }
1607
danc9461ec2018-08-29 21:00:16 +00001608 return;
1609}
1610
dandd1a9c82018-09-05 08:28:30 +00001611/*
1612** An SQL user function that checks that there are no parse or symbol
1613** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
1614** After an ALTER TABLE .. RENAME operation is performed and the schema
1615** reloaded, this function is called on each SQL statement in the schema
dan1d85c6b2018-09-06 16:01:37 +00001616** to ensure that it is still usable.
dandd1a9c82018-09-05 08:28:30 +00001617**
1618** 0: Database name ("main", "temp" etc.).
1619** 1: SQL statement.
1620** 2: Object type ("view", "table", "trigger" or "index").
1621** 3: Object name.
1622** 4: True if object is from temp schema.
dan1d85c6b2018-09-06 16:01:37 +00001623**
1624** Unless it finds an error, this function normally returns NULL. However, it
1625** returns integer value 1 if:
1626**
1627** * the SQL argument creates a trigger, and
1628** * the table that the trigger is attached to is in database zDb.
dandd1a9c82018-09-05 08:28:30 +00001629*/
dan9d324822018-08-30 20:03:44 +00001630static void renameTableTest(
1631 sqlite3_context *context,
1632 int NotUsed,
1633 sqlite3_value **argv
1634){
1635 sqlite3 *db = sqlite3_context_db_handle(context);
drh5b1da302018-09-01 20:02:07 +00001636 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
1637 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
dan9d324822018-08-30 20:03:44 +00001638 int bTemp = sqlite3_value_int(argv[4]);
dan674b8942018-09-20 08:28:01 +00001639 int isLegacy = (db->flags & SQLITE_LegacyAlter);
dan9d324822018-08-30 20:03:44 +00001640
dan141e1192018-08-31 18:23:53 +00001641#ifndef SQLITE_OMIT_AUTHORIZATION
1642 sqlite3_xauth xAuth = db->xAuth;
1643 db->xAuth = 0;
1644#endif
1645
drh5b1da302018-09-01 20:02:07 +00001646 UNUSED_PARAMETER(NotUsed);
dan09236502018-09-01 16:05:50 +00001647 if( zDb && zInput ){
1648 int rc;
1649 Parse sParse;
1650 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
1651 if( rc==SQLITE_OK ){
dan674b8942018-09-20 08:28:01 +00001652 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
dan09236502018-09-01 16:05:50 +00001653 NameContext sNC;
1654 memset(&sNC, 0, sizeof(sNC));
1655 sNC.pParse = &sParse;
1656 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
1657 if( sParse.nErr ) rc = sParse.rc;
1658 }
1659
1660 else if( sParse.pNewTrigger ){
dan674b8942018-09-20 08:28:01 +00001661 if( isLegacy==0 ){
1662 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
1663 }
drh3b700452018-09-07 19:12:08 +00001664 if( rc==SQLITE_OK ){
dan1d85c6b2018-09-06 16:01:37 +00001665 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
1666 int i2 = sqlite3FindDbName(db, zDb);
1667 if( i1==i2 ) sqlite3_result_int(context, 1);
1668 }
dan09236502018-09-01 16:05:50 +00001669 }
dan9d324822018-08-30 20:03:44 +00001670 }
1671
dan09236502018-09-01 16:05:50 +00001672 if( rc!=SQLITE_OK ){
1673 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
dan9d324822018-08-30 20:03:44 +00001674 }
dan09236502018-09-01 16:05:50 +00001675 renameParseCleanup(&sParse);
dan9d324822018-08-30 20:03:44 +00001676 }
1677
dan141e1192018-08-31 18:23:53 +00001678#ifndef SQLITE_OMIT_AUTHORIZATION
1679 db->xAuth = xAuth;
1680#endif
dan9d324822018-08-30 20:03:44 +00001681}
1682
dancf8f2892018-08-09 20:47:01 +00001683/*
1684** Register built-in functions used to help implement ALTER TABLE
1685*/
1686void sqlite3AlterFunctions(void){
1687 static FuncDef aAlterTableFuncs[] = {
drheea8eb62018-11-26 18:09:15 +00001688 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
1689 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc),
1690 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest),
dancf8f2892018-08-09 20:47:01 +00001691 };
1692 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
1693}
drhd0e4a6c2005-02-15 20:47:57 +00001694#endif /* SQLITE_ALTER_TABLE */