blob: fe1e2cb192e8bf64975bcd2321439e5530e616d7 [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**
danielk197762c14b32008-11-19 09:05:26 +000015** $Id: alter.c,v 1.50 2008/11/19 09:05:27 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,
danielk197762c14b32008-11-19 09:05:26 +000042 int NotUsed,
drh1f01ec12005-02-15 21:36:18 +000043 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
drhfa4a4b92008-03-19 21:45:51 +000054 sqlite3 *db = sqlite3_context_db_handle(context);
danielk19771e536952007-08-16 10:09:01 +000055
danielk197762c14b32008-11-19 09:05:26 +000056 UNUSED_PARAMETER(NotUsed);
57
drh1f01ec12005-02-15 21:36:18 +000058 /* The principle used to locate the table name in the CREATE TABLE
drh73829452008-05-09 14:17:51 +000059 ** statement is that the table name is the first non-space token that
drh6aa1edc2008-07-07 12:44:58 +000060 ** is immediately followed by a TK_LP or TK_USING token.
drh1f01ec12005-02-15 21:36:18 +000061 */
62 if( zSql ){
63 do {
danielk1977dce872b2007-05-08 12:37:45 +000064 if( !*zCsr ){
65 /* Ran out of input before finding an opening bracket. Return NULL. */
66 return;
67 }
68
drh1f01ec12005-02-15 21:36:18 +000069 /* Store the token that zCsr points to in tname. */
70 tname.z = zCsr;
71 tname.n = len;
72
73 /* Advance zCsr to the next token. Store that token type in 'token',
drh85b623f2007-12-13 21:54:09 +000074 ** and its length in 'len' (to be used next iteration of this loop).
drh1f01ec12005-02-15 21:36:18 +000075 */
76 do {
77 zCsr += len;
78 len = sqlite3GetToken(zCsr, &token);
drh200a81d2008-08-08 14:19:41 +000079 } while( token==TK_SPACE );
drh1f01ec12005-02-15 21:36:18 +000080 assert( len>0 );
danielk1977182c4ba2007-06-27 15:53:34 +000081 } while( token!=TK_LP && token!=TK_USING );
drh1f01ec12005-02-15 21:36:18 +000082
drh8e5b5f82008-02-09 14:30:29 +000083 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql,
drh1f01ec12005-02-15 21:36:18 +000084 zTableName, tname.z+tname.n);
drh633e6d52008-07-28 19:34:53 +000085 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
drh1f01ec12005-02-15 21:36:18 +000086 }
87}
88
89#ifndef SQLITE_OMIT_TRIGGER
drhf11c34d2006-09-08 12:27:36 +000090/* This function is used by SQL generated to implement the
drh1f01ec12005-02-15 21:36:18 +000091** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
92** statement. The second is a table name. The table name in the CREATE
drhf11c34d2006-09-08 12:27:36 +000093** TRIGGER statement is replaced with the third argument and the result
drh1f01ec12005-02-15 21:36:18 +000094** returned. This is analagous to renameTableFunc() above, except for CREATE
95** TRIGGER, not CREATE INDEX and CREATE TABLE.
96*/
97static void renameTriggerFunc(
98 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +000099 int NotUsed,
drh1f01ec12005-02-15 21:36:18 +0000100 sqlite3_value **argv
101){
102 unsigned char const *zSql = sqlite3_value_text(argv[0]);
103 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
104
105 int token;
106 Token tname;
107 int dist = 3;
drh2646da72005-12-09 20:02:05 +0000108 unsigned char const *zCsr = zSql;
drh1f01ec12005-02-15 21:36:18 +0000109 int len = 0;
110 char *zRet;
drhfa4a4b92008-03-19 21:45:51 +0000111 sqlite3 *db = sqlite3_context_db_handle(context);
danielk19771e536952007-08-16 10:09:01 +0000112
danielk197762c14b32008-11-19 09:05:26 +0000113 UNUSED_PARAMETER(NotUsed);
114
drh1f01ec12005-02-15 21:36:18 +0000115 /* The principle used to locate the table name in the CREATE TRIGGER
116 ** statement is that the table name is the first token that is immediatedly
117 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
118 ** of TK_WHEN, TK_BEGIN or TK_FOR.
119 */
120 if( zSql ){
121 do {
danielk1977dce872b2007-05-08 12:37:45 +0000122
123 if( !*zCsr ){
124 /* Ran out of input before finding the table name. Return NULL. */
125 return;
126 }
127
drh1f01ec12005-02-15 21:36:18 +0000128 /* Store the token that zCsr points to in tname. */
129 tname.z = zCsr;
130 tname.n = len;
131
132 /* Advance zCsr to the next token. Store that token type in 'token',
drh85b623f2007-12-13 21:54:09 +0000133 ** and its length in 'len' (to be used next iteration of this loop).
drh1f01ec12005-02-15 21:36:18 +0000134 */
135 do {
136 zCsr += len;
137 len = sqlite3GetToken(zCsr, &token);
138 }while( token==TK_SPACE );
139 assert( len>0 );
140
141 /* Variable 'dist' stores the number of tokens read since the most
142 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
143 ** token is read and 'dist' equals 2, the condition stated above
144 ** to be met.
145 **
146 ** Note that ON cannot be a database, table or column name, so
147 ** there is no need to worry about syntax like
148 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
149 */
150 dist++;
151 if( token==TK_DOT || token==TK_ON ){
152 dist = 0;
153 }
154 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
155
156 /* Variable tname now contains the token that is the old table-name
157 ** in the CREATE TRIGGER statement.
158 */
drh8e5b5f82008-02-09 14:30:29 +0000159 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql,
drh1f01ec12005-02-15 21:36:18 +0000160 zTableName, tname.z+tname.n);
drh633e6d52008-07-28 19:34:53 +0000161 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
drh1f01ec12005-02-15 21:36:18 +0000162 }
163}
164#endif /* !SQLITE_OMIT_TRIGGER */
165
166/*
167** Register built-in functions used to help implement ALTER TABLE
168*/
169void sqlite3AlterFunctions(sqlite3 *db){
drh5d414832008-07-15 14:47:18 +0000170 sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
171 renameTableFunc, 0, 0);
drh1f01ec12005-02-15 21:36:18 +0000172#ifndef SQLITE_OMIT_TRIGGER
drh5d414832008-07-15 14:47:18 +0000173 sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
174 renameTriggerFunc, 0, 0);
drh1f01ec12005-02-15 21:36:18 +0000175#endif
drh1f01ec12005-02-15 21:36:18 +0000176}
177
drhd0e4a6c2005-02-15 20:47:57 +0000178/*
danielk197719a8e7e2005-03-17 05:03:38 +0000179** Generate the text of a WHERE expression which can be used to select all
180** temporary triggers on table pTab from the sqlite_temp_master table. If
181** table pTab has no temporary triggers, or is itself stored in the
182** temporary database, NULL is returned.
183*/
184static char *whereTempTriggers(Parse *pParse, Table *pTab){
185 Trigger *pTrig;
186 char *zWhere = 0;
187 char *tmp = 0;
danielk1977e501b892006-01-09 06:29:47 +0000188 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
danielk1977da184232006-01-05 11:34:32 +0000189
190 /* If the table is not located in the temp-db (in which case NULL is
191 ** returned, loop through the tables list of triggers. For each trigger
192 ** that is not part of the temp-db schema, add a clause to the WHERE
193 ** expression being built up in zWhere.
194 */
195 if( pTab->pSchema!=pTempSchema ){
danielk19771e536952007-08-16 10:09:01 +0000196 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000197 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
danielk1977da184232006-01-05 11:34:32 +0000198 if( pTrig->pSchema==pTempSchema ){
danielk197719a8e7e2005-03-17 05:03:38 +0000199 if( !zWhere ){
danielk19771e536952007-08-16 10:09:01 +0000200 zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
danielk197719a8e7e2005-03-17 05:03:38 +0000201 }else{
202 tmp = zWhere;
danielk19771e536952007-08-16 10:09:01 +0000203 zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
drh633e6d52008-07-28 19:34:53 +0000204 sqlite3DbFree(db, tmp);
danielk197719a8e7e2005-03-17 05:03:38 +0000205 }
206 }
207 }
208 }
209 return zWhere;
210}
211
212/*
213** Generate code to drop and reload the internal representation of table
214** pTab from the database, including triggers and temporary triggers.
215** Argument zName is the name of the table in the database schema at
216** the time the generated code is executed. This can be different from
217** pTab->zName if this function is being called to code part of an
218** "ALTER TABLE RENAME TO" statement.
219*/
220static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
221 Vdbe *v;
222 char *zWhere;
danielk1977da184232006-01-05 11:34:32 +0000223 int iDb; /* Index of database containing pTab */
danielk197719a8e7e2005-03-17 05:03:38 +0000224#ifndef SQLITE_OMIT_TRIGGER
225 Trigger *pTrig;
226#endif
227
228 v = sqlite3GetVdbe(pParse);
229 if( !v ) return;
drh1fee73e2007-08-29 04:00:57 +0000230 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000231 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
232 assert( iDb>=0 );
danielk197719a8e7e2005-03-17 05:03:38 +0000233
234#ifndef SQLITE_OMIT_TRIGGER
235 /* Drop any table triggers from the internal schema. */
236 for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
danielk1977da184232006-01-05 11:34:32 +0000237 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
238 assert( iTrigDb==iDb || iTrigDb==1 );
drh66a51672008-01-03 00:01:23 +0000239 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->name, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000240 }
241#endif
242
243 /* Drop the table and index from the internal schema */
drh66a51672008-01-03 00:01:23 +0000244 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000245
246 /* Reload the table, index and permanent trigger schemas. */
danielk19771e536952007-08-16 10:09:01 +0000247 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000248 if( !zWhere ) return;
drh66a51672008-01-03 00:01:23 +0000249 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
danielk197719a8e7e2005-03-17 05:03:38 +0000250
251#ifndef SQLITE_OMIT_TRIGGER
252 /* Now, if the table is not stored in the temp database, reload any temp
253 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
254 */
drhd9cb6ac2005-10-20 07:28:17 +0000255 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
drh66a51672008-01-03 00:01:23 +0000256 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
danielk197719a8e7e2005-03-17 05:03:38 +0000257 }
258#endif
259}
260
261/*
drhd0e4a6c2005-02-15 20:47:57 +0000262** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
263** command.
264*/
265void sqlite3AlterRenameTable(
266 Parse *pParse, /* Parser context. */
267 SrcList *pSrc, /* The table to rename. */
268 Token *pName /* The new table name. */
269){
270 int iDb; /* Database that contains the table */
271 char *zDb; /* Name of database iDb */
272 Table *pTab; /* Table being renamed */
273 char *zName = 0; /* NULL-terminated version of pName */
drhd0e4a6c2005-02-15 20:47:57 +0000274 sqlite3 *db = pParse->db; /* Database connection */
drh4e5dd852007-05-15 03:56:49 +0000275 int nTabName; /* Number of UTF-8 characters in zTabName */
276 const char *zTabName; /* Original name of the table */
drhd0e4a6c2005-02-15 20:47:57 +0000277 Vdbe *v;
278#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000279 char *zWhere = 0; /* Where clause to locate temp triggers */
drhd0e4a6c2005-02-15 20:47:57 +0000280#endif
danielk1977182c4ba2007-06-27 15:53:34 +0000281 int isVirtualRename = 0; /* True if this is a v-table with an xRename() */
drhd0e4a6c2005-02-15 20:47:57 +0000282
drh17435752007-08-16 04:30:38 +0000283 if( db->mallocFailed ) goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000284 assert( pSrc->nSrc==1 );
drh1fee73e2007-08-29 04:00:57 +0000285 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhd0e4a6c2005-02-15 20:47:57 +0000286
drhca424112008-01-25 15:04:48 +0000287 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
drhd0e4a6c2005-02-15 20:47:57 +0000288 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. */
drh17435752007-08-16 04:30:38 +0000293 zName = sqlite3NameFromToken(db, pName);
drhd0e4a6c2005-02-15 20:47:57 +0000294 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
danielk197761116ae2007-12-13 08:15:30 +0000316#ifndef SQLITE_OMIT_VIEW
317 if( pTab->pSelect ){
318 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
319 goto exit_rename_table;
320 }
321#endif
322
drhd0e4a6c2005-02-15 20:47:57 +0000323#ifndef SQLITE_OMIT_AUTHORIZATION
324 /* Invoke the authorization callback. */
325 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
326 goto exit_rename_table;
327 }
328#endif
329
danielk1977182c4ba2007-06-27 15:53:34 +0000330#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19775c558862007-06-27 17:09:24 +0000331 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
332 goto exit_rename_table;
333 }
danielk1977182c4ba2007-06-27 15:53:34 +0000334 if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
335 isVirtualRename = 1;
336 }
337#endif
338
drhd0e4a6c2005-02-15 20:47:57 +0000339 /* Begin a transaction and code the VerifyCookie for database iDb.
340 ** Then modify the schema cookie (since the ALTER TABLE modifies the
danielk1977182c4ba2007-06-27 15:53:34 +0000341 ** schema). Open a statement transaction if the table is a virtual
342 ** table.
drhd0e4a6c2005-02-15 20:47:57 +0000343 */
344 v = sqlite3GetVdbe(pParse);
345 if( v==0 ){
346 goto exit_rename_table;
347 }
danielk1977182c4ba2007-06-27 15:53:34 +0000348 sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
drh9cbf3422008-01-17 16:22:13 +0000349 sqlite3ChangeCookie(pParse, iDb);
drhd0e4a6c2005-02-15 20:47:57 +0000350
danielk1977182c4ba2007-06-27 15:53:34 +0000351 /* If this is a virtual table, invoke the xRename() function if
352 ** one is defined. The xRename() callback will modify the names
353 ** of any resources used by the v-table implementation (including other
354 ** SQLite tables) that are identified by the name of the virtual table.
355 */
356#ifndef SQLITE_OMIT_VIRTUALTABLE
357 if( isVirtualRename ){
danielk1977a29f18c2008-01-04 11:01:03 +0000358 int i = ++pParse->nMem;
drh4c583122008-01-04 22:01:03 +0000359 sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
danielk19776dbee812008-01-03 18:39:41 +0000360 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pTab->pVtab, P4_VTAB);
danielk1977182c4ba2007-06-27 15:53:34 +0000361 }
362#endif
363
drh4e5dd852007-05-15 03:56:49 +0000364 /* figure out how many UTF-8 characters are in zName */
365 zTabName = pTab->zName;
drh9a087a92007-05-15 14:34:32 +0000366 nTabName = sqlite3Utf8CharLen(zTabName, -1);
drh4e5dd852007-05-15 03:56:49 +0000367
drhd0e4a6c2005-02-15 20:47:57 +0000368 /* Modify the sqlite_master table to use the new table name. */
369 sqlite3NestedParse(pParse,
370 "UPDATE %Q.%s SET "
371#ifdef SQLITE_OMIT_TRIGGER
372 "sql = sqlite_rename_table(sql, %Q), "
373#else
374 "sql = CASE "
375 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
376 "ELSE sqlite_rename_table(sql, %Q) END, "
377#endif
378 "tbl_name = %Q, "
379 "name = CASE "
380 "WHEN type='table' THEN %Q "
381 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
drha21a9292007-10-20 20:58:57 +0000382 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
drhd0e4a6c2005-02-15 20:47:57 +0000383 "ELSE name END "
384 "WHERE tbl_name=%Q AND "
385 "(type='table' OR type='index' OR type='trigger');",
386 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
387#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000388 zName,
drhd0e4a6c2005-02-15 20:47:57 +0000389#endif
drh4e5dd852007-05-15 03:56:49 +0000390 zName, nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000391 );
392
393#ifndef SQLITE_OMIT_AUTOINCREMENT
394 /* If the sqlite_sequence table exists in this database, then update
395 ** it with the new table name.
396 */
397 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
398 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000399 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
drhd0e4a6c2005-02-15 20:47:57 +0000400 zDb, zName, pTab->zName);
401 }
402#endif
403
404#ifndef SQLITE_OMIT_TRIGGER
405 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
406 ** table. Don't do this if the table being ALTERed is itself located in
407 ** the temp database.
408 */
drhd9cb6ac2005-10-20 07:28:17 +0000409 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000410 sqlite3NestedParse(pParse,
411 "UPDATE sqlite_temp_master SET "
412 "sql = sqlite_rename_trigger(sql, %Q), "
413 "tbl_name = %Q "
414 "WHERE %s;", zName, zName, zWhere);
drh633e6d52008-07-28 19:34:53 +0000415 sqlite3DbFree(db, zWhere);
drhd0e4a6c2005-02-15 20:47:57 +0000416 }
417#endif
418
danielk197719a8e7e2005-03-17 05:03:38 +0000419 /* Drop and reload the internal table schema. */
420 reloadTableSchema(pParse, pTab, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000421
422exit_rename_table:
drh633e6d52008-07-28 19:34:53 +0000423 sqlite3SrcListDelete(db, pSrc);
424 sqlite3DbFree(db, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000425}
danielk197719a8e7e2005-03-17 05:03:38 +0000426
427
428/*
429** This function is called after an "ALTER TABLE ... ADD" statement
430** has been parsed. Argument pColDef contains the text of the new
431** column definition.
432**
433** The Table structure pParse->pNewTable was extended to include
434** the new column during parsing.
435*/
436void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
437 Table *pNew; /* Copy of pParse->pNewTable */
438 Table *pTab; /* Table being altered */
439 int iDb; /* Database number */
440 const char *zDb; /* Database name */
441 const char *zTab; /* Table name */
442 char *zCol; /* Null-terminated column definition */
443 Column *pCol; /* The new column */
444 Expr *pDflt; /* Default value for the new column */
drh17435752007-08-16 04:30:38 +0000445 sqlite3 *db; /* The database connection; */
danielk197719a8e7e2005-03-17 05:03:38 +0000446
danielk1977f150c9d2008-10-30 17:21:12 +0000447 db = pParse->db;
448 if( pParse->nErr || db->mallocFailed ) return;
danielk197719a8e7e2005-03-17 05:03:38 +0000449 pNew = pParse->pNewTable;
450 assert( pNew );
451
drh1fee73e2007-08-29 04:00:57 +0000452 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000453 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
454 zDb = db->aDb[iDb].zName;
danielk197719a8e7e2005-03-17 05:03:38 +0000455 zTab = pNew->zName;
456 pCol = &pNew->aCol[pNew->nCol-1];
457 pDflt = pCol->pDflt;
drh17435752007-08-16 04:30:38 +0000458 pTab = sqlite3FindTable(db, zTab, zDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000459 assert( pTab );
460
drh81f2ccd2006-01-31 14:28:44 +0000461#ifndef SQLITE_OMIT_AUTHORIZATION
462 /* Invoke the authorization callback. */
463 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
464 return;
465 }
466#endif
467
danielk197719a8e7e2005-03-17 05:03:38 +0000468 /* If the default value for the new column was specified with a
469 ** literal NULL, then set pDflt to 0. This simplifies checking
470 ** for an SQL NULL default below.
471 */
472 if( pDflt && pDflt->op==TK_NULL ){
473 pDflt = 0;
474 }
475
476 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
477 ** If there is a NOT NULL constraint, then the default value for the
478 ** column must not be NULL.
479 */
480 if( pCol->isPrimKey ){
481 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
482 return;
483 }
484 if( pNew->pIndex ){
485 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
486 return;
487 }
488 if( pCol->notNull && !pDflt ){
489 sqlite3ErrorMsg(pParse,
490 "Cannot add a NOT NULL column with default value NULL");
491 return;
492 }
493
494 /* Ensure the default expression is something that sqlite3ValueFromExpr()
495 ** can handle (i.e. not CURRENT_TIME etc.)
496 */
497 if( pDflt ){
498 sqlite3_value *pVal;
danielk19771e536952007-08-16 10:09:01 +0000499 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
drh17435752007-08-16 04:30:38 +0000500 db->mallocFailed = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000501 return;
502 }
503 if( !pVal ){
504 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
505 return;
506 }
507 sqlite3ValueFree(pVal);
508 }
509
510 /* Modify the CREATE TABLE statement. */
drh17435752007-08-16 04:30:38 +0000511 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000512 if( zCol ){
513 char *zEnd = &zCol[pColDef->n-1];
drh47b4b292005-03-19 14:45:48 +0000514 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000515 *zEnd-- = '\0';
516 }
517 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000518 "UPDATE \"%w\".%s SET "
drha21a9292007-10-20 20:58:57 +0000519 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
danielk197719a8e7e2005-03-17 05:03:38 +0000520 "WHERE type = 'table' AND name = %Q",
521 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
522 zTab
523 );
drh633e6d52008-07-28 19:34:53 +0000524 sqlite3DbFree(db, zCol);
danielk197719a8e7e2005-03-17 05:03:38 +0000525 }
526
527 /* If the default value of the new column is NULL, then set the file
528 ** format to 2. If the default value of the new column is not NULL,
529 ** the file format becomes 3.
530 */
drhfdd6e852005-12-16 01:06:16 +0000531 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
danielk197719a8e7e2005-03-17 05:03:38 +0000532
533 /* Reload the schema of the modified table. */
534 reloadTableSchema(pParse, pTab, pTab->zName);
535}
536
drhfdd6e852005-12-16 01:06:16 +0000537/*
danielk197719a8e7e2005-03-17 05:03:38 +0000538** This function is called by the parser after the table-name in
539** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
540** pSrc is the full-name of the table being altered.
541**
542** This routine makes a (partial) copy of the Table structure
543** for the table being altered and sets Parse.pNewTable to point
544** to it. Routines called by the parser as the column definition
545** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
546** the copy. The copy of the Table structure is deleted by tokenize.c
547** after parsing is finished.
548**
549** Routine sqlite3AlterFinishAddColumn() will be called to complete
550** coding the "ALTER TABLE ... ADD" statement.
551*/
552void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
553 Table *pNew;
554 Table *pTab;
555 Vdbe *v;
556 int iDb;
557 int i;
558 int nAlloc;
drh17435752007-08-16 04:30:38 +0000559 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000560
561 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000562 assert( pParse->pNewTable==0 );
drh1fee73e2007-08-29 04:00:57 +0000563 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000564 if( db->mallocFailed ) goto exit_begin_add_column;
drhca424112008-01-25 15:04:48 +0000565 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
danielk197719a8e7e2005-03-17 05:03:38 +0000566 if( !pTab ) goto exit_begin_add_column;
567
danielk19775ee9d692006-06-21 12:36:25 +0000568#ifndef SQLITE_OMIT_VIRTUALTABLE
569 if( IsVirtual(pTab) ){
570 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
571 goto exit_begin_add_column;
572 }
573#endif
574
danielk197719a8e7e2005-03-17 05:03:38 +0000575 /* Make sure this is not an attempt to ALTER a view. */
576 if( pTab->pSelect ){
577 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
578 goto exit_begin_add_column;
579 }
580
581 assert( pTab->addColOffset>0 );
drh17435752007-08-16 04:30:38 +0000582 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000583
584 /* Put a copy of the Table struct in Parse.pNewTable for the
585 ** sqlite3AddColumn() function and friends to modify.
586 */
drh17435752007-08-16 04:30:38 +0000587 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
danielk197719a8e7e2005-03-17 05:03:38 +0000588 if( !pNew ) goto exit_begin_add_column;
589 pParse->pNewTable = pNew;
drh0bbaa1b2005-08-19 19:14:12 +0000590 pNew->nRef = 1;
drh633e6d52008-07-28 19:34:53 +0000591 pNew->db = db;
danielk197719a8e7e2005-03-17 05:03:38 +0000592 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000593 assert( pNew->nCol>0 );
594 nAlloc = (((pNew->nCol-1)/8)*8)+8;
595 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197726783a52007-08-29 14:06:22 +0000596 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
drh17435752007-08-16 04:30:38 +0000597 pNew->zName = sqlite3DbStrDup(db, pTab->zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000598 if( !pNew->aCol || !pNew->zName ){
drh17435752007-08-16 04:30:38 +0000599 db->mallocFailed = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000600 goto exit_begin_add_column;
601 }
602 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
603 for(i=0; i<pNew->nCol; i++){
604 Column *pCol = &pNew->aCol[i];
drh17435752007-08-16 04:30:38 +0000605 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
drhff22e182006-02-09 02:56:02 +0000606 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000607 pCol->zType = 0;
608 pCol->pDflt = 0;
609 }
drh17435752007-08-16 04:30:38 +0000610 pNew->pSchema = db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000611 pNew->addColOffset = pTab->addColOffset;
drhed8a3bb2005-06-06 21:19:56 +0000612 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000613
614 /* Begin a transaction and increment the schema cookie. */
615 sqlite3BeginWriteOperation(pParse, 0, iDb);
616 v = sqlite3GetVdbe(pParse);
617 if( !v ) goto exit_begin_add_column;
drh9cbf3422008-01-17 16:22:13 +0000618 sqlite3ChangeCookie(pParse, iDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000619
620exit_begin_add_column:
drh633e6d52008-07-28 19:34:53 +0000621 sqlite3SrcListDelete(db, pSrc);
danielk197719a8e7e2005-03-17 05:03:38 +0000622 return;
623}
drhd0e4a6c2005-02-15 20:47:57 +0000624#endif /* SQLITE_ALTER_TABLE */