1434
|
1 /*
|
|
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 **
|
|
15 ** $Id: alter.c,v 1.20 2006/02/09 02:56:03 drh Exp $
|
|
16 */
|
|
17 #include "sqliteInt.h"
|
|
18 #include <ctype.h>
|
|
19
|
|
20 /*
|
|
21 ** The code in this file only exists if we are not omitting the
|
|
22 ** ALTER TABLE logic from the build.
|
|
23 */
|
|
24 #ifndef SQLITE_OMIT_ALTERTABLE
|
|
25
|
|
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
|
|
31 ** the CREATE TABLE or CREATE INDEX statement is replaced with the second
|
|
32 ** 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 */
|
|
40 static 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;
|
|
50 unsigned char const *zCsr = zSql;
|
|
51 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 {
|
|
60 /* Store the token that zCsr points to in tname. */
|
|
61 tname.z = zCsr;
|
|
62 tname.n = len;
|
|
63
|
|
64 /* Advance zCsr to the next token. Store that token type in 'token',
|
|
65 ** and it's length in 'len' (to be used next iteration of this loop).
|
|
66 */
|
|
67 do {
|
|
68 zCsr += len;
|
|
69 len = sqlite3GetToken(zCsr, &token);
|
|
70 } while( token==TK_SPACE );
|
|
71 assert( len>0 );
|
|
72 } while( token!=TK_LP );
|
|
73
|
|
74 zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
|
|
75 zTableName, tname.z+tname.n);
|
|
76 sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
|
|
77 }
|
|
78 }
|
|
79
|
|
80 #ifndef SQLITE_OMIT_TRIGGER
|
|
81 /* This function is used by SQL generated to implement the ALTER TABLE
|
|
82 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
|
|
83 ** statement. The second is a table name. The table name in the CREATE
|
|
84 ** TRIGGER statement is replaced with the second argument and the result
|
|
85 ** returned. This is analagous to renameTableFunc() above, except for CREATE
|
|
86 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
|
|
87 */
|
|
88 static void renameTriggerFunc(
|
|
89 sqlite3_context *context,
|
|
90 int argc,
|
|
91 sqlite3_value **argv
|
|
92 ){
|
|
93 unsigned char const *zSql = sqlite3_value_text(argv[0]);
|
|
94 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
|
|
95
|
|
96 int token;
|
|
97 Token tname;
|
|
98 int dist = 3;
|
|
99 unsigned char const *zCsr = zSql;
|
|
100 int len = 0;
|
|
101 char *zRet;
|
|
102
|
|
103 /* The principle used to locate the table name in the CREATE TRIGGER
|
|
104 ** statement is that the table name is the first token that is immediatedly
|
|
105 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
|
|
106 ** of TK_WHEN, TK_BEGIN or TK_FOR.
|
|
107 */
|
|
108 if( zSql ){
|
|
109 do {
|
|
110 /* Store the token that zCsr points to in tname. */
|
|
111 tname.z = zCsr;
|
|
112 tname.n = len;
|
|
113
|
|
114 /* Advance zCsr to the next token. Store that token type in 'token',
|
|
115 ** and it's length in 'len' (to be used next iteration of this loop).
|
|
116 */
|
|
117 do {
|
|
118 zCsr += len;
|
|
119 len = sqlite3GetToken(zCsr, &token);
|
|
120 }while( token==TK_SPACE );
|
|
121 assert( len>0 );
|
|
122
|
|
123 /* Variable 'dist' stores the number of tokens read since the most
|
|
124 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
|
|
125 ** token is read and 'dist' equals 2, the condition stated above
|
|
126 ** to be met.
|
|
127 **
|
|
128 ** Note that ON cannot be a database, table or column name, so
|
|
129 ** there is no need to worry about syntax like
|
|
130 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
|
|
131 */
|
|
132 dist++;
|
|
133 if( token==TK_DOT || token==TK_ON ){
|
|
134 dist = 0;
|
|
135 }
|
|
136 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
|
|
137
|
|
138 /* Variable tname now contains the token that is the old table-name
|
|
139 ** in the CREATE TRIGGER statement.
|
|
140 */
|
|
141 zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
|
|
142 zTableName, tname.z+tname.n);
|
|
143 sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
|
|
144 }
|
|
145 }
|
|
146 #endif /* !SQLITE_OMIT_TRIGGER */
|
|
147
|
|
148 /*
|
|
149 ** Register built-in functions used to help implement ALTER TABLE
|
|
150 */
|
|
151 void sqlite3AlterFunctions(sqlite3 *db){
|
|
152 static const struct {
|
|
153 char *zName;
|
|
154 signed char nArg;
|
|
155 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
|
|
156 } aFuncs[] = {
|
|
157 { "sqlite_rename_table", 2, renameTableFunc},
|
|
158 #ifndef SQLITE_OMIT_TRIGGER
|
|
159 { "sqlite_rename_trigger", 2, renameTriggerFunc},
|
|
160 #endif
|
|
161 };
|
|
162 int i;
|
|
163
|
|
164 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
|
|
165 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
|
|
166 SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
|
|
167 }
|
|
168 }
|
|
169
|
|
170 /*
|
|
171 ** Generate the text of a WHERE expression which can be used to select all
|
|
172 ** temporary triggers on table pTab from the sqlite_temp_master table. If
|
|
173 ** table pTab has no temporary triggers, or is itself stored in the
|
|
174 ** temporary database, NULL is returned.
|
|
175 */
|
|
176 static char *whereTempTriggers(Parse *pParse, Table *pTab){
|
|
177 Trigger *pTrig;
|
|
178 char *zWhere = 0;
|
|
179 char *tmp = 0;
|
|
180 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
|
|
181
|
|
182 /* If the table is not located in the temp-db (in which case NULL is
|
|
183 ** returned, loop through the tables list of triggers. For each trigger
|
|
184 ** that is not part of the temp-db schema, add a clause to the WHERE
|
|
185 ** expression being built up in zWhere.
|
|
186 */
|
|
187 if( pTab->pSchema!=pTempSchema ){
|
|
188 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
|
|
189 if( pTrig->pSchema==pTempSchema ){
|
|
190 if( !zWhere ){
|
|
191 zWhere = sqlite3MPrintf("name=%Q", pTrig->name);
|
|
192 }else{
|
|
193 tmp = zWhere;
|
|
194 zWhere = sqlite3MPrintf("%s OR name=%Q", zWhere, pTrig->name);
|
|
195 sqliteFree(tmp);
|
|
196 }
|
|
197 }
|
|
198 }
|
|
199 }
|
|
200 return zWhere;
|
|
201 }
|
|
202
|
|
203 /*
|
|
204 ** Generate code to drop and reload the internal representation of table
|
|
205 ** pTab from the database, including triggers and temporary triggers.
|
|
206 ** Argument zName is the name of the table in the database schema at
|
|
207 ** the time the generated code is executed. This can be different from
|
|
208 ** pTab->zName if this function is being called to code part of an
|
|
209 ** "ALTER TABLE RENAME TO" statement.
|
|
210 */
|
|
211 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
|
|
212 Vdbe *v;
|
|
213 char *zWhere;
|
|
214 int iDb; /* Index of database containing pTab */
|
|
215 #ifndef SQLITE_OMIT_TRIGGER
|
|
216 Trigger *pTrig;
|
|
217 #endif
|
|
218
|
|
219 v = sqlite3GetVdbe(pParse);
|
|
220 if( !v ) return;
|
|
221 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
|
|
222 assert( iDb>=0 );
|
|
223
|
|
224 #ifndef SQLITE_OMIT_TRIGGER
|
|
225 /* Drop any table triggers from the internal schema. */
|
|
226 for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
|
|
227 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
|
|
228 assert( iTrigDb==iDb || iTrigDb==1 );
|
|
229 sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0);
|
|
230 }
|
|
231 #endif
|
|
232
|
|
233 /* Drop the table and index from the internal schema */
|
|
234 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
|
|
235
|
|
236 /* Reload the table, index and permanent trigger schemas. */
|
|
237 zWhere = sqlite3MPrintf("tbl_name=%Q", zName);
|
|
238 if( !zWhere ) return;
|
|
239 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
|
|
240
|
|
241 #ifndef SQLITE_OMIT_TRIGGER
|
|
242 /* Now, if the table is not stored in the temp database, reload any temp
|
|
243 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
|
|
244 */
|
|
245 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
|
|
246 sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC);
|
|
247 }
|
|
248 #endif
|
|
249 }
|
|
250
|
|
251 /*
|
|
252 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
|
|
253 ** command.
|
|
254 */
|
|
255 void sqlite3AlterRenameTable(
|
|
256 Parse *pParse, /* Parser context. */
|
|
257 SrcList *pSrc, /* The table to rename. */
|
|
258 Token *pName /* The new table name. */
|
|
259 ){
|
|
260 int iDb; /* Database that contains the table */
|
|
261 char *zDb; /* Name of database iDb */
|
|
262 Table *pTab; /* Table being renamed */
|
|
263 char *zName = 0; /* NULL-terminated version of pName */
|
|
264 sqlite3 *db = pParse->db; /* Database connection */
|
|
265 Vdbe *v;
|
|
266 #ifndef SQLITE_OMIT_TRIGGER
|
|
267 char *zWhere = 0; /* Where clause to locate temp triggers */
|
|
268 #endif
|
|
269
|
|
270 if( sqlite3MallocFailed() ) goto exit_rename_table;
|
|
271 assert( pSrc->nSrc==1 );
|
|
272
|
|
273 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
|
|
274 if( !pTab ) goto exit_rename_table;
|
|
275 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
|
|
276 zDb = db->aDb[iDb].zName;
|
|
277
|
|
278 /* Get a NULL terminated version of the new table name. */
|
|
279 zName = sqlite3NameFromToken(pName);
|
|
280 if( !zName ) goto exit_rename_table;
|
|
281
|
|
282 /* Check that a table or index named 'zName' does not already exist
|
|
283 ** in database iDb. If so, this is an error.
|
|
284 */
|
|
285 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
|
|
286 sqlite3ErrorMsg(pParse,
|
|
287 "there is already another table or index with this name: %s", zName);
|
|
288 goto exit_rename_table;
|
|
289 }
|
|
290
|
|
291 /* Make sure it is not a system table being altered, or a reserved name
|
|
292 ** that the table is being renamed to.
|
|
293 */
|
|
294 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
|
|
295 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
|
|
296 goto exit_rename_table;
|
|
297 }
|
|
298 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
|
|
299 goto exit_rename_table;
|
|
300 }
|
|
301
|
|
302 #ifndef SQLITE_OMIT_AUTHORIZATION
|
|
303 /* Invoke the authorization callback. */
|
|
304 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
|
|
305 goto exit_rename_table;
|
|
306 }
|
|
307 #endif
|
|
308
|
|
309 /* Begin a transaction and code the VerifyCookie for database iDb.
|
|
310 ** Then modify the schema cookie (since the ALTER TABLE modifies the
|
|
311 ** schema).
|
|
312 */
|
|
313 v = sqlite3GetVdbe(pParse);
|
|
314 if( v==0 ){
|
|
315 goto exit_rename_table;
|
|
316 }
|
|
317 sqlite3BeginWriteOperation(pParse, 0, iDb);
|
|
318 sqlite3ChangeCookie(db, v, iDb);
|
|
319
|
|
320 /* Modify the sqlite_master table to use the new table name. */
|
|
321 sqlite3NestedParse(pParse,
|
|
322 "UPDATE %Q.%s SET "
|
|
323 #ifdef SQLITE_OMIT_TRIGGER
|
|
324 "sql = sqlite_rename_table(sql, %Q), "
|
|
325 #else
|
|
326 "sql = CASE "
|
|
327 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
|
|
328 "ELSE sqlite_rename_table(sql, %Q) END, "
|
|
329 #endif
|
|
330 "tbl_name = %Q, "
|
|
331 "name = CASE "
|
|
332 "WHEN type='table' THEN %Q "
|
|
333 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
|
|
334 "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) "
|
|
335 "ELSE name END "
|
|
336 "WHERE tbl_name=%Q AND "
|
|
337 "(type='table' OR type='index' OR type='trigger');",
|
|
338 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
|
|
339 #ifndef SQLITE_OMIT_TRIGGER
|
|
340 zName,
|
|
341 #endif
|
|
342 zName, strlen(pTab->zName), pTab->zName
|
|
343 );
|
|
344
|
|
345 #ifndef SQLITE_OMIT_AUTOINCREMENT
|
|
346 /* If the sqlite_sequence table exists in this database, then update
|
|
347 ** it with the new table name.
|
|
348 */
|
|
349 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
|
|
350 sqlite3NestedParse(pParse,
|
|
351 "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
|
|
352 zDb, zName, pTab->zName);
|
|
353 }
|
|
354 #endif
|
|
355
|
|
356 #ifndef SQLITE_OMIT_TRIGGER
|
|
357 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
|
|
358 ** table. Don't do this if the table being ALTERed is itself located in
|
|
359 ** the temp database.
|
|
360 */
|
|
361 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
|
|
362 sqlite3NestedParse(pParse,
|
|
363 "UPDATE sqlite_temp_master SET "
|
|
364 "sql = sqlite_rename_trigger(sql, %Q), "
|
|
365 "tbl_name = %Q "
|
|
366 "WHERE %s;", zName, zName, zWhere);
|
|
367 sqliteFree(zWhere);
|
|
368 }
|
|
369 #endif
|
|
370
|
|
371 /* Drop and reload the internal table schema. */
|
|
372 reloadTableSchema(pParse, pTab, zName);
|
|
373
|
|
374 exit_rename_table:
|
|
375 sqlite3SrcListDelete(pSrc);
|
|
376 sqliteFree(zName);
|
|
377 }
|
|
378
|
|
379
|
|
380 /*
|
|
381 ** This function is called after an "ALTER TABLE ... ADD" statement
|
|
382 ** has been parsed. Argument pColDef contains the text of the new
|
|
383 ** column definition.
|
|
384 **
|
|
385 ** The Table structure pParse->pNewTable was extended to include
|
|
386 ** the new column during parsing.
|
|
387 */
|
|
388 void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
|
|
389 Table *pNew; /* Copy of pParse->pNewTable */
|
|
390 Table *pTab; /* Table being altered */
|
|
391 int iDb; /* Database number */
|
|
392 const char *zDb; /* Database name */
|
|
393 const char *zTab; /* Table name */
|
|
394 char *zCol; /* Null-terminated column definition */
|
|
395 Column *pCol; /* The new column */
|
|
396 Expr *pDflt; /* Default value for the new column */
|
|
397
|
|
398 if( pParse->nErr ) return;
|
|
399 pNew = pParse->pNewTable;
|
|
400 assert( pNew );
|
|
401
|
|
402 iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
|
|
403 zDb = pParse->db->aDb[iDb].zName;
|
|
404 zTab = pNew->zName;
|
|
405 pCol = &pNew->aCol[pNew->nCol-1];
|
|
406 pDflt = pCol->pDflt;
|
|
407 pTab = sqlite3FindTable(pParse->db, zTab, zDb);
|
|
408 assert( pTab );
|
|
409
|
|
410 #ifndef SQLITE_OMIT_AUTHORIZATION
|
|
411 /* Invoke the authorization callback. */
|
|
412 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
|
|
413 return;
|
|
414 }
|
|
415 #endif
|
|
416
|
|
417 /* If the default value for the new column was specified with a
|
|
418 ** literal NULL, then set pDflt to 0. This simplifies checking
|
|
419 ** for an SQL NULL default below.
|
|
420 */
|
|
421 if( pDflt && pDflt->op==TK_NULL ){
|
|
422 pDflt = 0;
|
|
423 }
|
|
424
|
|
425 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
|
|
426 ** If there is a NOT NULL constraint, then the default value for the
|
|
427 ** column must not be NULL.
|
|
428 */
|
|
429 if( pCol->isPrimKey ){
|
|
430 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
|
|
431 return;
|
|
432 }
|
|
433 if( pNew->pIndex ){
|
|
434 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
|
|
435 return;
|
|
436 }
|
|
437 if( pCol->notNull && !pDflt ){
|
|
438 sqlite3ErrorMsg(pParse,
|
|
439 "Cannot add a NOT NULL column with default value NULL");
|
|
440 return;
|
|
441 }
|
|
442
|
|
443 /* Ensure the default expression is something that sqlite3ValueFromExpr()
|
|
444 ** can handle (i.e. not CURRENT_TIME etc.)
|
|
445 */
|
|
446 if( pDflt ){
|
|
447 sqlite3_value *pVal;
|
|
448 if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
|
|
449 /* malloc() has failed */
|
|
450 return;
|
|
451 }
|
|
452 if( !pVal ){
|
|
453 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
|
|
454 return;
|
|
455 }
|
|
456 sqlite3ValueFree(pVal);
|
|
457 }
|
|
458
|
|
459 /* Modify the CREATE TABLE statement. */
|
|
460 zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
|
|
461 if( zCol ){
|
|
462 char *zEnd = &zCol[pColDef->n-1];
|
|
463 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
|
|
464 *zEnd-- = '\0';
|
|
465 }
|
|
466 sqlite3NestedParse(pParse,
|
|
467 "UPDATE %Q.%s SET "
|
|
468 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
|
|
469 "WHERE type = 'table' AND name = %Q",
|
|
470 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
|
|
471 zTab
|
|
472 );
|
|
473 sqliteFree(zCol);
|
|
474 }
|
|
475
|
|
476 /* If the default value of the new column is NULL, then set the file
|
|
477 ** format to 2. If the default value of the new column is not NULL,
|
|
478 ** the file format becomes 3.
|
|
479 */
|
|
480 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
|
|
481
|
|
482 /* Reload the schema of the modified table. */
|
|
483 reloadTableSchema(pParse, pTab, pTab->zName);
|
|
484 }
|
|
485
|
|
486 /*
|
|
487 ** This function is called by the parser after the table-name in
|
|
488 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
|
|
489 ** pSrc is the full-name of the table being altered.
|
|
490 **
|
|
491 ** This routine makes a (partial) copy of the Table structure
|
|
492 ** for the table being altered and sets Parse.pNewTable to point
|
|
493 ** to it. Routines called by the parser as the column definition
|
|
494 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
|
|
495 ** the copy. The copy of the Table structure is deleted by tokenize.c
|
|
496 ** after parsing is finished.
|
|
497 **
|
|
498 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
|
|
499 ** coding the "ALTER TABLE ... ADD" statement.
|
|
500 */
|
|
501 void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
|
|
502 Table *pNew;
|
|
503 Table *pTab;
|
|
504 Vdbe *v;
|
|
505 int iDb;
|
|
506 int i;
|
|
507 int nAlloc;
|
|
508
|
|
509 /* Look up the table being altered. */
|
|
510 assert( pParse->pNewTable==0 );
|
|
511 if( sqlite3MallocFailed() ) goto exit_begin_add_column;
|
|
512 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
|
|
513 if( !pTab ) goto exit_begin_add_column;
|
|
514
|
|
515 /* Make sure this is not an attempt to ALTER a view. */
|
|
516 if( pTab->pSelect ){
|
|
517 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
|
|
518 goto exit_begin_add_column;
|
|
519 }
|
|
520
|
|
521 assert( pTab->addColOffset>0 );
|
|
522 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
|
|
523
|
|
524 /* Put a copy of the Table struct in Parse.pNewTable for the
|
|
525 ** sqlite3AddColumn() function and friends to modify.
|
|
526 */
|
|
527 pNew = (Table *)sqliteMalloc(sizeof(Table));
|
|
528 if( !pNew ) goto exit_begin_add_column;
|
|
529 pParse->pNewTable = pNew;
|
|
530 pNew->nRef = 1;
|
|
531 pNew->nCol = pTab->nCol;
|
|
532 assert( pNew->nCol>0 );
|
|
533 nAlloc = (((pNew->nCol-1)/8)*8)+8;
|
|
534 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
|
|
535 pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
|
|
536 pNew->zName = sqliteStrDup(pTab->zName);
|
|
537 if( !pNew->aCol || !pNew->zName ){
|
|
538 goto exit_begin_add_column;
|
|
539 }
|
|
540 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
|
|
541 for(i=0; i<pNew->nCol; i++){
|
|
542 Column *pCol = &pNew->aCol[i];
|
|
543 pCol->zName = sqliteStrDup(pCol->zName);
|
|
544 pCol->zColl = 0;
|
|
545 pCol->zType = 0;
|
|
546 pCol->pDflt = 0;
|
|
547 }
|
|
548 pNew->pSchema = pParse->db->aDb[iDb].pSchema;
|
|
549 pNew->addColOffset = pTab->addColOffset;
|
|
550 pNew->nRef = 1;
|
|
551
|
|
552 /* Begin a transaction and increment the schema cookie. */
|
|
553 sqlite3BeginWriteOperation(pParse, 0, iDb);
|
|
554 v = sqlite3GetVdbe(pParse);
|
|
555 if( !v ) goto exit_begin_add_column;
|
|
556 sqlite3ChangeCookie(pParse->db, v, iDb);
|
|
557
|
|
558 exit_begin_add_column:
|
|
559 sqlite3SrcListDelete(pSrc);
|
|
560 return;
|
|
561 }
|
|
562 #endif /* SQLITE_ALTER_TABLE */
|