alter.c 20 KB

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