blob: 7049674b26d935ad60599eb6e82f7f965a435142 [file] [log] [blame]
drhd0e4a6c2005-02-15 20:47:57 +00001/*
2** 2005 February 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains C code routines that used to generate VDBE code
13** that implements the ALTER TABLE command.
14**
drh4e5dd852007-05-15 03:56:49 +000015** $Id: alter.c,v 1.24 2007/05/15 03:56:49 drh Exp $
drhd0e4a6c2005-02-15 20:47:57 +000016*/
17#include "sqliteInt.h"
danielk197719a8e7e2005-03-17 05:03:38 +000018#include <ctype.h>
drhd0e4a6c2005-02-15 20:47:57 +000019
drh1f01ec12005-02-15 21:36:18 +000020/*
21** The code in this file only exists if we are not omitting the
22** ALTER TABLE logic from the build.
23*/
drhd0e4a6c2005-02-15 20:47:57 +000024#ifndef SQLITE_OMIT_ALTERTABLE
drh1f01ec12005-02-15 21:36:18 +000025
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
drhf11c34d2006-09-08 12:27:36 +000031** the CREATE TABLE or CREATE INDEX statement is replaced with the third
drh1f01ec12005-02-15 21:36:18 +000032** 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*/
40static 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;
drh2646da72005-12-09 20:02:05 +000050 unsigned char const *zCsr = zSql;
drh1f01ec12005-02-15 21:36:18 +000051 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.
57 */
58 if( zSql ){
59 do {
danielk1977dce872b2007-05-08 12:37:45 +000060 if( !*zCsr ){
61 /* Ran out of input before finding an opening bracket. Return NULL. */
62 return;
63 }
64
drh1f01ec12005-02-15 21:36:18 +000065 /* 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 );
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
drhf11c34d2006-09-08 12:27:36 +000086/* This function is used by SQL generated to implement the
drh1f01ec12005-02-15 21:36:18 +000087** 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
drhf11c34d2006-09-08 12:27:36 +000089** TRIGGER statement is replaced with the third argument and the result
drh1f01ec12005-02-15 21:36:18 +000090** returned. This is analagous to renameTableFunc() above, except for CREATE
91** TRIGGER, not CREATE INDEX and CREATE TABLE.
92*/
93static 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;
drh2646da72005-12-09 20:02:05 +0000104 unsigned char const *zCsr = zSql;
drh1f01ec12005-02-15 21:36:18 +0000105 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 {
danielk1977dce872b2007-05-08 12:37:45 +0000115
116 if( !*zCsr ){
117 /* Ran out of input before finding the table name. Return NULL. */
118 return;
119 }
120
drh1f01ec12005-02-15 21:36:18 +0000121 /* 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*/
162void 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++){
danielk1977771151b2006-01-17 13:21:40 +0000176 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
drh1f01ec12005-02-15 21:36:18 +0000177 SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
178 }
179}
180
drhd0e4a6c2005-02-15 20:47:57 +0000181/*
danielk197719a8e7e2005-03-17 05:03:38 +0000182** 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*/
187static char *whereTempTriggers(Parse *pParse, Table *pTab){
188 Trigger *pTrig;
189 char *zWhere = 0;
190 char *tmp = 0;
danielk1977e501b892006-01-09 06:29:47 +0000191 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
danielk1977da184232006-01-05 11:34:32 +0000192
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 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000199 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
danielk1977da184232006-01-05 11:34:32 +0000200 if( pTrig->pSchema==pTempSchema ){
danielk197719a8e7e2005-03-17 05:03:38 +0000201 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*/
222static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
223 Vdbe *v;
224 char *zWhere;
danielk1977da184232006-01-05 11:34:32 +0000225 int iDb; /* Index of database containing pTab */
danielk197719a8e7e2005-03-17 05:03:38 +0000226#ifndef SQLITE_OMIT_TRIGGER
227 Trigger *pTrig;
228#endif
229
230 v = sqlite3GetVdbe(pParse);
231 if( !v ) return;
danielk1977da184232006-01-05 11:34:32 +0000232 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
233 assert( iDb>=0 );
danielk197719a8e7e2005-03-17 05:03:38 +0000234
235#ifndef SQLITE_OMIT_TRIGGER
236 /* Drop any table triggers from the internal schema. */
237 for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
danielk1977da184232006-01-05 11:34:32 +0000238 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
239 assert( iTrigDb==iDb || iTrigDb==1 );
240 sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000241 }
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 */
drhd9cb6ac2005-10-20 07:28:17 +0000256 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000257 sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC);
258 }
259#endif
260}
261
262/*
drhd0e4a6c2005-02-15 20:47:57 +0000263** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
264** command.
265*/
266void 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 */
drhd0e4a6c2005-02-15 20:47:57 +0000275 sqlite3 *db = pParse->db; /* Database connection */
drh4e5dd852007-05-15 03:56:49 +0000276 int i; /* Loop counter */
277 int nTabName; /* Number of UTF-8 characters in zTabName */
278 const char *zTabName; /* Original name of the table */
drhd0e4a6c2005-02-15 20:47:57 +0000279 Vdbe *v;
280#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000281 char *zWhere = 0; /* Where clause to locate temp triggers */
drhd0e4a6c2005-02-15 20:47:57 +0000282#endif
283
danielk19779e128002006-01-18 16:51:35 +0000284 if( sqlite3MallocFailed() ) goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000285 assert( pSrc->nSrc==1 );
286
287 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
288 if( !pTab ) goto exit_rename_table;
danielk19775ee9d692006-06-21 12:36:25 +0000289#ifndef SQLITE_OMIT_VIRTUALTABLE
290 if( IsVirtual(pTab) ){
291 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
292 goto exit_rename_table;
293 }
294#endif
danielk1977da184232006-01-05 11:34:32 +0000295 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhd0e4a6c2005-02-15 20:47:57 +0000296 zDb = db->aDb[iDb].zName;
297
298 /* Get a NULL terminated version of the new table name. */
299 zName = sqlite3NameFromToken(pName);
300 if( !zName ) goto exit_rename_table;
301
302 /* Check that a table or index named 'zName' does not already exist
303 ** in database iDb. If so, this is an error.
304 */
305 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
306 sqlite3ErrorMsg(pParse,
307 "there is already another table or index with this name: %s", zName);
308 goto exit_rename_table;
309 }
310
311 /* Make sure it is not a system table being altered, or a reserved name
312 ** that the table is being renamed to.
313 */
314 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
315 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
316 goto exit_rename_table;
317 }
318 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
319 goto exit_rename_table;
320 }
321
322#ifndef SQLITE_OMIT_AUTHORIZATION
323 /* Invoke the authorization callback. */
324 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
325 goto exit_rename_table;
326 }
327#endif
328
329 /* Begin a transaction and code the VerifyCookie for database iDb.
330 ** Then modify the schema cookie (since the ALTER TABLE modifies the
331 ** schema).
332 */
333 v = sqlite3GetVdbe(pParse);
334 if( v==0 ){
335 goto exit_rename_table;
336 }
337 sqlite3BeginWriteOperation(pParse, 0, iDb);
338 sqlite3ChangeCookie(db, v, iDb);
339
drh4e5dd852007-05-15 03:56:49 +0000340 /* figure out how many UTF-8 characters are in zName */
341 zTabName = pTab->zName;
342 for(i=nTabName=0; zTabName[i]; i++){
343 if( (zTabName[i]&0xc0)!=0x80 ) nTabName++;
344 }
345
drhd0e4a6c2005-02-15 20:47:57 +0000346 /* Modify the sqlite_master table to use the new table name. */
347 sqlite3NestedParse(pParse,
348 "UPDATE %Q.%s SET "
349#ifdef SQLITE_OMIT_TRIGGER
350 "sql = sqlite_rename_table(sql, %Q), "
351#else
352 "sql = CASE "
353 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
354 "ELSE sqlite_rename_table(sql, %Q) END, "
355#endif
356 "tbl_name = %Q, "
357 "name = CASE "
358 "WHEN type='table' THEN %Q "
359 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
drh4e5dd852007-05-15 03:56:49 +0000360 "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) "
drhd0e4a6c2005-02-15 20:47:57 +0000361 "ELSE name END "
362 "WHERE tbl_name=%Q AND "
363 "(type='table' OR type='index' OR type='trigger');",
364 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
365#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000366 zName,
drhd0e4a6c2005-02-15 20:47:57 +0000367#endif
drh4e5dd852007-05-15 03:56:49 +0000368 zName, nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000369 );
370
371#ifndef SQLITE_OMIT_AUTOINCREMENT
372 /* If the sqlite_sequence table exists in this database, then update
373 ** it with the new table name.
374 */
375 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
376 sqlite3NestedParse(pParse,
377 "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
378 zDb, zName, pTab->zName);
379 }
380#endif
381
382#ifndef SQLITE_OMIT_TRIGGER
383 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
384 ** table. Don't do this if the table being ALTERed is itself located in
385 ** the temp database.
386 */
drhd9cb6ac2005-10-20 07:28:17 +0000387 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000388 sqlite3NestedParse(pParse,
389 "UPDATE sqlite_temp_master SET "
390 "sql = sqlite_rename_trigger(sql, %Q), "
391 "tbl_name = %Q "
392 "WHERE %s;", zName, zName, zWhere);
393 sqliteFree(zWhere);
drhd0e4a6c2005-02-15 20:47:57 +0000394 }
395#endif
396
danielk197719a8e7e2005-03-17 05:03:38 +0000397 /* Drop and reload the internal table schema. */
398 reloadTableSchema(pParse, pTab, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000399
400exit_rename_table:
401 sqlite3SrcListDelete(pSrc);
402 sqliteFree(zName);
403}
danielk197719a8e7e2005-03-17 05:03:38 +0000404
405
406/*
407** This function is called after an "ALTER TABLE ... ADD" statement
408** has been parsed. Argument pColDef contains the text of the new
409** column definition.
410**
411** The Table structure pParse->pNewTable was extended to include
412** the new column during parsing.
413*/
414void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
415 Table *pNew; /* Copy of pParse->pNewTable */
416 Table *pTab; /* Table being altered */
417 int iDb; /* Database number */
418 const char *zDb; /* Database name */
419 const char *zTab; /* Table name */
420 char *zCol; /* Null-terminated column definition */
421 Column *pCol; /* The new column */
422 Expr *pDflt; /* Default value for the new column */
danielk197719a8e7e2005-03-17 05:03:38 +0000423
424 if( pParse->nErr ) return;
425 pNew = pParse->pNewTable;
426 assert( pNew );
427
danielk1977da184232006-01-05 11:34:32 +0000428 iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000429 zDb = pParse->db->aDb[iDb].zName;
430 zTab = pNew->zName;
431 pCol = &pNew->aCol[pNew->nCol-1];
432 pDflt = pCol->pDflt;
433 pTab = sqlite3FindTable(pParse->db, zTab, zDb);
434 assert( pTab );
435
drh81f2ccd2006-01-31 14:28:44 +0000436#ifndef SQLITE_OMIT_AUTHORIZATION
437 /* Invoke the authorization callback. */
438 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
439 return;
440 }
441#endif
442
danielk197719a8e7e2005-03-17 05:03:38 +0000443 /* If the default value for the new column was specified with a
444 ** literal NULL, then set pDflt to 0. This simplifies checking
445 ** for an SQL NULL default below.
446 */
447 if( pDflt && pDflt->op==TK_NULL ){
448 pDflt = 0;
449 }
450
451 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
452 ** If there is a NOT NULL constraint, then the default value for the
453 ** column must not be NULL.
454 */
455 if( pCol->isPrimKey ){
456 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
457 return;
458 }
459 if( pNew->pIndex ){
460 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
461 return;
462 }
463 if( pCol->notNull && !pDflt ){
464 sqlite3ErrorMsg(pParse,
465 "Cannot add a NOT NULL column with default value NULL");
466 return;
467 }
468
469 /* Ensure the default expression is something that sqlite3ValueFromExpr()
470 ** can handle (i.e. not CURRENT_TIME etc.)
471 */
472 if( pDflt ){
473 sqlite3_value *pVal;
474 if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
475 /* malloc() has failed */
476 return;
477 }
478 if( !pVal ){
479 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
480 return;
481 }
482 sqlite3ValueFree(pVal);
483 }
484
485 /* Modify the CREATE TABLE statement. */
drh2646da72005-12-09 20:02:05 +0000486 zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000487 if( zCol ){
488 char *zEnd = &zCol[pColDef->n-1];
drh47b4b292005-03-19 14:45:48 +0000489 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000490 *zEnd-- = '\0';
491 }
492 sqlite3NestedParse(pParse,
493 "UPDATE %Q.%s SET "
danielk1977f0b57922005-03-28 00:07:16 +0000494 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
danielk197719a8e7e2005-03-17 05:03:38 +0000495 "WHERE type = 'table' AND name = %Q",
496 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
497 zTab
498 );
499 sqliteFree(zCol);
500 }
501
502 /* If the default value of the new column is NULL, then set the file
503 ** format to 2. If the default value of the new column is not NULL,
504 ** the file format becomes 3.
505 */
drhfdd6e852005-12-16 01:06:16 +0000506 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
danielk197719a8e7e2005-03-17 05:03:38 +0000507
508 /* Reload the schema of the modified table. */
509 reloadTableSchema(pParse, pTab, pTab->zName);
510}
511
drhfdd6e852005-12-16 01:06:16 +0000512/*
danielk197719a8e7e2005-03-17 05:03:38 +0000513** This function is called by the parser after the table-name in
514** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
515** pSrc is the full-name of the table being altered.
516**
517** This routine makes a (partial) copy of the Table structure
518** for the table being altered and sets Parse.pNewTable to point
519** to it. Routines called by the parser as the column definition
520** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
521** the copy. The copy of the Table structure is deleted by tokenize.c
522** after parsing is finished.
523**
524** Routine sqlite3AlterFinishAddColumn() will be called to complete
525** coding the "ALTER TABLE ... ADD" statement.
526*/
527void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
528 Table *pNew;
529 Table *pTab;
530 Vdbe *v;
531 int iDb;
532 int i;
533 int nAlloc;
534
535 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000536 assert( pParse->pNewTable==0 );
danielk19779e128002006-01-18 16:51:35 +0000537 if( sqlite3MallocFailed() ) goto exit_begin_add_column;
danielk197719a8e7e2005-03-17 05:03:38 +0000538 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
539 if( !pTab ) goto exit_begin_add_column;
540
danielk19775ee9d692006-06-21 12:36:25 +0000541#ifndef SQLITE_OMIT_VIRTUALTABLE
542 if( IsVirtual(pTab) ){
543 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
544 goto exit_begin_add_column;
545 }
546#endif
547
danielk197719a8e7e2005-03-17 05:03:38 +0000548 /* Make sure this is not an attempt to ALTER a view. */
549 if( pTab->pSelect ){
550 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
551 goto exit_begin_add_column;
552 }
553
554 assert( pTab->addColOffset>0 );
danielk1977da184232006-01-05 11:34:32 +0000555 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000556
557 /* Put a copy of the Table struct in Parse.pNewTable for the
558 ** sqlite3AddColumn() function and friends to modify.
559 */
560 pNew = (Table *)sqliteMalloc(sizeof(Table));
561 if( !pNew ) goto exit_begin_add_column;
562 pParse->pNewTable = pNew;
drh0bbaa1b2005-08-19 19:14:12 +0000563 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000564 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000565 assert( pNew->nCol>0 );
566 nAlloc = (((pNew->nCol-1)/8)*8)+8;
567 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197719a8e7e2005-03-17 05:03:38 +0000568 pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
569 pNew->zName = sqliteStrDup(pTab->zName);
570 if( !pNew->aCol || !pNew->zName ){
571 goto exit_begin_add_column;
572 }
573 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
574 for(i=0; i<pNew->nCol; i++){
575 Column *pCol = &pNew->aCol[i];
576 pCol->zName = sqliteStrDup(pCol->zName);
drhff22e182006-02-09 02:56:02 +0000577 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000578 pCol->zType = 0;
579 pCol->pDflt = 0;
580 }
danielk1977da184232006-01-05 11:34:32 +0000581 pNew->pSchema = pParse->db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000582 pNew->addColOffset = pTab->addColOffset;
drhed8a3bb2005-06-06 21:19:56 +0000583 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000584
585 /* Begin a transaction and increment the schema cookie. */
586 sqlite3BeginWriteOperation(pParse, 0, iDb);
587 v = sqlite3GetVdbe(pParse);
588 if( !v ) goto exit_begin_add_column;
589 sqlite3ChangeCookie(pParse->db, v, iDb);
590
591exit_begin_add_column:
592 sqlite3SrcListDelete(pSrc);
593 return;
594}
drhd0e4a6c2005-02-15 20:47:57 +0000595#endif /* SQLITE_ALTER_TABLE */