pragma.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. ** 2003 April 6
  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 code used to implement the PRAGMA command.
  13. **
  14. ** $Id: pragma.c,v 1.152 2007/12/13 21:54:11 drh Exp $
  15. */
  16. #include "sqliteInt.h"
  17. #include <ctype.h>
  18. /* Ignore this whole file if pragmas are disabled
  19. */
  20. #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
  21. /*
  22. ** Interpret the given string as a safety level. Return 0 for OFF,
  23. ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
  24. ** unrecognized string argument.
  25. **
  26. ** Note that the values returned are one less that the values that
  27. ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
  28. ** to support legacy SQL code. The safety level used to be boolean
  29. ** and older scripts may have used numbers 0 for OFF and 1 for ON.
  30. */
  31. static int getSafetyLevel(const char *z){
  32. /* 123456789 123456789 */
  33. static const char zText[] = "onoffalseyestruefull";
  34. static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
  35. static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
  36. static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
  37. int i, n;
  38. if( isdigit(*z) ){
  39. return atoi(z);
  40. }
  41. n = strlen(z);
  42. for(i=0; i<sizeof(iLength); i++){
  43. if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
  44. return iValue[i];
  45. }
  46. }
  47. return 1;
  48. }
  49. /*
  50. ** Interpret the given string as a boolean value.
  51. */
  52. static int getBoolean(const char *z){
  53. return getSafetyLevel(z)&1;
  54. }
  55. /*
  56. ** Interpret the given string as a locking mode value.
  57. */
  58. static int getLockingMode(const char *z){
  59. if( z ){
  60. if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
  61. if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
  62. }
  63. return PAGER_LOCKINGMODE_QUERY;
  64. }
  65. #ifndef SQLITE_OMIT_AUTOVACUUM
  66. /*
  67. ** Interpret the given string as an auto-vacuum mode value.
  68. **
  69. ** The following strings, "none", "full" and "incremental" are
  70. ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
  71. */
  72. static int getAutoVacuum(const char *z){
  73. int i;
  74. if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
  75. if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
  76. if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
  77. i = atoi(z);
  78. return ((i>=0&&i<=2)?i:0);
  79. }
  80. #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
  81. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  82. /*
  83. ** Interpret the given string as a temp db location. Return 1 for file
  84. ** backed temporary databases, 2 for the Red-Black tree in memory database
  85. ** and 0 to use the compile-time default.
  86. */
  87. static int getTempStore(const char *z){
  88. if( z[0]>='0' && z[0]<='2' ){
  89. return z[0] - '0';
  90. }else if( sqlite3StrICmp(z, "file")==0 ){
  91. return 1;
  92. }else if( sqlite3StrICmp(z, "memory")==0 ){
  93. return 2;
  94. }else{
  95. return 0;
  96. }
  97. }
  98. #endif /* SQLITE_PAGER_PRAGMAS */
  99. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  100. /*
  101. ** Invalidate temp storage, either when the temp storage is changed
  102. ** from default, or when 'file' and the temp_store_directory has changed
  103. */
  104. static int invalidateTempStorage(Parse *pParse){
  105. sqlite3 *db = pParse->db;
  106. if( db->aDb[1].pBt!=0 ){
  107. if( !db->autoCommit ){
  108. sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
  109. "from within a transaction");
  110. return SQLITE_ERROR;
  111. }
  112. sqlite3BtreeClose(db->aDb[1].pBt);
  113. db->aDb[1].pBt = 0;
  114. sqlite3ResetInternalSchema(db, 0);
  115. }
  116. return SQLITE_OK;
  117. }
  118. #endif /* SQLITE_PAGER_PRAGMAS */
  119. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  120. /*
  121. ** If the TEMP database is open, close it and mark the database schema
  122. ** as needing reloading. This must be done when using the TEMP_STORE
  123. ** or DEFAULT_TEMP_STORE pragmas.
  124. */
  125. static int changeTempStorage(Parse *pParse, const char *zStorageType){
  126. int ts = getTempStore(zStorageType);
  127. sqlite3 *db = pParse->db;
  128. if( db->temp_store==ts ) return SQLITE_OK;
  129. if( invalidateTempStorage( pParse ) != SQLITE_OK ){
  130. return SQLITE_ERROR;
  131. }
  132. db->temp_store = ts;
  133. return SQLITE_OK;
  134. }
  135. #endif /* SQLITE_PAGER_PRAGMAS */
  136. /*
  137. ** Generate code to return a single integer value.
  138. */
  139. static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
  140. Vdbe *v = sqlite3GetVdbe(pParse);
  141. sqlite3VdbeAddOp(v, OP_Integer, value, 0);
  142. if( pParse->explain==0 ){
  143. sqlite3VdbeSetNumCols(v, 1);
  144. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P3_STATIC);
  145. }
  146. sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
  147. }
  148. #ifndef SQLITE_OMIT_FLAG_PRAGMAS
  149. /*
  150. ** Check to see if zRight and zLeft refer to a pragma that queries
  151. ** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
  152. ** Also, implement the pragma.
  153. */
  154. static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
  155. static const struct sPragmaType {
  156. const char *zName; /* Name of the pragma */
  157. int mask; /* Mask for the db->flags value */
  158. } aPragma[] = {
  159. { "full_column_names", SQLITE_FullColNames },
  160. { "short_column_names", SQLITE_ShortColNames },
  161. { "count_changes", SQLITE_CountRows },
  162. { "empty_result_callbacks", SQLITE_NullCallback },
  163. { "legacy_file_format", SQLITE_LegacyFileFmt },
  164. { "fullfsync", SQLITE_FullFSync },
  165. #ifdef SQLITE_DEBUG
  166. { "sql_trace", SQLITE_SqlTrace },
  167. { "vdbe_listing", SQLITE_VdbeListing },
  168. { "vdbe_trace", SQLITE_VdbeTrace },
  169. #endif
  170. #ifndef SQLITE_OMIT_CHECK
  171. { "ignore_check_constraints", SQLITE_IgnoreChecks },
  172. #endif
  173. /* The following is VERY experimental */
  174. { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode },
  175. { "omit_readlock", SQLITE_NoReadlock },
  176. /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
  177. ** flag if there are any active statements. */
  178. { "read_uncommitted", SQLITE_ReadUncommitted },
  179. };
  180. int i;
  181. const struct sPragmaType *p;
  182. for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
  183. if( sqlite3StrICmp(zLeft, p->zName)==0 ){
  184. sqlite3 *db = pParse->db;
  185. Vdbe *v;
  186. v = sqlite3GetVdbe(pParse);
  187. if( v ){
  188. if( zRight==0 ){
  189. returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
  190. }else{
  191. if( getBoolean(zRight) ){
  192. db->flags |= p->mask;
  193. }else{
  194. db->flags &= ~p->mask;
  195. }
  196. /* Many of the flag-pragmas modify the code generated by the SQL
  197. ** compiler (eg. count_changes). So add an opcode to expire all
  198. ** compiled SQL statements after modifying a pragma value.
  199. */
  200. sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
  201. }
  202. }
  203. return 1;
  204. }
  205. }
  206. return 0;
  207. }
  208. #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
  209. /*
  210. ** Process a pragma statement.
  211. **
  212. ** Pragmas are of this form:
  213. **
  214. ** PRAGMA [database.]id [= value]
  215. **
  216. ** The identifier might also be a string. The value is a string, and
  217. ** identifier, or a number. If minusFlag is true, then the value is
  218. ** a number that was preceded by a minus sign.
  219. **
  220. ** If the left side is "database.id" then pId1 is the database name
  221. ** and pId2 is the id. If the left side is just "id" then pId1 is the
  222. ** id and pId2 is any empty string.
  223. */
  224. void sqlite3Pragma(
  225. Parse *pParse,
  226. Token *pId1, /* First part of [database.]id field */
  227. Token *pId2, /* Second part of [database.]id field, or NULL */
  228. Token *pValue, /* Token for <value>, or NULL */
  229. int minusFlag /* True if a '-' sign preceded <value> */
  230. ){
  231. char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
  232. char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
  233. const char *zDb = 0; /* The database name */
  234. Token *pId; /* Pointer to <id> token */
  235. int iDb; /* Database index for <database> */
  236. sqlite3 *db = pParse->db;
  237. Db *pDb;
  238. Vdbe *v = sqlite3GetVdbe(pParse);
  239. if( v==0 ) return;
  240. /* Interpret the [database.] part of the pragma statement. iDb is the
  241. ** index of the database this pragma is being applied to in db.aDb[]. */
  242. iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
  243. if( iDb<0 ) return;
  244. pDb = &db->aDb[iDb];
  245. /* If the temp database has been explicitly named as part of the
  246. ** pragma, make sure it is open.
  247. */
  248. if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
  249. return;
  250. }
  251. zLeft = sqlite3NameFromToken(db, pId);
  252. if( !zLeft ) return;
  253. if( minusFlag ){
  254. zRight = sqlite3MPrintf(db, "-%T", pValue);
  255. }else{
  256. zRight = sqlite3NameFromToken(db, pValue);
  257. }
  258. zDb = ((iDb>0)?pDb->zName:0);
  259. if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
  260. goto pragma_out;
  261. }
  262. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  263. /*
  264. ** PRAGMA [database.]default_cache_size
  265. ** PRAGMA [database.]default_cache_size=N
  266. **
  267. ** The first form reports the current persistent setting for the
  268. ** page cache size. The value returned is the maximum number of
  269. ** pages in the page cache. The second form sets both the current
  270. ** page cache size value and the persistent page cache size value
  271. ** stored in the database file.
  272. **
  273. ** The default cache size is stored in meta-value 2 of page 1 of the
  274. ** database file. The cache size is actually the absolute value of
  275. ** this memory location. The sign of meta-value 2 determines the
  276. ** synchronous setting. A negative value means synchronous is off
  277. ** and a positive value means synchronous is on.
  278. */
  279. if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
  280. static const VdbeOpList getCacheSize[] = {
  281. { OP_ReadCookie, 0, 2, 0}, /* 0 */
  282. { OP_AbsValue, 0, 0, 0},
  283. { OP_Dup, 0, 0, 0},
  284. { OP_Integer, 0, 0, 0},
  285. { OP_Ne, 0, 6, 0},
  286. { OP_Integer, 0, 0, 0}, /* 5 */
  287. { OP_Callback, 1, 0, 0},
  288. };
  289. int addr;
  290. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  291. sqlite3VdbeUsesBtree(v, iDb);
  292. if( !zRight ){
  293. sqlite3VdbeSetNumCols(v, 1);
  294. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC);
  295. addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
  296. sqlite3VdbeChangeP1(v, addr, iDb);
  297. sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE);
  298. }else{
  299. int size = atoi(zRight);
  300. if( size<0 ) size = -size;
  301. sqlite3BeginWriteOperation(pParse, 0, iDb);
  302. sqlite3VdbeAddOp(v, OP_Integer, size, 0);
  303. sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2);
  304. addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
  305. sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3);
  306. sqlite3VdbeAddOp(v, OP_Negative, 0, 0);
  307. sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2);
  308. pDb->pSchema->cache_size = size;
  309. sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
  310. }
  311. }else
  312. /*
  313. ** PRAGMA [database.]page_size
  314. ** PRAGMA [database.]page_size=N
  315. **
  316. ** The first form reports the current setting for the
  317. ** database page size in bytes. The second form sets the
  318. ** database page size value. The value can only be set if
  319. ** the database has not yet been created.
  320. */
  321. if( sqlite3StrICmp(zLeft,"page_size")==0 ){
  322. Btree *pBt = pDb->pBt;
  323. if( !zRight ){
  324. int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
  325. returnSingleInt(pParse, "page_size", size);
  326. }else{
  327. /* Malloc may fail when setting the page-size, as there is an internal
  328. ** buffer that the pager module resizes using sqlite3_realloc().
  329. */
  330. if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1) ){
  331. db->mallocFailed = 1;
  332. }
  333. }
  334. }else
  335. /*
  336. ** PRAGMA [database.]max_page_count
  337. ** PRAGMA [database.]max_page_count=N
  338. **
  339. ** The first form reports the current setting for the
  340. ** maximum number of pages in the database file. The
  341. ** second form attempts to change this setting. Both
  342. ** forms return the current setting.
  343. */
  344. if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){
  345. Btree *pBt = pDb->pBt;
  346. int newMax = 0;
  347. if( zRight ){
  348. newMax = atoi(zRight);
  349. }
  350. if( pBt ){
  351. newMax = sqlite3BtreeMaxPageCount(pBt, newMax);
  352. }
  353. returnSingleInt(pParse, "max_page_count", newMax);
  354. }else
  355. /*
  356. ** PRAGMA [database.]locking_mode
  357. ** PRAGMA [database.]locking_mode = (normal|exclusive)
  358. */
  359. if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
  360. const char *zRet = "normal";
  361. int eMode = getLockingMode(zRight);
  362. if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
  363. /* Simple "PRAGMA locking_mode;" statement. This is a query for
  364. ** the current default locking mode (which may be different to
  365. ** the locking-mode of the main database).
  366. */
  367. eMode = db->dfltLockMode;
  368. }else{
  369. Pager *pPager;
  370. if( pId2->n==0 ){
  371. /* This indicates that no database name was specified as part
  372. ** of the PRAGMA command. In this case the locking-mode must be
  373. ** set on all attached databases, as well as the main db file.
  374. **
  375. ** Also, the sqlite3.dfltLockMode variable is set so that
  376. ** any subsequently attached databases also use the specified
  377. ** locking mode.
  378. */
  379. int ii;
  380. assert(pDb==&db->aDb[0]);
  381. for(ii=2; ii<db->nDb; ii++){
  382. pPager = sqlite3BtreePager(db->aDb[ii].pBt);
  383. sqlite3PagerLockingMode(pPager, eMode);
  384. }
  385. db->dfltLockMode = eMode;
  386. }
  387. pPager = sqlite3BtreePager(pDb->pBt);
  388. eMode = sqlite3PagerLockingMode(pPager, eMode);
  389. }
  390. assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
  391. if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
  392. zRet = "exclusive";
  393. }
  394. sqlite3VdbeSetNumCols(v, 1);
  395. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P3_STATIC);
  396. sqlite3VdbeOp3(v, OP_String8, 0, 0, zRet, 0);
  397. sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
  398. }else
  399. #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
  400. /*
  401. ** PRAGMA [database.]auto_vacuum
  402. ** PRAGMA [database.]auto_vacuum=N
  403. **
  404. ** Get or set the (boolean) value of the database 'auto-vacuum' parameter.
  405. */
  406. #ifndef SQLITE_OMIT_AUTOVACUUM
  407. if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
  408. Btree *pBt = pDb->pBt;
  409. if( sqlite3ReadSchema(pParse) ){
  410. goto pragma_out;
  411. }
  412. if( !zRight ){
  413. int auto_vacuum =
  414. pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM;
  415. returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
  416. }else{
  417. int eAuto = getAutoVacuum(zRight);
  418. db->nextAutovac = eAuto;
  419. if( eAuto>=0 ){
  420. /* Call SetAutoVacuum() to set initialize the internal auto and
  421. ** incr-vacuum flags. This is required in case this connection
  422. ** creates the database file. It is important that it is created
  423. ** as an auto-vacuum capable db.
  424. */
  425. int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
  426. if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
  427. /* When setting the auto_vacuum mode to either "full" or
  428. ** "incremental", write the value of meta[6] in the database
  429. ** file. Before writing to meta[6], check that meta[3] indicates
  430. ** that this really is an auto-vacuum capable database.
  431. */
  432. static const VdbeOpList setMeta6[] = {
  433. { OP_Transaction, 0, 1, 0}, /* 0 */
  434. { OP_ReadCookie, 0, 3, 0}, /* 1 */
  435. { OP_If, 0, 0, 0}, /* 2 */
  436. { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
  437. { OP_Integer, 0, 0, 0}, /* 4 */
  438. { OP_SetCookie, 0, 6, 0}, /* 5 */
  439. };
  440. int iAddr;
  441. iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
  442. sqlite3VdbeChangeP1(v, iAddr, iDb);
  443. sqlite3VdbeChangeP1(v, iAddr+1, iDb);
  444. sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
  445. sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
  446. sqlite3VdbeChangeP1(v, iAddr+5, iDb);
  447. sqlite3VdbeUsesBtree(v, iDb);
  448. }
  449. }
  450. }
  451. }else
  452. #endif
  453. /*
  454. ** PRAGMA [database.]incremental_vacuum(N)
  455. **
  456. ** Do N steps of incremental vacuuming on a database.
  457. */
  458. #ifndef SQLITE_OMIT_AUTOVACUUM
  459. if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
  460. int iLimit, addr;
  461. if( sqlite3ReadSchema(pParse) ){
  462. goto pragma_out;
  463. }
  464. if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
  465. iLimit = 0x7fffffff;
  466. }
  467. sqlite3BeginWriteOperation(pParse, 0, iDb);
  468. sqlite3VdbeAddOp(v, OP_MemInt, iLimit, 0);
  469. addr = sqlite3VdbeAddOp(v, OP_IncrVacuum, iDb, 0);
  470. sqlite3VdbeAddOp(v, OP_Callback, 0, 0);
  471. sqlite3VdbeAddOp(v, OP_MemIncr, -1, 0);
  472. sqlite3VdbeAddOp(v, OP_IfMemPos, 0, addr);
  473. sqlite3VdbeJumpHere(v, addr);
  474. }else
  475. #endif
  476. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  477. /*
  478. ** PRAGMA [database.]cache_size
  479. ** PRAGMA [database.]cache_size=N
  480. **
  481. ** The first form reports the current local setting for the
  482. ** page cache size. The local setting can be different from
  483. ** the persistent cache size value that is stored in the database
  484. ** file itself. The value returned is the maximum number of
  485. ** pages in the page cache. The second form sets the local
  486. ** page cache size value. It does not change the persistent
  487. ** cache size stored on the disk so the cache size will revert
  488. ** to its default value when the database is closed and reopened.
  489. ** N should be a positive integer.
  490. */
  491. if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
  492. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  493. if( !zRight ){
  494. returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
  495. }else{
  496. int size = atoi(zRight);
  497. if( size<0 ) size = -size;
  498. pDb->pSchema->cache_size = size;
  499. sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
  500. }
  501. }else
  502. /*
  503. ** PRAGMA temp_store
  504. ** PRAGMA temp_store = "default"|"memory"|"file"
  505. **
  506. ** Return or set the local value of the temp_store flag. Changing
  507. ** the local value does not make changes to the disk file and the default
  508. ** value will be restored the next time the database is opened.
  509. **
  510. ** Note that it is possible for the library compile-time options to
  511. ** override this setting
  512. */
  513. if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
  514. if( !zRight ){
  515. returnSingleInt(pParse, "temp_store", db->temp_store);
  516. }else{
  517. changeTempStorage(pParse, zRight);
  518. }
  519. }else
  520. /*
  521. ** PRAGMA temp_store_directory
  522. ** PRAGMA temp_store_directory = ""|"directory_name"
  523. **
  524. ** Return or set the local value of the temp_store_directory flag. Changing
  525. ** the value sets a specific directory to be used for temporary files.
  526. ** Setting to a null string reverts to the default temporary directory search.
  527. ** If temporary directory is changed, then invalidateTempStorage.
  528. **
  529. */
  530. if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
  531. if( !zRight ){
  532. if( sqlite3_temp_directory ){
  533. sqlite3VdbeSetNumCols(v, 1);
  534. sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
  535. "temp_store_directory", P3_STATIC);
  536. sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
  537. sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
  538. }
  539. }else{
  540. if( zRight[0]
  541. && !sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE)
  542. ){
  543. sqlite3ErrorMsg(pParse, "not a writable directory");
  544. goto pragma_out;
  545. }
  546. if( TEMP_STORE==0
  547. || (TEMP_STORE==1 && db->temp_store<=1)
  548. || (TEMP_STORE==2 && db->temp_store==1)
  549. ){
  550. invalidateTempStorage(pParse);
  551. }
  552. sqlite3_free(sqlite3_temp_directory);
  553. if( zRight[0] ){
  554. sqlite3_temp_directory = zRight;
  555. zRight = 0;
  556. }else{
  557. sqlite3_temp_directory = 0;
  558. }
  559. }
  560. }else
  561. /*
  562. ** PRAGMA [database.]synchronous
  563. ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
  564. **
  565. ** Return or set the local value of the synchronous flag. Changing
  566. ** the local value does not make changes to the disk file and the
  567. ** default value will be restored the next time the database is
  568. ** opened.
  569. */
  570. if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
  571. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  572. if( !zRight ){
  573. returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
  574. }else{
  575. if( !db->autoCommit ){
  576. sqlite3ErrorMsg(pParse,
  577. "Safety level may not be changed inside a transaction");
  578. }else{
  579. pDb->safety_level = getSafetyLevel(zRight)+1;
  580. }
  581. }
  582. }else
  583. #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
  584. #ifndef SQLITE_OMIT_FLAG_PRAGMAS
  585. if( flagPragma(pParse, zLeft, zRight) ){
  586. /* The flagPragma() subroutine also generates any necessary code
  587. ** there is nothing more to do here */
  588. }else
  589. #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
  590. #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
  591. /*
  592. ** PRAGMA table_info(<table>)
  593. **
  594. ** Return a single row for each column of the named table. The columns of
  595. ** the returned data set are:
  596. **
  597. ** cid: Column id (numbered from left to right, starting at 0)
  598. ** name: Column name
  599. ** type: Column declaration type.
  600. ** notnull: True if 'NOT NULL' is part of column declaration
  601. ** dflt_value: The default value for the column, if any.
  602. */
  603. if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
  604. Table *pTab;
  605. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  606. pTab = sqlite3FindTable(db, zRight, zDb);
  607. if( pTab ){
  608. int i;
  609. int nHidden = 0;
  610. Column *pCol;
  611. sqlite3VdbeSetNumCols(v, 6);
  612. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P3_STATIC);
  613. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
  614. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P3_STATIC);
  615. sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P3_STATIC);
  616. sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P3_STATIC);
  617. sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P3_STATIC);
  618. sqlite3ViewGetColumnNames(pParse, pTab);
  619. for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
  620. const Token *pDflt;
  621. if( IsHiddenColumn(pCol) ){
  622. nHidden++;
  623. continue;
  624. }
  625. sqlite3VdbeAddOp(v, OP_Integer, i-nHidden, 0);
  626. sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0);
  627. sqlite3VdbeOp3(v, OP_String8, 0, 0,
  628. pCol->zType ? pCol->zType : "", 0);
  629. sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0);
  630. if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){
  631. sqlite3VdbeOp3(v, OP_String8, 0, 0, (char*)pDflt->z, pDflt->n);
  632. }else{
  633. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  634. }
  635. sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0);
  636. sqlite3VdbeAddOp(v, OP_Callback, 6, 0);
  637. }
  638. }
  639. }else
  640. if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
  641. Index *pIdx;
  642. Table *pTab;
  643. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  644. pIdx = sqlite3FindIndex(db, zRight, zDb);
  645. if( pIdx ){
  646. int i;
  647. pTab = pIdx->pTable;
  648. sqlite3VdbeSetNumCols(v, 3);
  649. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P3_STATIC);
  650. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P3_STATIC);
  651. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P3_STATIC);
  652. for(i=0; i<pIdx->nColumn; i++){
  653. int cnum = pIdx->aiColumn[i];
  654. sqlite3VdbeAddOp(v, OP_Integer, i, 0);
  655. sqlite3VdbeAddOp(v, OP_Integer, cnum, 0);
  656. assert( pTab->nCol>cnum );
  657. sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0);
  658. sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
  659. }
  660. }
  661. }else
  662. if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
  663. Index *pIdx;
  664. Table *pTab;
  665. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  666. pTab = sqlite3FindTable(db, zRight, zDb);
  667. if( pTab ){
  668. v = sqlite3GetVdbe(pParse);
  669. pIdx = pTab->pIndex;
  670. if( pIdx ){
  671. int i = 0;
  672. sqlite3VdbeSetNumCols(v, 3);
  673. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
  674. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
  675. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P3_STATIC);
  676. while(pIdx){
  677. sqlite3VdbeAddOp(v, OP_Integer, i, 0);
  678. sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0);
  679. sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
  680. sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
  681. ++i;
  682. pIdx = pIdx->pNext;
  683. }
  684. }
  685. }
  686. }else
  687. if( sqlite3StrICmp(zLeft, "database_list")==0 ){
  688. int i;
  689. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  690. sqlite3VdbeSetNumCols(v, 3);
  691. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
  692. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
  693. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P3_STATIC);
  694. for(i=0; i<db->nDb; i++){
  695. if( db->aDb[i].pBt==0 ) continue;
  696. assert( db->aDb[i].zName!=0 );
  697. sqlite3VdbeAddOp(v, OP_Integer, i, 0);
  698. sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0);
  699. sqlite3VdbeOp3(v, OP_String8, 0, 0,
  700. sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
  701. sqlite3VdbeAddOp(v, OP_Callback, 3, 0);
  702. }
  703. }else
  704. if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
  705. int i = 0;
  706. HashElem *p;
  707. sqlite3VdbeSetNumCols(v, 2);
  708. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC);
  709. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC);
  710. for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
  711. CollSeq *pColl = (CollSeq *)sqliteHashData(p);
  712. sqlite3VdbeAddOp(v, OP_Integer, i++, 0);
  713. sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0);
  714. sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
  715. }
  716. }else
  717. #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
  718. #ifndef SQLITE_OMIT_FOREIGN_KEY
  719. if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
  720. FKey *pFK;
  721. Table *pTab;
  722. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  723. pTab = sqlite3FindTable(db, zRight, zDb);
  724. if( pTab ){
  725. v = sqlite3GetVdbe(pParse);
  726. pFK = pTab->pFKey;
  727. if( pFK ){
  728. int i = 0;
  729. sqlite3VdbeSetNumCols(v, 5);
  730. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P3_STATIC);
  731. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P3_STATIC);
  732. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P3_STATIC);
  733. sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P3_STATIC);
  734. sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P3_STATIC);
  735. while(pFK){
  736. int j;
  737. for(j=0; j<pFK->nCol; j++){
  738. char *zCol = pFK->aCol[j].zCol;
  739. sqlite3VdbeAddOp(v, OP_Integer, i, 0);
  740. sqlite3VdbeAddOp(v, OP_Integer, j, 0);
  741. sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0);
  742. sqlite3VdbeOp3(v, OP_String8, 0, 0,
  743. pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
  744. sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0);
  745. sqlite3VdbeAddOp(v, OP_Callback, 5, 0);
  746. }
  747. ++i;
  748. pFK = pFK->pNextFrom;
  749. }
  750. }
  751. }
  752. }else
  753. #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
  754. #ifndef NDEBUG
  755. if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
  756. if( zRight ){
  757. if( getBoolean(zRight) ){
  758. sqlite3ParserTrace(stderr, "parser: ");
  759. }else{
  760. sqlite3ParserTrace(0, 0);
  761. }
  762. }
  763. }else
  764. #endif
  765. /* Reinstall the LIKE and GLOB functions. The variant of LIKE
  766. ** used will be case sensitive or not depending on the RHS.
  767. */
  768. if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
  769. if( zRight ){
  770. sqlite3RegisterLikeFunctions(db, getBoolean(zRight));
  771. }
  772. }else
  773. #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
  774. # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
  775. #endif
  776. #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  777. if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){
  778. int i, j, addr, mxErr;
  779. /* Code that appears at the end of the integrity check. If no error
  780. ** messages have been generated, output OK. Otherwise output the
  781. ** error message
  782. */
  783. static const VdbeOpList endCode[] = {
  784. { OP_MemLoad, 0, 0, 0},
  785. { OP_Integer, 0, 0, 0},
  786. { OP_Ne, 0, 0, 0}, /* 2 */
  787. { OP_String8, 0, 0, "ok"},
  788. { OP_Callback, 1, 0, 0},
  789. };
  790. /* Initialize the VDBE program */
  791. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  792. sqlite3VdbeSetNumCols(v, 1);
  793. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P3_STATIC);
  794. /* Set the maximum error count */
  795. mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  796. if( zRight ){
  797. mxErr = atoi(zRight);
  798. if( mxErr<=0 ){
  799. mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  800. }
  801. }
  802. sqlite3VdbeAddOp(v, OP_MemInt, mxErr, 0);
  803. /* Do an integrity check on each database file */
  804. for(i=0; i<db->nDb; i++){
  805. HashElem *x;
  806. Hash *pTbls;
  807. int cnt = 0;
  808. if( OMIT_TEMPDB && i==1 ) continue;
  809. sqlite3CodeVerifySchema(pParse, i);
  810. addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0);
  811. sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
  812. sqlite3VdbeJumpHere(v, addr);
  813. /* Do an integrity check of the B-Tree
  814. */
  815. pTbls = &db->aDb[i].pSchema->tblHash;
  816. for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  817. Table *pTab = sqliteHashData(x);
  818. Index *pIdx;
  819. sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0);
  820. cnt++;
  821. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  822. sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0);
  823. cnt++;
  824. }
  825. }
  826. if( cnt==0 ) continue;
  827. sqlite3VdbeAddOp(v, OP_IntegrityCk, 0, i);
  828. addr = sqlite3VdbeAddOp(v, OP_IsNull, -1, 0);
  829. sqlite3VdbeOp3(v, OP_String8, 0, 0,
  830. sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
  831. P3_DYNAMIC);
  832. sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
  833. sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
  834. sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
  835. sqlite3VdbeJumpHere(v, addr);
  836. /* Make sure all the indices are constructed correctly.
  837. */
  838. for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  839. Table *pTab = sqliteHashData(x);
  840. Index *pIdx;
  841. int loopTop;
  842. if( pTab->pIndex==0 ) continue;
  843. addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0);
  844. sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
  845. sqlite3VdbeJumpHere(v, addr);
  846. sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
  847. sqlite3VdbeAddOp(v, OP_MemInt, 0, 1);
  848. loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0);
  849. sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1);
  850. for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  851. int jmp2;
  852. static const VdbeOpList idxErr[] = {
  853. { OP_MemIncr, -1, 0, 0},
  854. { OP_String8, 0, 0, "rowid "},
  855. { OP_Rowid, 1, 0, 0},
  856. { OP_String8, 0, 0, " missing from index "},
  857. { OP_String8, 0, 0, 0}, /* 4 */
  858. { OP_Concat, 2, 0, 0},
  859. { OP_Callback, 1, 0, 0},
  860. };
  861. sqlite3GenerateIndexKey(v, pIdx, 1);
  862. jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0);
  863. addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
  864. sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC);
  865. sqlite3VdbeJumpHere(v, jmp2);
  866. }
  867. sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1);
  868. sqlite3VdbeJumpHere(v, loopTop);
  869. for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  870. static const VdbeOpList cntIdx[] = {
  871. { OP_MemInt, 0, 2, 0},
  872. { OP_Rewind, 0, 0, 0}, /* 1 */
  873. { OP_MemIncr, 1, 2, 0},
  874. { OP_Next, 0, 0, 0}, /* 3 */
  875. { OP_MemLoad, 1, 0, 0},
  876. { OP_MemLoad, 2, 0, 0},
  877. { OP_Eq, 0, 0, 0}, /* 6 */
  878. { OP_MemIncr, -1, 0, 0},
  879. { OP_String8, 0, 0, "wrong # of entries in index "},
  880. { OP_String8, 0, 0, 0}, /* 9 */
  881. { OP_Concat, 0, 0, 0},
  882. { OP_Callback, 1, 0, 0},
  883. };
  884. if( pIdx->tnum==0 ) continue;
  885. addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0);
  886. sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
  887. sqlite3VdbeJumpHere(v, addr);
  888. addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
  889. sqlite3VdbeChangeP1(v, addr+1, j+2);
  890. sqlite3VdbeChangeP2(v, addr+1, addr+4);
  891. sqlite3VdbeChangeP1(v, addr+3, j+2);
  892. sqlite3VdbeChangeP2(v, addr+3, addr+2);
  893. sqlite3VdbeJumpHere(v, addr+6);
  894. sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC);
  895. }
  896. }
  897. }
  898. addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
  899. sqlite3VdbeChangeP1(v, addr+1, mxErr);
  900. sqlite3VdbeJumpHere(v, addr+2);
  901. }else
  902. #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  903. #ifndef SQLITE_OMIT_UTF16
  904. /*
  905. ** PRAGMA encoding
  906. ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
  907. **
  908. ** In its first form, this pragma returns the encoding of the main
  909. ** database. If the database is not initialized, it is initialized now.
  910. **
  911. ** The second form of this pragma is a no-op if the main database file
  912. ** has not already been initialized. In this case it sets the default
  913. ** encoding that will be used for the main database file if a new file
  914. ** is created. If an existing main database file is opened, then the
  915. ** default text encoding for the existing database is used.
  916. **
  917. ** In all cases new databases created using the ATTACH command are
  918. ** created to use the same default text encoding as the main database. If
  919. ** the main database has not been initialized and/or created when ATTACH
  920. ** is executed, this is done before the ATTACH operation.
  921. **
  922. ** In the second form this pragma sets the text encoding to be used in
  923. ** new database files created using this database handle. It is only
  924. ** useful if invoked immediately after the main database i
  925. */
  926. if( sqlite3StrICmp(zLeft, "encoding")==0 ){
  927. static const struct EncName {
  928. char *zName;
  929. u8 enc;
  930. } encnames[] = {
  931. { "UTF-8", SQLITE_UTF8 },
  932. { "UTF8", SQLITE_UTF8 },
  933. { "UTF-16le", SQLITE_UTF16LE },
  934. { "UTF16le", SQLITE_UTF16LE },
  935. { "UTF-16be", SQLITE_UTF16BE },
  936. { "UTF16be", SQLITE_UTF16BE },
  937. { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
  938. { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
  939. { 0, 0 }
  940. };
  941. const struct EncName *pEnc;
  942. if( !zRight ){ /* "PRAGMA encoding" */
  943. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  944. sqlite3VdbeSetNumCols(v, 1);
  945. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P3_STATIC);
  946. sqlite3VdbeAddOp(v, OP_String8, 0, 0);
  947. for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
  948. if( pEnc->enc==ENC(pParse->db) ){
  949. sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC);
  950. break;
  951. }
  952. }
  953. sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
  954. }else{ /* "PRAGMA encoding = XXX" */
  955. /* Only change the value of sqlite.enc if the database handle is not
  956. ** initialized. If the main database exists, the new sqlite.enc value
  957. ** will be overwritten when the schema is next loaded. If it does not
  958. ** already exists, it will be created to use the new encoding value.
  959. */
  960. if(
  961. !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
  962. DbHasProperty(db, 0, DB_Empty)
  963. ){
  964. for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
  965. if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
  966. ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
  967. break;
  968. }
  969. }
  970. if( !pEnc->zName ){
  971. sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
  972. }
  973. }
  974. }
  975. }else
  976. #endif /* SQLITE_OMIT_UTF16 */
  977. #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
  978. /*
  979. ** PRAGMA [database.]schema_version
  980. ** PRAGMA [database.]schema_version = <integer>
  981. **
  982. ** PRAGMA [database.]user_version
  983. ** PRAGMA [database.]user_version = <integer>
  984. **
  985. ** The pragma's schema_version and user_version are used to set or get
  986. ** the value of the schema-version and user-version, respectively. Both
  987. ** the schema-version and the user-version are 32-bit signed integers
  988. ** stored in the database header.
  989. **
  990. ** The schema-cookie is usually only manipulated internally by SQLite. It
  991. ** is incremented by SQLite whenever the database schema is modified (by
  992. ** creating or dropping a table or index). The schema version is used by
  993. ** SQLite each time a query is executed to ensure that the internal cache
  994. ** of the schema used when compiling the SQL query matches the schema of
  995. ** the database against which the compiled query is actually executed.
  996. ** Subverting this mechanism by using "PRAGMA schema_version" to modify
  997. ** the schema-version is potentially dangerous and may lead to program
  998. ** crashes or database corruption. Use with caution!
  999. **
  1000. ** The user-version is not used internally by SQLite. It may be used by
  1001. ** applications for any purpose.
  1002. */
  1003. if( sqlite3StrICmp(zLeft, "schema_version")==0
  1004. || sqlite3StrICmp(zLeft, "user_version")==0
  1005. || sqlite3StrICmp(zLeft, "freelist_count")==0
  1006. ){
  1007. int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
  1008. sqlite3VdbeUsesBtree(v, iDb);
  1009. switch( zLeft[0] ){
  1010. case 's': case 'S':
  1011. iCookie = 0;
  1012. break;
  1013. case 'f': case 'F':
  1014. iCookie = 1;
  1015. iDb = (-1*(iDb+1));
  1016. assert(iDb<=0);
  1017. break;
  1018. default:
  1019. iCookie = 5;
  1020. break;
  1021. }
  1022. if( zRight && iDb>=0 ){
  1023. /* Write the specified cookie value */
  1024. static const VdbeOpList setCookie[] = {
  1025. { OP_Transaction, 0, 1, 0}, /* 0 */
  1026. { OP_Integer, 0, 0, 0}, /* 1 */
  1027. { OP_SetCookie, 0, 0, 0}, /* 2 */
  1028. };
  1029. int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
  1030. sqlite3VdbeChangeP1(v, addr, iDb);
  1031. sqlite3VdbeChangeP1(v, addr+1, atoi(zRight));
  1032. sqlite3VdbeChangeP1(v, addr+2, iDb);
  1033. sqlite3VdbeChangeP2(v, addr+2, iCookie);
  1034. }else{
  1035. /* Read the specified cookie value */
  1036. static const VdbeOpList readCookie[] = {
  1037. { OP_ReadCookie, 0, 0, 0}, /* 0 */
  1038. { OP_Callback, 1, 0, 0}
  1039. };
  1040. int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
  1041. sqlite3VdbeChangeP1(v, addr, iDb);
  1042. sqlite3VdbeChangeP2(v, addr, iCookie);
  1043. sqlite3VdbeSetNumCols(v, 1);
  1044. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P3_TRANSIENT);
  1045. }
  1046. }else
  1047. #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
  1048. #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1049. /*
  1050. ** Report the current state of file logs for all databases
  1051. */
  1052. if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
  1053. static const char *const azLockName[] = {
  1054. "unlocked", "shared", "reserved", "pending", "exclusive"
  1055. };
  1056. int i;
  1057. Vdbe *v = sqlite3GetVdbe(pParse);
  1058. sqlite3VdbeSetNumCols(v, 2);
  1059. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P3_STATIC);
  1060. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC);
  1061. for(i=0; i<db->nDb; i++){
  1062. Btree *pBt;
  1063. Pager *pPager;
  1064. const char *zState = "unknown";
  1065. int j;
  1066. if( db->aDb[i].zName==0 ) continue;
  1067. sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC);
  1068. pBt = db->aDb[i].pBt;
  1069. if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
  1070. zState = "closed";
  1071. }else if( sqlite3_file_control(db, db->aDb[i].zName,
  1072. SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
  1073. zState = azLockName[j];
  1074. }
  1075. sqlite3VdbeOp3(v, OP_String8, 0, 0, zState, P3_STATIC);
  1076. sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
  1077. }
  1078. }else
  1079. #endif
  1080. #ifdef SQLITE_SSE
  1081. /*
  1082. ** Check to see if the sqlite_statements table exists. Create it
  1083. ** if it does not.
  1084. */
  1085. if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){
  1086. extern int sqlite3CreateStatementsTable(Parse*);
  1087. sqlite3CreateStatementsTable(pParse);
  1088. }else
  1089. #endif
  1090. #if SQLITE_HAS_CODEC
  1091. if( sqlite3StrICmp(zLeft, "key")==0 ){
  1092. sqlite3_key(db, zRight, strlen(zRight));
  1093. }else
  1094. #endif
  1095. #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD)
  1096. if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
  1097. #if SQLITE_HAS_CODEC
  1098. if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
  1099. extern void sqlite3_activate_see(const char*);
  1100. sqlite3_activate_see(&zRight[4]);
  1101. }
  1102. #endif
  1103. #ifdef SQLITE_ENABLE_CEROD
  1104. if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
  1105. extern void sqlite3_activate_cerod(const char*);
  1106. sqlite3_activate_cerod(&zRight[6]);
  1107. }
  1108. #endif
  1109. }
  1110. #endif
  1111. {}
  1112. if( v ){
  1113. /* Code an OP_Expire at the end of each PRAGMA program to cause
  1114. ** the VDBE implementing the pragma to expire. Most (all?) pragmas
  1115. ** are only valid for a single execution.
  1116. */
  1117. sqlite3VdbeAddOp(v, OP_Expire, 1, 0);
  1118. /*
  1119. ** Reset the safety level, in case the fullfsync flag or synchronous
  1120. ** setting changed.
  1121. */
  1122. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1123. if( db->autoCommit ){
  1124. sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
  1125. (db->flags&SQLITE_FullFSync)!=0);
  1126. }
  1127. #endif
  1128. }
  1129. pragma_out:
  1130. sqlite3_free(zLeft);
  1131. sqlite3_free(zRight);
  1132. }
  1133. #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */