extensions/sqlite/sqlite-source/alter.c
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sqlite/sqlite-source/alter.c	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,622 +0,0 @@
     1.4 -/*
     1.5 -** 2005 February 15
     1.6 -**
     1.7 -** The author disclaims copyright to this source code.  In place of
     1.8 -** a legal notice, here is a blessing:
     1.9 -**
    1.10 -**    May you do good and not evil.
    1.11 -**    May you find forgiveness for yourself and forgive others.
    1.12 -**    May you share freely, never taking more than you give.
    1.13 -**
    1.14 -*************************************************************************
    1.15 -** This file contains C code routines that used to generate VDBE code
    1.16 -** that implements the ALTER TABLE command.
    1.17 -**
    1.18 -** $Id$
    1.19 -*/
    1.20 -#include "sqliteInt.h"
    1.21 -#include <ctype.h>
    1.22 -
    1.23 -/*
    1.24 -** The code in this file only exists if we are not omitting the
    1.25 -** ALTER TABLE logic from the build.
    1.26 -*/
    1.27 -#ifndef SQLITE_OMIT_ALTERTABLE
    1.28 -
    1.29 -
    1.30 -/*
    1.31 -** This function is used by SQL generated to implement the 
    1.32 -** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
    1.33 -** CREATE INDEX command. The second is a table name. The table name in 
    1.34 -** the CREATE TABLE or CREATE INDEX statement is replaced with the third
    1.35 -** argument and the result returned. Examples:
    1.36 -**
    1.37 -** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
    1.38 -**     -> 'CREATE TABLE def(a, b, c)'
    1.39 -**
    1.40 -** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
    1.41 -**     -> 'CREATE INDEX i ON def(a, b, c)'
    1.42 -*/
    1.43 -static void renameTableFunc(
    1.44 -  sqlite3_context *context,
    1.45 -  int argc,
    1.46 -  sqlite3_value **argv
    1.47 -){
    1.48 -  unsigned char const *zSql = sqlite3_value_text(argv[0]);
    1.49 -  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
    1.50 -
    1.51 -  int token;
    1.52 -  Token tname;
    1.53 -  unsigned char const *zCsr = zSql;
    1.54 -  int len = 0;
    1.55 -  char *zRet;
    1.56 -
    1.57 -  sqlite3 *db = sqlite3_user_data(context);
    1.58 -
    1.59 -  /* The principle used to locate the table name in the CREATE TABLE 
    1.60 -  ** statement is that the table name is the first token that is immediatedly
    1.61 -  ** followed by a left parenthesis - TK_LP - or "USING" TK_USING.
    1.62 -  */
    1.63 -  if( zSql ){
    1.64 -    do {
    1.65 -      if( !*zCsr ){
    1.66 -        /* Ran out of input before finding an opening bracket. Return NULL. */
    1.67 -        return;
    1.68 -      }
    1.69 -
    1.70 -      /* Store the token that zCsr points to in tname. */
    1.71 -      tname.z = zCsr;
    1.72 -      tname.n = len;
    1.73 -
    1.74 -      /* Advance zCsr to the next token. Store that token type in 'token',
    1.75 -      ** and it's length in 'len' (to be used next iteration of this loop).
    1.76 -      */
    1.77 -      do {
    1.78 -        zCsr += len;
    1.79 -        len = sqlite3GetToken(zCsr, &token);
    1.80 -      } while( token==TK_SPACE );
    1.81 -      assert( len>0 );
    1.82 -    } while( token!=TK_LP && token!=TK_USING );
    1.83 -
    1.84 -    zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql, 
    1.85 -       zTableName, tname.z+tname.n);
    1.86 -    sqlite3_result_text(context, zRet, -1, sqlite3_free);
    1.87 -  }
    1.88 -}
    1.89 -
    1.90 -#ifndef SQLITE_OMIT_TRIGGER
    1.91 -/* This function is used by SQL generated to implement the
    1.92 -** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
    1.93 -** statement. The second is a table name. The table name in the CREATE 
    1.94 -** TRIGGER statement is replaced with the third argument and the result 
    1.95 -** returned. This is analagous to renameTableFunc() above, except for CREATE
    1.96 -** TRIGGER, not CREATE INDEX and CREATE TABLE.
    1.97 -*/
    1.98 -static void renameTriggerFunc(
    1.99 -  sqlite3_context *context,
   1.100 -  int argc,
   1.101 -  sqlite3_value **argv
   1.102 -){
   1.103 -  unsigned char const *zSql = sqlite3_value_text(argv[0]);
   1.104 -  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
   1.105 -
   1.106 -  int token;
   1.107 -  Token tname;
   1.108 -  int dist = 3;
   1.109 -  unsigned char const *zCsr = zSql;
   1.110 -  int len = 0;
   1.111 -  char *zRet;
   1.112 -
   1.113 -  sqlite3 *db = sqlite3_user_data(context);
   1.114 -
   1.115 -  /* The principle used to locate the table name in the CREATE TRIGGER 
   1.116 -  ** statement is that the table name is the first token that is immediatedly
   1.117 -  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
   1.118 -  ** of TK_WHEN, TK_BEGIN or TK_FOR.
   1.119 -  */
   1.120 -  if( zSql ){
   1.121 -    do {
   1.122 -
   1.123 -      if( !*zCsr ){
   1.124 -        /* Ran out of input before finding the table name. Return NULL. */
   1.125 -        return;
   1.126 -      }
   1.127 -
   1.128 -      /* Store the token that zCsr points to in tname. */
   1.129 -      tname.z = zCsr;
   1.130 -      tname.n = len;
   1.131 -
   1.132 -      /* Advance zCsr to the next token. Store that token type in 'token',
   1.133 -      ** and it's length in 'len' (to be used next iteration of this loop).
   1.134 -      */
   1.135 -      do {
   1.136 -        zCsr += len;
   1.137 -        len = sqlite3GetToken(zCsr, &token);
   1.138 -      }while( token==TK_SPACE );
   1.139 -      assert( len>0 );
   1.140 -
   1.141 -      /* Variable 'dist' stores the number of tokens read since the most
   1.142 -      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
   1.143 -      ** token is read and 'dist' equals 2, the condition stated above
   1.144 -      ** to be met.
   1.145 -      **
   1.146 -      ** Note that ON cannot be a database, table or column name, so
   1.147 -      ** there is no need to worry about syntax like 
   1.148 -      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
   1.149 -      */
   1.150 -      dist++;
   1.151 -      if( token==TK_DOT || token==TK_ON ){
   1.152 -        dist = 0;
   1.153 -      }
   1.154 -    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
   1.155 -
   1.156 -    /* Variable tname now contains the token that is the old table-name
   1.157 -    ** in the CREATE TRIGGER statement.
   1.158 -    */
   1.159 -    zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql, 
   1.160 -       zTableName, tname.z+tname.n);
   1.161 -    sqlite3_result_text(context, zRet, -1, sqlite3_free);
   1.162 -  }
   1.163 -}
   1.164 -#endif   /* !SQLITE_OMIT_TRIGGER */
   1.165 -
   1.166 -/*
   1.167 -** Register built-in functions used to help implement ALTER TABLE
   1.168 -*/
   1.169 -void sqlite3AlterFunctions(sqlite3 *db){
   1.170 -  static const struct {
   1.171 -     char *zName;
   1.172 -     signed char nArg;
   1.173 -     void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
   1.174 -  } aFuncs[] = {
   1.175 -    { "sqlite_rename_table",    2, renameTableFunc},
   1.176 -#ifndef SQLITE_OMIT_TRIGGER
   1.177 -    { "sqlite_rename_trigger",  2, renameTriggerFunc},
   1.178 -#endif
   1.179 -  };
   1.180 -  int i;
   1.181 -
   1.182 -  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   1.183 -    sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
   1.184 -        SQLITE_UTF8, (void *)db, aFuncs[i].xFunc, 0, 0);
   1.185 -  }
   1.186 -}
   1.187 -
   1.188 -/*
   1.189 -** Generate the text of a WHERE expression which can be used to select all
   1.190 -** temporary triggers on table pTab from the sqlite_temp_master table. If
   1.191 -** table pTab has no temporary triggers, or is itself stored in the 
   1.192 -** temporary database, NULL is returned.
   1.193 -*/
   1.194 -static char *whereTempTriggers(Parse *pParse, Table *pTab){
   1.195 -  Trigger *pTrig;
   1.196 -  char *zWhere = 0;
   1.197 -  char *tmp = 0;
   1.198 -  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
   1.199 -
   1.200 -  /* If the table is not located in the temp-db (in which case NULL is 
   1.201 -  ** returned, loop through the tables list of triggers. For each trigger
   1.202 -  ** that is not part of the temp-db schema, add a clause to the WHERE 
   1.203 -  ** expression being built up in zWhere.
   1.204 -  */
   1.205 -  if( pTab->pSchema!=pTempSchema ){
   1.206 -    sqlite3 *db = pParse->db;
   1.207 -    for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
   1.208 -      if( pTrig->pSchema==pTempSchema ){
   1.209 -        if( !zWhere ){
   1.210 -          zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
   1.211 -        }else{
   1.212 -          tmp = zWhere;
   1.213 -          zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
   1.214 -          sqlite3_free(tmp);
   1.215 -        }
   1.216 -      }
   1.217 -    }
   1.218 -  }
   1.219 -  return zWhere;
   1.220 -}
   1.221 -
   1.222 -/*
   1.223 -** Generate code to drop and reload the internal representation of table
   1.224 -** pTab from the database, including triggers and temporary triggers.
   1.225 -** Argument zName is the name of the table in the database schema at
   1.226 -** the time the generated code is executed. This can be different from
   1.227 -** pTab->zName if this function is being called to code part of an 
   1.228 -** "ALTER TABLE RENAME TO" statement.
   1.229 -*/
   1.230 -static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
   1.231 -  Vdbe *v;
   1.232 -  char *zWhere;
   1.233 -  int iDb;                   /* Index of database containing pTab */
   1.234 -#ifndef SQLITE_OMIT_TRIGGER
   1.235 -  Trigger *pTrig;
   1.236 -#endif
   1.237 -
   1.238 -  v = sqlite3GetVdbe(pParse);
   1.239 -  if( !v ) return;
   1.240 -  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   1.241 -  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   1.242 -  assert( iDb>=0 );
   1.243 -
   1.244 -#ifndef SQLITE_OMIT_TRIGGER
   1.245 -  /* Drop any table triggers from the internal schema. */
   1.246 -  for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
   1.247 -    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   1.248 -    assert( iTrigDb==iDb || iTrigDb==1 );
   1.249 -    sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0);
   1.250 -  }
   1.251 -#endif
   1.252 -
   1.253 -  /* Drop the table and index from the internal schema */
   1.254 -  sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
   1.255 -
   1.256 -  /* Reload the table, index and permanent trigger schemas. */
   1.257 -  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
   1.258 -  if( !zWhere ) return;
   1.259 -  sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
   1.260 -
   1.261 -#ifndef SQLITE_OMIT_TRIGGER
   1.262 -  /* Now, if the table is not stored in the temp database, reload any temp 
   1.263 -  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
   1.264 -  */
   1.265 -  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   1.266 -    sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC);
   1.267 -  }
   1.268 -#endif
   1.269 -}
   1.270 -
   1.271 -/*
   1.272 -** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
   1.273 -** command. 
   1.274 -*/
   1.275 -void sqlite3AlterRenameTable(
   1.276 -  Parse *pParse,            /* Parser context. */
   1.277 -  SrcList *pSrc,            /* The table to rename. */
   1.278 -  Token *pName              /* The new table name. */
   1.279 -){
   1.280 -  int iDb;                  /* Database that contains the table */
   1.281 -  char *zDb;                /* Name of database iDb */
   1.282 -  Table *pTab;              /* Table being renamed */
   1.283 -  char *zName = 0;          /* NULL-terminated version of pName */ 
   1.284 -  sqlite3 *db = pParse->db; /* Database connection */
   1.285 -  int nTabName;             /* Number of UTF-8 characters in zTabName */
   1.286 -  const char *zTabName;     /* Original name of the table */
   1.287 -  Vdbe *v;
   1.288 -#ifndef SQLITE_OMIT_TRIGGER
   1.289 -  char *zWhere = 0;         /* Where clause to locate temp triggers */
   1.290 -#endif
   1.291 -  int isVirtualRename = 0;  /* True if this is a v-table with an xRename() */
   1.292 -  
   1.293 -  if( db->mallocFailed ) goto exit_rename_table;
   1.294 -  assert( pSrc->nSrc==1 );
   1.295 -  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   1.296 -
   1.297 -  pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   1.298 -  if( !pTab ) goto exit_rename_table;
   1.299 -  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   1.300 -  zDb = db->aDb[iDb].zName;
   1.301 -
   1.302 -  /* Get a NULL terminated version of the new table name. */
   1.303 -  zName = sqlite3NameFromToken(db, pName);
   1.304 -  if( !zName ) goto exit_rename_table;
   1.305 -
   1.306 -  /* Check that a table or index named 'zName' does not already exist
   1.307 -  ** in database iDb. If so, this is an error.
   1.308 -  */
   1.309 -  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
   1.310 -    sqlite3ErrorMsg(pParse, 
   1.311 -        "there is already another table or index with this name: %s", zName);
   1.312 -    goto exit_rename_table;
   1.313 -  }
   1.314 -
   1.315 -  /* Make sure it is not a system table being altered, or a reserved name
   1.316 -  ** that the table is being renamed to.
   1.317 -  */
   1.318 -  if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
   1.319 -    sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
   1.320 -    goto exit_rename_table;
   1.321 -  }
   1.322 -  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   1.323 -    goto exit_rename_table;
   1.324 -  }
   1.325 -
   1.326 -#ifndef SQLITE_OMIT_AUTHORIZATION
   1.327 -  /* Invoke the authorization callback. */
   1.328 -  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
   1.329 -    goto exit_rename_table;
   1.330 -  }
   1.331 -#endif
   1.332 -
   1.333 -#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.334 -  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   1.335 -    goto exit_rename_table;
   1.336 -  }
   1.337 -  if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
   1.338 -    isVirtualRename = 1;
   1.339 -  }
   1.340 -#endif
   1.341 -
   1.342 -  /* Begin a transaction and code the VerifyCookie for database iDb. 
   1.343 -  ** Then modify the schema cookie (since the ALTER TABLE modifies the
   1.344 -  ** schema). Open a statement transaction if the table is a virtual
   1.345 -  ** table.
   1.346 -  */
   1.347 -  v = sqlite3GetVdbe(pParse);
   1.348 -  if( v==0 ){
   1.349 -    goto exit_rename_table;
   1.350 -  }
   1.351 -  sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
   1.352 -  sqlite3ChangeCookie(db, v, iDb);
   1.353 -
   1.354 -  /* If this is a virtual table, invoke the xRename() function if
   1.355 -  ** one is defined. The xRename() callback will modify the names
   1.356 -  ** of any resources used by the v-table implementation (including other
   1.357 -  ** SQLite tables) that are identified by the name of the virtual table.
   1.358 -  */
   1.359 -#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.360 -  if( isVirtualRename ){
   1.361 -    sqlite3VdbeOp3(v, OP_String8, 0, 0, zName, 0);
   1.362 -    sqlite3VdbeOp3(v, OP_VRename, 0, 0, (const char*)pTab->pVtab, P3_VTAB);
   1.363 -  }
   1.364 -#endif
   1.365 -
   1.366 -  /* figure out how many UTF-8 characters are in zName */
   1.367 -  zTabName = pTab->zName;
   1.368 -  nTabName = sqlite3Utf8CharLen(zTabName, -1);
   1.369 -
   1.370 -  /* Modify the sqlite_master table to use the new table name. */
   1.371 -  sqlite3NestedParse(pParse,
   1.372 -      "UPDATE %Q.%s SET "
   1.373 -#ifdef SQLITE_OMIT_TRIGGER
   1.374 -          "sql = sqlite_rename_table(sql, %Q), "
   1.375 -#else
   1.376 -          "sql = CASE "
   1.377 -            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
   1.378 -            "ELSE sqlite_rename_table(sql, %Q) END, "
   1.379 -#endif
   1.380 -          "tbl_name = %Q, "
   1.381 -          "name = CASE "
   1.382 -            "WHEN type='table' THEN %Q "
   1.383 -            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
   1.384 -             "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) "
   1.385 -            "ELSE name END "
   1.386 -      "WHERE tbl_name=%Q AND "
   1.387 -          "(type='table' OR type='index' OR type='trigger');", 
   1.388 -      zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
   1.389 -#ifndef SQLITE_OMIT_TRIGGER
   1.390 -      zName,
   1.391 -#endif
   1.392 -      zName, nTabName, zTabName
   1.393 -  );
   1.394 -
   1.395 -#ifndef SQLITE_OMIT_AUTOINCREMENT
   1.396 -  /* If the sqlite_sequence table exists in this database, then update 
   1.397 -  ** it with the new table name.
   1.398 -  */
   1.399 -  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
   1.400 -    sqlite3NestedParse(pParse,
   1.401 -        "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
   1.402 -        zDb, zName, pTab->zName);
   1.403 -  }
   1.404 -#endif
   1.405 -
   1.406 -#ifndef SQLITE_OMIT_TRIGGER
   1.407 -  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
   1.408 -  ** table. Don't do this if the table being ALTERed is itself located in
   1.409 -  ** the temp database.
   1.410 -  */
   1.411 -  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   1.412 -    sqlite3NestedParse(pParse, 
   1.413 -        "UPDATE sqlite_temp_master SET "
   1.414 -            "sql = sqlite_rename_trigger(sql, %Q), "
   1.415 -            "tbl_name = %Q "
   1.416 -            "WHERE %s;", zName, zName, zWhere);
   1.417 -    sqlite3_free(zWhere);
   1.418 -  }
   1.419 -#endif
   1.420 -
   1.421 -  /* Drop and reload the internal table schema. */
   1.422 -  reloadTableSchema(pParse, pTab, zName);
   1.423 -
   1.424 -exit_rename_table:
   1.425 -  sqlite3SrcListDelete(pSrc);
   1.426 -  sqlite3_free(zName);
   1.427 -}
   1.428 -
   1.429 -
   1.430 -/*
   1.431 -** This function is called after an "ALTER TABLE ... ADD" statement
   1.432 -** has been parsed. Argument pColDef contains the text of the new
   1.433 -** column definition.
   1.434 -**
   1.435 -** The Table structure pParse->pNewTable was extended to include
   1.436 -** the new column during parsing.
   1.437 -*/
   1.438 -void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
   1.439 -  Table *pNew;              /* Copy of pParse->pNewTable */
   1.440 -  Table *pTab;              /* Table being altered */
   1.441 -  int iDb;                  /* Database number */
   1.442 -  const char *zDb;          /* Database name */
   1.443 -  const char *zTab;         /* Table name */
   1.444 -  char *zCol;               /* Null-terminated column definition */
   1.445 -  Column *pCol;             /* The new column */
   1.446 -  Expr *pDflt;              /* Default value for the new column */
   1.447 -  sqlite3 *db;              /* The database connection; */
   1.448 -
   1.449 -  if( pParse->nErr ) return;
   1.450 -  pNew = pParse->pNewTable;
   1.451 -  assert( pNew );
   1.452 -
   1.453 -  db = pParse->db;
   1.454 -  assert( sqlite3BtreeHoldsAllMutexes(db) );
   1.455 -  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
   1.456 -  zDb = db->aDb[iDb].zName;
   1.457 -  zTab = pNew->zName;
   1.458 -  pCol = &pNew->aCol[pNew->nCol-1];
   1.459 -  pDflt = pCol->pDflt;
   1.460 -  pTab = sqlite3FindTable(db, zTab, zDb);
   1.461 -  assert( pTab );
   1.462 -
   1.463 -#ifndef SQLITE_OMIT_AUTHORIZATION
   1.464 -  /* Invoke the authorization callback. */
   1.465 -  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
   1.466 -    return;
   1.467 -  }
   1.468 -#endif
   1.469 -
   1.470 -  /* If the default value for the new column was specified with a 
   1.471 -  ** literal NULL, then set pDflt to 0. This simplifies checking
   1.472 -  ** for an SQL NULL default below.
   1.473 -  */
   1.474 -  if( pDflt && pDflt->op==TK_NULL ){
   1.475 -    pDflt = 0;
   1.476 -  }
   1.477 -
   1.478 -  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
   1.479 -  ** If there is a NOT NULL constraint, then the default value for the
   1.480 -  ** column must not be NULL.
   1.481 -  */
   1.482 -  if( pCol->isPrimKey ){
   1.483 -    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
   1.484 -    return;
   1.485 -  }
   1.486 -  if( pNew->pIndex ){
   1.487 -    sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
   1.488 -    return;
   1.489 -  }
   1.490 -  if( pCol->notNull && !pDflt ){
   1.491 -    sqlite3ErrorMsg(pParse, 
   1.492 -        "Cannot add a NOT NULL column with default value NULL");
   1.493 -    return;
   1.494 -  }
   1.495 -
   1.496 -  /* Ensure the default expression is something that sqlite3ValueFromExpr()
   1.497 -  ** can handle (i.e. not CURRENT_TIME etc.)
   1.498 -  */
   1.499 -  if( pDflt ){
   1.500 -    sqlite3_value *pVal;
   1.501 -    if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
   1.502 -      db->mallocFailed = 1;
   1.503 -      return;
   1.504 -    }
   1.505 -    if( !pVal ){
   1.506 -      sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
   1.507 -      return;
   1.508 -    }
   1.509 -    sqlite3ValueFree(pVal);
   1.510 -  }
   1.511 -
   1.512 -  /* Modify the CREATE TABLE statement. */
   1.513 -  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
   1.514 -  if( zCol ){
   1.515 -    char *zEnd = &zCol[pColDef->n-1];
   1.516 -    while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
   1.517 -      *zEnd-- = '\0';
   1.518 -    }
   1.519 -    sqlite3NestedParse(pParse, 
   1.520 -        "UPDATE %Q.%s SET "
   1.521 -          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
   1.522 -        "WHERE type = 'table' AND name = %Q", 
   1.523 -      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
   1.524 -      zTab
   1.525 -    );
   1.526 -    sqlite3_free(zCol);
   1.527 -  }
   1.528 -
   1.529 -  /* If the default value of the new column is NULL, then set the file
   1.530 -  ** format to 2. If the default value of the new column is not NULL,
   1.531 -  ** the file format becomes 3.
   1.532 -  */
   1.533 -  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
   1.534 -
   1.535 -  /* Reload the schema of the modified table. */
   1.536 -  reloadTableSchema(pParse, pTab, pTab->zName);
   1.537 -}
   1.538 -
   1.539 -/*
   1.540 -** This function is called by the parser after the table-name in
   1.541 -** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
   1.542 -** pSrc is the full-name of the table being altered.
   1.543 -**
   1.544 -** This routine makes a (partial) copy of the Table structure
   1.545 -** for the table being altered and sets Parse.pNewTable to point
   1.546 -** to it. Routines called by the parser as the column definition
   1.547 -** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
   1.548 -** the copy. The copy of the Table structure is deleted by tokenize.c 
   1.549 -** after parsing is finished.
   1.550 -**
   1.551 -** Routine sqlite3AlterFinishAddColumn() will be called to complete
   1.552 -** coding the "ALTER TABLE ... ADD" statement.
   1.553 -*/
   1.554 -void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
   1.555 -  Table *pNew;
   1.556 -  Table *pTab;
   1.557 -  Vdbe *v;
   1.558 -  int iDb;
   1.559 -  int i;
   1.560 -  int nAlloc;
   1.561 -  sqlite3 *db = pParse->db;
   1.562 -
   1.563 -  /* Look up the table being altered. */
   1.564 -  assert( pParse->pNewTable==0 );
   1.565 -  assert( sqlite3BtreeHoldsAllMutexes(db) );
   1.566 -  if( db->mallocFailed ) goto exit_begin_add_column;
   1.567 -  pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   1.568 -  if( !pTab ) goto exit_begin_add_column;
   1.569 -
   1.570 -#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.571 -  if( IsVirtual(pTab) ){
   1.572 -    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
   1.573 -    goto exit_begin_add_column;
   1.574 -  }
   1.575 -#endif
   1.576 -
   1.577 -  /* Make sure this is not an attempt to ALTER a view. */
   1.578 -  if( pTab->pSelect ){
   1.579 -    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
   1.580 -    goto exit_begin_add_column;
   1.581 -  }
   1.582 -
   1.583 -  assert( pTab->addColOffset>0 );
   1.584 -  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   1.585 -
   1.586 -  /* Put a copy of the Table struct in Parse.pNewTable for the
   1.587 -  ** sqlite3AddColumn() function and friends to modify.
   1.588 -  */
   1.589 -  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
   1.590 -  if( !pNew ) goto exit_begin_add_column;
   1.591 -  pParse->pNewTable = pNew;
   1.592 -  pNew->nRef = 1;
   1.593 -  pNew->nCol = pTab->nCol;
   1.594 -  assert( pNew->nCol>0 );
   1.595 -  nAlloc = (((pNew->nCol-1)/8)*8)+8;
   1.596 -  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
   1.597 -  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
   1.598 -  pNew->zName = sqlite3DbStrDup(db, pTab->zName);
   1.599 -  if( !pNew->aCol || !pNew->zName ){
   1.600 -    db->mallocFailed = 1;
   1.601 -    goto exit_begin_add_column;
   1.602 -  }
   1.603 -  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
   1.604 -  for(i=0; i<pNew->nCol; i++){
   1.605 -    Column *pCol = &pNew->aCol[i];
   1.606 -    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
   1.607 -    pCol->zColl = 0;
   1.608 -    pCol->zType = 0;
   1.609 -    pCol->pDflt = 0;
   1.610 -  }
   1.611 -  pNew->pSchema = db->aDb[iDb].pSchema;
   1.612 -  pNew->addColOffset = pTab->addColOffset;
   1.613 -  pNew->nRef = 1;
   1.614 -
   1.615 -  /* Begin a transaction and increment the schema cookie.  */
   1.616 -  sqlite3BeginWriteOperation(pParse, 0, iDb);
   1.617 -  v = sqlite3GetVdbe(pParse);
   1.618 -  if( !v ) goto exit_begin_add_column;
   1.619 -  sqlite3ChangeCookie(db, v, iDb);
   1.620 -
   1.621 -exit_begin_add_column:
   1.622 -  sqlite3SrcListDelete(pSrc);
   1.623 -  return;
   1.624 -}
   1.625 -#endif  /* SQLITE_ALTER_TABLE */