drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 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 | ** |
danielk1977 | cdc3a6b | 2007-08-25 13:09:26 +0000 | [diff] [blame] | 15 | ** $Id: alter.c,v 1.30 2007/08/25 13:09:26 danielk1977 Exp $ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 19 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** The code in this file only exists if we are not omitting the |
| 22 | ** ALTER TABLE logic from the build. |
| 23 | */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 24 | #ifndef SQLITE_OMIT_ALTERTABLE |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 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 |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame] | 31 | ** the CREATE TABLE or CREATE INDEX statement is replaced with the third |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 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; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 50 | unsigned char const *zCsr = zSql; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 51 | int len = 0; |
| 52 | char *zRet; |
| 53 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 54 | sqlite3 *db = sqlite3_user_data(context); |
| 55 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 56 | /* The principle used to locate the table name in the CREATE TABLE |
| 57 | ** statement is that the table name is the first token that is immediatedly |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 58 | ** followed by a left parenthesis - TK_LP - or "USING" TK_USING. |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 59 | */ |
| 60 | if( zSql ){ |
| 61 | do { |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 62 | if( !*zCsr ){ |
| 63 | /* Ran out of input before finding an opening bracket. Return NULL. */ |
| 64 | return; |
| 65 | } |
| 66 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 67 | /* 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', |
| 72 | ** and it's length in 'len' (to be used next iteration of this loop). |
| 73 | */ |
| 74 | do { |
| 75 | zCsr += len; |
| 76 | len = sqlite3GetToken(zCsr, &token); |
| 77 | } while( token==TK_SPACE ); |
| 78 | assert( len>0 ); |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 79 | } while( token!=TK_LP && token!=TK_USING ); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 80 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 81 | zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql, |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 82 | zTableName, tname.z+tname.n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 83 | sqlite3_result_text(context, zRet, -1, sqlite3_free); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | #ifndef SQLITE_OMIT_TRIGGER |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame] | 88 | /* This function is used by SQL generated to implement the |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 89 | ** 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 |
drh | f11c34d | 2006-09-08 12:27:36 +0000 | [diff] [blame] | 91 | ** TRIGGER statement is replaced with the third argument and the result |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 92 | ** returned. This is analagous to renameTableFunc() above, except for CREATE |
| 93 | ** TRIGGER, not CREATE INDEX and CREATE TABLE. |
| 94 | */ |
| 95 | static 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; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 106 | unsigned char const *zCsr = zSql; |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 107 | int len = 0; |
| 108 | char *zRet; |
| 109 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 110 | sqlite3 *db = sqlite3_user_data(context); |
| 111 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 112 | /* 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 { |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 119 | |
| 120 | if( !*zCsr ){ |
| 121 | /* Ran out of input before finding the table name. Return NULL. */ |
| 122 | return; |
| 123 | } |
| 124 | |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 125 | /* 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', |
| 130 | ** and it's length in 'len' (to be used next iteration of this loop). |
| 131 | */ |
| 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 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 156 | zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql, |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 157 | zTableName, tname.z+tname.n); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 158 | sqlite3_result_text(context, zRet, -1, sqlite3_free); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | #endif /* !SQLITE_OMIT_TRIGGER */ |
| 162 | |
| 163 | /* |
| 164 | ** Register built-in functions used to help implement ALTER TABLE |
| 165 | */ |
| 166 | void sqlite3AlterFunctions(sqlite3 *db){ |
| 167 | static const struct { |
| 168 | char *zName; |
| 169 | signed char nArg; |
| 170 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **); |
| 171 | } aFuncs[] = { |
| 172 | { "sqlite_rename_table", 2, renameTableFunc}, |
| 173 | #ifndef SQLITE_OMIT_TRIGGER |
| 174 | { "sqlite_rename_trigger", 2, renameTriggerFunc}, |
| 175 | #endif |
| 176 | }; |
| 177 | int i; |
| 178 | |
| 179 | for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 180 | sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 181 | SQLITE_UTF8, (void *)db, aFuncs[i].xFunc, 0, 0); |
drh | 1f01ec1 | 2005-02-15 21:36:18 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 185 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 186 | ** Generate the text of a WHERE expression which can be used to select all |
| 187 | ** temporary triggers on table pTab from the sqlite_temp_master table. If |
| 188 | ** table pTab has no temporary triggers, or is itself stored in the |
| 189 | ** temporary database, NULL is returned. |
| 190 | */ |
| 191 | static char *whereTempTriggers(Parse *pParse, Table *pTab){ |
| 192 | Trigger *pTrig; |
| 193 | char *zWhere = 0; |
| 194 | char *tmp = 0; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 195 | const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 196 | |
| 197 | /* If the table is not located in the temp-db (in which case NULL is |
| 198 | ** returned, loop through the tables list of triggers. For each trigger |
| 199 | ** that is not part of the temp-db schema, add a clause to the WHERE |
| 200 | ** expression being built up in zWhere. |
| 201 | */ |
| 202 | if( pTab->pSchema!=pTempSchema ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 203 | sqlite3 *db = pParse->db; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 204 | for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 205 | if( pTrig->pSchema==pTempSchema ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 206 | if( !zWhere ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 207 | zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 208 | }else{ |
| 209 | tmp = zWhere; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 210 | zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 211 | sqlite3_free(tmp); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | return zWhere; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | ** Generate code to drop and reload the internal representation of table |
| 221 | ** pTab from the database, including triggers and temporary triggers. |
| 222 | ** Argument zName is the name of the table in the database schema at |
| 223 | ** the time the generated code is executed. This can be different from |
| 224 | ** pTab->zName if this function is being called to code part of an |
| 225 | ** "ALTER TABLE RENAME TO" statement. |
| 226 | */ |
| 227 | static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ |
| 228 | Vdbe *v; |
| 229 | char *zWhere; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 230 | int iDb; /* Index of database containing pTab */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 231 | #ifndef SQLITE_OMIT_TRIGGER |
| 232 | Trigger *pTrig; |
| 233 | #endif |
| 234 | |
| 235 | v = sqlite3GetVdbe(pParse); |
| 236 | if( !v ) return; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 237 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 238 | assert( iDb>=0 ); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 239 | |
| 240 | #ifndef SQLITE_OMIT_TRIGGER |
| 241 | /* Drop any table triggers from the internal schema. */ |
| 242 | for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 243 | int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); |
| 244 | assert( iTrigDb==iDb || iTrigDb==1 ); |
| 245 | sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 246 | } |
| 247 | #endif |
| 248 | |
| 249 | /* Drop the table and index from the internal schema */ |
| 250 | sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0); |
| 251 | |
| 252 | /* Reload the table, index and permanent trigger schemas. */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 253 | zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 254 | if( !zWhere ) return; |
| 255 | sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC); |
| 256 | |
| 257 | #ifndef SQLITE_OMIT_TRIGGER |
| 258 | /* Now, if the table is not stored in the temp database, reload any temp |
| 259 | ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. |
| 260 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 261 | if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 262 | sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC); |
| 263 | } |
| 264 | #endif |
| 265 | } |
| 266 | |
| 267 | /* |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 268 | ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" |
| 269 | ** command. |
| 270 | */ |
| 271 | void sqlite3AlterRenameTable( |
| 272 | Parse *pParse, /* Parser context. */ |
| 273 | SrcList *pSrc, /* The table to rename. */ |
| 274 | Token *pName /* The new table name. */ |
| 275 | ){ |
| 276 | int iDb; /* Database that contains the table */ |
| 277 | char *zDb; /* Name of database iDb */ |
| 278 | Table *pTab; /* Table being renamed */ |
| 279 | char *zName = 0; /* NULL-terminated version of pName */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 280 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 281 | int nTabName; /* Number of UTF-8 characters in zTabName */ |
| 282 | const char *zTabName; /* Original name of the table */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 283 | Vdbe *v; |
| 284 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 285 | char *zWhere = 0; /* Where clause to locate temp triggers */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 286 | #endif |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 287 | int isVirtualRename = 0; /* True if this is a v-table with an xRename() */ |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 288 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 289 | if( db->mallocFailed ) goto exit_rename_table; |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 290 | assert( pSrc->nSrc==1 ); |
| 291 | |
| 292 | pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase); |
| 293 | if( !pTab ) goto exit_rename_table; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 294 | iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 295 | zDb = db->aDb[iDb].zName; |
| 296 | |
| 297 | /* Get a NULL terminated version of the new table name. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 298 | zName = sqlite3NameFromToken(db, pName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 299 | if( !zName ) goto exit_rename_table; |
| 300 | |
| 301 | /* Check that a table or index named 'zName' does not already exist |
| 302 | ** in database iDb. If so, this is an error. |
| 303 | */ |
| 304 | if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ |
| 305 | sqlite3ErrorMsg(pParse, |
| 306 | "there is already another table or index with this name: %s", zName); |
| 307 | goto exit_rename_table; |
| 308 | } |
| 309 | |
| 310 | /* Make sure it is not a system table being altered, or a reserved name |
| 311 | ** that the table is being renamed to. |
| 312 | */ |
| 313 | if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){ |
| 314 | sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); |
| 315 | goto exit_rename_table; |
| 316 | } |
| 317 | if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
| 318 | goto exit_rename_table; |
| 319 | } |
| 320 | |
| 321 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 322 | /* Invoke the authorization callback. */ |
| 323 | if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
| 324 | goto exit_rename_table; |
| 325 | } |
| 326 | #endif |
| 327 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 328 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | 5c55886 | 2007-06-27 17:09:24 +0000 | [diff] [blame] | 329 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
| 330 | goto exit_rename_table; |
| 331 | } |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 332 | if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){ |
| 333 | isVirtualRename = 1; |
| 334 | } |
| 335 | #endif |
| 336 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 337 | /* Begin a transaction and code the VerifyCookie for database iDb. |
| 338 | ** Then modify the schema cookie (since the ALTER TABLE modifies the |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 339 | ** schema). Open a statement transaction if the table is a virtual |
| 340 | ** table. |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 341 | */ |
| 342 | v = sqlite3GetVdbe(pParse); |
| 343 | if( v==0 ){ |
| 344 | goto exit_rename_table; |
| 345 | } |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 346 | sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 347 | sqlite3ChangeCookie(db, v, iDb); |
| 348 | |
danielk1977 | 182c4ba | 2007-06-27 15:53:34 +0000 | [diff] [blame] | 349 | /* If this is a virtual table, invoke the xRename() function if |
| 350 | ** one is defined. The xRename() callback will modify the names |
| 351 | ** of any resources used by the v-table implementation (including other |
| 352 | ** SQLite tables) that are identified by the name of the virtual table. |
| 353 | */ |
| 354 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 355 | if( isVirtualRename ){ |
| 356 | sqlite3VdbeOp3(v, OP_String8, 0, 0, zName, 0); |
| 357 | sqlite3VdbeOp3(v, OP_VRename, 0, 0, (const char*)pTab->pVtab, P3_VTAB); |
| 358 | } |
| 359 | #endif |
| 360 | |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 361 | /* figure out how many UTF-8 characters are in zName */ |
| 362 | zTabName = pTab->zName; |
drh | 9a087a9 | 2007-05-15 14:34:32 +0000 | [diff] [blame] | 363 | nTabName = sqlite3Utf8CharLen(zTabName, -1); |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 364 | |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 365 | /* 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 " |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 379 | "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) " |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 380 | "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 |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 385 | zName, |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 386 | #endif |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 387 | zName, nTabName, zTabName |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 388 | ); |
| 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, |
| 396 | "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q", |
| 397 | 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 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 406 | if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 407 | 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); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 412 | sqlite3_free(zWhere); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 413 | } |
| 414 | #endif |
| 415 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 416 | /* Drop and reload the internal table schema. */ |
| 417 | reloadTableSchema(pParse, pTab, zName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 418 | |
| 419 | exit_rename_table: |
| 420 | sqlite3SrcListDelete(pSrc); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 421 | sqlite3_free(zName); |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 422 | } |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 423 | |
| 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 | */ |
| 433 | void 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 */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 442 | sqlite3 *db; /* The database connection; */ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 443 | |
| 444 | if( pParse->nErr ) return; |
| 445 | pNew = pParse->pNewTable; |
| 446 | assert( pNew ); |
| 447 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 448 | db = pParse->db; |
| 449 | iDb = sqlite3SchemaToIndex(db, pNew->pSchema); |
| 450 | zDb = db->aDb[iDb].zName; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 451 | zTab = pNew->zName; |
| 452 | pCol = &pNew->aCol[pNew->nCol-1]; |
| 453 | pDflt = pCol->pDflt; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 454 | pTab = sqlite3FindTable(db, zTab, zDb); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 455 | assert( pTab ); |
| 456 | |
drh | 81f2ccd | 2006-01-31 14:28:44 +0000 | [diff] [blame] | 457 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 458 | /* Invoke the authorization callback. */ |
| 459 | if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ |
| 460 | return; |
| 461 | } |
| 462 | #endif |
| 463 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 464 | /* If the default value for the new column was specified with a |
| 465 | ** literal NULL, then set pDflt to 0. This simplifies checking |
| 466 | ** for an SQL NULL default below. |
| 467 | */ |
| 468 | if( pDflt && pDflt->op==TK_NULL ){ |
| 469 | pDflt = 0; |
| 470 | } |
| 471 | |
| 472 | /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. |
| 473 | ** If there is a NOT NULL constraint, then the default value for the |
| 474 | ** column must not be NULL. |
| 475 | */ |
| 476 | if( pCol->isPrimKey ){ |
| 477 | sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); |
| 478 | return; |
| 479 | } |
| 480 | if( pNew->pIndex ){ |
| 481 | sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); |
| 482 | return; |
| 483 | } |
| 484 | if( pCol->notNull && !pDflt ){ |
| 485 | sqlite3ErrorMsg(pParse, |
| 486 | "Cannot add a NOT NULL column with default value NULL"); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | /* Ensure the default expression is something that sqlite3ValueFromExpr() |
| 491 | ** can handle (i.e. not CURRENT_TIME etc.) |
| 492 | */ |
| 493 | if( pDflt ){ |
| 494 | sqlite3_value *pVal; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 495 | if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 496 | db->mallocFailed = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | if( !pVal ){ |
| 500 | sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); |
| 501 | return; |
| 502 | } |
| 503 | sqlite3ValueFree(pVal); |
| 504 | } |
| 505 | |
| 506 | /* Modify the CREATE TABLE statement. */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 507 | zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 508 | if( zCol ){ |
| 509 | char *zEnd = &zCol[pColDef->n-1]; |
drh | 47b4b29 | 2005-03-19 14:45:48 +0000 | [diff] [blame] | 510 | while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){ |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 511 | *zEnd-- = '\0'; |
| 512 | } |
| 513 | sqlite3NestedParse(pParse, |
| 514 | "UPDATE %Q.%s SET " |
danielk1977 | f0b5792 | 2005-03-28 00:07:16 +0000 | [diff] [blame] | 515 | "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d,length(sql)) " |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 516 | "WHERE type = 'table' AND name = %Q", |
| 517 | zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, |
| 518 | zTab |
| 519 | ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 520 | sqlite3_free(zCol); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* If the default value of the new column is NULL, then set the file |
| 524 | ** format to 2. If the default value of the new column is not NULL, |
| 525 | ** the file format becomes 3. |
| 526 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 527 | sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 528 | |
| 529 | /* Reload the schema of the modified table. */ |
| 530 | reloadTableSchema(pParse, pTab, pTab->zName); |
| 531 | } |
| 532 | |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 533 | /* |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 534 | ** This function is called by the parser after the table-name in |
| 535 | ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument |
| 536 | ** pSrc is the full-name of the table being altered. |
| 537 | ** |
| 538 | ** This routine makes a (partial) copy of the Table structure |
| 539 | ** for the table being altered and sets Parse.pNewTable to point |
| 540 | ** to it. Routines called by the parser as the column definition |
| 541 | ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to |
| 542 | ** the copy. The copy of the Table structure is deleted by tokenize.c |
| 543 | ** after parsing is finished. |
| 544 | ** |
| 545 | ** Routine sqlite3AlterFinishAddColumn() will be called to complete |
| 546 | ** coding the "ALTER TABLE ... ADD" statement. |
| 547 | */ |
| 548 | void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ |
| 549 | Table *pNew; |
| 550 | Table *pTab; |
| 551 | Vdbe *v; |
| 552 | int iDb; |
| 553 | int i; |
| 554 | int nAlloc; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 555 | sqlite3 *db = pParse->db; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 556 | |
| 557 | /* Look up the table being altered. */ |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 558 | assert( pParse->pNewTable==0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 559 | if( db->mallocFailed ) goto exit_begin_add_column; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 560 | pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase); |
| 561 | if( !pTab ) goto exit_begin_add_column; |
| 562 | |
danielk1977 | 5ee9d69 | 2006-06-21 12:36:25 +0000 | [diff] [blame] | 563 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 564 | if( IsVirtual(pTab) ){ |
| 565 | sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); |
| 566 | goto exit_begin_add_column; |
| 567 | } |
| 568 | #endif |
| 569 | |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 570 | /* Make sure this is not an attempt to ALTER a view. */ |
| 571 | if( pTab->pSelect ){ |
| 572 | sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); |
| 573 | goto exit_begin_add_column; |
| 574 | } |
| 575 | |
| 576 | assert( pTab->addColOffset>0 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 577 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 578 | |
| 579 | /* Put a copy of the Table struct in Parse.pNewTable for the |
| 580 | ** sqlite3AddColumn() function and friends to modify. |
| 581 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 582 | pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 583 | if( !pNew ) goto exit_begin_add_column; |
| 584 | pParse->pNewTable = pNew; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 585 | pNew->nRef = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 586 | pNew->nCol = pTab->nCol; |
danielk1977 | b3a2cce | 2005-03-27 01:56:30 +0000 | [diff] [blame] | 587 | assert( pNew->nCol>0 ); |
| 588 | nAlloc = (((pNew->nCol-1)/8)*8)+8; |
| 589 | assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); |
danielk1977 | cdc3a6b | 2007-08-25 13:09:26 +0000 | [diff] [blame] | 590 | pNew->aCol = (Column*)sqlite3MallocZero(sizeof(Column)*nAlloc); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 591 | pNew->zName = sqlite3DbStrDup(db, pTab->zName); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 592 | if( !pNew->aCol || !pNew->zName ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 593 | db->mallocFailed = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 594 | goto exit_begin_add_column; |
| 595 | } |
| 596 | memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); |
| 597 | for(i=0; i<pNew->nCol; i++){ |
| 598 | Column *pCol = &pNew->aCol[i]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 599 | pCol->zName = sqlite3DbStrDup(db, pCol->zName); |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 600 | pCol->zColl = 0; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 601 | pCol->zType = 0; |
| 602 | pCol->pDflt = 0; |
| 603 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 604 | pNew->pSchema = db->aDb[iDb].pSchema; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 605 | pNew->addColOffset = pTab->addColOffset; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 606 | pNew->nRef = 1; |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 607 | |
| 608 | /* Begin a transaction and increment the schema cookie. */ |
| 609 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
| 610 | v = sqlite3GetVdbe(pParse); |
| 611 | if( !v ) goto exit_begin_add_column; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 612 | sqlite3ChangeCookie(db, v, iDb); |
danielk1977 | 19a8e7e | 2005-03-17 05:03:38 +0000 | [diff] [blame] | 613 | |
| 614 | exit_begin_add_column: |
| 615 | sqlite3SrcListDelete(pSrc); |
| 616 | return; |
| 617 | } |
drh | d0e4a6c | 2005-02-15 20:47:57 +0000 | [diff] [blame] | 618 | #endif /* SQLITE_ALTER_TABLE */ |