xref: /sqlite-3.40.0/src/alter.c (revision 9f8a4b43)
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 ** $Id: alter.c,v 1.27 2007/06/27 17:09:24 danielk1977 Exp $
16 */
17 #include "sqliteInt.h"
18 #include <ctype.h>
19 
20 /*
21 ** The code in this file only exists if we are not omitting the
22 ** ALTER TABLE logic from the build.
23 */
24 #ifndef SQLITE_OMIT_ALTERTABLE
25 
26 
27 /*
28 ** This function is used by SQL generated to implement the
29 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
30 ** CREATE INDEX command. The second is a table name. The table name in
31 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
32 ** argument and the result returned. Examples:
33 **
34 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
35 **     -> 'CREATE TABLE def(a, b, c)'
36 **
37 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
38 **     -> 'CREATE INDEX i ON def(a, b, c)'
39 */
40 static void renameTableFunc(
41   sqlite3_context *context,
42   int argc,
43   sqlite3_value **argv
44 ){
45   unsigned char const *zSql = sqlite3_value_text(argv[0]);
46   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
47 
48   int token;
49   Token tname;
50   unsigned char const *zCsr = zSql;
51   int len = 0;
52   char *zRet;
53 
54   /* The principle used to locate the table name in the CREATE TABLE
55   ** statement is that the table name is the first token that is immediatedly
56   ** followed by a left parenthesis - TK_LP - or "USING" TK_USING.
57   */
58   if( zSql ){
59     do {
60       if( !*zCsr ){
61         /* Ran out of input before finding an opening bracket. Return NULL. */
62         return;
63       }
64 
65       /* Store the token that zCsr points to in tname. */
66       tname.z = zCsr;
67       tname.n = len;
68 
69       /* Advance zCsr to the next token. Store that token type in 'token',
70       ** and it's length in 'len' (to be used next iteration of this loop).
71       */
72       do {
73         zCsr += len;
74         len = sqlite3GetToken(zCsr, &token);
75       } while( token==TK_SPACE );
76       assert( len>0 );
77     } while( token!=TK_LP && token!=TK_USING );
78 
79     zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
80        zTableName, tname.z+tname.n);
81     sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
82   }
83 }
84 
85 #ifndef SQLITE_OMIT_TRIGGER
86 /* This function is used by SQL generated to implement the
87 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
88 ** statement. The second is a table name. The table name in the CREATE
89 ** TRIGGER statement is replaced with the third argument and the result
90 ** returned. This is analagous to renameTableFunc() above, except for CREATE
91 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
92 */
93 static void renameTriggerFunc(
94   sqlite3_context *context,
95   int argc,
96   sqlite3_value **argv
97 ){
98   unsigned char const *zSql = sqlite3_value_text(argv[0]);
99   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
100 
101   int token;
102   Token tname;
103   int dist = 3;
104   unsigned char const *zCsr = zSql;
105   int len = 0;
106   char *zRet;
107 
108   /* The principle used to locate the table name in the CREATE TRIGGER
109   ** statement is that the table name is the first token that is immediatedly
110   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
111   ** of TK_WHEN, TK_BEGIN or TK_FOR.
112   */
113   if( zSql ){
114     do {
115 
116       if( !*zCsr ){
117         /* Ran out of input before finding the table name. Return NULL. */
118         return;
119       }
120 
121       /* Store the token that zCsr points to in tname. */
122       tname.z = zCsr;
123       tname.n = len;
124 
125       /* Advance zCsr to the next token. Store that token type in 'token',
126       ** and it's length in 'len' (to be used next iteration of this loop).
127       */
128       do {
129         zCsr += len;
130         len = sqlite3GetToken(zCsr, &token);
131       }while( token==TK_SPACE );
132       assert( len>0 );
133 
134       /* Variable 'dist' stores the number of tokens read since the most
135       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
136       ** token is read and 'dist' equals 2, the condition stated above
137       ** to be met.
138       **
139       ** Note that ON cannot be a database, table or column name, so
140       ** there is no need to worry about syntax like
141       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
142       */
143       dist++;
144       if( token==TK_DOT || token==TK_ON ){
145         dist = 0;
146       }
147     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
148 
149     /* Variable tname now contains the token that is the old table-name
150     ** in the CREATE TRIGGER statement.
151     */
152     zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
153        zTableName, tname.z+tname.n);
154     sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
155   }
156 }
157 #endif   /* !SQLITE_OMIT_TRIGGER */
158 
159 /*
160 ** Register built-in functions used to help implement ALTER TABLE
161 */
162 void sqlite3AlterFunctions(sqlite3 *db){
163   static const struct {
164      char *zName;
165      signed char nArg;
166      void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
167   } aFuncs[] = {
168     { "sqlite_rename_table",    2, renameTableFunc},
169 #ifndef SQLITE_OMIT_TRIGGER
170     { "sqlite_rename_trigger",  2, renameTriggerFunc},
171 #endif
172   };
173   int i;
174 
175   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
176     sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
177         SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
178   }
179 }
180 
181 /*
182 ** Generate the text of a WHERE expression which can be used to select all
183 ** temporary triggers on table pTab from the sqlite_temp_master table. If
184 ** table pTab has no temporary triggers, or is itself stored in the
185 ** temporary database, NULL is returned.
186 */
187 static char *whereTempTriggers(Parse *pParse, Table *pTab){
188   Trigger *pTrig;
189   char *zWhere = 0;
190   char *tmp = 0;
191   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
192 
193   /* If the table is not located in the temp-db (in which case NULL is
194   ** returned, loop through the tables list of triggers. For each trigger
195   ** that is not part of the temp-db schema, add a clause to the WHERE
196   ** expression being built up in zWhere.
197   */
198   if( pTab->pSchema!=pTempSchema ){
199     for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
200       if( pTrig->pSchema==pTempSchema ){
201         if( !zWhere ){
202           zWhere = sqlite3MPrintf("name=%Q", pTrig->name);
203         }else{
204           tmp = zWhere;
205           zWhere = sqlite3MPrintf("%s OR name=%Q", zWhere, pTrig->name);
206           sqliteFree(tmp);
207         }
208       }
209     }
210   }
211   return zWhere;
212 }
213 
214 /*
215 ** Generate code to drop and reload the internal representation of table
216 ** pTab from the database, including triggers and temporary triggers.
217 ** Argument zName is the name of the table in the database schema at
218 ** the time the generated code is executed. This can be different from
219 ** pTab->zName if this function is being called to code part of an
220 ** "ALTER TABLE RENAME TO" statement.
221 */
222 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
223   Vdbe *v;
224   char *zWhere;
225   int iDb;                   /* Index of database containing pTab */
226 #ifndef SQLITE_OMIT_TRIGGER
227   Trigger *pTrig;
228 #endif
229 
230   v = sqlite3GetVdbe(pParse);
231   if( !v ) return;
232   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
233   assert( iDb>=0 );
234 
235 #ifndef SQLITE_OMIT_TRIGGER
236   /* Drop any table triggers from the internal schema. */
237   for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
238     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
239     assert( iTrigDb==iDb || iTrigDb==1 );
240     sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0);
241   }
242 #endif
243 
244   /* Drop the table and index from the internal schema */
245   sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
246 
247   /* Reload the table, index and permanent trigger schemas. */
248   zWhere = sqlite3MPrintf("tbl_name=%Q", zName);
249   if( !zWhere ) return;
250   sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
251 
252 #ifndef SQLITE_OMIT_TRIGGER
253   /* Now, if the table is not stored in the temp database, reload any temp
254   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
255   */
256   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
257     sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC);
258   }
259 #endif
260 }
261 
262 /*
263 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
264 ** command.
265 */
266 void sqlite3AlterRenameTable(
267   Parse *pParse,            /* Parser context. */
268   SrcList *pSrc,            /* The table to rename. */
269   Token *pName              /* The new table name. */
270 ){
271   int iDb;                  /* Database that contains the table */
272   char *zDb;                /* Name of database iDb */
273   Table *pTab;              /* Table being renamed */
274   char *zName = 0;          /* NULL-terminated version of pName */
275   sqlite3 *db = pParse->db; /* Database connection */
276   int nTabName;             /* Number of UTF-8 characters in zTabName */
277   const char *zTabName;     /* Original name of the table */
278   Vdbe *v;
279 #ifndef SQLITE_OMIT_TRIGGER
280   char *zWhere = 0;         /* Where clause to locate temp triggers */
281 #endif
282   int isVirtualRename = 0;  /* True if this is a v-table with an xRename() */
283 
284   if( sqlite3MallocFailed() ) goto exit_rename_table;
285   assert( pSrc->nSrc==1 );
286 
287   pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
288   if( !pTab ) goto exit_rename_table;
289   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
290   zDb = db->aDb[iDb].zName;
291 
292   /* Get a NULL terminated version of the new table name. */
293   zName = sqlite3NameFromToken(pName);
294   if( !zName ) goto exit_rename_table;
295 
296   /* Check that a table or index named 'zName' does not already exist
297   ** in database iDb. If so, this is an error.
298   */
299   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
300     sqlite3ErrorMsg(pParse,
301         "there is already another table or index with this name: %s", zName);
302     goto exit_rename_table;
303   }
304 
305   /* Make sure it is not a system table being altered, or a reserved name
306   ** that the table is being renamed to.
307   */
308   if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
309     sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
310     goto exit_rename_table;
311   }
312   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
313     goto exit_rename_table;
314   }
315 
316 #ifndef SQLITE_OMIT_AUTHORIZATION
317   /* Invoke the authorization callback. */
318   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
319     goto exit_rename_table;
320   }
321 #endif
322 
323 #ifndef SQLITE_OMIT_VIRTUALTABLE
324   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
325     goto exit_rename_table;
326   }
327   if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
328     isVirtualRename = 1;
329   }
330 #endif
331 
332   /* Begin a transaction and code the VerifyCookie for database iDb.
333   ** Then modify the schema cookie (since the ALTER TABLE modifies the
334   ** schema). Open a statement transaction if the table is a virtual
335   ** table.
336   */
337   v = sqlite3GetVdbe(pParse);
338   if( v==0 ){
339     goto exit_rename_table;
340   }
341   sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
342   sqlite3ChangeCookie(db, v, iDb);
343 
344   /* If this is a virtual table, invoke the xRename() function if
345   ** one is defined. The xRename() callback will modify the names
346   ** of any resources used by the v-table implementation (including other
347   ** SQLite tables) that are identified by the name of the virtual table.
348   */
349 #ifndef SQLITE_OMIT_VIRTUALTABLE
350   if( isVirtualRename ){
351     sqlite3VdbeOp3(v, OP_String8, 0, 0, zName, 0);
352     sqlite3VdbeOp3(v, OP_VRename, 0, 0, (const char*)pTab->pVtab, P3_VTAB);
353   }
354 #endif
355 
356   /* figure out how many UTF-8 characters are in zName */
357   zTabName = pTab->zName;
358   nTabName = sqlite3Utf8CharLen(zTabName, -1);
359 
360   /* Modify the sqlite_master table to use the new table name. */
361   sqlite3NestedParse(pParse,
362       "UPDATE %Q.%s SET "
363 #ifdef SQLITE_OMIT_TRIGGER
364           "sql = sqlite_rename_table(sql, %Q), "
365 #else
366           "sql = CASE "
367             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
368             "ELSE sqlite_rename_table(sql, %Q) END, "
369 #endif
370           "tbl_name = %Q, "
371           "name = CASE "
372             "WHEN type='table' THEN %Q "
373             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
374              "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) "
375             "ELSE name END "
376       "WHERE tbl_name=%Q AND "
377           "(type='table' OR type='index' OR type='trigger');",
378       zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
379 #ifndef SQLITE_OMIT_TRIGGER
380       zName,
381 #endif
382       zName, nTabName, zTabName
383   );
384 
385 #ifndef SQLITE_OMIT_AUTOINCREMENT
386   /* If the sqlite_sequence table exists in this database, then update
387   ** it with the new table name.
388   */
389   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
390     sqlite3NestedParse(pParse,
391         "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
392         zDb, zName, pTab->zName);
393   }
394 #endif
395 
396 #ifndef SQLITE_OMIT_TRIGGER
397   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
398   ** table. Don't do this if the table being ALTERed is itself located in
399   ** the temp database.
400   */
401   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
402     sqlite3NestedParse(pParse,
403         "UPDATE sqlite_temp_master SET "
404             "sql = sqlite_rename_trigger(sql, %Q), "
405             "tbl_name = %Q "
406             "WHERE %s;", zName, zName, zWhere);
407     sqliteFree(zWhere);
408   }
409 #endif
410 
411   /* Drop and reload the internal table schema. */
412   reloadTableSchema(pParse, pTab, zName);
413 
414 exit_rename_table:
415   sqlite3SrcListDelete(pSrc);
416   sqliteFree(zName);
417 }
418 
419 
420 /*
421 ** This function is called after an "ALTER TABLE ... ADD" statement
422 ** has been parsed. Argument pColDef contains the text of the new
423 ** column definition.
424 **
425 ** The Table structure pParse->pNewTable was extended to include
426 ** the new column during parsing.
427 */
428 void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
429   Table *pNew;              /* Copy of pParse->pNewTable */
430   Table *pTab;              /* Table being altered */
431   int iDb;                  /* Database number */
432   const char *zDb;          /* Database name */
433   const char *zTab;         /* Table name */
434   char *zCol;               /* Null-terminated column definition */
435   Column *pCol;             /* The new column */
436   Expr *pDflt;              /* Default value for the new column */
437 
438   if( pParse->nErr ) return;
439   pNew = pParse->pNewTable;
440   assert( pNew );
441 
442   iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
443   zDb = pParse->db->aDb[iDb].zName;
444   zTab = pNew->zName;
445   pCol = &pNew->aCol[pNew->nCol-1];
446   pDflt = pCol->pDflt;
447   pTab = sqlite3FindTable(pParse->db, zTab, zDb);
448   assert( pTab );
449 
450 #ifndef SQLITE_OMIT_AUTHORIZATION
451   /* Invoke the authorization callback. */
452   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
453     return;
454   }
455 #endif
456 
457   /* If the default value for the new column was specified with a
458   ** literal NULL, then set pDflt to 0. This simplifies checking
459   ** for an SQL NULL default below.
460   */
461   if( pDflt && pDflt->op==TK_NULL ){
462     pDflt = 0;
463   }
464 
465   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
466   ** If there is a NOT NULL constraint, then the default value for the
467   ** column must not be NULL.
468   */
469   if( pCol->isPrimKey ){
470     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
471     return;
472   }
473   if( pNew->pIndex ){
474     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
475     return;
476   }
477   if( pCol->notNull && !pDflt ){
478     sqlite3ErrorMsg(pParse,
479         "Cannot add a NOT NULL column with default value NULL");
480     return;
481   }
482 
483   /* Ensure the default expression is something that sqlite3ValueFromExpr()
484   ** can handle (i.e. not CURRENT_TIME etc.)
485   */
486   if( pDflt ){
487     sqlite3_value *pVal;
488     if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
489       /* malloc() has failed */
490       return;
491     }
492     if( !pVal ){
493       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
494       return;
495     }
496     sqlite3ValueFree(pVal);
497   }
498 
499   /* Modify the CREATE TABLE statement. */
500   zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
501   if( zCol ){
502     char *zEnd = &zCol[pColDef->n-1];
503     while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
504       *zEnd-- = '\0';
505     }
506     sqlite3NestedParse(pParse,
507         "UPDATE %Q.%s SET "
508           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
509         "WHERE type = 'table' AND name = %Q",
510       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
511       zTab
512     );
513     sqliteFree(zCol);
514   }
515 
516   /* If the default value of the new column is NULL, then set the file
517   ** format to 2. If the default value of the new column is not NULL,
518   ** the file format becomes 3.
519   */
520   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
521 
522   /* Reload the schema of the modified table. */
523   reloadTableSchema(pParse, pTab, pTab->zName);
524 }
525 
526 /*
527 ** This function is called by the parser after the table-name in
528 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
529 ** pSrc is the full-name of the table being altered.
530 **
531 ** This routine makes a (partial) copy of the Table structure
532 ** for the table being altered and sets Parse.pNewTable to point
533 ** to it. Routines called by the parser as the column definition
534 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
535 ** the copy. The copy of the Table structure is deleted by tokenize.c
536 ** after parsing is finished.
537 **
538 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
539 ** coding the "ALTER TABLE ... ADD" statement.
540 */
541 void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
542   Table *pNew;
543   Table *pTab;
544   Vdbe *v;
545   int iDb;
546   int i;
547   int nAlloc;
548 
549   /* Look up the table being altered. */
550   assert( pParse->pNewTable==0 );
551   if( sqlite3MallocFailed() ) goto exit_begin_add_column;
552   pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
553   if( !pTab ) goto exit_begin_add_column;
554 
555 #ifndef SQLITE_OMIT_VIRTUALTABLE
556   if( IsVirtual(pTab) ){
557     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
558     goto exit_begin_add_column;
559   }
560 #endif
561 
562   /* Make sure this is not an attempt to ALTER a view. */
563   if( pTab->pSelect ){
564     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
565     goto exit_begin_add_column;
566   }
567 
568   assert( pTab->addColOffset>0 );
569   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
570 
571   /* Put a copy of the Table struct in Parse.pNewTable for the
572   ** sqlite3AddColumn() function and friends to modify.
573   */
574   pNew = (Table *)sqliteMalloc(sizeof(Table));
575   if( !pNew ) goto exit_begin_add_column;
576   pParse->pNewTable = pNew;
577   pNew->nRef = 1;
578   pNew->nCol = pTab->nCol;
579   assert( pNew->nCol>0 );
580   nAlloc = (((pNew->nCol-1)/8)*8)+8;
581   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
582   pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
583   pNew->zName = sqliteStrDup(pTab->zName);
584   if( !pNew->aCol || !pNew->zName ){
585     goto exit_begin_add_column;
586   }
587   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
588   for(i=0; i<pNew->nCol; i++){
589     Column *pCol = &pNew->aCol[i];
590     pCol->zName = sqliteStrDup(pCol->zName);
591     pCol->zColl = 0;
592     pCol->zType = 0;
593     pCol->pDflt = 0;
594   }
595   pNew->pSchema = pParse->db->aDb[iDb].pSchema;
596   pNew->addColOffset = pTab->addColOffset;
597   pNew->nRef = 1;
598 
599   /* Begin a transaction and increment the schema cookie.  */
600   sqlite3BeginWriteOperation(pParse, 0, iDb);
601   v = sqlite3GetVdbe(pParse);
602   if( !v ) goto exit_begin_add_column;
603   sqlite3ChangeCookie(pParse->db, v, iDb);
604 
605 exit_begin_add_column:
606   sqlite3SrcListDelete(pSrc);
607   return;
608 }
609 #endif  /* SQLITE_ALTER_TABLE */
610