blob: ec315da48721222cc17436305640c2803de07646 [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**
drh200a81d2008-08-08 14:19:41 +000015** $Id: alter.c,v 1.48 2008/08/08 14:19:41 drh 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
drhfa4a4b92008-03-19 21:45:51 +000054 sqlite3 *db = sqlite3_context_db_handle(context);
danielk19771e536952007-08-16 10:09:01 +000055
drh1f01ec12005-02-15 21:36:18 +000056 /* The principle used to locate the table name in the CREATE TABLE
drh73829452008-05-09 14:17:51 +000057 ** statement is that the table name is the first non-space token that
drh6aa1edc2008-07-07 12:44:58 +000058 ** is immediately followed by a TK_LP or TK_USING token.
drh1f01ec12005-02-15 21:36:18 +000059 */
60 if( zSql ){
61 do {
danielk1977dce872b2007-05-08 12:37:45 +000062 if( !*zCsr ){
63 /* Ran out of input before finding an opening bracket. Return NULL. */
64 return;
65 }
66
drh1f01ec12005-02-15 21:36:18 +000067 /* Store the token that zCsr points to in tname. */
68 tname.z = zCsr;
69 tname.n = len;
70
71 /* Advance zCsr to the next token. Store that token type in 'token',
drh85b623f2007-12-13 21:54:09 +000072 ** and its length in 'len' (to be used next iteration of this loop).
drh1f01ec12005-02-15 21:36:18 +000073 */
74 do {
75 zCsr += len;
76 len = sqlite3GetToken(zCsr, &token);
drh200a81d2008-08-08 14:19:41 +000077 } while( token==TK_SPACE );
drh1f01ec12005-02-15 21:36:18 +000078 assert( len>0 );
danielk1977182c4ba2007-06-27 15:53:34 +000079 } while( token!=TK_LP && token!=TK_USING );
drh1f01ec12005-02-15 21:36:18 +000080
drh8e5b5f82008-02-09 14:30:29 +000081 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql,
drh1f01ec12005-02-15 21:36:18 +000082 zTableName, tname.z+tname.n);
drh633e6d52008-07-28 19:34:53 +000083 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
drh1f01ec12005-02-15 21:36:18 +000084 }
85}
86
87#ifndef SQLITE_OMIT_TRIGGER
drhf11c34d2006-09-08 12:27:36 +000088/* This function is used by SQL generated to implement the
drh1f01ec12005-02-15 21:36:18 +000089** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
90** statement. The second is a table name. The table name in the CREATE
drhf11c34d2006-09-08 12:27:36 +000091** TRIGGER statement is replaced with the third argument and the result
drh1f01ec12005-02-15 21:36:18 +000092** returned. This is analagous to renameTableFunc() above, except for CREATE
93** TRIGGER, not CREATE INDEX and CREATE TABLE.
94*/
95static void renameTriggerFunc(
96 sqlite3_context *context,
97 int argc,
98 sqlite3_value **argv
99){
100 unsigned char const *zSql = sqlite3_value_text(argv[0]);
101 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
102
103 int token;
104 Token tname;
105 int dist = 3;
drh2646da72005-12-09 20:02:05 +0000106 unsigned char const *zCsr = zSql;
drh1f01ec12005-02-15 21:36:18 +0000107 int len = 0;
108 char *zRet;
109
drhfa4a4b92008-03-19 21:45:51 +0000110 sqlite3 *db = sqlite3_context_db_handle(context);
danielk19771e536952007-08-16 10:09:01 +0000111
drh1f01ec12005-02-15 21:36:18 +0000112 /* The principle used to locate the table name in the CREATE TRIGGER
113 ** statement is that the table name is the first token that is immediatedly
114 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
115 ** of TK_WHEN, TK_BEGIN or TK_FOR.
116 */
117 if( zSql ){
118 do {
danielk1977dce872b2007-05-08 12:37:45 +0000119
120 if( !*zCsr ){
121 /* Ran out of input before finding the table name. Return NULL. */
122 return;
123 }
124
drh1f01ec12005-02-15 21:36:18 +0000125 /* Store the token that zCsr points to in tname. */
126 tname.z = zCsr;
127 tname.n = len;
128
129 /* Advance zCsr to the next token. Store that token type in 'token',
drh85b623f2007-12-13 21:54:09 +0000130 ** and its length in 'len' (to be used next iteration of this loop).
drh1f01ec12005-02-15 21:36:18 +0000131 */
132 do {
133 zCsr += len;
134 len = sqlite3GetToken(zCsr, &token);
135 }while( token==TK_SPACE );
136 assert( len>0 );
137
138 /* Variable 'dist' stores the number of tokens read since the most
139 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
140 ** token is read and 'dist' equals 2, the condition stated above
141 ** to be met.
142 **
143 ** Note that ON cannot be a database, table or column name, so
144 ** there is no need to worry about syntax like
145 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
146 */
147 dist++;
148 if( token==TK_DOT || token==TK_ON ){
149 dist = 0;
150 }
151 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
152
153 /* Variable tname now contains the token that is the old table-name
154 ** in the CREATE TRIGGER statement.
155 */
drh8e5b5f82008-02-09 14:30:29 +0000156 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql,
drh1f01ec12005-02-15 21:36:18 +0000157 zTableName, tname.z+tname.n);
drh633e6d52008-07-28 19:34:53 +0000158 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
drh1f01ec12005-02-15 21:36:18 +0000159 }
160}
161#endif /* !SQLITE_OMIT_TRIGGER */
162
163/*
164** Register built-in functions used to help implement ALTER TABLE
165*/
166void sqlite3AlterFunctions(sqlite3 *db){
drh5d414832008-07-15 14:47:18 +0000167 sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
168 renameTableFunc, 0, 0);
drh1f01ec12005-02-15 21:36:18 +0000169#ifndef SQLITE_OMIT_TRIGGER
drh5d414832008-07-15 14:47:18 +0000170 sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
171 renameTriggerFunc, 0, 0);
drh1f01ec12005-02-15 21:36:18 +0000172#endif
drh1f01ec12005-02-15 21:36:18 +0000173}
174
drhd0e4a6c2005-02-15 20:47:57 +0000175/*
danielk197719a8e7e2005-03-17 05:03:38 +0000176** Generate the text of a WHERE expression which can be used to select all
177** temporary triggers on table pTab from the sqlite_temp_master table. If
178** table pTab has no temporary triggers, or is itself stored in the
179** temporary database, NULL is returned.
180*/
181static char *whereTempTriggers(Parse *pParse, Table *pTab){
182 Trigger *pTrig;
183 char *zWhere = 0;
184 char *tmp = 0;
danielk1977e501b892006-01-09 06:29:47 +0000185 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
danielk1977da184232006-01-05 11:34:32 +0000186
187 /* If the table is not located in the temp-db (in which case NULL is
188 ** returned, loop through the tables list of triggers. For each trigger
189 ** that is not part of the temp-db schema, add a clause to the WHERE
190 ** expression being built up in zWhere.
191 */
192 if( pTab->pSchema!=pTempSchema ){
danielk19771e536952007-08-16 10:09:01 +0000193 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000194 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
danielk1977da184232006-01-05 11:34:32 +0000195 if( pTrig->pSchema==pTempSchema ){
danielk197719a8e7e2005-03-17 05:03:38 +0000196 if( !zWhere ){
danielk19771e536952007-08-16 10:09:01 +0000197 zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
danielk197719a8e7e2005-03-17 05:03:38 +0000198 }else{
199 tmp = zWhere;
danielk19771e536952007-08-16 10:09:01 +0000200 zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
drh633e6d52008-07-28 19:34:53 +0000201 sqlite3DbFree(db, tmp);
danielk197719a8e7e2005-03-17 05:03:38 +0000202 }
203 }
204 }
205 }
206 return zWhere;
207}
208
209/*
210** Generate code to drop and reload the internal representation of table
211** pTab from the database, including triggers and temporary triggers.
212** Argument zName is the name of the table in the database schema at
213** the time the generated code is executed. This can be different from
214** pTab->zName if this function is being called to code part of an
215** "ALTER TABLE RENAME TO" statement.
216*/
217static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
218 Vdbe *v;
219 char *zWhere;
danielk1977da184232006-01-05 11:34:32 +0000220 int iDb; /* Index of database containing pTab */
danielk197719a8e7e2005-03-17 05:03:38 +0000221#ifndef SQLITE_OMIT_TRIGGER
222 Trigger *pTrig;
223#endif
224
225 v = sqlite3GetVdbe(pParse);
226 if( !v ) return;
drh1fee73e2007-08-29 04:00:57 +0000227 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000228 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
229 assert( iDb>=0 );
danielk197719a8e7e2005-03-17 05:03:38 +0000230
231#ifndef SQLITE_OMIT_TRIGGER
232 /* Drop any table triggers from the internal schema. */
233 for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
danielk1977da184232006-01-05 11:34:32 +0000234 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
235 assert( iTrigDb==iDb || iTrigDb==1 );
drh66a51672008-01-03 00:01:23 +0000236 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->name, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000237 }
238#endif
239
240 /* Drop the table and index from the internal schema */
drh66a51672008-01-03 00:01:23 +0000241 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000242
243 /* Reload the table, index and permanent trigger schemas. */
danielk19771e536952007-08-16 10:09:01 +0000244 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000245 if( !zWhere ) return;
drh66a51672008-01-03 00:01:23 +0000246 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
danielk197719a8e7e2005-03-17 05:03:38 +0000247
248#ifndef SQLITE_OMIT_TRIGGER
249 /* Now, if the table is not stored in the temp database, reload any temp
250 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
251 */
drhd9cb6ac2005-10-20 07:28:17 +0000252 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
drh66a51672008-01-03 00:01:23 +0000253 sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
danielk197719a8e7e2005-03-17 05:03:38 +0000254 }
255#endif
256}
257
258/*
drhd0e4a6c2005-02-15 20:47:57 +0000259** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
260** command.
261*/
262void sqlite3AlterRenameTable(
263 Parse *pParse, /* Parser context. */
264 SrcList *pSrc, /* The table to rename. */
265 Token *pName /* The new table name. */
266){
267 int iDb; /* Database that contains the table */
268 char *zDb; /* Name of database iDb */
269 Table *pTab; /* Table being renamed */
270 char *zName = 0; /* NULL-terminated version of pName */
drhd0e4a6c2005-02-15 20:47:57 +0000271 sqlite3 *db = pParse->db; /* Database connection */
drh4e5dd852007-05-15 03:56:49 +0000272 int nTabName; /* Number of UTF-8 characters in zTabName */
273 const char *zTabName; /* Original name of the table */
drhd0e4a6c2005-02-15 20:47:57 +0000274 Vdbe *v;
275#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000276 char *zWhere = 0; /* Where clause to locate temp triggers */
drhd0e4a6c2005-02-15 20:47:57 +0000277#endif
danielk1977182c4ba2007-06-27 15:53:34 +0000278 int isVirtualRename = 0; /* True if this is a v-table with an xRename() */
drhd0e4a6c2005-02-15 20:47:57 +0000279
drh17435752007-08-16 04:30:38 +0000280 if( db->mallocFailed ) goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000281 assert( pSrc->nSrc==1 );
drh1fee73e2007-08-29 04:00:57 +0000282 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhd0e4a6c2005-02-15 20:47:57 +0000283
drhca424112008-01-25 15:04:48 +0000284 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
drhd0e4a6c2005-02-15 20:47:57 +0000285 if( !pTab ) goto exit_rename_table;
danielk1977da184232006-01-05 11:34:32 +0000286 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drhd0e4a6c2005-02-15 20:47:57 +0000287 zDb = db->aDb[iDb].zName;
288
289 /* Get a NULL terminated version of the new table name. */
drh17435752007-08-16 04:30:38 +0000290 zName = sqlite3NameFromToken(db, pName);
drhd0e4a6c2005-02-15 20:47:57 +0000291 if( !zName ) goto exit_rename_table;
292
293 /* Check that a table or index named 'zName' does not already exist
294 ** in database iDb. If so, this is an error.
295 */
296 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
297 sqlite3ErrorMsg(pParse,
298 "there is already another table or index with this name: %s", zName);
299 goto exit_rename_table;
300 }
301
302 /* Make sure it is not a system table being altered, or a reserved name
303 ** that the table is being renamed to.
304 */
305 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
306 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
307 goto exit_rename_table;
308 }
309 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
310 goto exit_rename_table;
311 }
312
danielk197761116ae2007-12-13 08:15:30 +0000313#ifndef SQLITE_OMIT_VIEW
314 if( pTab->pSelect ){
315 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
316 goto exit_rename_table;
317 }
318#endif
319
drhd0e4a6c2005-02-15 20:47:57 +0000320#ifndef SQLITE_OMIT_AUTHORIZATION
321 /* Invoke the authorization callback. */
322 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
323 goto exit_rename_table;
324 }
325#endif
326
danielk1977182c4ba2007-06-27 15:53:34 +0000327#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19775c558862007-06-27 17:09:24 +0000328 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
329 goto exit_rename_table;
330 }
danielk1977182c4ba2007-06-27 15:53:34 +0000331 if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
332 isVirtualRename = 1;
333 }
334#endif
335
drhd0e4a6c2005-02-15 20:47:57 +0000336 /* Begin a transaction and code the VerifyCookie for database iDb.
337 ** Then modify the schema cookie (since the ALTER TABLE modifies the
danielk1977182c4ba2007-06-27 15:53:34 +0000338 ** schema). Open a statement transaction if the table is a virtual
339 ** table.
drhd0e4a6c2005-02-15 20:47:57 +0000340 */
341 v = sqlite3GetVdbe(pParse);
342 if( v==0 ){
343 goto exit_rename_table;
344 }
danielk1977182c4ba2007-06-27 15:53:34 +0000345 sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
drh9cbf3422008-01-17 16:22:13 +0000346 sqlite3ChangeCookie(pParse, iDb);
drhd0e4a6c2005-02-15 20:47:57 +0000347
danielk1977182c4ba2007-06-27 15:53:34 +0000348 /* If this is a virtual table, invoke the xRename() function if
349 ** one is defined. The xRename() callback will modify the names
350 ** of any resources used by the v-table implementation (including other
351 ** SQLite tables) that are identified by the name of the virtual table.
352 */
353#ifndef SQLITE_OMIT_VIRTUALTABLE
354 if( isVirtualRename ){
danielk1977a29f18c2008-01-04 11:01:03 +0000355 int i = ++pParse->nMem;
drh4c583122008-01-04 22:01:03 +0000356 sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
danielk19776dbee812008-01-03 18:39:41 +0000357 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pTab->pVtab, P4_VTAB);
danielk1977182c4ba2007-06-27 15:53:34 +0000358 }
359#endif
360
drh4e5dd852007-05-15 03:56:49 +0000361 /* figure out how many UTF-8 characters are in zName */
362 zTabName = pTab->zName;
drh9a087a92007-05-15 14:34:32 +0000363 nTabName = sqlite3Utf8CharLen(zTabName, -1);
drh4e5dd852007-05-15 03:56:49 +0000364
drhd0e4a6c2005-02-15 20:47:57 +0000365 /* Modify the sqlite_master table to use the new table name. */
366 sqlite3NestedParse(pParse,
367 "UPDATE %Q.%s SET "
368#ifdef SQLITE_OMIT_TRIGGER
369 "sql = sqlite_rename_table(sql, %Q), "
370#else
371 "sql = CASE "
372 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
373 "ELSE sqlite_rename_table(sql, %Q) END, "
374#endif
375 "tbl_name = %Q, "
376 "name = CASE "
377 "WHEN type='table' THEN %Q "
378 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
drha21a9292007-10-20 20:58:57 +0000379 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
drhd0e4a6c2005-02-15 20:47:57 +0000380 "ELSE name END "
381 "WHERE tbl_name=%Q AND "
382 "(type='table' OR type='index' OR type='trigger');",
383 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
384#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000385 zName,
drhd0e4a6c2005-02-15 20:47:57 +0000386#endif
drh4e5dd852007-05-15 03:56:49 +0000387 zName, nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000388 );
389
390#ifndef SQLITE_OMIT_AUTOINCREMENT
391 /* If the sqlite_sequence table exists in this database, then update
392 ** it with the new table name.
393 */
394 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
395 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000396 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
drhd0e4a6c2005-02-15 20:47:57 +0000397 zDb, zName, pTab->zName);
398 }
399#endif
400
401#ifndef SQLITE_OMIT_TRIGGER
402 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
403 ** table. Don't do this if the table being ALTERed is itself located in
404 ** the temp database.
405 */
drhd9cb6ac2005-10-20 07:28:17 +0000406 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000407 sqlite3NestedParse(pParse,
408 "UPDATE sqlite_temp_master SET "
409 "sql = sqlite_rename_trigger(sql, %Q), "
410 "tbl_name = %Q "
411 "WHERE %s;", zName, zName, zWhere);
drh633e6d52008-07-28 19:34:53 +0000412 sqlite3DbFree(db, zWhere);
drhd0e4a6c2005-02-15 20:47:57 +0000413 }
414#endif
415
danielk197719a8e7e2005-03-17 05:03:38 +0000416 /* Drop and reload the internal table schema. */
417 reloadTableSchema(pParse, pTab, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000418
419exit_rename_table:
drh633e6d52008-07-28 19:34:53 +0000420 sqlite3SrcListDelete(db, pSrc);
421 sqlite3DbFree(db, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000422}
danielk197719a8e7e2005-03-17 05:03:38 +0000423
424
425/*
426** This function is called after an "ALTER TABLE ... ADD" statement
427** has been parsed. Argument pColDef contains the text of the new
428** column definition.
429**
430** The Table structure pParse->pNewTable was extended to include
431** the new column during parsing.
432*/
433void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
434 Table *pNew; /* Copy of pParse->pNewTable */
435 Table *pTab; /* Table being altered */
436 int iDb; /* Database number */
437 const char *zDb; /* Database name */
438 const char *zTab; /* Table name */
439 char *zCol; /* Null-terminated column definition */
440 Column *pCol; /* The new column */
441 Expr *pDflt; /* Default value for the new column */
drh17435752007-08-16 04:30:38 +0000442 sqlite3 *db; /* The database connection; */
danielk197719a8e7e2005-03-17 05:03:38 +0000443
444 if( pParse->nErr ) return;
445 pNew = pParse->pNewTable;
446 assert( pNew );
447
drh17435752007-08-16 04:30:38 +0000448 db = pParse->db;
drh1fee73e2007-08-29 04:00:57 +0000449 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000450 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
451 zDb = db->aDb[iDb].zName;
danielk197719a8e7e2005-03-17 05:03:38 +0000452 zTab = pNew->zName;
453 pCol = &pNew->aCol[pNew->nCol-1];
454 pDflt = pCol->pDflt;
drh17435752007-08-16 04:30:38 +0000455 pTab = sqlite3FindTable(db, zTab, zDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000456 assert( pTab );
457
drh81f2ccd2006-01-31 14:28:44 +0000458#ifndef SQLITE_OMIT_AUTHORIZATION
459 /* Invoke the authorization callback. */
460 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
461 return;
462 }
463#endif
464
danielk197719a8e7e2005-03-17 05:03:38 +0000465 /* If the default value for the new column was specified with a
466 ** literal NULL, then set pDflt to 0. This simplifies checking
467 ** for an SQL NULL default below.
468 */
469 if( pDflt && pDflt->op==TK_NULL ){
470 pDflt = 0;
471 }
472
473 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
474 ** If there is a NOT NULL constraint, then the default value for the
475 ** column must not be NULL.
476 */
477 if( pCol->isPrimKey ){
478 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
479 return;
480 }
481 if( pNew->pIndex ){
482 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
483 return;
484 }
485 if( pCol->notNull && !pDflt ){
486 sqlite3ErrorMsg(pParse,
487 "Cannot add a NOT NULL column with default value NULL");
488 return;
489 }
490
491 /* Ensure the default expression is something that sqlite3ValueFromExpr()
492 ** can handle (i.e. not CURRENT_TIME etc.)
493 */
494 if( pDflt ){
495 sqlite3_value *pVal;
danielk19771e536952007-08-16 10:09:01 +0000496 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
drh17435752007-08-16 04:30:38 +0000497 db->mallocFailed = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000498 return;
499 }
500 if( !pVal ){
501 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
502 return;
503 }
504 sqlite3ValueFree(pVal);
505 }
506
507 /* Modify the CREATE TABLE statement. */
drh17435752007-08-16 04:30:38 +0000508 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000509 if( zCol ){
510 char *zEnd = &zCol[pColDef->n-1];
drh47b4b292005-03-19 14:45:48 +0000511 while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000512 *zEnd-- = '\0';
513 }
514 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000515 "UPDATE \"%w\".%s SET "
drha21a9292007-10-20 20:58:57 +0000516 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
danielk197719a8e7e2005-03-17 05:03:38 +0000517 "WHERE type = 'table' AND name = %Q",
518 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
519 zTab
520 );
drh633e6d52008-07-28 19:34:53 +0000521 sqlite3DbFree(db, zCol);
danielk197719a8e7e2005-03-17 05:03:38 +0000522 }
523
524 /* If the default value of the new column is NULL, then set the file
525 ** format to 2. If the default value of the new column is not NULL,
526 ** the file format becomes 3.
527 */
drhfdd6e852005-12-16 01:06:16 +0000528 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
danielk197719a8e7e2005-03-17 05:03:38 +0000529
530 /* Reload the schema of the modified table. */
531 reloadTableSchema(pParse, pTab, pTab->zName);
532}
533
drhfdd6e852005-12-16 01:06:16 +0000534/*
danielk197719a8e7e2005-03-17 05:03:38 +0000535** This function is called by the parser after the table-name in
536** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
537** pSrc is the full-name of the table being altered.
538**
539** This routine makes a (partial) copy of the Table structure
540** for the table being altered and sets Parse.pNewTable to point
541** to it. Routines called by the parser as the column definition
542** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
543** the copy. The copy of the Table structure is deleted by tokenize.c
544** after parsing is finished.
545**
546** Routine sqlite3AlterFinishAddColumn() will be called to complete
547** coding the "ALTER TABLE ... ADD" statement.
548*/
549void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
550 Table *pNew;
551 Table *pTab;
552 Vdbe *v;
553 int iDb;
554 int i;
555 int nAlloc;
drh17435752007-08-16 04:30:38 +0000556 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000557
558 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000559 assert( pParse->pNewTable==0 );
drh1fee73e2007-08-29 04:00:57 +0000560 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000561 if( db->mallocFailed ) goto exit_begin_add_column;
drhca424112008-01-25 15:04:48 +0000562 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
danielk197719a8e7e2005-03-17 05:03:38 +0000563 if( !pTab ) goto exit_begin_add_column;
564
danielk19775ee9d692006-06-21 12:36:25 +0000565#ifndef SQLITE_OMIT_VIRTUALTABLE
566 if( IsVirtual(pTab) ){
567 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
568 goto exit_begin_add_column;
569 }
570#endif
571
danielk197719a8e7e2005-03-17 05:03:38 +0000572 /* Make sure this is not an attempt to ALTER a view. */
573 if( pTab->pSelect ){
574 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
575 goto exit_begin_add_column;
576 }
577
578 assert( pTab->addColOffset>0 );
drh17435752007-08-16 04:30:38 +0000579 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000580
581 /* Put a copy of the Table struct in Parse.pNewTable for the
582 ** sqlite3AddColumn() function and friends to modify.
583 */
drh17435752007-08-16 04:30:38 +0000584 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
danielk197719a8e7e2005-03-17 05:03:38 +0000585 if( !pNew ) goto exit_begin_add_column;
586 pParse->pNewTable = pNew;
drh0bbaa1b2005-08-19 19:14:12 +0000587 pNew->nRef = 1;
drh633e6d52008-07-28 19:34:53 +0000588 pNew->db = db;
danielk197719a8e7e2005-03-17 05:03:38 +0000589 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000590 assert( pNew->nCol>0 );
591 nAlloc = (((pNew->nCol-1)/8)*8)+8;
592 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197726783a52007-08-29 14:06:22 +0000593 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
drh17435752007-08-16 04:30:38 +0000594 pNew->zName = sqlite3DbStrDup(db, pTab->zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000595 if( !pNew->aCol || !pNew->zName ){
drh17435752007-08-16 04:30:38 +0000596 db->mallocFailed = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000597 goto exit_begin_add_column;
598 }
599 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
600 for(i=0; i<pNew->nCol; i++){
601 Column *pCol = &pNew->aCol[i];
drh17435752007-08-16 04:30:38 +0000602 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
drhff22e182006-02-09 02:56:02 +0000603 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000604 pCol->zType = 0;
605 pCol->pDflt = 0;
606 }
drh17435752007-08-16 04:30:38 +0000607 pNew->pSchema = db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000608 pNew->addColOffset = pTab->addColOffset;
drhed8a3bb2005-06-06 21:19:56 +0000609 pNew->nRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000610
611 /* Begin a transaction and increment the schema cookie. */
612 sqlite3BeginWriteOperation(pParse, 0, iDb);
613 v = sqlite3GetVdbe(pParse);
614 if( !v ) goto exit_begin_add_column;
drh9cbf3422008-01-17 16:22:13 +0000615 sqlite3ChangeCookie(pParse, iDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000616
617exit_begin_add_column:
drh633e6d52008-07-28 19:34:53 +0000618 sqlite3SrcListDelete(db, pSrc);
danielk197719a8e7e2005-03-17 05:03:38 +0000619 return;
620}
drhd0e4a6c2005-02-15 20:47:57 +0000621#endif /* SQLITE_ALTER_TABLE */