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 229394 2007-02-09 03:17:47Z iliaa $
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 third
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 0 : ){
45 0 : unsigned char const *zSql = sqlite3_value_text(argv[0]);
46 0 : unsigned char const *zTableName = sqlite3_value_text(argv[1]);
47 :
48 : int token;
49 : Token tname;
50 0 : unsigned char const *zCsr = zSql;
51 0 : 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 0 : if( zSql ){
59 : do {
60 : /* Store the token that zCsr points to in tname. */
61 0 : tname.z = zCsr;
62 0 : 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 0 : zCsr += len;
69 0 : len = sqlite3GetToken(zCsr, &token);
70 0 : } while( token==TK_SPACE );
71 : assert( len>0 );
72 0 : } while( token!=TK_LP );
73 :
74 0 : zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
75 : zTableName, tname.z+tname.n);
76 0 : sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
77 : }
78 0 : }
79 :
80 : #ifndef SQLITE_OMIT_TRIGGER
81 : /* This function is used by SQL generated to implement the
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 third 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 0 : ){
93 0 : unsigned char const *zSql = sqlite3_value_text(argv[0]);
94 0 : unsigned char const *zTableName = sqlite3_value_text(argv[1]);
95 :
96 : int token;
97 : Token tname;
98 0 : int dist = 3;
99 0 : unsigned char const *zCsr = zSql;
100 0 : 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 0 : if( zSql ){
109 : do {
110 : /* Store the token that zCsr points to in tname. */
111 0 : tname.z = zCsr;
112 0 : 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 0 : zCsr += len;
119 0 : len = sqlite3GetToken(zCsr, &token);
120 0 : }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 0 : dist++;
133 0 : if( token==TK_DOT || token==TK_ON ){
134 0 : dist = 0;
135 : }
136 0 : } 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 0 : zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
142 : zTableName, tname.z+tname.n);
143 0 : sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
144 : }
145 0 : }
146 : #endif /* !SQLITE_OMIT_TRIGGER */
147 :
148 : /*
149 : ** Register built-in functions used to help implement ALTER TABLE
150 : */
151 130 : 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 390 : for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
165 260 : sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
166 : SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
167 : }
168 130 : }
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 0 : static char *whereTempTriggers(Parse *pParse, Table *pTab){
177 : Trigger *pTrig;
178 0 : char *zWhere = 0;
179 0 : char *tmp = 0;
180 0 : 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 0 : if( pTab->pSchema!=pTempSchema ){
188 0 : for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
189 0 : if( pTrig->pSchema==pTempSchema ){
190 0 : if( !zWhere ){
191 0 : zWhere = sqlite3MPrintf("name=%Q", pTrig->name);
192 : }else{
193 0 : tmp = zWhere;
194 0 : zWhere = sqlite3MPrintf("%s OR name=%Q", zWhere, pTrig->name);
195 0 : sqliteFree(tmp);
196 : }
197 : }
198 : }
199 : }
200 0 : 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 0 : 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 0 : v = sqlite3GetVdbe(pParse);
220 0 : if( !v ) return;
221 0 : 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 0 : for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
227 0 : int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
228 : assert( iTrigDb==iDb || iTrigDb==1 );
229 0 : sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0);
230 : }
231 : #endif
232 :
233 : /* Drop the table and index from the internal schema */
234 0 : sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
235 :
236 : /* Reload the table, index and permanent trigger schemas. */
237 0 : zWhere = sqlite3MPrintf("tbl_name=%Q", zName);
238 0 : if( !zWhere ) return;
239 0 : 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 0 : if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
246 0 : 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 0 : ){
260 : int iDb; /* Database that contains the table */
261 : char *zDb; /* Name of database iDb */
262 : Table *pTab; /* Table being renamed */
263 0 : char *zName = 0; /* NULL-terminated version of pName */
264 0 : sqlite3 *db = pParse->db; /* Database connection */
265 : Vdbe *v;
266 : #ifndef SQLITE_OMIT_TRIGGER
267 0 : char *zWhere = 0; /* Where clause to locate temp triggers */
268 : #endif
269 :
270 0 : if( sqlite3MallocFailed() ) goto exit_rename_table;
271 : assert( pSrc->nSrc==1 );
272 :
273 0 : pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
274 0 : if( !pTab ) goto exit_rename_table;
275 : #ifndef SQLITE_OMIT_VIRTUALTABLE
276 0 : if( IsVirtual(pTab) ){
277 0 : sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
278 0 : goto exit_rename_table;
279 : }
280 : #endif
281 0 : iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
282 0 : zDb = db->aDb[iDb].zName;
283 :
284 : /* Get a NULL terminated version of the new table name. */
285 0 : zName = sqlite3NameFromToken(pName);
286 0 : if( !zName ) goto exit_rename_table;
287 :
288 : /* Check that a table or index named 'zName' does not already exist
289 : ** in database iDb. If so, this is an error.
290 : */
291 0 : if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
292 0 : sqlite3ErrorMsg(pParse,
293 : "there is already another table or index with this name: %s", zName);
294 0 : goto exit_rename_table;
295 : }
296 :
297 : /* Make sure it is not a system table being altered, or a reserved name
298 : ** that the table is being renamed to.
299 : */
300 0 : if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
301 0 : sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
302 0 : goto exit_rename_table;
303 : }
304 0 : if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
305 0 : goto exit_rename_table;
306 : }
307 :
308 : #ifndef SQLITE_OMIT_AUTHORIZATION
309 : /* Invoke the authorization callback. */
310 0 : if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
311 0 : goto exit_rename_table;
312 : }
313 : #endif
314 :
315 : /* Begin a transaction and code the VerifyCookie for database iDb.
316 : ** Then modify the schema cookie (since the ALTER TABLE modifies the
317 : ** schema).
318 : */
319 0 : v = sqlite3GetVdbe(pParse);
320 0 : if( v==0 ){
321 0 : goto exit_rename_table;
322 : }
323 0 : sqlite3BeginWriteOperation(pParse, 0, iDb);
324 0 : sqlite3ChangeCookie(db, v, iDb);
325 :
326 : /* Modify the sqlite_master table to use the new table name. */
327 0 : sqlite3NestedParse(pParse,
328 : "UPDATE %Q.%s SET "
329 : #ifdef SQLITE_OMIT_TRIGGER
330 : "sql = sqlite_rename_table(sql, %Q), "
331 : #else
332 : "sql = CASE "
333 : "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
334 : "ELSE sqlite_rename_table(sql, %Q) END, "
335 : #endif
336 : "tbl_name = %Q, "
337 : "name = CASE "
338 : "WHEN type='table' THEN %Q "
339 : "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
340 : "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) "
341 : "ELSE name END "
342 : "WHERE tbl_name=%Q AND "
343 : "(type='table' OR type='index' OR type='trigger');",
344 : zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
345 : #ifndef SQLITE_OMIT_TRIGGER
346 : zName,
347 : #endif
348 : zName, strlen(pTab->zName), pTab->zName
349 : );
350 :
351 : #ifndef SQLITE_OMIT_AUTOINCREMENT
352 : /* If the sqlite_sequence table exists in this database, then update
353 : ** it with the new table name.
354 : */
355 0 : if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
356 0 : sqlite3NestedParse(pParse,
357 : "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
358 : zDb, zName, pTab->zName);
359 : }
360 : #endif
361 :
362 : #ifndef SQLITE_OMIT_TRIGGER
363 : /* If there are TEMP triggers on this table, modify the sqlite_temp_master
364 : ** table. Don't do this if the table being ALTERed is itself located in
365 : ** the temp database.
366 : */
367 0 : if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
368 0 : sqlite3NestedParse(pParse,
369 : "UPDATE sqlite_temp_master SET "
370 : "sql = sqlite_rename_trigger(sql, %Q), "
371 : "tbl_name = %Q "
372 : "WHERE %s;", zName, zName, zWhere);
373 0 : sqliteFree(zWhere);
374 : }
375 : #endif
376 :
377 : /* Drop and reload the internal table schema. */
378 0 : reloadTableSchema(pParse, pTab, zName);
379 :
380 0 : exit_rename_table:
381 0 : sqlite3SrcListDelete(pSrc);
382 0 : sqliteFree(zName);
383 0 : }
384 :
385 :
386 : /*
387 : ** This function is called after an "ALTER TABLE ... ADD" statement
388 : ** has been parsed. Argument pColDef contains the text of the new
389 : ** column definition.
390 : **
391 : ** The Table structure pParse->pNewTable was extended to include
392 : ** the new column during parsing.
393 : */
394 0 : void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
395 : Table *pNew; /* Copy of pParse->pNewTable */
396 : Table *pTab; /* Table being altered */
397 : int iDb; /* Database number */
398 : const char *zDb; /* Database name */
399 : const char *zTab; /* Table name */
400 : char *zCol; /* Null-terminated column definition */
401 : Column *pCol; /* The new column */
402 : Expr *pDflt; /* Default value for the new column */
403 :
404 0 : if( pParse->nErr ) return;
405 0 : pNew = pParse->pNewTable;
406 : assert( pNew );
407 :
408 0 : iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
409 0 : zDb = pParse->db->aDb[iDb].zName;
410 0 : zTab = pNew->zName;
411 0 : pCol = &pNew->aCol[pNew->nCol-1];
412 0 : pDflt = pCol->pDflt;
413 0 : pTab = sqlite3FindTable(pParse->db, zTab, zDb);
414 : assert( pTab );
415 :
416 : #ifndef SQLITE_OMIT_AUTHORIZATION
417 : /* Invoke the authorization callback. */
418 0 : if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
419 0 : return;
420 : }
421 : #endif
422 :
423 : /* If the default value for the new column was specified with a
424 : ** literal NULL, then set pDflt to 0. This simplifies checking
425 : ** for an SQL NULL default below.
426 : */
427 0 : if( pDflt && pDflt->op==TK_NULL ){
428 0 : pDflt = 0;
429 : }
430 :
431 : /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
432 : ** If there is a NOT NULL constraint, then the default value for the
433 : ** column must not be NULL.
434 : */
435 0 : if( pCol->isPrimKey ){
436 0 : sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
437 0 : return;
438 : }
439 0 : if( pNew->pIndex ){
440 0 : sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
441 0 : return;
442 : }
443 0 : if( pCol->notNull && !pDflt ){
444 0 : sqlite3ErrorMsg(pParse,
445 : "Cannot add a NOT NULL column with default value NULL");
446 0 : return;
447 : }
448 :
449 : /* Ensure the default expression is something that sqlite3ValueFromExpr()
450 : ** can handle (i.e. not CURRENT_TIME etc.)
451 : */
452 0 : if( pDflt ){
453 : sqlite3_value *pVal;
454 0 : if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
455 : /* malloc() has failed */
456 0 : return;
457 : }
458 0 : if( !pVal ){
459 0 : sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
460 0 : return;
461 : }
462 0 : sqlite3ValueFree(pVal);
463 : }
464 :
465 : /* Modify the CREATE TABLE statement. */
466 0 : zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
467 0 : if( zCol ){
468 0 : char *zEnd = &zCol[pColDef->n-1];
469 0 : while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
470 0 : *zEnd-- = '\0';
471 : }
472 0 : sqlite3NestedParse(pParse,
473 : "UPDATE %Q.%s SET "
474 : "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) "
475 : "WHERE type = 'table' AND name = %Q",
476 : zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
477 : zTab
478 : );
479 0 : sqliteFree(zCol);
480 : }
481 :
482 : /* If the default value of the new column is NULL, then set the file
483 : ** format to 2. If the default value of the new column is not NULL,
484 : ** the file format becomes 3.
485 : */
486 0 : sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
487 :
488 : /* Reload the schema of the modified table. */
489 0 : reloadTableSchema(pParse, pTab, pTab->zName);
490 : }
491 :
492 : /*
493 : ** This function is called by the parser after the table-name in
494 : ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
495 : ** pSrc is the full-name of the table being altered.
496 : **
497 : ** This routine makes a (partial) copy of the Table structure
498 : ** for the table being altered and sets Parse.pNewTable to point
499 : ** to it. Routines called by the parser as the column definition
500 : ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
501 : ** the copy. The copy of the Table structure is deleted by tokenize.c
502 : ** after parsing is finished.
503 : **
504 : ** Routine sqlite3AlterFinishAddColumn() will be called to complete
505 : ** coding the "ALTER TABLE ... ADD" statement.
506 : */
507 0 : void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
508 : Table *pNew;
509 : Table *pTab;
510 : Vdbe *v;
511 : int iDb;
512 : int i;
513 : int nAlloc;
514 :
515 : /* Look up the table being altered. */
516 : assert( pParse->pNewTable==0 );
517 0 : if( sqlite3MallocFailed() ) goto exit_begin_add_column;
518 0 : pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
519 0 : if( !pTab ) goto exit_begin_add_column;
520 :
521 : #ifndef SQLITE_OMIT_VIRTUALTABLE
522 0 : if( IsVirtual(pTab) ){
523 0 : sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
524 0 : goto exit_begin_add_column;
525 : }
526 : #endif
527 :
528 : /* Make sure this is not an attempt to ALTER a view. */
529 0 : if( pTab->pSelect ){
530 0 : sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
531 0 : goto exit_begin_add_column;
532 : }
533 :
534 : assert( pTab->addColOffset>0 );
535 0 : iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
536 :
537 : /* Put a copy of the Table struct in Parse.pNewTable for the
538 : ** sqlite3AddColumn() function and friends to modify.
539 : */
540 0 : pNew = (Table *)sqliteMalloc(sizeof(Table));
541 0 : if( !pNew ) goto exit_begin_add_column;
542 0 : pParse->pNewTable = pNew;
543 0 : pNew->nRef = 1;
544 0 : pNew->nCol = pTab->nCol;
545 : assert( pNew->nCol>0 );
546 0 : nAlloc = (((pNew->nCol-1)/8)*8)+8;
547 : assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
548 0 : pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
549 0 : pNew->zName = sqliteStrDup(pTab->zName);
550 0 : if( !pNew->aCol || !pNew->zName ){
551 : goto exit_begin_add_column;
552 : }
553 0 : memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
554 0 : for(i=0; i<pNew->nCol; i++){
555 0 : Column *pCol = &pNew->aCol[i];
556 0 : pCol->zName = sqliteStrDup(pCol->zName);
557 0 : pCol->zColl = 0;
558 0 : pCol->zType = 0;
559 0 : pCol->pDflt = 0;
560 : }
561 0 : pNew->pSchema = pParse->db->aDb[iDb].pSchema;
562 0 : pNew->addColOffset = pTab->addColOffset;
563 0 : pNew->nRef = 1;
564 :
565 : /* Begin a transaction and increment the schema cookie. */
566 0 : sqlite3BeginWriteOperation(pParse, 0, iDb);
567 0 : v = sqlite3GetVdbe(pParse);
568 0 : if( !v ) goto exit_begin_add_column;
569 0 : sqlite3ChangeCookie(pParse->db, v, iDb);
570 :
571 0 : exit_begin_add_column:
572 0 : sqlite3SrcListDelete(pSrc);
573 : return;
574 : }
575 : #endif /* SQLITE_ALTER_TABLE */
|