blob: d93da8f299b40a98bbc13c8ac8219b6a54c4c2f6 [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**
danielk1977182c4ba2007-06-27 15:53:34 +000015** $Id: alter.c,v 1.26 2007/06/27 15:53:35 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
danielk1977182c4ba2007-06-27 15:53:34 +000056 ** followed by a left parenthesis - TK_LP - or "USING" TK_USING.
drh1f01ec12005-02-15 21:36:18 +000057 */
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 );
danielk1977182c4ba2007-06-27 15:53:34 +000077 } while( token!=TK_LP && token!=TK_USING );
drh1f01ec12005-02-15 21:36:18 +000078
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 nTabName; /* Number of UTF-8 characters in zTabName */
277 const char *zTabName; /* Original name of the table */
drhd0e4a6c2005-02-15 20:47:57 +0000278 Vdbe *v;
279#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000280 char *zWhere = 0; /* Where clause to locate temp triggers */
drhd0e4a6c2005-02-15 20:47:57 +0000281#endif
danielk1977182c4ba2007-06-27 15:53:34 +0000282 int isVirtualRename = 0; /* True if this is a v-table with an xRename() */
drhd0e4a6c2005-02-15 20:47:57 +0000283
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;
danielk1977da184232006-01-05 11:34:32 +0000289 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhd0e4a6c2005-02-15 20:47:57 +0000290 zDb = db->aDb[iDb].zName;
291
292 /* Get a NULL terminated version of the new table name. */
293 zName = sqlite3NameFromToken(pName);
294 if( !zName ) goto exit_rename_table;
295
296 /* Check that a table or index named 'zName' does not already exist
297 ** in database iDb. If so, this is an error.
298 */
299 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
300 sqlite3ErrorMsg(pParse,
301 "there is already another table or index with this name: %s", zName);
302 goto exit_rename_table;
303 }
304
305 /* Make sure it is not a system table being altered, or a reserved name
306 ** that the table is being renamed to.
307 */
308 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
309 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
310 goto exit_rename_table;
311 }
312 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
313 goto exit_rename_table;
314 }
315
316#ifndef SQLITE_OMIT_AUTHORIZATION
317 /* Invoke the authorization callback. */
318 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
319 goto exit_rename_table;
320 }
321#endif
322
danielk1977182c4ba2007-06-27 15:53:34 +0000323#ifndef SQLITE_OMIT_VIRTUALTABLE
324 if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
325 isVirtualRename = 1;
326 }
327#endif
328
drhd0e4a6c2005-02-15 20:47:57 +0000329 /* Begin a transaction and code the VerifyCookie for database iDb.
330 ** Then modify the schema cookie (since the ALTER TABLE modifies the
danielk1977182c4ba2007-06-27 15:53:34 +0000331 ** schema). Open a statement transaction if the table is a virtual
332 ** table.
drhd0e4a6c2005-02-15 20:47:57 +0000333 */
334 v = sqlite3GetVdbe(pParse);
335 if( v==0 ){
336 goto exit_rename_table;
337 }
danielk1977182c4ba2007-06-27 15:53:34 +0000338 sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
drhd0e4a6c2005-02-15 20:47:57 +0000339 sqlite3ChangeCookie(db, v, iDb);
340
danielk1977182c4ba2007-06-27 15:53:34 +0000341 /* If this is a virtual table, invoke the xRename() function if
342 ** one is defined. The xRename() callback will modify the names
343 ** of any resources used by the v-table implementation (including other
344 ** SQLite tables) that are identified by the name of the virtual table.
345 */
346#ifndef SQLITE_OMIT_VIRTUALTABLE
347 if( isVirtualRename ){
348 sqlite3VdbeOp3(v, OP_String8, 0, 0, zName, 0);
349 sqlite3VdbeOp3(v, OP_VRename, 0, 0, (const char*)pTab->pVtab, P3_VTAB);
350 }
351#endif
352
drh4e5dd852007-05-15 03:56:49 +0000353 /* figure out how many UTF-8 characters are in zName */
354 zTabName = pTab->zName;
drh9a087a92007-05-15 14:34:32 +0000355 nTabName = sqlite3Utf8CharLen(zTabName, -1);
drh4e5dd852007-05-15 03:56:49 +0000356
drhd0e4a6c2005-02-15 20:47:57 +0000357 /* Modify the sqlite_master table to use the new table name. */
358 sqlite3NestedParse(pParse,
359 "UPDATE %Q.%s SET "
360#ifdef SQLITE_OMIT_TRIGGER
361 "sql = sqlite_rename_table(sql, %Q), "
362#else
363 "sql = CASE "
364 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
365 "ELSE sqlite_rename_table(sql, %Q) END, "
366#endif
367 "tbl_name = %Q, "
368 "name = CASE "
369 "WHEN type='table' THEN %Q "
370 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
drh4e5dd852007-05-15 03:56:49 +0000371 "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) "
drhd0e4a6c2005-02-15 20:47:57 +0000372 "ELSE name END "
373 "WHERE tbl_name=%Q AND "
374 "(type='table' OR type='index' OR type='trigger');",
375 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
376#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000377 zName,
drhd0e4a6c2005-02-15 20:47:57 +0000378#endif
drh4e5dd852007-05-15 03:56:49 +0000379 zName, nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000380 );
381
382#ifndef SQLITE_OMIT_AUTOINCREMENT
383 /* If the sqlite_sequence table exists in this database, then update
384 ** it with the new table name.
385 */
386 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
387 sqlite3NestedParse(pParse,
388 "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
389 zDb, zName, pTab->zName);
390 }
391#endif
392
393#ifndef SQLITE_OMIT_TRIGGER
394 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
395 ** table. Don't do this if the table being ALTERed is itself located in
396 ** the temp database.
397 */
drhd9cb6ac2005-10-20 07:28:17 +0000398 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000399 sqlite3NestedParse(pParse,
400 "UPDATE sqlite_temp_master SET "
401 "sql = sqlite_rename_trigger(sql, %Q), "
402 "tbl_name = %Q "
403 "WHERE %s;", zName, zName, zWhere);
404 sqliteFree(zWhere);
drhd0e4a6c2005-02-15 20:47:57 +0000405 }
406#endif
407
danielk197719a8e7e2005-03-17 05:03:38 +0000408 /* Drop and reload the internal table schema. */
409 reloadTableSchema(pParse, pTab, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000410
411exit_rename_table:
412 sqlite3SrcListDelete(pSrc);
413 sqliteFree(zName);
414}
danielk197719a8e7e2005-03-17 05:03:38 +0000415
416
417/*
418** This function is called after an "ALTER TABLE ... ADD" statement
419** has been parsed. Argument pColDef contains the text of the new
420** column definition.
421**
422** The Table structure pParse->pNewTable was extended to include
423** the new column during parsing.
424*/
425void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
426 Table *pNew; /* Copy of pParse->pNewTable */
427 Table *pTab; /* Table being altered */
428 int iDb; /* Database number */
429 const char *zDb; /* Database name */
430 const char *zTab; /* Table name */
431 char *zCol; /* Null-terminated column definition */
432 Column *pCol; /* The new column */
433 Expr *pDflt; /* Default value for the new column */
danielk197719a8e7e2005-03-17 05:03:38 +0000434
435 if( pParse->nErr ) return;
436 pNew = pParse->pNewTable;
437 assert( pNew );
438
danielk1977da184232006-01-05 11:34:32 +0000439 iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000440 zDb = pParse->db->aDb[iDb].zName;
441 zTab = pNew->zName;
442 pCol = &pNew->aCol[pNew->nCol-1];
443 pDflt = pCol->pDflt;
444 pTab = sqlite3FindTable(pParse->db, zTab, zDb);
445 assert( pTab );
446
drh81f2ccd2006-01-31 14:28:44 +0000447#ifndef SQLITE_OMIT_AUTHORIZATION
448 /* Invoke the authorization callback. */
449 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
450 return;
451 }
452#endif
453
danielk197719a8e7e2005-03-17 05:03:38 +0000454 /* If the default value for the new column was specified with a
455 ** literal NULL, then set pDflt to 0. This simplifies checking
456 ** for an SQL NULL default below.
457 */
458 if( pDflt && pDflt->op==TK_NULL ){
459 pDflt = 0;
460 }
461
462 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
463 ** If there is a NOT NULL constraint, then the default value for the
464 ** column must not be NULL.
465 */
466 if( pCol->isPrimKey ){
467 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
468 return;
469 }
470 if( pNew->pIndex ){
471 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
472 return;
473 }
474 if( pCol->notNull && !pDflt ){
475 sqlite3ErrorMsg(pParse,
476 "Cannot add a NOT NULL column with default value NULL");
477 return;
478 }
479
480 /* Ensure the default expression is something that sqlite3ValueFromExpr()
481 ** can handle (i.e. not CURRENT_TIME etc.)
482 */
483 if( pDflt ){
484 sqlite3_value *pVal;
485 if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
486 /* malloc() has failed */
487 return;
488 }
489 if( !pVal ){
490 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
491 return;
492 }
493 sqlite3ValueFree(pVal);
494 }
495
496 /* Modify the CREATE TABLE statement. */
drh2646da72005-12-09 20:02:05 +0000497 zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000498 if( zCol ){
499 char *zEnd = &zCol[pColDef->n-1];
drh47b4b292005-03-19 14:45:48 +0000500 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000501 *zEnd-- = '\0';
502 }
503 sqlite3NestedParse(pParse,
504 "UPDATE %Q.%s SET "
danielk1977f0b57922005-03-28 00:07:16 +0000505 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
danielk197719a8e7e2005-03-17 05:03:38 +0000506 "WHERE type = 'table' AND name = %Q",
507 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
508 zTab
509 );
510 sqliteFree(zCol);
511 }
512
513 /* If the default value of the new column is NULL, then set the file
514 ** format to 2. If the default value of the new column is not NULL,
515 ** the file format becomes 3.
516 */
drhfdd6e852005-12-16 01:06:16 +0000517 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
danielk197719a8e7e2005-03-17 05:03:38 +0000518
519 /* Reload the schema of the modified table. */
520 reloadTableSchema(pParse, pTab, pTab->zName);
521}
522
drhfdd6e852005-12-16 01:06:16 +0000523/*
danielk197719a8e7e2005-03-17 05:03:38 +0000524** This function is called by the parser after the table-name in
525** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
526** pSrc is the full-name of the table being altered.
527**
528** This routine makes a (partial) copy of the Table structure
529** for the table being altered and sets Parse.pNewTable to point
530** to it. Routines called by the parser as the column definition
531** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
532** the copy. The copy of the Table structure is deleted by tokenize.c
533** after parsing is finished.
534**
535** Routine sqlite3AlterFinishAddColumn() will be called to complete
536** coding the "ALTER TABLE ... ADD" statement.
537*/
538void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
539 Table *pNew;
540 Table *pTab;
541 Vdbe *v;
542 int iDb;
543 int i;
544 int nAlloc;
545
546 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000547 assert( pParse->pNewTable==0 );
danielk19779e128002006-01-18 16:51:35 +0000548 if( sqlite3MallocFailed() ) goto exit_begin_add_column;
danielk197719a8e7e2005-03-17 05:03:38 +0000549 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
550 if( !pTab ) goto exit_begin_add_column;
551
danielk19775ee9d692006-06-21 12:36:25 +0000552#ifndef SQLITE_OMIT_VIRTUALTABLE
553 if( IsVirtual(pTab) ){
554 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
555 goto exit_begin_add_column;
556 }
557#endif
558
danielk197719a8e7e2005-03-17 05:03:38 +0000559 /* Make sure this is not an attempt to ALTER a view. */
560 if( pTab->pSelect ){
561 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
562 goto exit_begin_add_column;
563 }
564
565 assert( pTab->addColOffset>0 );
danielk1977da184232006-01-05 11:34:32 +0000566 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000567
568 /* Put a copy of the Table struct in Parse.pNewTable for the
569 ** sqlite3AddColumn() function and friends to modify.
570 */
571 pNew = (Table *)sqliteMalloc(sizeof(Table));
572 if( !pNew ) goto exit_begin_add_column;
573 pParse->pNewTable = pNew;
drh0bbaa1b2005-08-19 19:14:12 +0000574 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000575 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000576 assert( pNew->nCol>0 );
577 nAlloc = (((pNew->nCol-1)/8)*8)+8;
578 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197719a8e7e2005-03-17 05:03:38 +0000579 pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
580 pNew->zName = sqliteStrDup(pTab->zName);
581 if( !pNew->aCol || !pNew->zName ){
582 goto exit_begin_add_column;
583 }
584 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
585 for(i=0; i<pNew->nCol; i++){
586 Column *pCol = &pNew->aCol[i];
587 pCol->zName = sqliteStrDup(pCol->zName);
drhff22e182006-02-09 02:56:02 +0000588 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000589 pCol->zType = 0;
590 pCol->pDflt = 0;
591 }
danielk1977da184232006-01-05 11:34:32 +0000592 pNew->pSchema = pParse->db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000593 pNew->addColOffset = pTab->addColOffset;
drhed8a3bb2005-06-06 21:19:56 +0000594 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000595
596 /* Begin a transaction and increment the schema cookie. */
597 sqlite3BeginWriteOperation(pParse, 0, iDb);
598 v = sqlite3GetVdbe(pParse);
599 if( !v ) goto exit_begin_add_column;
600 sqlite3ChangeCookie(pParse->db, v, iDb);
601
602exit_begin_add_column:
603 sqlite3SrcListDelete(pSrc);
604 return;
605}
drhd0e4a6c2005-02-15 20:47:57 +0000606#endif /* SQLITE_ALTER_TABLE */