blob: d6f930eee27c3ff6721019bc163cd15f0faa9ad4 [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**
danielk1977dce872b2007-05-08 12:37:45 +000015** $Id: alter.c,v 1.23 2007/05/08 12:37:46 danielk1977 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 */
276 Vdbe *v;
277#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000278 char *zWhere = 0; /* Where clause to locate temp triggers */
drhd0e4a6c2005-02-15 20:47:57 +0000279#endif
280
danielk19779e128002006-01-18 16:51:35 +0000281 if( sqlite3MallocFailed() ) goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000282 assert( pSrc->nSrc==1 );
283
284 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
285 if( !pTab ) goto exit_rename_table;
danielk19775ee9d692006-06-21 12:36:25 +0000286#ifndef SQLITE_OMIT_VIRTUALTABLE
287 if( IsVirtual(pTab) ){
288 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
289 goto exit_rename_table;
290 }
291#endif
danielk1977da184232006-01-05 11:34:32 +0000292 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhd0e4a6c2005-02-15 20:47:57 +0000293 zDb = db->aDb[iDb].zName;
294
295 /* Get a NULL terminated version of the new table name. */
296 zName = sqlite3NameFromToken(pName);
297 if( !zName ) goto exit_rename_table;
298
299 /* Check that a table or index named 'zName' does not already exist
300 ** in database iDb. If so, this is an error.
301 */
302 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
303 sqlite3ErrorMsg(pParse,
304 "there is already another table or index with this name: %s", zName);
305 goto exit_rename_table;
306 }
307
308 /* Make sure it is not a system table being altered, or a reserved name
309 ** that the table is being renamed to.
310 */
311 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
312 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
313 goto exit_rename_table;
314 }
315 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
316 goto exit_rename_table;
317 }
318
319#ifndef SQLITE_OMIT_AUTHORIZATION
320 /* Invoke the authorization callback. */
321 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
322 goto exit_rename_table;
323 }
324#endif
325
326 /* Begin a transaction and code the VerifyCookie for database iDb.
327 ** Then modify the schema cookie (since the ALTER TABLE modifies the
328 ** schema).
329 */
330 v = sqlite3GetVdbe(pParse);
331 if( v==0 ){
332 goto exit_rename_table;
333 }
334 sqlite3BeginWriteOperation(pParse, 0, iDb);
335 sqlite3ChangeCookie(db, v, iDb);
336
337 /* Modify the sqlite_master table to use the new table name. */
338 sqlite3NestedParse(pParse,
339 "UPDATE %Q.%s SET "
340#ifdef SQLITE_OMIT_TRIGGER
341 "sql = sqlite_rename_table(sql, %Q), "
342#else
343 "sql = CASE "
344 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
345 "ELSE sqlite_rename_table(sql, %Q) END, "
346#endif
347 "tbl_name = %Q, "
348 "name = CASE "
349 "WHEN type='table' THEN %Q "
350 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
351 "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) "
352 "ELSE name END "
353 "WHERE tbl_name=%Q AND "
354 "(type='table' OR type='index' OR type='trigger');",
355 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
356#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000357 zName,
drhd0e4a6c2005-02-15 20:47:57 +0000358#endif
359 zName, strlen(pTab->zName), pTab->zName
360 );
361
362#ifndef SQLITE_OMIT_AUTOINCREMENT
363 /* If the sqlite_sequence table exists in this database, then update
364 ** it with the new table name.
365 */
366 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
367 sqlite3NestedParse(pParse,
368 "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
369 zDb, zName, pTab->zName);
370 }
371#endif
372
373#ifndef SQLITE_OMIT_TRIGGER
374 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
375 ** table. Don't do this if the table being ALTERed is itself located in
376 ** the temp database.
377 */
drhd9cb6ac2005-10-20 07:28:17 +0000378 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000379 sqlite3NestedParse(pParse,
380 "UPDATE sqlite_temp_master SET "
381 "sql = sqlite_rename_trigger(sql, %Q), "
382 "tbl_name = %Q "
383 "WHERE %s;", zName, zName, zWhere);
384 sqliteFree(zWhere);
drhd0e4a6c2005-02-15 20:47:57 +0000385 }
386#endif
387
danielk197719a8e7e2005-03-17 05:03:38 +0000388 /* Drop and reload the internal table schema. */
389 reloadTableSchema(pParse, pTab, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000390
391exit_rename_table:
392 sqlite3SrcListDelete(pSrc);
393 sqliteFree(zName);
394}
danielk197719a8e7e2005-03-17 05:03:38 +0000395
396
397/*
398** This function is called after an "ALTER TABLE ... ADD" statement
399** has been parsed. Argument pColDef contains the text of the new
400** column definition.
401**
402** The Table structure pParse->pNewTable was extended to include
403** the new column during parsing.
404*/
405void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
406 Table *pNew; /* Copy of pParse->pNewTable */
407 Table *pTab; /* Table being altered */
408 int iDb; /* Database number */
409 const char *zDb; /* Database name */
410 const char *zTab; /* Table name */
411 char *zCol; /* Null-terminated column definition */
412 Column *pCol; /* The new column */
413 Expr *pDflt; /* Default value for the new column */
danielk197719a8e7e2005-03-17 05:03:38 +0000414
415 if( pParse->nErr ) return;
416 pNew = pParse->pNewTable;
417 assert( pNew );
418
danielk1977da184232006-01-05 11:34:32 +0000419 iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000420 zDb = pParse->db->aDb[iDb].zName;
421 zTab = pNew->zName;
422 pCol = &pNew->aCol[pNew->nCol-1];
423 pDflt = pCol->pDflt;
424 pTab = sqlite3FindTable(pParse->db, zTab, zDb);
425 assert( pTab );
426
drh81f2ccd2006-01-31 14:28:44 +0000427#ifndef SQLITE_OMIT_AUTHORIZATION
428 /* Invoke the authorization callback. */
429 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
430 return;
431 }
432#endif
433
danielk197719a8e7e2005-03-17 05:03:38 +0000434 /* If the default value for the new column was specified with a
435 ** literal NULL, then set pDflt to 0. This simplifies checking
436 ** for an SQL NULL default below.
437 */
438 if( pDflt && pDflt->op==TK_NULL ){
439 pDflt = 0;
440 }
441
442 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
443 ** If there is a NOT NULL constraint, then the default value for the
444 ** column must not be NULL.
445 */
446 if( pCol->isPrimKey ){
447 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
448 return;
449 }
450 if( pNew->pIndex ){
451 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
452 return;
453 }
454 if( pCol->notNull && !pDflt ){
455 sqlite3ErrorMsg(pParse,
456 "Cannot add a NOT NULL column with default value NULL");
457 return;
458 }
459
460 /* Ensure the default expression is something that sqlite3ValueFromExpr()
461 ** can handle (i.e. not CURRENT_TIME etc.)
462 */
463 if( pDflt ){
464 sqlite3_value *pVal;
465 if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
466 /* malloc() has failed */
467 return;
468 }
469 if( !pVal ){
470 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
471 return;
472 }
473 sqlite3ValueFree(pVal);
474 }
475
476 /* Modify the CREATE TABLE statement. */
drh2646da72005-12-09 20:02:05 +0000477 zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000478 if( zCol ){
479 char *zEnd = &zCol[pColDef->n-1];
drh47b4b292005-03-19 14:45:48 +0000480 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000481 *zEnd-- = '\0';
482 }
483 sqlite3NestedParse(pParse,
484 "UPDATE %Q.%s SET "
danielk1977f0b57922005-03-28 00:07:16 +0000485 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
danielk197719a8e7e2005-03-17 05:03:38 +0000486 "WHERE type = 'table' AND name = %Q",
487 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
488 zTab
489 );
490 sqliteFree(zCol);
491 }
492
493 /* If the default value of the new column is NULL, then set the file
494 ** format to 2. If the default value of the new column is not NULL,
495 ** the file format becomes 3.
496 */
drhfdd6e852005-12-16 01:06:16 +0000497 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
danielk197719a8e7e2005-03-17 05:03:38 +0000498
499 /* Reload the schema of the modified table. */
500 reloadTableSchema(pParse, pTab, pTab->zName);
501}
502
drhfdd6e852005-12-16 01:06:16 +0000503/*
danielk197719a8e7e2005-03-17 05:03:38 +0000504** This function is called by the parser after the table-name in
505** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
506** pSrc is the full-name of the table being altered.
507**
508** This routine makes a (partial) copy of the Table structure
509** for the table being altered and sets Parse.pNewTable to point
510** to it. Routines called by the parser as the column definition
511** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
512** the copy. The copy of the Table structure is deleted by tokenize.c
513** after parsing is finished.
514**
515** Routine sqlite3AlterFinishAddColumn() will be called to complete
516** coding the "ALTER TABLE ... ADD" statement.
517*/
518void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
519 Table *pNew;
520 Table *pTab;
521 Vdbe *v;
522 int iDb;
523 int i;
524 int nAlloc;
525
526 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000527 assert( pParse->pNewTable==0 );
danielk19779e128002006-01-18 16:51:35 +0000528 if( sqlite3MallocFailed() ) goto exit_begin_add_column;
danielk197719a8e7e2005-03-17 05:03:38 +0000529 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
530 if( !pTab ) goto exit_begin_add_column;
531
danielk19775ee9d692006-06-21 12:36:25 +0000532#ifndef SQLITE_OMIT_VIRTUALTABLE
533 if( IsVirtual(pTab) ){
534 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
535 goto exit_begin_add_column;
536 }
537#endif
538
danielk197719a8e7e2005-03-17 05:03:38 +0000539 /* Make sure this is not an attempt to ALTER a view. */
540 if( pTab->pSelect ){
541 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
542 goto exit_begin_add_column;
543 }
544
545 assert( pTab->addColOffset>0 );
danielk1977da184232006-01-05 11:34:32 +0000546 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000547
548 /* Put a copy of the Table struct in Parse.pNewTable for the
549 ** sqlite3AddColumn() function and friends to modify.
550 */
551 pNew = (Table *)sqliteMalloc(sizeof(Table));
552 if( !pNew ) goto exit_begin_add_column;
553 pParse->pNewTable = pNew;
drh0bbaa1b2005-08-19 19:14:12 +0000554 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000555 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000556 assert( pNew->nCol>0 );
557 nAlloc = (((pNew->nCol-1)/8)*8)+8;
558 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197719a8e7e2005-03-17 05:03:38 +0000559 pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
560 pNew->zName = sqliteStrDup(pTab->zName);
561 if( !pNew->aCol || !pNew->zName ){
562 goto exit_begin_add_column;
563 }
564 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
565 for(i=0; i<pNew->nCol; i++){
566 Column *pCol = &pNew->aCol[i];
567 pCol->zName = sqliteStrDup(pCol->zName);
drhff22e182006-02-09 02:56:02 +0000568 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000569 pCol->zType = 0;
570 pCol->pDflt = 0;
571 }
danielk1977da184232006-01-05 11:34:32 +0000572 pNew->pSchema = pParse->db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000573 pNew->addColOffset = pTab->addColOffset;
drhed8a3bb2005-06-06 21:19:56 +0000574 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000575
576 /* Begin a transaction and increment the schema cookie. */
577 sqlite3BeginWriteOperation(pParse, 0, iDb);
578 v = sqlite3GetVdbe(pParse);
579 if( !v ) goto exit_begin_add_column;
580 sqlite3ChangeCookie(pParse->db, v, iDb);
581
582exit_begin_add_column:
583 sqlite3SrcListDelete(pSrc);
584 return;
585}
drhd0e4a6c2005-02-15 20:47:57 +0000586#endif /* SQLITE_ALTER_TABLE */