blob: 067cbb896db5b78c9e7d9012d6cce26351d96596 [file] [log] [blame]
drhd0e4a6c2005-02-15 20:47:57 +00001/*
2** 2005 February 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains C code routines that used to generate VDBE code
13** that implements the ALTER TABLE command.
drhd0e4a6c2005-02-15 20:47:57 +000014*/
15#include "sqliteInt.h"
16
drh1f01ec12005-02-15 21:36:18 +000017/*
18** The code in this file only exists if we are not omitting the
19** ALTER TABLE logic from the build.
20*/
drhd0e4a6c2005-02-15 20:47:57 +000021#ifndef SQLITE_OMIT_ALTERTABLE
drh1f01ec12005-02-15 21:36:18 +000022
23
24/*
25** This function is used by SQL generated to implement the
26** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
27** CREATE INDEX command. The second is a table name. The table name in
drhf11c34d2006-09-08 12:27:36 +000028** the CREATE TABLE or CREATE INDEX statement is replaced with the third
drh1f01ec12005-02-15 21:36:18 +000029** argument and the result returned. Examples:
30**
31** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
32** -> 'CREATE TABLE def(a, b, c)'
33**
34** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
35** -> 'CREATE INDEX i ON def(a, b, c)'
36*/
37static void renameTableFunc(
38 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +000039 int NotUsed,
drh1f01ec12005-02-15 21:36:18 +000040 sqlite3_value **argv
41){
42 unsigned char const *zSql = sqlite3_value_text(argv[0]);
43 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
44
45 int token;
46 Token tname;
drh2646da72005-12-09 20:02:05 +000047 unsigned char const *zCsr = zSql;
drh1f01ec12005-02-15 21:36:18 +000048 int len = 0;
49 char *zRet;
50
drhfa4a4b92008-03-19 21:45:51 +000051 sqlite3 *db = sqlite3_context_db_handle(context);
danielk19771e536952007-08-16 10:09:01 +000052
danielk197762c14b32008-11-19 09:05:26 +000053 UNUSED_PARAMETER(NotUsed);
54
drh1f01ec12005-02-15 21:36:18 +000055 /* The principle used to locate the table name in the CREATE TABLE
drh73829452008-05-09 14:17:51 +000056 ** statement is that the table name is the first non-space token that
drh6aa1edc2008-07-07 12:44:58 +000057 ** is immediately followed by a TK_LP or TK_USING token.
drh1f01ec12005-02-15 21:36:18 +000058 */
59 if( zSql ){
60 do {
danielk1977dce872b2007-05-08 12:37:45 +000061 if( !*zCsr ){
62 /* Ran out of input before finding an opening bracket. Return NULL. */
63 return;
64 }
65
drh1f01ec12005-02-15 21:36:18 +000066 /* Store the token that zCsr points to in tname. */
drhb7916a72009-05-27 10:31:29 +000067 tname.z = (char*)zCsr;
drh1f01ec12005-02-15 21:36:18 +000068 tname.n = len;
69
70 /* Advance zCsr to the next token. Store that token type in 'token',
drh85b623f2007-12-13 21:54:09 +000071 ** and its length in 'len' (to be used next iteration of this loop).
drh1f01ec12005-02-15 21:36:18 +000072 */
73 do {
74 zCsr += len;
75 len = sqlite3GetToken(zCsr, &token);
drh200a81d2008-08-08 14:19:41 +000076 } while( token==TK_SPACE );
drh1f01ec12005-02-15 21:36:18 +000077 assert( len>0 );
danielk1977182c4ba2007-06-27 15:53:34 +000078 } while( token!=TK_LP && token!=TK_USING );
drh1f01ec12005-02-15 21:36:18 +000079
drh4300c1a2014-02-20 19:23:15 +000080 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
81 zSql, zTableName, tname.z+tname.n);
drh633e6d52008-07-28 19:34:53 +000082 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
drh1f01ec12005-02-15 21:36:18 +000083 }
84}
85
dan432cc5b2009-09-26 17:51:48 +000086/*
87** This C function implements an SQL user function that is used by SQL code
88** generated by the ALTER TABLE ... RENAME command to modify the definition
89** of any foreign key constraints that use the table being renamed as the
90** parent table. It is passed three arguments:
91**
92** 1) The complete text of the CREATE TABLE statement being modified,
93** 2) The old name of the table being renamed, and
94** 3) The new name of the table being renamed.
95**
96** It returns the new CREATE TABLE statement. For example:
97**
98** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
99** -> 'CREATE TABLE t1(a REFERENCES t3)'
100*/
101#ifndef SQLITE_OMIT_FOREIGN_KEY
102static void renameParentFunc(
103 sqlite3_context *context,
104 int NotUsed,
105 sqlite3_value **argv
106){
107 sqlite3 *db = sqlite3_context_db_handle(context);
108 char *zOutput = 0;
109 char *zResult;
110 unsigned char const *zInput = sqlite3_value_text(argv[0]);
111 unsigned char const *zOld = sqlite3_value_text(argv[1]);
112 unsigned char const *zNew = sqlite3_value_text(argv[2]);
113
114 unsigned const char *z; /* Pointer to token */
115 int n; /* Length of token z */
116 int token; /* Type of token */
117
drhd3ceeb52009-10-13 13:08:19 +0000118 UNUSED_PARAMETER(NotUsed);
drh65b9ac52014-04-14 19:48:25 +0000119 if( zInput==0 || zOld==0 ) return;
dan432cc5b2009-09-26 17:51:48 +0000120 for(z=zInput; *z; z=z+n){
121 n = sqlite3GetToken(z, &token);
122 if( token==TK_REFERENCES ){
123 char *zParent;
124 do {
125 z += n;
126 n = sqlite3GetToken(z, &token);
127 }while( token==TK_SPACE );
128
dand68d1f42015-04-28 14:07:02 +0000129 if( token==TK_ILLEGAL ) break;
dan432cc5b2009-09-26 17:51:48 +0000130 zParent = sqlite3DbStrNDup(db, (const char *)z, n);
drha172c342009-10-08 01:43:55 +0000131 if( zParent==0 ) break;
dan432cc5b2009-09-26 17:51:48 +0000132 sqlite3Dequote(zParent);
133 if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
134 char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
drh4300c1a2014-02-20 19:23:15 +0000135 (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
dan432cc5b2009-09-26 17:51:48 +0000136 );
137 sqlite3DbFree(db, zOutput);
138 zOutput = zOut;
139 zInput = &z[n];
140 }
141 sqlite3DbFree(db, zParent);
142 }
143 }
144
145 zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
146 sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
147 sqlite3DbFree(db, zOutput);
148}
149#endif
150
drh1f01ec12005-02-15 21:36:18 +0000151#ifndef SQLITE_OMIT_TRIGGER
drhf11c34d2006-09-08 12:27:36 +0000152/* This function is used by SQL generated to implement the
drh1f01ec12005-02-15 21:36:18 +0000153** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
154** statement. The second is a table name. The table name in the CREATE
drhf11c34d2006-09-08 12:27:36 +0000155** TRIGGER statement is replaced with the third argument and the result
drh1f01ec12005-02-15 21:36:18 +0000156** returned. This is analagous to renameTableFunc() above, except for CREATE
157** TRIGGER, not CREATE INDEX and CREATE TABLE.
158*/
159static void renameTriggerFunc(
160 sqlite3_context *context,
danielk197762c14b32008-11-19 09:05:26 +0000161 int NotUsed,
drh1f01ec12005-02-15 21:36:18 +0000162 sqlite3_value **argv
163){
164 unsigned char const *zSql = sqlite3_value_text(argv[0]);
165 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
166
167 int token;
168 Token tname;
169 int dist = 3;
drh2646da72005-12-09 20:02:05 +0000170 unsigned char const *zCsr = zSql;
drh1f01ec12005-02-15 21:36:18 +0000171 int len = 0;
172 char *zRet;
drhfa4a4b92008-03-19 21:45:51 +0000173 sqlite3 *db = sqlite3_context_db_handle(context);
danielk19771e536952007-08-16 10:09:01 +0000174
danielk197762c14b32008-11-19 09:05:26 +0000175 UNUSED_PARAMETER(NotUsed);
176
drh1f01ec12005-02-15 21:36:18 +0000177 /* The principle used to locate the table name in the CREATE TRIGGER
peter.d.reid60ec9142014-09-06 16:39:46 +0000178 ** statement is that the table name is the first token that is immediately
179 ** preceded by either TK_ON or TK_DOT and immediately followed by one
drh1f01ec12005-02-15 21:36:18 +0000180 ** of TK_WHEN, TK_BEGIN or TK_FOR.
181 */
182 if( zSql ){
183 do {
danielk1977dce872b2007-05-08 12:37:45 +0000184
185 if( !*zCsr ){
186 /* Ran out of input before finding the table name. Return NULL. */
187 return;
188 }
189
drh1f01ec12005-02-15 21:36:18 +0000190 /* Store the token that zCsr points to in tname. */
drhb7916a72009-05-27 10:31:29 +0000191 tname.z = (char*)zCsr;
drh1f01ec12005-02-15 21:36:18 +0000192 tname.n = len;
193
194 /* Advance zCsr to the next token. Store that token type in 'token',
drh85b623f2007-12-13 21:54:09 +0000195 ** and its length in 'len' (to be used next iteration of this loop).
drh1f01ec12005-02-15 21:36:18 +0000196 */
197 do {
198 zCsr += len;
199 len = sqlite3GetToken(zCsr, &token);
200 }while( token==TK_SPACE );
201 assert( len>0 );
202
203 /* Variable 'dist' stores the number of tokens read since the most
204 ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
205 ** token is read and 'dist' equals 2, the condition stated above
206 ** to be met.
207 **
208 ** Note that ON cannot be a database, table or column name, so
209 ** there is no need to worry about syntax like
210 ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
211 */
212 dist++;
213 if( token==TK_DOT || token==TK_ON ){
214 dist = 0;
215 }
216 } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
217
218 /* Variable tname now contains the token that is the old table-name
219 ** in the CREATE TRIGGER statement.
220 */
drh4300c1a2014-02-20 19:23:15 +0000221 zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
222 zSql, zTableName, tname.z+tname.n);
drh633e6d52008-07-28 19:34:53 +0000223 sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
drh1f01ec12005-02-15 21:36:18 +0000224 }
225}
226#endif /* !SQLITE_OMIT_TRIGGER */
227
228/*
229** Register built-in functions used to help implement ALTER TABLE
230*/
drh545f5872010-04-24 14:02:59 +0000231void sqlite3AlterFunctions(void){
drh80738d92016-02-15 00:34:16 +0000232 static FuncDef aAlterTableFuncs[] = {
drh545f5872010-04-24 14:02:59 +0000233 FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc),
drh1f01ec12005-02-15 21:36:18 +0000234#ifndef SQLITE_OMIT_TRIGGER
drh545f5872010-04-24 14:02:59 +0000235 FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
drh1f01ec12005-02-15 21:36:18 +0000236#endif
dan432cc5b2009-09-26 17:51:48 +0000237#ifndef SQLITE_OMIT_FOREIGN_KEY
drh545f5872010-04-24 14:02:59 +0000238 FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc),
dan432cc5b2009-09-26 17:51:48 +0000239#endif
drh545f5872010-04-24 14:02:59 +0000240 };
drh80738d92016-02-15 00:34:16 +0000241 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
dan432cc5b2009-09-26 17:51:48 +0000242}
243
244/*
245** This function is used to create the text of expressions of the form:
246**
247** name=<constant1> OR name=<constant2> OR ...
248**
249** If argument zWhere is NULL, then a pointer string containing the text
250** "name=<constant>" is returned, where <constant> is the quoted version
251** of the string passed as argument zConstant. The returned buffer is
252** allocated using sqlite3DbMalloc(). It is the responsibility of the
253** caller to ensure that it is eventually freed.
254**
255** If argument zWhere is not NULL, then the string returned is
256** "<where> OR name=<constant>", where <where> is the contents of zWhere.
257** In this case zWhere is passed to sqlite3DbFree() before returning.
258**
259*/
260static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
261 char *zNew;
262 if( !zWhere ){
263 zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
264 }else{
265 zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
266 sqlite3DbFree(db, zWhere);
267 }
268 return zNew;
269}
270
dan856ef1a2009-09-29 06:33:23 +0000271#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan432cc5b2009-09-26 17:51:48 +0000272/*
273** Generate the text of a WHERE expression which can be used to select all
274** tables that have foreign key constraints that refer to table pTab (i.e.
275** constraints for which pTab is the parent table) from the sqlite_master
276** table.
277*/
278static char *whereForeignKeys(Parse *pParse, Table *pTab){
279 FKey *p;
280 char *zWhere = 0;
281 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
282 zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
283 }
284 return zWhere;
drh1f01ec12005-02-15 21:36:18 +0000285}
dan856ef1a2009-09-29 06:33:23 +0000286#endif
drh1f01ec12005-02-15 21:36:18 +0000287
drhd0e4a6c2005-02-15 20:47:57 +0000288/*
danielk197719a8e7e2005-03-17 05:03:38 +0000289** Generate the text of a WHERE expression which can be used to select all
290** temporary triggers on table pTab from the sqlite_temp_master table. If
291** table pTab has no temporary triggers, or is itself stored in the
292** temporary database, NULL is returned.
293*/
294static char *whereTempTriggers(Parse *pParse, Table *pTab){
295 Trigger *pTrig;
296 char *zWhere = 0;
danielk1977e501b892006-01-09 06:29:47 +0000297 const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
danielk1977da184232006-01-05 11:34:32 +0000298
299 /* If the table is not located in the temp-db (in which case NULL is
300 ** returned, loop through the tables list of triggers. For each trigger
301 ** that is not part of the temp-db schema, add a clause to the WHERE
302 ** expression being built up in zWhere.
303 */
304 if( pTab->pSchema!=pTempSchema ){
danielk19771e536952007-08-16 10:09:01 +0000305 sqlite3 *db = pParse->db;
danielk19772f886d12009-02-28 10:47:41 +0000306 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
danielk1977da184232006-01-05 11:34:32 +0000307 if( pTrig->pSchema==pTempSchema ){
dan432cc5b2009-09-26 17:51:48 +0000308 zWhere = whereOrName(db, zWhere, pTrig->zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000309 }
310 }
311 }
dan39f1bcb2010-09-29 07:16:46 +0000312 if( zWhere ){
313 char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
314 sqlite3DbFree(pParse->db, zWhere);
315 zWhere = zNew;
316 }
danielk197719a8e7e2005-03-17 05:03:38 +0000317 return zWhere;
318}
319
320/*
321** Generate code to drop and reload the internal representation of table
322** pTab from the database, including triggers and temporary triggers.
323** Argument zName is the name of the table in the database schema at
324** the time the generated code is executed. This can be different from
325** pTab->zName if this function is being called to code part of an
326** "ALTER TABLE RENAME TO" statement.
327*/
328static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
329 Vdbe *v;
330 char *zWhere;
danielk1977da184232006-01-05 11:34:32 +0000331 int iDb; /* Index of database containing pTab */
danielk197719a8e7e2005-03-17 05:03:38 +0000332#ifndef SQLITE_OMIT_TRIGGER
333 Trigger *pTrig;
334#endif
335
336 v = sqlite3GetVdbe(pParse);
drhd3264c72009-04-15 13:39:47 +0000337 if( NEVER(v==0) ) return;
drh1fee73e2007-08-29 04:00:57 +0000338 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
danielk1977da184232006-01-05 11:34:32 +0000339 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
340 assert( iDb>=0 );
danielk197719a8e7e2005-03-17 05:03:38 +0000341
342#ifndef SQLITE_OMIT_TRIGGER
343 /* Drop any table triggers from the internal schema. */
danielk19772f886d12009-02-28 10:47:41 +0000344 for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
danielk1977da184232006-01-05 11:34:32 +0000345 int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
346 assert( iTrigDb==iDb || iTrigDb==1 );
dan165921a2009-08-28 18:53:45 +0000347 sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000348 }
349#endif
350
dan432cc5b2009-09-26 17:51:48 +0000351 /* Drop the table and index from the internal schema. */
drh66a51672008-01-03 00:01:23 +0000352 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
danielk197719a8e7e2005-03-17 05:03:38 +0000353
354 /* Reload the table, index and permanent trigger schemas. */
danielk19771e536952007-08-16 10:09:01 +0000355 zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000356 if( !zWhere ) return;
drh5d9c9da2011-06-03 20:11:17 +0000357 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
danielk197719a8e7e2005-03-17 05:03:38 +0000358
359#ifndef SQLITE_OMIT_TRIGGER
360 /* Now, if the table is not stored in the temp database, reload any temp
361 ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
362 */
drhd9cb6ac2005-10-20 07:28:17 +0000363 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
drh5d9c9da2011-06-03 20:11:17 +0000364 sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
danielk197719a8e7e2005-03-17 05:03:38 +0000365 }
366#endif
367}
368
369/*
danbe535002011-04-01 15:15:58 +0000370** Parameter zName is the name of a table that is about to be altered
371** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
372** If the table is a system table, this function leaves an error message
373** in pParse->zErr (system tables may not be altered) and returns non-zero.
374**
375** Or, if zName is not a system table, zero is returned.
376*/
377static int isSystemTable(Parse *pParse, const char *zName){
378 if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
379 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
380 return 1;
381 }
382 return 0;
383}
384
385/*
drhd0e4a6c2005-02-15 20:47:57 +0000386** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
387** command.
388*/
389void sqlite3AlterRenameTable(
390 Parse *pParse, /* Parser context. */
391 SrcList *pSrc, /* The table to rename. */
392 Token *pName /* The new table name. */
393){
394 int iDb; /* Database that contains the table */
395 char *zDb; /* Name of database iDb */
396 Table *pTab; /* Table being renamed */
397 char *zName = 0; /* NULL-terminated version of pName */
drhd0e4a6c2005-02-15 20:47:57 +0000398 sqlite3 *db = pParse->db; /* Database connection */
drh4e5dd852007-05-15 03:56:49 +0000399 int nTabName; /* Number of UTF-8 characters in zTabName */
400 const char *zTabName; /* Original name of the table */
drhd0e4a6c2005-02-15 20:47:57 +0000401 Vdbe *v;
402#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000403 char *zWhere = 0; /* Where clause to locate temp triggers */
drhd0e4a6c2005-02-15 20:47:57 +0000404#endif
danielk1977595a5232009-07-24 17:58:53 +0000405 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
drh545f5872010-04-24 14:02:59 +0000406 int savedDbFlags; /* Saved value of db->flags */
407
408 savedDbFlags = db->flags;
drh56d56f72009-04-16 16:30:17 +0000409 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000410 assert( pSrc->nSrc==1 );
drh1fee73e2007-08-29 04:00:57 +0000411 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
drhd0e4a6c2005-02-15 20:47:57 +0000412
dan41fb5cd2012-10-04 19:33:00 +0000413 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
drhd0e4a6c2005-02-15 20:47:57 +0000414 if( !pTab ) goto exit_rename_table;
danielk1977da184232006-01-05 11:34:32 +0000415 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
drh69c33822016-08-18 14:33:11 +0000416 zDb = db->aDb[iDb].zDbSName;
drh545f5872010-04-24 14:02:59 +0000417 db->flags |= SQLITE_PreferBuiltin;
drhd0e4a6c2005-02-15 20:47:57 +0000418
419 /* Get a NULL terminated version of the new table name. */
drh17435752007-08-16 04:30:38 +0000420 zName = sqlite3NameFromToken(db, pName);
drhd0e4a6c2005-02-15 20:47:57 +0000421 if( !zName ) goto exit_rename_table;
422
423 /* Check that a table or index named 'zName' does not already exist
424 ** in database iDb. If so, this is an error.
425 */
426 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
427 sqlite3ErrorMsg(pParse,
428 "there is already another table or index with this name: %s", zName);
429 goto exit_rename_table;
430 }
431
432 /* Make sure it is not a system table being altered, or a reserved name
433 ** that the table is being renamed to.
434 */
danbe535002011-04-01 15:15:58 +0000435 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
drhd0e4a6c2005-02-15 20:47:57 +0000436 goto exit_rename_table;
437 }
danbe535002011-04-01 15:15:58 +0000438 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
439 exit_rename_table;
drhd0e4a6c2005-02-15 20:47:57 +0000440 }
441
danielk197761116ae2007-12-13 08:15:30 +0000442#ifndef SQLITE_OMIT_VIEW
443 if( pTab->pSelect ){
444 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
445 goto exit_rename_table;
446 }
447#endif
448
drhd0e4a6c2005-02-15 20:47:57 +0000449#ifndef SQLITE_OMIT_AUTHORIZATION
450 /* Invoke the authorization callback. */
451 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
452 goto exit_rename_table;
453 }
454#endif
455
danielk1977182c4ba2007-06-27 15:53:34 +0000456#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19775c558862007-06-27 17:09:24 +0000457 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
458 goto exit_rename_table;
459 }
danielk1977595a5232009-07-24 17:58:53 +0000460 if( IsVirtual(pTab) ){
461 pVTab = sqlite3GetVTable(db, pTab);
462 if( pVTab->pVtab->pModule->xRename==0 ){
463 pVTab = 0;
464 }
danielk1977182c4ba2007-06-27 15:53:34 +0000465 }
466#endif
467
drhb22f7c82014-02-06 23:56:27 +0000468 /* Begin a transaction for database iDb.
drhd0e4a6c2005-02-15 20:47:57 +0000469 ** Then modify the schema cookie (since the ALTER TABLE modifies the
danielk1977182c4ba2007-06-27 15:53:34 +0000470 ** schema). Open a statement transaction if the table is a virtual
471 ** table.
drhd0e4a6c2005-02-15 20:47:57 +0000472 */
473 v = sqlite3GetVdbe(pParse);
474 if( v==0 ){
475 goto exit_rename_table;
476 }
danielk1977595a5232009-07-24 17:58:53 +0000477 sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
drh9cbf3422008-01-17 16:22:13 +0000478 sqlite3ChangeCookie(pParse, iDb);
drhd0e4a6c2005-02-15 20:47:57 +0000479
danielk1977182c4ba2007-06-27 15:53:34 +0000480 /* If this is a virtual table, invoke the xRename() function if
481 ** one is defined. The xRename() callback will modify the names
482 ** of any resources used by the v-table implementation (including other
483 ** SQLite tables) that are identified by the name of the virtual table.
484 */
485#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977595a5232009-07-24 17:58:53 +0000486 if( pVTab ){
danielk1977a29f18c2008-01-04 11:01:03 +0000487 int i = ++pParse->nMem;
drh076e85f2015-09-03 13:46:12 +0000488 sqlite3VdbeLoadString(v, i, zName);
danielk1977595a5232009-07-24 17:58:53 +0000489 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
dane0af83a2009-09-08 19:15:01 +0000490 sqlite3MayAbort(pParse);
danielk1977182c4ba2007-06-27 15:53:34 +0000491 }
492#endif
493
drh4e5dd852007-05-15 03:56:49 +0000494 /* figure out how many UTF-8 characters are in zName */
495 zTabName = pTab->zName;
drh9a087a92007-05-15 14:34:32 +0000496 nTabName = sqlite3Utf8CharLen(zTabName, -1);
drh4e5dd852007-05-15 03:56:49 +0000497
dan856ef1a2009-09-29 06:33:23 +0000498#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan432cc5b2009-09-26 17:51:48 +0000499 if( db->flags&SQLITE_ForeignKeys ){
500 /* If foreign-key support is enabled, rewrite the CREATE TABLE
501 ** statements corresponding to all child tables of foreign key constraints
502 ** for which the renamed table is the parent table. */
503 if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
504 sqlite3NestedParse(pParse,
drh9a6ffc82010-02-15 18:03:20 +0000505 "UPDATE \"%w\".%s SET "
dan432cc5b2009-09-26 17:51:48 +0000506 "sql = sqlite_rename_parent(sql, %Q, %Q) "
drhe0a04a32016-12-16 01:00:21 +0000507 "WHERE %s;", zDb, MASTER_NAME, zTabName, zName, zWhere);
dan432cc5b2009-09-26 17:51:48 +0000508 sqlite3DbFree(db, zWhere);
509 }
510 }
511#endif
512
drhd0e4a6c2005-02-15 20:47:57 +0000513 /* Modify the sqlite_master table to use the new table name. */
514 sqlite3NestedParse(pParse,
515 "UPDATE %Q.%s SET "
516#ifdef SQLITE_OMIT_TRIGGER
517 "sql = sqlite_rename_table(sql, %Q), "
518#else
519 "sql = CASE "
520 "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
521 "ELSE sqlite_rename_table(sql, %Q) END, "
522#endif
523 "tbl_name = %Q, "
524 "name = CASE "
525 "WHEN type='table' THEN %Q "
526 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
drha21a9292007-10-20 20:58:57 +0000527 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
drhd0e4a6c2005-02-15 20:47:57 +0000528 "ELSE name END "
drh01522682012-02-01 01:13:10 +0000529 "WHERE tbl_name=%Q COLLATE nocase AND "
drhd0e4a6c2005-02-15 20:47:57 +0000530 "(type='table' OR type='index' OR type='trigger');",
drhe0a04a32016-12-16 01:00:21 +0000531 zDb, MASTER_NAME, zName, zName, zName,
drhd0e4a6c2005-02-15 20:47:57 +0000532#ifndef SQLITE_OMIT_TRIGGER
danielk197719a8e7e2005-03-17 05:03:38 +0000533 zName,
drhd0e4a6c2005-02-15 20:47:57 +0000534#endif
drh4e5dd852007-05-15 03:56:49 +0000535 zName, nTabName, zTabName
drhd0e4a6c2005-02-15 20:47:57 +0000536 );
537
538#ifndef SQLITE_OMIT_AUTOINCREMENT
539 /* If the sqlite_sequence table exists in this database, then update
540 ** it with the new table name.
541 */
542 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
543 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000544 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
drhd0e4a6c2005-02-15 20:47:57 +0000545 zDb, zName, pTab->zName);
546 }
547#endif
548
549#ifndef SQLITE_OMIT_TRIGGER
550 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
551 ** table. Don't do this if the table being ALTERed is itself located in
552 ** the temp database.
553 */
drhd9cb6ac2005-10-20 07:28:17 +0000554 if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
danielk197719a8e7e2005-03-17 05:03:38 +0000555 sqlite3NestedParse(pParse,
556 "UPDATE sqlite_temp_master SET "
557 "sql = sqlite_rename_trigger(sql, %Q), "
558 "tbl_name = %Q "
559 "WHERE %s;", zName, zName, zWhere);
drh633e6d52008-07-28 19:34:53 +0000560 sqlite3DbFree(db, zWhere);
drhd0e4a6c2005-02-15 20:47:57 +0000561 }
562#endif
563
dan856ef1a2009-09-29 06:33:23 +0000564#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
dan432cc5b2009-09-26 17:51:48 +0000565 if( db->flags&SQLITE_ForeignKeys ){
566 FKey *p;
567 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
568 Table *pFrom = p->pFrom;
569 if( pFrom!=pTab ){
570 reloadTableSchema(pParse, p->pFrom, pFrom->zName);
571 }
572 }
573 }
574#endif
575
danielk197719a8e7e2005-03-17 05:03:38 +0000576 /* Drop and reload the internal table schema. */
577 reloadTableSchema(pParse, pTab, zName);
drhd0e4a6c2005-02-15 20:47:57 +0000578
579exit_rename_table:
drh633e6d52008-07-28 19:34:53 +0000580 sqlite3SrcListDelete(db, pSrc);
581 sqlite3DbFree(db, zName);
drh545f5872010-04-24 14:02:59 +0000582 db->flags = savedDbFlags;
drhd0e4a6c2005-02-15 20:47:57 +0000583}
danielk197719a8e7e2005-03-17 05:03:38 +0000584
drhd3001712009-05-12 17:46:53 +0000585/*
danielk197719a8e7e2005-03-17 05:03:38 +0000586** This function is called after an "ALTER TABLE ... ADD" statement
587** has been parsed. Argument pColDef contains the text of the new
588** column definition.
589**
590** The Table structure pParse->pNewTable was extended to include
591** the new column during parsing.
592*/
593void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
594 Table *pNew; /* Copy of pParse->pNewTable */
595 Table *pTab; /* Table being altered */
596 int iDb; /* Database number */
597 const char *zDb; /* Database name */
598 const char *zTab; /* Table name */
599 char *zCol; /* Null-terminated column definition */
600 Column *pCol; /* The new column */
601 Expr *pDflt; /* Default value for the new column */
drh17435752007-08-16 04:30:38 +0000602 sqlite3 *db; /* The database connection; */
drhbbde0182016-02-09 16:09:22 +0000603 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
drh86396212016-07-14 19:13:11 +0000604 int r1; /* Temporary registers */
danielk197719a8e7e2005-03-17 05:03:38 +0000605
danielk1977f150c9d2008-10-30 17:21:12 +0000606 db = pParse->db;
607 if( pParse->nErr || db->mallocFailed ) return;
drhbbde0182016-02-09 16:09:22 +0000608 assert( v!=0 );
danielk197719a8e7e2005-03-17 05:03:38 +0000609 pNew = pParse->pNewTable;
610 assert( pNew );
611
drh1fee73e2007-08-29 04:00:57 +0000612 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000613 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
drh69c33822016-08-18 14:33:11 +0000614 zDb = db->aDb[iDb].zDbSName;
drh03881232009-02-13 03:43:31 +0000615 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
danielk197719a8e7e2005-03-17 05:03:38 +0000616 pCol = &pNew->aCol[pNew->nCol-1];
617 pDflt = pCol->pDflt;
drh17435752007-08-16 04:30:38 +0000618 pTab = sqlite3FindTable(db, zTab, zDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000619 assert( pTab );
620
drh81f2ccd2006-01-31 14:28:44 +0000621#ifndef SQLITE_OMIT_AUTHORIZATION
622 /* Invoke the authorization callback. */
623 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
624 return;
625 }
626#endif
627
danielk197719a8e7e2005-03-17 05:03:38 +0000628 /* If the default value for the new column was specified with a
629 ** literal NULL, then set pDflt to 0. This simplifies checking
630 ** for an SQL NULL default below.
631 */
drh94fa9c42016-02-27 21:16:04 +0000632 assert( pDflt==0 || pDflt->op==TK_SPAN );
633 if( pDflt && pDflt->pLeft->op==TK_NULL ){
danielk197719a8e7e2005-03-17 05:03:38 +0000634 pDflt = 0;
635 }
636
637 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
638 ** If there is a NOT NULL constraint, then the default value for the
639 ** column must not be NULL.
640 */
drha371ace2012-09-13 14:22:47 +0000641 if( pCol->colFlags & COLFLAG_PRIMKEY ){
danielk197719a8e7e2005-03-17 05:03:38 +0000642 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
643 return;
644 }
645 if( pNew->pIndex ){
646 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
647 return;
648 }
dan53c3fa82009-09-25 11:26:54 +0000649 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
650 sqlite3ErrorMsg(pParse,
651 "Cannot add a REFERENCES column with non-NULL default value");
652 return;
653 }
danielk197719a8e7e2005-03-17 05:03:38 +0000654 if( pCol->notNull && !pDflt ){
655 sqlite3ErrorMsg(pParse,
656 "Cannot add a NOT NULL column with default value NULL");
657 return;
658 }
659
660 /* Ensure the default expression is something that sqlite3ValueFromExpr()
661 ** can handle (i.e. not CURRENT_TIME etc.)
662 */
663 if( pDflt ){
dan7a419232013-08-06 20:01:43 +0000664 sqlite3_value *pVal = 0;
drh96f4ad22015-03-12 21:02:36 +0000665 int rc;
drh05883a32015-06-02 15:32:08 +0000666 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
drh96f4ad22015-03-12 21:02:36 +0000667 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
668 if( rc!=SQLITE_OK ){
pdrbb3da062016-02-06 14:14:43 +0000669 assert( db->mallocFailed == 1 );
danielk197719a8e7e2005-03-17 05:03:38 +0000670 return;
671 }
672 if( !pVal ){
673 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
674 return;
675 }
676 sqlite3ValueFree(pVal);
677 }
678
679 /* Modify the CREATE TABLE statement. */
drh17435752007-08-16 04:30:38 +0000680 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
danielk197719a8e7e2005-03-17 05:03:38 +0000681 if( zCol ){
682 char *zEnd = &zCol[pColDef->n-1];
drh545f5872010-04-24 14:02:59 +0000683 int savedDbFlags = db->flags;
drh56d56f72009-04-16 16:30:17 +0000684 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
danielk197719a8e7e2005-03-17 05:03:38 +0000685 *zEnd-- = '\0';
686 }
drh545f5872010-04-24 14:02:59 +0000687 db->flags |= SQLITE_PreferBuiltin;
danielk197719a8e7e2005-03-17 05:03:38 +0000688 sqlite3NestedParse(pParse,
drh8e5b5f82008-02-09 14:30:29 +0000689 "UPDATE \"%w\".%s SET "
drha21a9292007-10-20 20:58:57 +0000690 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
danielk197719a8e7e2005-03-17 05:03:38 +0000691 "WHERE type = 'table' AND name = %Q",
drhe0a04a32016-12-16 01:00:21 +0000692 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
danielk197719a8e7e2005-03-17 05:03:38 +0000693 zTab
694 );
drh633e6d52008-07-28 19:34:53 +0000695 sqlite3DbFree(db, zCol);
drh545f5872010-04-24 14:02:59 +0000696 db->flags = savedDbFlags;
danielk197719a8e7e2005-03-17 05:03:38 +0000697 }
698
drh86396212016-07-14 19:13:11 +0000699 /* Make sure the schema version is at least 3. But do not upgrade
700 ** from less than 3 to 4, as that will corrupt any preexisting DESC
701 ** index.
danielk197719a8e7e2005-03-17 05:03:38 +0000702 */
drh86396212016-07-14 19:13:11 +0000703 r1 = sqlite3GetTempReg(pParse);
704 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
705 sqlite3VdbeUsesBtree(v, iDb);
706 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
707 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
708 VdbeCoverage(v);
709 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
710 sqlite3ReleaseTempReg(pParse, r1);
danielk197719a8e7e2005-03-17 05:03:38 +0000711
712 /* Reload the schema of the modified table. */
713 reloadTableSchema(pParse, pTab, pTab->zName);
714}
715
drhfdd6e852005-12-16 01:06:16 +0000716/*
danielk197719a8e7e2005-03-17 05:03:38 +0000717** This function is called by the parser after the table-name in
718** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
719** pSrc is the full-name of the table being altered.
720**
721** This routine makes a (partial) copy of the Table structure
722** for the table being altered and sets Parse.pNewTable to point
723** to it. Routines called by the parser as the column definition
724** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
725** the copy. The copy of the Table structure is deleted by tokenize.c
726** after parsing is finished.
727**
728** Routine sqlite3AlterFinishAddColumn() will be called to complete
729** coding the "ALTER TABLE ... ADD" statement.
730*/
731void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
732 Table *pNew;
733 Table *pTab;
734 Vdbe *v;
735 int iDb;
736 int i;
737 int nAlloc;
drh17435752007-08-16 04:30:38 +0000738 sqlite3 *db = pParse->db;
danielk197719a8e7e2005-03-17 05:03:38 +0000739
740 /* Look up the table being altered. */
drh0bbaa1b2005-08-19 19:14:12 +0000741 assert( pParse->pNewTable==0 );
drh1fee73e2007-08-29 04:00:57 +0000742 assert( sqlite3BtreeHoldsAllMutexes(db) );
drh17435752007-08-16 04:30:38 +0000743 if( db->mallocFailed ) goto exit_begin_add_column;
dan41fb5cd2012-10-04 19:33:00 +0000744 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
danielk197719a8e7e2005-03-17 05:03:38 +0000745 if( !pTab ) goto exit_begin_add_column;
746
danielk19775ee9d692006-06-21 12:36:25 +0000747#ifndef SQLITE_OMIT_VIRTUALTABLE
748 if( IsVirtual(pTab) ){
749 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
750 goto exit_begin_add_column;
751 }
752#endif
753
danielk197719a8e7e2005-03-17 05:03:38 +0000754 /* Make sure this is not an attempt to ALTER a view. */
755 if( pTab->pSelect ){
756 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
757 goto exit_begin_add_column;
758 }
danbe535002011-04-01 15:15:58 +0000759 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
760 goto exit_begin_add_column;
761 }
danielk197719a8e7e2005-03-17 05:03:38 +0000762
763 assert( pTab->addColOffset>0 );
drh17435752007-08-16 04:30:38 +0000764 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk197719a8e7e2005-03-17 05:03:38 +0000765
766 /* Put a copy of the Table struct in Parse.pNewTable for the
drh03881232009-02-13 03:43:31 +0000767 ** sqlite3AddColumn() function and friends to modify. But modify
768 ** the name by adding an "sqlite_altertab_" prefix. By adding this
769 ** prefix, we insure that the name will not collide with an existing
770 ** table because user table are not allowed to have the "sqlite_"
771 ** prefix on their name.
danielk197719a8e7e2005-03-17 05:03:38 +0000772 */
drh17435752007-08-16 04:30:38 +0000773 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
danielk197719a8e7e2005-03-17 05:03:38 +0000774 if( !pNew ) goto exit_begin_add_column;
775 pParse->pNewTable = pNew;
drh79df7782016-12-14 14:07:35 +0000776 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000777 pNew->nCol = pTab->nCol;
danielk1977b3a2cce2005-03-27 01:56:30 +0000778 assert( pNew->nCol>0 );
779 nAlloc = (((pNew->nCol-1)/8)*8)+8;
780 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
danielk197726783a52007-08-29 14:06:22 +0000781 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
drh03881232009-02-13 03:43:31 +0000782 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
danielk197719a8e7e2005-03-17 05:03:38 +0000783 if( !pNew->aCol || !pNew->zName ){
drh4df86af2016-02-04 11:48:00 +0000784 assert( db->mallocFailed );
danielk197719a8e7e2005-03-17 05:03:38 +0000785 goto exit_begin_add_column;
786 }
787 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
788 for(i=0; i<pNew->nCol; i++){
789 Column *pCol = &pNew->aCol[i];
drh17435752007-08-16 04:30:38 +0000790 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
drhff22e182006-02-09 02:56:02 +0000791 pCol->zColl = 0;
danielk197719a8e7e2005-03-17 05:03:38 +0000792 pCol->pDflt = 0;
793 }
drh17435752007-08-16 04:30:38 +0000794 pNew->pSchema = db->aDb[iDb].pSchema;
danielk197719a8e7e2005-03-17 05:03:38 +0000795 pNew->addColOffset = pTab->addColOffset;
drh79df7782016-12-14 14:07:35 +0000796 pNew->nTabRef = 1;
danielk197719a8e7e2005-03-17 05:03:38 +0000797
798 /* Begin a transaction and increment the schema cookie. */
799 sqlite3BeginWriteOperation(pParse, 0, iDb);
800 v = sqlite3GetVdbe(pParse);
801 if( !v ) goto exit_begin_add_column;
drh9cbf3422008-01-17 16:22:13 +0000802 sqlite3ChangeCookie(pParse, iDb);
danielk197719a8e7e2005-03-17 05:03:38 +0000803
804exit_begin_add_column:
drh633e6d52008-07-28 19:34:53 +0000805 sqlite3SrcListDelete(db, pSrc);
danielk197719a8e7e2005-03-17 05:03:38 +0000806 return;
807}
drhd0e4a6c2005-02-15 20:47:57 +0000808#endif /* SQLITE_ALTER_TABLE */