annotate sqlite/alter.c @ 1581:68aec931b0b6 trunk

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