alter.c

Go to the documentation of this file.
00001 /*
00002 ** 2005 February 15
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains C code routines that used to generate VDBE code
00013 ** that implements the ALTER TABLE command.
00014 **
00015 ** $Id: alter.c,v 1.49 2008/10/30 17:21:13 danielk1977 Exp $
00016 */
00017 #include "sqliteInt.h"
00018 #include <ctype.h>
00019 
00020 /*
00021 ** The code in this file only exists if we are not omitting the
00022 ** ALTER TABLE logic from the build.
00023 */
00024 #ifndef SQLITE_OMIT_ALTERTABLE
00025 
00026 
00027 /*
00028 ** This function is used by SQL generated to implement the 
00029 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
00030 ** CREATE INDEX command. The second is a table name. The table name in 
00031 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
00032 ** argument and the result returned. Examples:
00033 **
00034 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
00035 **     -> 'CREATE TABLE def(a, b, c)'
00036 **
00037 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
00038 **     -> 'CREATE INDEX i ON def(a, b, c)'
00039 */
00040 static void renameTableFunc(
00041   sqlite3_context *context,
00042   int argc,
00043   sqlite3_value **argv
00044 ){
00045   unsigned char const *zSql = sqlite3_value_text(argv[0]);
00046   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
00047 
00048   int token;
00049   Token tname;
00050   unsigned char const *zCsr = zSql;
00051   int len = 0;
00052   char *zRet;
00053 
00054   sqlite3 *db = sqlite3_context_db_handle(context);
00055 
00056   /* The principle used to locate the table name in the CREATE TABLE 
00057   ** statement is that the table name is the first non-space token that
00058   ** is immediately followed by a TK_LP or TK_USING token.
00059   */
00060   if( zSql ){
00061     do {
00062       if( !*zCsr ){
00063         /* Ran out of input before finding an opening bracket. Return NULL. */
00064         return;
00065       }
00066 
00067       /* Store the token that zCsr points to in tname. */
00068       tname.z = zCsr;
00069       tname.n = len;
00070 
00071       /* Advance zCsr to the next token. Store that token type in 'token',
00072       ** and its length in 'len' (to be used next iteration of this loop).
00073       */
00074       do {
00075         zCsr += len;
00076         len = sqlite3GetToken(zCsr, &token);
00077       } while( token==TK_SPACE );
00078       assert( len>0 );
00079     } while( token!=TK_LP && token!=TK_USING );
00080 
00081     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
00082        zTableName, tname.z+tname.n);
00083     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
00084   }
00085 }
00086 
00087 #ifndef SQLITE_OMIT_TRIGGER
00088 /* This function is used by SQL generated to implement the
00089 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
00090 ** statement. The second is a table name. The table name in the CREATE 
00091 ** TRIGGER statement is replaced with the third argument and the result 
00092 ** returned. This is analagous to renameTableFunc() above, except for CREATE
00093 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
00094 */
00095 static void renameTriggerFunc(
00096   sqlite3_context *context,
00097   int argc,
00098   sqlite3_value **argv
00099 ){
00100   unsigned char const *zSql = sqlite3_value_text(argv[0]);
00101   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
00102 
00103   int token;
00104   Token tname;
00105   int dist = 3;
00106   unsigned char const *zCsr = zSql;
00107   int len = 0;
00108   char *zRet;
00109 
00110   sqlite3 *db = sqlite3_context_db_handle(context);
00111 
00112   /* The principle used to locate the table name in the CREATE TRIGGER 
00113   ** statement is that the table name is the first token that is immediatedly
00114   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
00115   ** of TK_WHEN, TK_BEGIN or TK_FOR.
00116   */
00117   if( zSql ){
00118     do {
00119 
00120       if( !*zCsr ){
00121         /* Ran out of input before finding the table name. Return NULL. */
00122         return;
00123       }
00124 
00125       /* Store the token that zCsr points to in tname. */
00126       tname.z = zCsr;
00127       tname.n = len;
00128 
00129       /* Advance zCsr to the next token. Store that token type in 'token',
00130       ** and its length in 'len' (to be used next iteration of this loop).
00131       */
00132       do {
00133         zCsr += len;
00134         len = sqlite3GetToken(zCsr, &token);
00135       }while( token==TK_SPACE );
00136       assert( len>0 );
00137 
00138       /* Variable 'dist' stores the number of tokens read since the most
00139       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
00140       ** token is read and 'dist' equals 2, the condition stated above
00141       ** to be met.
00142       **
00143       ** Note that ON cannot be a database, table or column name, so
00144       ** there is no need to worry about syntax like 
00145       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
00146       */
00147       dist++;
00148       if( token==TK_DOT || token==TK_ON ){
00149         dist = 0;
00150       }
00151     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
00152 
00153     /* Variable tname now contains the token that is the old table-name
00154     ** in the CREATE TRIGGER statement.
00155     */
00156     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
00157        zTableName, tname.z+tname.n);
00158     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
00159   }
00160 }
00161 #endif   /* !SQLITE_OMIT_TRIGGER */
00162 
00163 /*
00164 ** Register built-in functions used to help implement ALTER TABLE
00165 */
00166 void sqlite3AlterFunctions(sqlite3 *db){
00167   sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
00168                          renameTableFunc, 0, 0);
00169 #ifndef SQLITE_OMIT_TRIGGER
00170   sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
00171                          renameTriggerFunc, 0, 0);
00172 #endif
00173 }
00174 
00175 /*
00176 ** Generate the text of a WHERE expression which can be used to select all
00177 ** temporary triggers on table pTab from the sqlite_temp_master table. If
00178 ** table pTab has no temporary triggers, or is itself stored in the 
00179 ** temporary database, NULL is returned.
00180 */
00181 static char *whereTempTriggers(Parse *pParse, Table *pTab){
00182   Trigger *pTrig;
00183   char *zWhere = 0;
00184   char *tmp = 0;
00185   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
00186 
00187   /* If the table is not located in the temp-db (in which case NULL is 
00188   ** returned, loop through the tables list of triggers. For each trigger
00189   ** that is not part of the temp-db schema, add a clause to the WHERE 
00190   ** expression being built up in zWhere.
00191   */
00192   if( pTab->pSchema!=pTempSchema ){
00193     sqlite3 *db = pParse->db;
00194     for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
00195       if( pTrig->pSchema==pTempSchema ){
00196         if( !zWhere ){
00197           zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
00198         }else{
00199           tmp = zWhere;
00200           zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
00201           sqlite3DbFree(db, tmp);
00202         }
00203       }
00204     }
00205   }
00206   return zWhere;
00207 }
00208 
00209 /*
00210 ** Generate code to drop and reload the internal representation of table
00211 ** pTab from the database, including triggers and temporary triggers.
00212 ** Argument zName is the name of the table in the database schema at
00213 ** the time the generated code is executed. This can be different from
00214 ** pTab->zName if this function is being called to code part of an 
00215 ** "ALTER TABLE RENAME TO" statement.
00216 */
00217 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
00218   Vdbe *v;
00219   char *zWhere;
00220   int iDb;                   /* Index of database containing pTab */
00221 #ifndef SQLITE_OMIT_TRIGGER
00222   Trigger *pTrig;
00223 #endif
00224 
00225   v = sqlite3GetVdbe(pParse);
00226   if( !v ) return;
00227   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
00228   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
00229   assert( iDb>=0 );
00230 
00231 #ifndef SQLITE_OMIT_TRIGGER
00232   /* Drop any table triggers from the internal schema. */
00233   for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
00234     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
00235     assert( iTrigDb==iDb || iTrigDb==1 );
00236     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->name, 0);
00237   }
00238 #endif
00239 
00240   /* Drop the table and index from the internal schema */
00241   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
00242 
00243   /* Reload the table, index and permanent trigger schemas. */
00244   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
00245   if( !zWhere ) return;
00246   sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
00247 
00248 #ifndef SQLITE_OMIT_TRIGGER
00249   /* Now, if the table is not stored in the temp database, reload any temp 
00250   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
00251   */
00252   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
00253     sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
00254   }
00255 #endif
00256 }
00257 
00258 /*
00259 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
00260 ** command. 
00261 */
00262 void sqlite3AlterRenameTable(
00263   Parse *pParse,            /* Parser context. */
00264   SrcList *pSrc,            /* The table to rename. */
00265   Token *pName              /* The new table name. */
00266 ){
00267   int iDb;                  /* Database that contains the table */
00268   char *zDb;                /* Name of database iDb */
00269   Table *pTab;              /* Table being renamed */
00270   char *zName = 0;          /* NULL-terminated version of pName */ 
00271   sqlite3 *db = pParse->db; /* Database connection */
00272   int nTabName;             /* Number of UTF-8 characters in zTabName */
00273   const char *zTabName;     /* Original name of the table */
00274   Vdbe *v;
00275 #ifndef SQLITE_OMIT_TRIGGER
00276   char *zWhere = 0;         /* Where clause to locate temp triggers */
00277 #endif
00278   int isVirtualRename = 0;  /* True if this is a v-table with an xRename() */
00279   
00280   if( db->mallocFailed ) goto exit_rename_table;
00281   assert( pSrc->nSrc==1 );
00282   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
00283 
00284   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
00285   if( !pTab ) goto exit_rename_table;
00286   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
00287   zDb = db->aDb[iDb].zName;
00288 
00289   /* Get a NULL terminated version of the new table name. */
00290   zName = sqlite3NameFromToken(db, pName);
00291   if( !zName ) goto exit_rename_table;
00292 
00293   /* Check that a table or index named 'zName' does not already exist
00294   ** in database iDb. If so, this is an error.
00295   */
00296   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
00297     sqlite3ErrorMsg(pParse, 
00298         "there is already another table or index with this name: %s", zName);
00299     goto exit_rename_table;
00300   }
00301 
00302   /* Make sure it is not a system table being altered, or a reserved name
00303   ** that the table is being renamed to.
00304   */
00305   if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
00306     sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
00307     goto exit_rename_table;
00308   }
00309   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
00310     goto exit_rename_table;
00311   }
00312 
00313 #ifndef SQLITE_OMIT_VIEW
00314   if( pTab->pSelect ){
00315     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
00316     goto exit_rename_table;
00317   }
00318 #endif
00319 
00320 #ifndef SQLITE_OMIT_AUTHORIZATION
00321   /* Invoke the authorization callback. */
00322   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
00323     goto exit_rename_table;
00324   }
00325 #endif
00326 
00327 #ifndef SQLITE_OMIT_VIRTUALTABLE
00328   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
00329     goto exit_rename_table;
00330   }
00331   if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
00332     isVirtualRename = 1;
00333   }
00334 #endif
00335 
00336   /* Begin a transaction and code the VerifyCookie for database iDb. 
00337   ** Then modify the schema cookie (since the ALTER TABLE modifies the
00338   ** schema). Open a statement transaction if the table is a virtual
00339   ** table.
00340   */
00341   v = sqlite3GetVdbe(pParse);
00342   if( v==0 ){
00343     goto exit_rename_table;
00344   }
00345   sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
00346   sqlite3ChangeCookie(pParse, iDb);
00347 
00348   /* If this is a virtual table, invoke the xRename() function if
00349   ** one is defined. The xRename() callback will modify the names
00350   ** of any resources used by the v-table implementation (including other
00351   ** SQLite tables) that are identified by the name of the virtual table.
00352   */
00353 #ifndef SQLITE_OMIT_VIRTUALTABLE
00354   if( isVirtualRename ){
00355     int i = ++pParse->nMem;
00356     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
00357     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pTab->pVtab, P4_VTAB);
00358   }
00359 #endif
00360 
00361   /* figure out how many UTF-8 characters are in zName */
00362   zTabName = pTab->zName;
00363   nTabName = sqlite3Utf8CharLen(zTabName, -1);
00364 
00365   /* Modify the sqlite_master table to use the new table name. */
00366   sqlite3NestedParse(pParse,
00367       "UPDATE %Q.%s SET "
00368 #ifdef SQLITE_OMIT_TRIGGER
00369           "sql = sqlite_rename_table(sql, %Q), "
00370 #else
00371           "sql = CASE "
00372             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
00373             "ELSE sqlite_rename_table(sql, %Q) END, "
00374 #endif
00375           "tbl_name = %Q, "
00376           "name = CASE "
00377             "WHEN type='table' THEN %Q "
00378             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
00379              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
00380             "ELSE name END "
00381       "WHERE tbl_name=%Q AND "
00382           "(type='table' OR type='index' OR type='trigger');", 
00383       zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
00384 #ifndef SQLITE_OMIT_TRIGGER
00385       zName,
00386 #endif
00387       zName, nTabName, zTabName
00388   );
00389 
00390 #ifndef SQLITE_OMIT_AUTOINCREMENT
00391   /* If the sqlite_sequence table exists in this database, then update 
00392   ** it with the new table name.
00393   */
00394   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
00395     sqlite3NestedParse(pParse,
00396         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
00397         zDb, zName, pTab->zName);
00398   }
00399 #endif
00400 
00401 #ifndef SQLITE_OMIT_TRIGGER
00402   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
00403   ** table. Don't do this if the table being ALTERed is itself located in
00404   ** the temp database.
00405   */
00406   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
00407     sqlite3NestedParse(pParse, 
00408         "UPDATE sqlite_temp_master SET "
00409             "sql = sqlite_rename_trigger(sql, %Q), "
00410             "tbl_name = %Q "
00411             "WHERE %s;", zName, zName, zWhere);
00412     sqlite3DbFree(db, zWhere);
00413   }
00414 #endif
00415 
00416   /* Drop and reload the internal table schema. */
00417   reloadTableSchema(pParse, pTab, zName);
00418 
00419 exit_rename_table:
00420   sqlite3SrcListDelete(db, pSrc);
00421   sqlite3DbFree(db, zName);
00422 }
00423 
00424 
00425 /*
00426 ** This function is called after an "ALTER TABLE ... ADD" statement
00427 ** has been parsed. Argument pColDef contains the text of the new
00428 ** column definition.
00429 **
00430 ** The Table structure pParse->pNewTable was extended to include
00431 ** the new column during parsing.
00432 */
00433 void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
00434   Table *pNew;              /* Copy of pParse->pNewTable */
00435   Table *pTab;              /* Table being altered */
00436   int iDb;                  /* Database number */
00437   const char *zDb;          /* Database name */
00438   const char *zTab;         /* Table name */
00439   char *zCol;               /* Null-terminated column definition */
00440   Column *pCol;             /* The new column */
00441   Expr *pDflt;              /* Default value for the new column */
00442   sqlite3 *db;              /* The database connection; */
00443 
00444   db = pParse->db;
00445   if( pParse->nErr || db->mallocFailed ) return;
00446   pNew = pParse->pNewTable;
00447   assert( pNew );
00448 
00449   assert( sqlite3BtreeHoldsAllMutexes(db) );
00450   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
00451   zDb = db->aDb[iDb].zName;
00452   zTab = pNew->zName;
00453   pCol = &pNew->aCol[pNew->nCol-1];
00454   pDflt = pCol->pDflt;
00455   pTab = sqlite3FindTable(db, zTab, zDb);
00456   assert( pTab );
00457 
00458 #ifndef SQLITE_OMIT_AUTHORIZATION
00459   /* Invoke the authorization callback. */
00460   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
00461     return;
00462   }
00463 #endif
00464 
00465   /* If the default value for the new column was specified with a 
00466   ** literal NULL, then set pDflt to 0. This simplifies checking
00467   ** for an SQL NULL default below.
00468   */
00469   if( pDflt && pDflt->op==TK_NULL ){
00470     pDflt = 0;
00471   }
00472 
00473   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
00474   ** If there is a NOT NULL constraint, then the default value for the
00475   ** column must not be NULL.
00476   */
00477   if( pCol->isPrimKey ){
00478     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
00479     return;
00480   }
00481   if( pNew->pIndex ){
00482     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
00483     return;
00484   }
00485   if( pCol->notNull && !pDflt ){
00486     sqlite3ErrorMsg(pParse, 
00487         "Cannot add a NOT NULL column with default value NULL");
00488     return;
00489   }
00490 
00491   /* Ensure the default expression is something that sqlite3ValueFromExpr()
00492   ** can handle (i.e. not CURRENT_TIME etc.)
00493   */
00494   if( pDflt ){
00495     sqlite3_value *pVal;
00496     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
00497       db->mallocFailed = 1;
00498       return;
00499     }
00500     if( !pVal ){
00501       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
00502       return;
00503     }
00504     sqlite3ValueFree(pVal);
00505   }
00506 
00507   /* Modify the CREATE TABLE statement. */
00508   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
00509   if( zCol ){
00510     char *zEnd = &zCol[pColDef->n-1];
00511     while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
00512       *zEnd-- = '\0';
00513     }
00514     sqlite3NestedParse(pParse, 
00515         "UPDATE \"%w\".%s SET "
00516           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
00517         "WHERE type = 'table' AND name = %Q", 
00518       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
00519       zTab
00520     );
00521     sqlite3DbFree(db, zCol);
00522   }
00523 
00524   /* If the default value of the new column is NULL, then set the file
00525   ** format to 2. If the default value of the new column is not NULL,
00526   ** the file format becomes 3.
00527   */
00528   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
00529 
00530   /* Reload the schema of the modified table. */
00531   reloadTableSchema(pParse, pTab, pTab->zName);
00532 }
00533 
00534 /*
00535 ** This function is called by the parser after the table-name in
00536 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
00537 ** pSrc is the full-name of the table being altered.
00538 **
00539 ** This routine makes a (partial) copy of the Table structure
00540 ** for the table being altered and sets Parse.pNewTable to point
00541 ** to it. Routines called by the parser as the column definition
00542 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
00543 ** the copy. The copy of the Table structure is deleted by tokenize.c 
00544 ** after parsing is finished.
00545 **
00546 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
00547 ** coding the "ALTER TABLE ... ADD" statement.
00548 */
00549 void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
00550   Table *pNew;
00551   Table *pTab;
00552   Vdbe *v;
00553   int iDb;
00554   int i;
00555   int nAlloc;
00556   sqlite3 *db = pParse->db;
00557 
00558   /* Look up the table being altered. */
00559   assert( pParse->pNewTable==0 );
00560   assert( sqlite3BtreeHoldsAllMutexes(db) );
00561   if( db->mallocFailed ) goto exit_begin_add_column;
00562   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
00563   if( !pTab ) goto exit_begin_add_column;
00564 
00565 #ifndef SQLITE_OMIT_VIRTUALTABLE
00566   if( IsVirtual(pTab) ){
00567     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
00568     goto exit_begin_add_column;
00569   }
00570 #endif
00571 
00572   /* Make sure this is not an attempt to ALTER a view. */
00573   if( pTab->pSelect ){
00574     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
00575     goto exit_begin_add_column;
00576   }
00577 
00578   assert( pTab->addColOffset>0 );
00579   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
00580 
00581   /* Put a copy of the Table struct in Parse.pNewTable for the
00582   ** sqlite3AddColumn() function and friends to modify.
00583   */
00584   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
00585   if( !pNew ) goto exit_begin_add_column;
00586   pParse->pNewTable = pNew;
00587   pNew->nRef = 1;
00588   pNew->db = db;
00589   pNew->nCol = pTab->nCol;
00590   assert( pNew->nCol>0 );
00591   nAlloc = (((pNew->nCol-1)/8)*8)+8;
00592   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
00593   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
00594   pNew->zName = sqlite3DbStrDup(db, pTab->zName);
00595   if( !pNew->aCol || !pNew->zName ){
00596     db->mallocFailed = 1;
00597     goto exit_begin_add_column;
00598   }
00599   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
00600   for(i=0; i<pNew->nCol; i++){
00601     Column *pCol = &pNew->aCol[i];
00602     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
00603     pCol->zColl = 0;
00604     pCol->zType = 0;
00605     pCol->pDflt = 0;
00606   }
00607   pNew->pSchema = db->aDb[iDb].pSchema;
00608   pNew->addColOffset = pTab->addColOffset;
00609   pNew->nRef = 1;
00610 
00611   /* Begin a transaction and increment the schema cookie.  */
00612   sqlite3BeginWriteOperation(pParse, 0, iDb);
00613   v = sqlite3GetVdbe(pParse);
00614   if( !v ) goto exit_begin_add_column;
00615   sqlite3ChangeCookie(pParse, iDb);
00616 
00617 exit_begin_add_column:
00618   sqlite3SrcListDelete(db, pSrc);
00619   return;
00620 }
00621 #endif  /* SQLITE_ALTER_TABLE */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:51 2011 by Doxygen 1.6.1