xref: /sqlite-3.40.0/src/alter.c (revision 1a7feefa)
1 /*
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.
14 */
15 #include "sqliteInt.h"
16 
17 /*
18 ** The code in this file only exists if we are not omitting the
19 ** ALTER TABLE logic from the build.
20 */
21 #ifndef SQLITE_OMIT_ALTERTABLE
22 
23 /*
24 ** 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 */
31 static int isAlterableTable(Parse *pParse, Table *pTab){
32   if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
33 #ifndef SQLITE_OMIT_VIRTUALTABLE
34    || ( (pTab->tabFlags & TF_Shadow)!=0
35         && sqlite3ReadOnlyShadowTables(pParse->db)
36    )
37 #endif
38   ){
39     sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
40     return 1;
41   }
42   return 0;
43 }
44 
45 /*
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 */
52 static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
53   sqlite3NestedParse(pParse,
54       "SELECT 1 "
55       "FROM \"%w\".%s "
56       "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
57       " AND sql NOT LIKE 'create virtual%%'"
58       " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
59       zDb, MASTER_NAME,
60       zDb, bTemp
61   );
62 
63   if( bTemp==0 ){
64     sqlite3NestedParse(pParse,
65         "SELECT 1 "
66         "FROM temp.%s "
67         "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
68         " AND sql NOT LIKE 'create virtual%%'"
69         " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
70         MASTER_NAME, zDb
71     );
72   }
73 }
74 
75 /*
76 ** Generate code to reload the schema for database iDb. And, if iDb!=1, for
77 ** the temp database as well.
78 */
79 static void renameReloadSchema(Parse *pParse, int iDb){
80   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 /*
89 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
90 ** command.
91 */
92 void 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 */
101   sqlite3 *db = pParse->db; /* Database connection */
102   int nTabName;             /* Number of UTF-8 characters in zTabName */
103   const char *zTabName;     /* Original name of the table */
104   Vdbe *v;
105   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
106   u32 savedDbFlags;         /* Saved value of db->mDbFlags */
107 
108   savedDbFlags = db->mDbFlags;
109   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
110   assert( pSrc->nSrc==1 );
111   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
112 
113   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
114   if( !pTab ) goto exit_rename_table;
115   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116   zDb = db->aDb[iDb].zDbSName;
117   db->mDbFlags |= DBFLAG_PreferBuiltin;
118 
119   /* Get a NULL terminated version of the new table name. */
120   zName = sqlite3NameFromToken(db, pName);
121   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)
127    || sqlite3FindIndex(db, zName, zDb)
128    || sqlite3IsShadowTableOf(db, pTab, zName)
129   ){
130     sqlite3ErrorMsg(pParse,
131         "there is already another table or index with this name: %s", zName);
132     goto exit_rename_table;
133   }
134 
135   /* Make sure it is not a system table being altered, or a reserved name
136   ** that the table is being renamed to.
137   */
138   if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
139     goto exit_rename_table;
140   }
141   if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
142     goto exit_rename_table;
143   }
144 
145 #ifndef SQLITE_OMIT_VIEW
146   if( pTab->pSelect ){
147     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
148     goto exit_rename_table;
149   }
150 #endif
151 
152 #ifndef SQLITE_OMIT_AUTHORIZATION
153   /* Invoke the authorization callback. */
154   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
155     goto exit_rename_table;
156   }
157 #endif
158 
159 #ifndef SQLITE_OMIT_VIRTUALTABLE
160   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
161     goto exit_rename_table;
162   }
163   if( IsVirtual(pTab) ){
164     pVTab = sqlite3GetVTable(db, pTab);
165     if( pVTab->pVtab->pModule->xRename==0 ){
166       pVTab = 0;
167     }
168   }
169 #endif
170 
171   /* Begin a transaction for database iDb. Then modify the schema cookie
172   ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
173   ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
174   ** nested SQL may raise an exception.  */
175   v = sqlite3GetVdbe(pParse);
176   if( v==0 ){
177     goto exit_rename_table;
178   }
179   sqlite3MayAbort(pParse);
180 
181   /* figure out how many UTF-8 characters are in zName */
182   zTabName = pTab->zName;
183   nTabName = sqlite3Utf8CharLen(zTabName, -1);
184 
185   /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
186   ** the schema to use the new table name.  */
187   sqlite3NestedParse(pParse,
188       "UPDATE \"%w\".%s SET "
189       "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
190       "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
191       "AND   name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
192       , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
193   );
194 
195   /* Update the tbl_name and name columns of the sqlite_master table
196   ** as required.  */
197   sqlite3NestedParse(pParse,
198       "UPDATE %Q.%s SET "
199           "tbl_name = %Q, "
200           "name = CASE "
201             "WHEN type='table' THEN %Q "
202             "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
203             "     AND type='index' THEN "
204              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
205             "ELSE name END "
206       "WHERE tbl_name=%Q COLLATE nocase AND "
207           "(type='table' OR type='index' OR type='trigger');",
208       zDb, MASTER_NAME,
209       zName, zName, zName,
210       nTabName, zTabName
211   );
212 
213 #ifndef SQLITE_OMIT_AUTOINCREMENT
214   /* If the sqlite_sequence table exists in this database, then update
215   ** it with the new table name.
216   */
217   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
218     sqlite3NestedParse(pParse,
219         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
220         zDb, zName, pTab->zName);
221   }
222 #endif
223 
224   /* If the table being renamed is not itself part of the temp database,
225   ** edit view and trigger definitions within the temp database
226   ** as required.  */
227   if( iDb!=1 ){
228     sqlite3NestedParse(pParse,
229         "UPDATE sqlite_temp_master SET "
230             "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
231             "tbl_name = "
232               "CASE WHEN tbl_name=%Q COLLATE nocase AND "
233               "          sqlite_rename_test(%Q, sql, type, name, 1) "
234               "THEN %Q ELSE tbl_name END "
235             "WHERE type IN ('view', 'trigger')"
236         , zDb, zTabName, zName, zTabName, zDb, zName);
237   }
238 
239   /* If this is a virtual table, invoke the xRename() function if
240   ** one is defined. The xRename() callback will modify the names
241   ** of any resources used by the v-table implementation (including other
242   ** SQLite tables) that are identified by the name of the virtual table.
243   */
244 #ifndef SQLITE_OMIT_VIRTUALTABLE
245   if( pVTab ){
246     int i = ++pParse->nMem;
247     sqlite3VdbeLoadString(v, i, zName);
248     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
249   }
250 #endif
251 
252   renameReloadSchema(pParse, iDb);
253   renameTestSchema(pParse, zDb, iDb==1);
254 
255 exit_rename_table:
256   sqlite3SrcListDelete(db, pSrc);
257   sqlite3DbFree(db, zName);
258   db->mDbFlags = savedDbFlags;
259 }
260 
261 /*
262 ** Write code that will raise an error if the table described by
263 ** zDb and zTab is not empty.
264 */
265 static void sqlite3ErrorIfNotEmpty(
266   Parse *pParse,        /* Parsing context */
267   const char *zDb,      /* Schema holding the table */
268   const char *zTab,     /* Table to check for empty */
269   const char *zErr      /* Error message text */
270 ){
271   sqlite3NestedParse(pParse,
272      "SELECT raise(ABORT,%Q) FROM \"%w\".\"%w\"",
273      zErr, zDb, zTab
274   );
275 }
276 
277 /*
278 ** This function is called after an "ALTER TABLE ... ADD" statement
279 ** has been parsed. Argument pColDef contains the text of the new
280 ** column definition.
281 **
282 ** The Table structure pParse->pNewTable was extended to include
283 ** the new column during parsing.
284 */
285 void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
286   Table *pNew;              /* Copy of pParse->pNewTable */
287   Table *pTab;              /* Table being altered */
288   int iDb;                  /* Database number */
289   const char *zDb;          /* Database name */
290   const char *zTab;         /* Table name */
291   char *zCol;               /* Null-terminated column definition */
292   Column *pCol;             /* The new column */
293   Expr *pDflt;              /* Default value for the new column */
294   sqlite3 *db;              /* The database connection; */
295   Vdbe *v;                  /* The prepared statement under construction */
296   int r1;                   /* Temporary registers */
297 
298   db = pParse->db;
299   if( pParse->nErr || db->mallocFailed ) return;
300   pNew = pParse->pNewTable;
301   assert( pNew );
302 
303   assert( sqlite3BtreeHoldsAllMutexes(db) );
304   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
305   zDb = db->aDb[iDb].zDbSName;
306   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
307   pCol = &pNew->aCol[pNew->nCol-1];
308   pDflt = pCol->pDflt;
309   pTab = sqlite3FindTable(db, zTab, zDb);
310   assert( pTab );
311 
312 #ifndef SQLITE_OMIT_AUTHORIZATION
313   /* Invoke the authorization callback. */
314   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
315     return;
316   }
317 #endif
318 
319 
320   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
321   ** If there is a NOT NULL constraint, then the default value for the
322   ** column must not be NULL.
323   */
324   if( pCol->colFlags & COLFLAG_PRIMKEY ){
325     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
326     return;
327   }
328   if( pNew->pIndex ){
329     sqlite3ErrorMsg(pParse,
330          "Cannot add a UNIQUE column");
331     return;
332   }
333   if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
334     /* If the default value for the new column was specified with a
335     ** literal NULL, then set pDflt to 0. This simplifies checking
336     ** for an SQL NULL default below.
337     */
338     assert( pDflt==0 || pDflt->op==TK_SPAN );
339     if( pDflt && pDflt->pLeft->op==TK_NULL ){
340       pDflt = 0;
341     }
342     if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
343       sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
344           "Cannot add a REFERENCES column with non-NULL default value");
345     }
346     if( pCol->notNull && !pDflt ){
347       sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
348           "Cannot add a NOT NULL column with default value NULL");
349     }
350 
351 
352     /* Ensure the default expression is something that sqlite3ValueFromExpr()
353     ** can handle (i.e. not CURRENT_TIME etc.)
354     */
355     if( pDflt ){
356       sqlite3_value *pVal = 0;
357       int rc;
358       rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
359       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
360       if( rc!=SQLITE_OK ){
361         assert( db->mallocFailed == 1 );
362         return;
363       }
364       if( !pVal ){
365         sqlite3ErrorIfNotEmpty(pParse, zDb, zTab,
366            "Cannot add a column with non-constant default");
367       }
368       sqlite3ValueFree(pVal);
369     }
370   }else if( pCol->colFlags & COLFLAG_STORED ){
371     sqlite3ErrorIfNotEmpty(pParse, zDb, zTab, "cannot add a STORED column");
372   }
373 
374 
375   /* Modify the CREATE TABLE statement. */
376   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
377   if( zCol ){
378     char *zEnd = &zCol[pColDef->n-1];
379     u32 savedDbFlags = db->mDbFlags;
380     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
381       *zEnd-- = '\0';
382     }
383     db->mDbFlags |= DBFLAG_PreferBuiltin;
384     sqlite3NestedParse(pParse,
385         "UPDATE \"%w\".%s SET "
386           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
387         "WHERE type = 'table' AND name = %Q",
388       zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
389       zTab
390     );
391     sqlite3DbFree(db, zCol);
392     db->mDbFlags = savedDbFlags;
393   }
394 
395   /* Make sure the schema version is at least 3.  But do not upgrade
396   ** from less than 3 to 4, as that will corrupt any preexisting DESC
397   ** index.
398   */
399   v = sqlite3GetVdbe(pParse);
400   if( v ){
401     r1 = sqlite3GetTempReg(pParse);
402     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
403     sqlite3VdbeUsesBtree(v, iDb);
404     sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
405     sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
406     VdbeCoverage(v);
407     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
408     sqlite3ReleaseTempReg(pParse, r1);
409   }
410 
411   /* Reload the table definition */
412   renameReloadSchema(pParse, iDb);
413 }
414 
415 /*
416 ** This function is called by the parser after the table-name in
417 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
418 ** pSrc is the full-name of the table being altered.
419 **
420 ** This routine makes a (partial) copy of the Table structure
421 ** for the table being altered and sets Parse.pNewTable to point
422 ** to it. Routines called by the parser as the column definition
423 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
424 ** the copy. The copy of the Table structure is deleted by tokenize.c
425 ** after parsing is finished.
426 **
427 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
428 ** coding the "ALTER TABLE ... ADD" statement.
429 */
430 void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
431   Table *pNew;
432   Table *pTab;
433   int iDb;
434   int i;
435   int nAlloc;
436   sqlite3 *db = pParse->db;
437 
438   /* Look up the table being altered. */
439   assert( pParse->pNewTable==0 );
440   assert( sqlite3BtreeHoldsAllMutexes(db) );
441   if( db->mallocFailed ) goto exit_begin_add_column;
442   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
443   if( !pTab ) goto exit_begin_add_column;
444 
445 #ifndef SQLITE_OMIT_VIRTUALTABLE
446   if( IsVirtual(pTab) ){
447     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
448     goto exit_begin_add_column;
449   }
450 #endif
451 
452   /* Make sure this is not an attempt to ALTER a view. */
453   if( pTab->pSelect ){
454     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
455     goto exit_begin_add_column;
456   }
457   if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
458     goto exit_begin_add_column;
459   }
460 
461   sqlite3MayAbort(pParse);
462   assert( pTab->addColOffset>0 );
463   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
464 
465   /* Put a copy of the Table struct in Parse.pNewTable for the
466   ** sqlite3AddColumn() function and friends to modify.  But modify
467   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
468   ** prefix, we insure that the name will not collide with an existing
469   ** table because user table are not allowed to have the "sqlite_"
470   ** prefix on their name.
471   */
472   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
473   if( !pNew ) goto exit_begin_add_column;
474   pParse->pNewTable = pNew;
475   pNew->nTabRef = 1;
476   pNew->nCol = pTab->nCol;
477   assert( pNew->nCol>0 );
478   nAlloc = (((pNew->nCol-1)/8)*8)+8;
479   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
480   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
481   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
482   if( !pNew->aCol || !pNew->zName ){
483     assert( db->mallocFailed );
484     goto exit_begin_add_column;
485   }
486   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
487   for(i=0; i<pNew->nCol; i++){
488     Column *pCol = &pNew->aCol[i];
489     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
490     pCol->hName = sqlite3StrIHash(pCol->zName);
491     pCol->zColl = 0;
492     pCol->pDflt = 0;
493   }
494   pNew->pSchema = db->aDb[iDb].pSchema;
495   pNew->addColOffset = pTab->addColOffset;
496   pNew->nTabRef = 1;
497 
498 exit_begin_add_column:
499   sqlite3SrcListDelete(db, pSrc);
500   return;
501 }
502 
503 /*
504 ** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
505 ** command. This function checks if the table is a view or virtual
506 ** table (columns of views or virtual tables may not be renamed). If so,
507 ** it loads an error message into pParse and returns non-zero.
508 **
509 ** Or, if pTab is not a view or virtual table, zero is returned.
510 */
511 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
512 static int isRealTable(Parse *pParse, Table *pTab){
513   const char *zType = 0;
514 #ifndef SQLITE_OMIT_VIEW
515   if( pTab->pSelect ){
516     zType = "view";
517   }
518 #endif
519 #ifndef SQLITE_OMIT_VIRTUALTABLE
520   if( IsVirtual(pTab) ){
521     zType = "virtual table";
522   }
523 #endif
524   if( zType ){
525     sqlite3ErrorMsg(
526         pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
527     );
528     return 1;
529   }
530   return 0;
531 }
532 #else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
533 # define isRealTable(x,y) (0)
534 #endif
535 
536 /*
537 ** Handles the following parser reduction:
538 **
539 **  cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
540 */
541 void sqlite3AlterRenameColumn(
542   Parse *pParse,                  /* Parsing context */
543   SrcList *pSrc,                  /* Table being altered.  pSrc->nSrc==1 */
544   Token *pOld,                    /* Name of column being changed */
545   Token *pNew                     /* New column name */
546 ){
547   sqlite3 *db = pParse->db;       /* Database connection */
548   Table *pTab;                    /* Table being updated */
549   int iCol;                       /* Index of column being renamed */
550   char *zOld = 0;                 /* Old column name */
551   char *zNew = 0;                 /* New column name */
552   const char *zDb;                /* Name of schema containing the table */
553   int iSchema;                    /* Index of the schema */
554   int bQuote;                     /* True to quote the new name */
555 
556   /* Locate the table to be altered */
557   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
558   if( !pTab ) goto exit_rename_column;
559 
560   /* Cannot alter a system table */
561   if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
562   if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
563 
564   /* Which schema holds the table to be altered */
565   iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
566   assert( iSchema>=0 );
567   zDb = db->aDb[iSchema].zDbSName;
568 
569 #ifndef SQLITE_OMIT_AUTHORIZATION
570   /* Invoke the authorization callback. */
571   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
572     goto exit_rename_column;
573   }
574 #endif
575 
576   /* Make sure the old name really is a column name in the table to be
577   ** altered.  Set iCol to be the index of the column being renamed */
578   zOld = sqlite3NameFromToken(db, pOld);
579   if( !zOld ) goto exit_rename_column;
580   for(iCol=0; iCol<pTab->nCol; iCol++){
581     if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
582   }
583   if( iCol==pTab->nCol ){
584     sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
585     goto exit_rename_column;
586   }
587 
588   /* Do the rename operation using a recursive UPDATE statement that
589   ** uses the sqlite_rename_column() SQL function to compute the new
590   ** CREATE statement text for the sqlite_master table.
591   */
592   sqlite3MayAbort(pParse);
593   zNew = sqlite3NameFromToken(db, pNew);
594   if( !zNew ) goto exit_rename_column;
595   assert( pNew->n>0 );
596   bQuote = sqlite3Isquote(pNew->z[0]);
597   sqlite3NestedParse(pParse,
598       "UPDATE \"%w\".%s SET "
599       "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
600       "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
601       " AND (type != 'index' OR tbl_name = %Q)"
602       " AND sql NOT LIKE 'create virtual%%'",
603       zDb, MASTER_NAME,
604       zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
605       pTab->zName
606   );
607 
608   sqlite3NestedParse(pParse,
609       "UPDATE temp.%s SET "
610       "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
611       "WHERE type IN ('trigger', 'view')",
612       MASTER_NAME,
613       zDb, pTab->zName, iCol, zNew, bQuote
614   );
615 
616   /* Drop and reload the database schema. */
617   renameReloadSchema(pParse, iSchema);
618   renameTestSchema(pParse, zDb, iSchema==1);
619 
620  exit_rename_column:
621   sqlite3SrcListDelete(db, pSrc);
622   sqlite3DbFree(db, zOld);
623   sqlite3DbFree(db, zNew);
624   return;
625 }
626 
627 /*
628 ** Each RenameToken object maps an element of the parse tree into
629 ** the token that generated that element.  The parse tree element
630 ** might be one of:
631 **
632 **     *  A pointer to an Expr that represents an ID
633 **     *  The name of a table column in Column.zName
634 **
635 ** A list of RenameToken objects can be constructed during parsing.
636 ** Each new object is created by sqlite3RenameTokenMap().
637 ** As the parse tree is transformed, the sqlite3RenameTokenRemap()
638 ** routine is used to keep the mapping current.
639 **
640 ** After the parse finishes, renameTokenFind() routine can be used
641 ** to look up the actual token value that created some element in
642 ** the parse tree.
643 */
644 struct RenameToken {
645   void *p;               /* Parse tree element created by token t */
646   Token t;               /* The token that created parse tree element p */
647   RenameToken *pNext;    /* Next is a list of all RenameToken objects */
648 };
649 
650 /*
651 ** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
652 ** down into the Walker.
653 */
654 typedef struct RenameCtx RenameCtx;
655 struct RenameCtx {
656   RenameToken *pList;             /* List of tokens to overwrite */
657   int nList;                      /* Number of tokens in pList */
658   int iCol;                       /* Index of column being renamed */
659   Table *pTab;                    /* Table being ALTERed */
660   const char *zOld;               /* Old column name */
661 };
662 
663 #ifdef SQLITE_DEBUG
664 /*
665 ** This function is only for debugging. It performs two tasks:
666 **
667 **   1. Checks that pointer pPtr does not already appear in the
668 **      rename-token list.
669 **
670 **   2. Dereferences each pointer in the rename-token list.
671 **
672 ** The second is most effective when debugging under valgrind or
673 ** address-sanitizer or similar. If any of these pointers no longer
674 ** point to valid objects, an exception is raised by the memory-checking
675 ** tool.
676 **
677 ** The point of this is to prevent comparisons of invalid pointer values.
678 ** Even though this always seems to work, it is undefined according to the
679 ** C standard. Example of undefined comparison:
680 **
681 **     sqlite3_free(x);
682 **     if( x==y ) ...
683 **
684 ** Technically, as x no longer points into a valid object or to the byte
685 ** following a valid object, it may not be used in comparison operations.
686 */
687 static void renameTokenCheckAll(Parse *pParse, void *pPtr){
688   if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
689     RenameToken *p;
690     u8 i = 0;
691     for(p=pParse->pRename; p; p=p->pNext){
692       if( p->p ){
693         assert( p->p!=pPtr );
694         i += *(u8*)(p->p);
695       }
696     }
697   }
698 }
699 #else
700 # define renameTokenCheckAll(x,y)
701 #endif
702 
703 /*
704 ** Remember that the parser tree element pPtr was created using
705 ** the token pToken.
706 **
707 ** In other words, construct a new RenameToken object and add it
708 ** to the list of RenameToken objects currently being built up
709 ** in pParse->pRename.
710 **
711 ** The pPtr argument is returned so that this routine can be used
712 ** with tail recursion in tokenExpr() routine, for a small performance
713 ** improvement.
714 */
715 void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
716   RenameToken *pNew;
717   assert( pPtr || pParse->db->mallocFailed );
718   renameTokenCheckAll(pParse, pPtr);
719   if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
720     pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
721     if( pNew ){
722       pNew->p = pPtr;
723       pNew->t = *pToken;
724       pNew->pNext = pParse->pRename;
725       pParse->pRename = pNew;
726     }
727   }
728 
729   return pPtr;
730 }
731 
732 /*
733 ** It is assumed that there is already a RenameToken object associated
734 ** with parse tree element pFrom. This function remaps the associated token
735 ** to parse tree element pTo.
736 */
737 void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
738   RenameToken *p;
739   renameTokenCheckAll(pParse, pTo);
740   for(p=pParse->pRename; p; p=p->pNext){
741     if( p->p==pFrom ){
742       p->p = pTo;
743       break;
744     }
745   }
746 }
747 
748 /*
749 ** Walker callback used by sqlite3RenameExprUnmap().
750 */
751 static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
752   Parse *pParse = pWalker->pParse;
753   sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
754   return WRC_Continue;
755 }
756 
757 /*
758 ** Iterate through the Select objects that are part of WITH clauses attached
759 ** to select statement pSelect.
760 */
761 static void renameWalkWith(Walker *pWalker, Select *pSelect){
762   With *pWith = pSelect->pWith;
763   if( pWith ){
764     int i;
765     for(i=0; i<pWith->nCte; i++){
766       Select *p = pWith->a[i].pSelect;
767       NameContext sNC;
768       memset(&sNC, 0, sizeof(sNC));
769       sNC.pParse = pWalker->pParse;
770       sqlite3SelectPrep(sNC.pParse, p, &sNC);
771       sqlite3WalkSelect(pWalker, p);
772       sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols);
773     }
774   }
775 }
776 
777 /*
778 ** Unmap all tokens in the IdList object passed as the second argument.
779 */
780 static void unmapColumnIdlistNames(
781   Parse *pParse,
782   IdList *pIdList
783 ){
784   if( pIdList ){
785     int ii;
786     for(ii=0; ii<pIdList->nId; ii++){
787       sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName);
788     }
789   }
790 }
791 
792 /*
793 ** Walker callback used by sqlite3RenameExprUnmap().
794 */
795 static int renameUnmapSelectCb(Walker *pWalker, Select *p){
796   Parse *pParse = pWalker->pParse;
797   int i;
798   if( pParse->nErr ) return WRC_Abort;
799   if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
800   if( ALWAYS(p->pEList) ){
801     ExprList *pList = p->pEList;
802     for(i=0; i<pList->nExpr; i++){
803       if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
804         sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
805       }
806     }
807   }
808   if( ALWAYS(p->pSrc) ){  /* Every Select as a SrcList, even if it is empty */
809     SrcList *pSrc = p->pSrc;
810     for(i=0; i<pSrc->nSrc; i++){
811       sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
812       if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort;
813       unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing);
814     }
815   }
816 
817   renameWalkWith(pWalker, p);
818   return WRC_Continue;
819 }
820 
821 /*
822 ** Remove all nodes that are part of expression pExpr from the rename list.
823 */
824 void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
825   u8 eMode = pParse->eParseMode;
826   Walker sWalker;
827   memset(&sWalker, 0, sizeof(Walker));
828   sWalker.pParse = pParse;
829   sWalker.xExprCallback = renameUnmapExprCb;
830   sWalker.xSelectCallback = renameUnmapSelectCb;
831   pParse->eParseMode = PARSE_MODE_UNMAP;
832   sqlite3WalkExpr(&sWalker, pExpr);
833   pParse->eParseMode = eMode;
834 }
835 
836 /*
837 ** Remove all nodes that are part of expression-list pEList from the
838 ** rename list.
839 */
840 void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
841   if( pEList ){
842     int i;
843     Walker sWalker;
844     memset(&sWalker, 0, sizeof(Walker));
845     sWalker.pParse = pParse;
846     sWalker.xExprCallback = renameUnmapExprCb;
847     sqlite3WalkExprList(&sWalker, pEList);
848     for(i=0; i<pEList->nExpr; i++){
849       if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
850         sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
851       }
852     }
853   }
854 }
855 
856 /*
857 ** Free the list of RenameToken objects given in the second argument
858 */
859 static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
860   RenameToken *pNext;
861   RenameToken *p;
862   for(p=pToken; p; p=pNext){
863     pNext = p->pNext;
864     sqlite3DbFree(db, p);
865   }
866 }
867 
868 /*
869 ** Search the Parse object passed as the first argument for a RenameToken
870 ** object associated with parse tree element pPtr. If found, remove it
871 ** from the Parse object and add it to the list maintained by the
872 ** RenameCtx object passed as the second argument.
873 */
874 static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
875   RenameToken **pp;
876   assert( pPtr!=0 );
877   for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
878     if( (*pp)->p==pPtr ){
879       RenameToken *pToken = *pp;
880       *pp = pToken->pNext;
881       pToken->pNext = pCtx->pList;
882       pCtx->pList = pToken;
883       pCtx->nList++;
884       break;
885     }
886   }
887 }
888 
889 /*
890 ** This is a Walker select callback. It does nothing. It is only required
891 ** because without a dummy callback, sqlite3WalkExpr() and similar do not
892 ** descend into sub-select statements.
893 */
894 static int renameColumnSelectCb(Walker *pWalker, Select *p){
895   if( p->selFlags & SF_View ) return WRC_Prune;
896   renameWalkWith(pWalker, p);
897   return WRC_Continue;
898 }
899 
900 /*
901 ** This is a Walker expression callback.
902 **
903 ** For every TK_COLUMN node in the expression tree, search to see
904 ** if the column being references is the column being renamed by an
905 ** ALTER TABLE statement.  If it is, then attach its associated
906 ** RenameToken object to the list of RenameToken objects being
907 ** constructed in RenameCtx object at pWalker->u.pRename.
908 */
909 static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
910   RenameCtx *p = pWalker->u.pRename;
911   if( pExpr->op==TK_TRIGGER
912    && pExpr->iColumn==p->iCol
913    && pWalker->pParse->pTriggerTab==p->pTab
914   ){
915     renameTokenFind(pWalker->pParse, p, (void*)pExpr);
916   }else if( pExpr->op==TK_COLUMN
917    && pExpr->iColumn==p->iCol
918    && p->pTab==pExpr->y.pTab
919   ){
920     renameTokenFind(pWalker->pParse, p, (void*)pExpr);
921   }
922   return WRC_Continue;
923 }
924 
925 /*
926 ** The RenameCtx contains a list of tokens that reference a column that
927 ** is being renamed by an ALTER TABLE statement.  Return the "last"
928 ** RenameToken in the RenameCtx and remove that RenameToken from the
929 ** RenameContext.  "Last" means the last RenameToken encountered when
930 ** the input SQL is parsed from left to right.  Repeated calls to this routine
931 ** return all column name tokens in the order that they are encountered
932 ** in the SQL statement.
933 */
934 static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
935   RenameToken *pBest = pCtx->pList;
936   RenameToken *pToken;
937   RenameToken **pp;
938 
939   for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
940     if( pToken->t.z>pBest->t.z ) pBest = pToken;
941   }
942   for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
943   *pp = pBest->pNext;
944 
945   return pBest;
946 }
947 
948 /*
949 ** An error occured while parsing or otherwise processing a database
950 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
951 ** ALTER TABLE RENAME COLUMN program. The error message emitted by the
952 ** sub-routine is currently stored in pParse->zErrMsg. This function
953 ** adds context to the error message and then stores it in pCtx.
954 */
955 static void renameColumnParseError(
956   sqlite3_context *pCtx,
957   int bPost,
958   sqlite3_value *pType,
959   sqlite3_value *pObject,
960   Parse *pParse
961 ){
962   const char *zT = (const char*)sqlite3_value_text(pType);
963   const char *zN = (const char*)sqlite3_value_text(pObject);
964   char *zErr;
965 
966   zErr = sqlite3_mprintf("error in %s %s%s: %s",
967       zT, zN, (bPost ? " after rename" : ""),
968       pParse->zErrMsg
969   );
970   sqlite3_result_error(pCtx, zErr, -1);
971   sqlite3_free(zErr);
972 }
973 
974 /*
975 ** For each name in the the expression-list pEList (i.e. each
976 ** pEList->a[i].zName) that matches the string in zOld, extract the
977 ** corresponding rename-token from Parse object pParse and add it
978 ** to the RenameCtx pCtx.
979 */
980 static void renameColumnElistNames(
981   Parse *pParse,
982   RenameCtx *pCtx,
983   ExprList *pEList,
984   const char *zOld
985 ){
986   if( pEList ){
987     int i;
988     for(i=0; i<pEList->nExpr; i++){
989       char *zName = pEList->a[i].zEName;
990       if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
991        && ALWAYS(zName!=0)
992        && 0==sqlite3_stricmp(zName, zOld)
993       ){
994         renameTokenFind(pParse, pCtx, (void*)zName);
995       }
996     }
997   }
998 }
999 
1000 /*
1001 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
1002 ** that matches the string in zOld, extract the corresponding rename-token
1003 ** from Parse object pParse and add it to the RenameCtx pCtx.
1004 */
1005 static void renameColumnIdlistNames(
1006   Parse *pParse,
1007   RenameCtx *pCtx,
1008   IdList *pIdList,
1009   const char *zOld
1010 ){
1011   if( pIdList ){
1012     int i;
1013     for(i=0; i<pIdList->nId; i++){
1014       char *zName = pIdList->a[i].zName;
1015       if( 0==sqlite3_stricmp(zName, zOld) ){
1016         renameTokenFind(pParse, pCtx, (void*)zName);
1017       }
1018     }
1019   }
1020 }
1021 
1022 
1023 /*
1024 ** Parse the SQL statement zSql using Parse object (*p). The Parse object
1025 ** is initialized by this function before it is used.
1026 */
1027 static int renameParseSql(
1028   Parse *p,                       /* Memory to use for Parse object */
1029   const char *zDb,                /* Name of schema SQL belongs to */
1030   sqlite3 *db,                    /* Database handle */
1031   const char *zSql,               /* SQL to parse */
1032   int bTemp                       /* True if SQL is from temp schema */
1033 ){
1034   int rc;
1035   char *zErr = 0;
1036 
1037   db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
1038 
1039   /* Parse the SQL statement passed as the first argument. If no error
1040   ** occurs and the parse does not result in a new table, index or
1041   ** trigger object, the database must be corrupt. */
1042   memset(p, 0, sizeof(Parse));
1043   p->eParseMode = PARSE_MODE_RENAME;
1044   p->db = db;
1045   p->nQueryLoop = 1;
1046   rc = sqlite3RunParser(p, zSql, &zErr);
1047   assert( p->zErrMsg==0 );
1048   assert( rc!=SQLITE_OK || zErr==0 );
1049   p->zErrMsg = zErr;
1050   if( db->mallocFailed ) rc = SQLITE_NOMEM;
1051   if( rc==SQLITE_OK
1052    && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
1053   ){
1054     rc = SQLITE_CORRUPT_BKPT;
1055   }
1056 
1057 #ifdef SQLITE_DEBUG
1058   /* Ensure that all mappings in the Parse.pRename list really do map to
1059   ** a part of the input string.  */
1060   if( rc==SQLITE_OK ){
1061     int nSql = sqlite3Strlen30(zSql);
1062     RenameToken *pToken;
1063     for(pToken=p->pRename; pToken; pToken=pToken->pNext){
1064       assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
1065     }
1066   }
1067 #endif
1068 
1069   db->init.iDb = 0;
1070   return rc;
1071 }
1072 
1073 /*
1074 ** This function edits SQL statement zSql, replacing each token identified
1075 ** by the linked list pRename with the text of zNew. If argument bQuote is
1076 ** true, then zNew is always quoted first. If no error occurs, the result
1077 ** is loaded into context object pCtx as the result.
1078 **
1079 ** Or, if an error occurs (i.e. an OOM condition), an error is left in
1080 ** pCtx and an SQLite error code returned.
1081 */
1082 static int renameEditSql(
1083   sqlite3_context *pCtx,          /* Return result here */
1084   RenameCtx *pRename,             /* Rename context */
1085   const char *zSql,               /* SQL statement to edit */
1086   const char *zNew,               /* New token text */
1087   int bQuote                      /* True to always quote token */
1088 ){
1089   int nNew = sqlite3Strlen30(zNew);
1090   int nSql = sqlite3Strlen30(zSql);
1091   sqlite3 *db = sqlite3_context_db_handle(pCtx);
1092   int rc = SQLITE_OK;
1093   char *zQuot;
1094   char *zOut;
1095   int nQuot;
1096 
1097   /* Set zQuot to point to a buffer containing a quoted copy of the
1098   ** identifier zNew. If the corresponding identifier in the original
1099   ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
1100   ** point to zQuot so that all substitutions are made using the
1101   ** quoted version of the new column name.  */
1102   zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
1103   if( zQuot==0 ){
1104     return SQLITE_NOMEM;
1105   }else{
1106     nQuot = sqlite3Strlen30(zQuot);
1107   }
1108   if( bQuote ){
1109     zNew = zQuot;
1110     nNew = nQuot;
1111   }
1112 
1113   /* At this point pRename->pList contains a list of RenameToken objects
1114   ** corresponding to all tokens in the input SQL that must be replaced
1115   ** with the new column name. All that remains is to construct and
1116   ** return the edited SQL string. */
1117   assert( nQuot>=nNew );
1118   zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
1119   if( zOut ){
1120     int nOut = nSql;
1121     memcpy(zOut, zSql, nSql);
1122     while( pRename->pList ){
1123       int iOff;                   /* Offset of token to replace in zOut */
1124       RenameToken *pBest = renameColumnTokenNext(pRename);
1125 
1126       u32 nReplace;
1127       const char *zReplace;
1128       if( sqlite3IsIdChar(*pBest->t.z) ){
1129         nReplace = nNew;
1130         zReplace = zNew;
1131       }else{
1132         nReplace = nQuot;
1133         zReplace = zQuot;
1134       }
1135 
1136       iOff = pBest->t.z - zSql;
1137       if( pBest->t.n!=nReplace ){
1138         memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
1139             nOut - (iOff + pBest->t.n)
1140         );
1141         nOut += nReplace - pBest->t.n;
1142         zOut[nOut] = '\0';
1143       }
1144       memcpy(&zOut[iOff], zReplace, nReplace);
1145       sqlite3DbFree(db, pBest);
1146     }
1147 
1148     sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
1149     sqlite3DbFree(db, zOut);
1150   }else{
1151     rc = SQLITE_NOMEM;
1152   }
1153 
1154   sqlite3_free(zQuot);
1155   return rc;
1156 }
1157 
1158 /*
1159 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
1160 ** it was read from the schema of database zDb. Return SQLITE_OK if
1161 ** successful. Otherwise, return an SQLite error code and leave an error
1162 ** message in the Parse object.
1163 */
1164 static int renameResolveTrigger(Parse *pParse, const char *zDb){
1165   sqlite3 *db = pParse->db;
1166   Trigger *pNew = pParse->pNewTrigger;
1167   TriggerStep *pStep;
1168   NameContext sNC;
1169   int rc = SQLITE_OK;
1170 
1171   memset(&sNC, 0, sizeof(sNC));
1172   sNC.pParse = pParse;
1173   assert( pNew->pTabSchema );
1174   pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
1175       db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
1176   );
1177   pParse->eTriggerOp = pNew->op;
1178   /* ALWAYS() because if the table of the trigger does not exist, the
1179   ** error would have been hit before this point */
1180   if( ALWAYS(pParse->pTriggerTab) ){
1181     rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
1182   }
1183 
1184   /* Resolve symbols in WHEN clause */
1185   if( rc==SQLITE_OK && pNew->pWhen ){
1186     rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
1187   }
1188 
1189   for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
1190     if( pStep->pSelect ){
1191       sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
1192       if( pParse->nErr ) rc = pParse->rc;
1193     }
1194     if( rc==SQLITE_OK && pStep->zTarget ){
1195       Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
1196       if( pTarget==0 ){
1197         rc = SQLITE_ERROR;
1198       }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
1199         SrcList sSrc;
1200         memset(&sSrc, 0, sizeof(sSrc));
1201         sSrc.nSrc = 1;
1202         sSrc.a[0].zName = pStep->zTarget;
1203         sSrc.a[0].pTab = pTarget;
1204         sNC.pSrcList = &sSrc;
1205         if( pStep->pWhere ){
1206           rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
1207         }
1208         if( rc==SQLITE_OK ){
1209           rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
1210         }
1211         assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
1212         if( pStep->pUpsert ){
1213           Upsert *pUpsert = pStep->pUpsert;
1214           assert( rc==SQLITE_OK );
1215           pUpsert->pUpsertSrc = &sSrc;
1216           sNC.uNC.pUpsert = pUpsert;
1217           sNC.ncFlags = NC_UUpsert;
1218           rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
1219           if( rc==SQLITE_OK ){
1220             ExprList *pUpsertSet = pUpsert->pUpsertSet;
1221             rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
1222           }
1223           if( rc==SQLITE_OK ){
1224             rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
1225           }
1226           if( rc==SQLITE_OK ){
1227             rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
1228           }
1229           sNC.ncFlags = 0;
1230         }
1231         sNC.pSrcList = 0;
1232       }
1233     }
1234   }
1235   return rc;
1236 }
1237 
1238 /*
1239 ** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
1240 ** objects that are part of the trigger passed as the second argument.
1241 */
1242 static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
1243   TriggerStep *pStep;
1244 
1245   /* Find tokens to edit in WHEN clause */
1246   sqlite3WalkExpr(pWalker, pTrigger->pWhen);
1247 
1248   /* Find tokens to edit in trigger steps */
1249   for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1250     sqlite3WalkSelect(pWalker, pStep->pSelect);
1251     sqlite3WalkExpr(pWalker, pStep->pWhere);
1252     sqlite3WalkExprList(pWalker, pStep->pExprList);
1253     if( pStep->pUpsert ){
1254       Upsert *pUpsert = pStep->pUpsert;
1255       sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
1256       sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
1257       sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
1258       sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
1259     }
1260   }
1261 }
1262 
1263 /*
1264 ** Free the contents of Parse object (*pParse). Do not free the memory
1265 ** occupied by the Parse object itself.
1266 */
1267 static void renameParseCleanup(Parse *pParse){
1268   sqlite3 *db = pParse->db;
1269   Index *pIdx;
1270   if( pParse->pVdbe ){
1271     sqlite3VdbeFinalize(pParse->pVdbe);
1272   }
1273   sqlite3DeleteTable(db, pParse->pNewTable);
1274   while( (pIdx = pParse->pNewIndex)!=0 ){
1275     pParse->pNewIndex = pIdx->pNext;
1276     sqlite3FreeIndex(db, pIdx);
1277   }
1278   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
1279   sqlite3DbFree(db, pParse->zErrMsg);
1280   renameTokenFree(db, pParse->pRename);
1281   sqlite3ParserReset(pParse);
1282 }
1283 
1284 /*
1285 ** SQL function:
1286 **
1287 **     sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
1288 **
1289 **   0. zSql:     SQL statement to rewrite
1290 **   1. type:     Type of object ("table", "view" etc.)
1291 **   2. object:   Name of object
1292 **   3. Database: Database name (e.g. "main")
1293 **   4. Table:    Table name
1294 **   5. iCol:     Index of column to rename
1295 **   6. zNew:     New column name
1296 **   7. bQuote:   Non-zero if the new column name should be quoted.
1297 **   8. bTemp:    True if zSql comes from temp schema
1298 **
1299 ** Do a column rename operation on the CREATE statement given in zSql.
1300 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
1301 ** into zNew.  The name should be quoted if bQuote is true.
1302 **
1303 ** This function is used internally by the ALTER TABLE RENAME COLUMN command.
1304 ** It is only accessible to SQL created using sqlite3NestedParse().  It is
1305 ** not reachable from ordinary SQL passed into sqlite3_prepare().
1306 */
1307 static void renameColumnFunc(
1308   sqlite3_context *context,
1309   int NotUsed,
1310   sqlite3_value **argv
1311 ){
1312   sqlite3 *db = sqlite3_context_db_handle(context);
1313   RenameCtx sCtx;
1314   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1315   const char *zDb = (const char*)sqlite3_value_text(argv[3]);
1316   const char *zTable = (const char*)sqlite3_value_text(argv[4]);
1317   int iCol = sqlite3_value_int(argv[5]);
1318   const char *zNew = (const char*)sqlite3_value_text(argv[6]);
1319   int bQuote = sqlite3_value_int(argv[7]);
1320   int bTemp = sqlite3_value_int(argv[8]);
1321   const char *zOld;
1322   int rc;
1323   Parse sParse;
1324   Walker sWalker;
1325   Index *pIdx;
1326   int i;
1327   Table *pTab;
1328 #ifndef SQLITE_OMIT_AUTHORIZATION
1329   sqlite3_xauth xAuth = db->xAuth;
1330 #endif
1331 
1332   UNUSED_PARAMETER(NotUsed);
1333   if( zSql==0 ) return;
1334   if( zTable==0 ) return;
1335   if( zNew==0 ) return;
1336   if( iCol<0 ) return;
1337   sqlite3BtreeEnterAll(db);
1338   pTab = sqlite3FindTable(db, zTable, zDb);
1339   if( pTab==0 || iCol>=pTab->nCol ){
1340     sqlite3BtreeLeaveAll(db);
1341     return;
1342   }
1343   zOld = pTab->aCol[iCol].zName;
1344   memset(&sCtx, 0, sizeof(sCtx));
1345   sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
1346 
1347 #ifndef SQLITE_OMIT_AUTHORIZATION
1348   db->xAuth = 0;
1349 #endif
1350   rc = renameParseSql(&sParse, zDb, db, zSql, bTemp);
1351 
1352   /* Find tokens that need to be replaced. */
1353   memset(&sWalker, 0, sizeof(Walker));
1354   sWalker.pParse = &sParse;
1355   sWalker.xExprCallback = renameColumnExprCb;
1356   sWalker.xSelectCallback = renameColumnSelectCb;
1357   sWalker.u.pRename = &sCtx;
1358 
1359   sCtx.pTab = pTab;
1360   if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
1361   if( sParse.pNewTable ){
1362     Select *pSelect = sParse.pNewTable->pSelect;
1363     if( pSelect ){
1364       pSelect->selFlags &= ~SF_View;
1365       sParse.rc = SQLITE_OK;
1366       sqlite3SelectPrep(&sParse, pSelect, 0);
1367       rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
1368       if( rc==SQLITE_OK ){
1369         sqlite3WalkSelect(&sWalker, pSelect);
1370       }
1371       if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
1372     }else{
1373       /* A regular table */
1374       int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
1375       FKey *pFKey;
1376       assert( sParse.pNewTable->pSelect==0 );
1377       sCtx.pTab = sParse.pNewTable;
1378       if( bFKOnly==0 ){
1379         renameTokenFind(
1380             &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
1381         );
1382         if( sCtx.iCol<0 ){
1383           renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
1384         }
1385         sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
1386         for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
1387           sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1388         }
1389         for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
1390           sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
1391         }
1392       }
1393 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
1394       for(i=0; i<sParse.pNewTable->nCol; i++){
1395         sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
1396       }
1397 #endif
1398 
1399       for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1400         for(i=0; i<pFKey->nCol; i++){
1401           if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
1402             renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
1403           }
1404           if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
1405            && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
1406           ){
1407             renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
1408           }
1409         }
1410       }
1411     }
1412   }else if( sParse.pNewIndex ){
1413     sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
1414     sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
1415   }else{
1416     /* A trigger */
1417     TriggerStep *pStep;
1418     rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
1419     if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
1420 
1421     for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
1422       if( pStep->zTarget ){
1423         Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
1424         if( pTarget==pTab ){
1425           if( pStep->pUpsert ){
1426             ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
1427             renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
1428           }
1429           renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
1430           renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
1431         }
1432       }
1433     }
1434 
1435 
1436     /* Find tokens to edit in UPDATE OF clause */
1437     if( sParse.pTriggerTab==pTab ){
1438       renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
1439     }
1440 
1441     /* Find tokens to edit in various expressions and selects */
1442     renameWalkTrigger(&sWalker, sParse.pNewTrigger);
1443   }
1444 
1445   assert( rc==SQLITE_OK );
1446   rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
1447 
1448 renameColumnFunc_done:
1449   if( rc!=SQLITE_OK ){
1450     if( sParse.zErrMsg ){
1451       renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
1452     }else{
1453       sqlite3_result_error_code(context, rc);
1454     }
1455   }
1456 
1457   renameParseCleanup(&sParse);
1458   renameTokenFree(db, sCtx.pList);
1459 #ifndef SQLITE_OMIT_AUTHORIZATION
1460   db->xAuth = xAuth;
1461 #endif
1462   sqlite3BtreeLeaveAll(db);
1463 }
1464 
1465 /*
1466 ** Walker expression callback used by "RENAME TABLE".
1467 */
1468 static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
1469   RenameCtx *p = pWalker->u.pRename;
1470   if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
1471     renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
1472   }
1473   return WRC_Continue;
1474 }
1475 
1476 /*
1477 ** Walker select callback used by "RENAME TABLE".
1478 */
1479 static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
1480   int i;
1481   RenameCtx *p = pWalker->u.pRename;
1482   SrcList *pSrc = pSelect->pSrc;
1483   if( pSelect->selFlags & SF_View ) return WRC_Prune;
1484   if( pSrc==0 ){
1485     assert( pWalker->pParse->db->mallocFailed );
1486     return WRC_Abort;
1487   }
1488   for(i=0; i<pSrc->nSrc; i++){
1489     struct SrcList_item *pItem = &pSrc->a[i];
1490     if( pItem->pTab==p->pTab ){
1491       renameTokenFind(pWalker->pParse, p, pItem->zName);
1492     }
1493   }
1494   renameWalkWith(pWalker, pSelect);
1495 
1496   return WRC_Continue;
1497 }
1498 
1499 
1500 /*
1501 ** This C function implements an SQL user function that is used by SQL code
1502 ** generated by the ALTER TABLE ... RENAME command to modify the definition
1503 ** of any foreign key constraints that use the table being renamed as the
1504 ** parent table. It is passed three arguments:
1505 **
1506 **   0: The database containing the table being renamed.
1507 **   1. type:     Type of object ("table", "view" etc.)
1508 **   2. object:   Name of object
1509 **   3: The complete text of the schema statement being modified,
1510 **   4: The old name of the table being renamed, and
1511 **   5: The new name of the table being renamed.
1512 **   6: True if the schema statement comes from the temp db.
1513 **
1514 ** It returns the new schema statement. For example:
1515 **
1516 ** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
1517 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
1518 */
1519 static void renameTableFunc(
1520   sqlite3_context *context,
1521   int NotUsed,
1522   sqlite3_value **argv
1523 ){
1524   sqlite3 *db = sqlite3_context_db_handle(context);
1525   const char *zDb = (const char*)sqlite3_value_text(argv[0]);
1526   const char *zInput = (const char*)sqlite3_value_text(argv[3]);
1527   const char *zOld = (const char*)sqlite3_value_text(argv[4]);
1528   const char *zNew = (const char*)sqlite3_value_text(argv[5]);
1529   int bTemp = sqlite3_value_int(argv[6]);
1530   UNUSED_PARAMETER(NotUsed);
1531 
1532   if( zInput && zOld && zNew ){
1533     Parse sParse;
1534     int rc;
1535     int bQuote = 1;
1536     RenameCtx sCtx;
1537     Walker sWalker;
1538 
1539 #ifndef SQLITE_OMIT_AUTHORIZATION
1540     sqlite3_xauth xAuth = db->xAuth;
1541     db->xAuth = 0;
1542 #endif
1543 
1544     sqlite3BtreeEnterAll(db);
1545 
1546     memset(&sCtx, 0, sizeof(RenameCtx));
1547     sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
1548     memset(&sWalker, 0, sizeof(Walker));
1549     sWalker.pParse = &sParse;
1550     sWalker.xExprCallback = renameTableExprCb;
1551     sWalker.xSelectCallback = renameTableSelectCb;
1552     sWalker.u.pRename = &sCtx;
1553 
1554     rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
1555 
1556     if( rc==SQLITE_OK ){
1557       int isLegacy = (db->flags & SQLITE_LegacyAlter);
1558       if( sParse.pNewTable ){
1559         Table *pTab = sParse.pNewTable;
1560 
1561         if( pTab->pSelect ){
1562           if( isLegacy==0 ){
1563             Select *pSelect = pTab->pSelect;
1564             NameContext sNC;
1565             memset(&sNC, 0, sizeof(sNC));
1566             sNC.pParse = &sParse;
1567 
1568             assert( pSelect->selFlags & SF_View );
1569             pSelect->selFlags &= ~SF_View;
1570             sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
1571             if( sParse.nErr ){
1572               rc = sParse.rc;
1573             }else{
1574               sqlite3WalkSelect(&sWalker, pTab->pSelect);
1575             }
1576           }
1577         }else{
1578           /* Modify any FK definitions to point to the new table. */
1579 #ifndef SQLITE_OMIT_FOREIGN_KEY
1580           if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
1581             FKey *pFKey;
1582             for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1583               if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
1584                 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
1585               }
1586             }
1587           }
1588 #endif
1589 
1590           /* If this is the table being altered, fix any table refs in CHECK
1591           ** expressions. Also update the name that appears right after the
1592           ** "CREATE [VIRTUAL] TABLE" bit. */
1593           if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
1594             sCtx.pTab = pTab;
1595             if( isLegacy==0 ){
1596               sqlite3WalkExprList(&sWalker, pTab->pCheck);
1597             }
1598             renameTokenFind(&sParse, &sCtx, pTab->zName);
1599           }
1600         }
1601       }
1602 
1603       else if( sParse.pNewIndex ){
1604         renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
1605         if( isLegacy==0 ){
1606           sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
1607         }
1608       }
1609 
1610 #ifndef SQLITE_OMIT_TRIGGER
1611       else{
1612         Trigger *pTrigger = sParse.pNewTrigger;
1613         TriggerStep *pStep;
1614         if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
1615             && sCtx.pTab->pSchema==pTrigger->pTabSchema
1616           ){
1617           renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
1618         }
1619 
1620         if( isLegacy==0 ){
1621           rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
1622           if( rc==SQLITE_OK ){
1623             renameWalkTrigger(&sWalker, pTrigger);
1624             for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
1625               if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
1626                 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
1627               }
1628             }
1629           }
1630         }
1631       }
1632 #endif
1633     }
1634 
1635     if( rc==SQLITE_OK ){
1636       rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
1637     }
1638     if( rc!=SQLITE_OK ){
1639       if( sParse.zErrMsg ){
1640         renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
1641       }else{
1642         sqlite3_result_error_code(context, rc);
1643       }
1644     }
1645 
1646     renameParseCleanup(&sParse);
1647     renameTokenFree(db, sCtx.pList);
1648     sqlite3BtreeLeaveAll(db);
1649 #ifndef SQLITE_OMIT_AUTHORIZATION
1650     db->xAuth = xAuth;
1651 #endif
1652   }
1653 
1654   return;
1655 }
1656 
1657 /*
1658 ** An SQL user function that checks that there are no parse or symbol
1659 ** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
1660 ** After an ALTER TABLE .. RENAME operation is performed and the schema
1661 ** reloaded, this function is called on each SQL statement in the schema
1662 ** to ensure that it is still usable.
1663 **
1664 **   0: Database name ("main", "temp" etc.).
1665 **   1: SQL statement.
1666 **   2: Object type ("view", "table", "trigger" or "index").
1667 **   3: Object name.
1668 **   4: True if object is from temp schema.
1669 **
1670 ** Unless it finds an error, this function normally returns NULL. However, it
1671 ** returns integer value 1 if:
1672 **
1673 **   * the SQL argument creates a trigger, and
1674 **   * the table that the trigger is attached to is in database zDb.
1675 */
1676 static void renameTableTest(
1677   sqlite3_context *context,
1678   int NotUsed,
1679   sqlite3_value **argv
1680 ){
1681   sqlite3 *db = sqlite3_context_db_handle(context);
1682   char const *zDb = (const char*)sqlite3_value_text(argv[0]);
1683   char const *zInput = (const char*)sqlite3_value_text(argv[1]);
1684   int bTemp = sqlite3_value_int(argv[4]);
1685   int isLegacy = (db->flags & SQLITE_LegacyAlter);
1686 
1687 #ifndef SQLITE_OMIT_AUTHORIZATION
1688   sqlite3_xauth xAuth = db->xAuth;
1689   db->xAuth = 0;
1690 #endif
1691 
1692   UNUSED_PARAMETER(NotUsed);
1693   if( zDb && zInput ){
1694     int rc;
1695     Parse sParse;
1696     rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
1697     if( rc==SQLITE_OK ){
1698       if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
1699         NameContext sNC;
1700         memset(&sNC, 0, sizeof(sNC));
1701         sNC.pParse = &sParse;
1702         sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
1703         if( sParse.nErr ) rc = sParse.rc;
1704       }
1705 
1706       else if( sParse.pNewTrigger ){
1707         if( isLegacy==0 ){
1708           rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
1709         }
1710         if( rc==SQLITE_OK ){
1711           int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
1712           int i2 = sqlite3FindDbName(db, zDb);
1713           if( i1==i2 ) sqlite3_result_int(context, 1);
1714         }
1715       }
1716     }
1717 
1718     if( rc!=SQLITE_OK ){
1719       renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
1720     }
1721     renameParseCleanup(&sParse);
1722   }
1723 
1724 #ifndef SQLITE_OMIT_AUTHORIZATION
1725   db->xAuth = xAuth;
1726 #endif
1727 }
1728 
1729 /*
1730 ** Register built-in functions used to help implement ALTER TABLE
1731 */
1732 void sqlite3AlterFunctions(void){
1733   static FuncDef aAlterTableFuncs[] = {
1734     INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
1735     INTERNAL_FUNCTION(sqlite_rename_table,  7, renameTableFunc),
1736     INTERNAL_FUNCTION(sqlite_rename_test,   5, renameTableTest),
1737   };
1738   sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
1739 }
1740 #endif  /* SQLITE_ALTER_TABLE */
1741