pragma.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  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. #include "sqliteInt.h"
  15. #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
  16. # if defined(__APPLE__)
  17. # define SQLITE_ENABLE_LOCKING_STYLE 1
  18. # else
  19. # define SQLITE_ENABLE_LOCKING_STYLE 0
  20. # endif
  21. #endif
  22. /***************************************************************************
  23. ** The next block of code, including the PragTyp_XXXX macro definitions and
  24. ** the aPragmaName[] object is composed of generated code. DO NOT EDIT.
  25. **
  26. ** To add new pragmas, edit the code in ../tool/mkpragmatab.tcl and rerun
  27. ** that script. Then copy/paste the output in place of the following:
  28. */
  29. #define PragTyp_HEADER_VALUE 0
  30. #define PragTyp_AUTO_VACUUM 1
  31. #define PragTyp_FLAG 2
  32. #define PragTyp_BUSY_TIMEOUT 3
  33. #define PragTyp_CACHE_SIZE 4
  34. #define PragTyp_CASE_SENSITIVE_LIKE 5
  35. #define PragTyp_COLLATION_LIST 6
  36. #define PragTyp_COMPILE_OPTIONS 7
  37. #define PragTyp_DATA_STORE_DIRECTORY 8
  38. #define PragTyp_DATABASE_LIST 9
  39. #define PragTyp_DEFAULT_CACHE_SIZE 10
  40. #define PragTyp_ENCODING 11
  41. #define PragTyp_FOREIGN_KEY_CHECK 12
  42. #define PragTyp_FOREIGN_KEY_LIST 13
  43. #define PragTyp_INCREMENTAL_VACUUM 14
  44. #define PragTyp_INDEX_INFO 15
  45. #define PragTyp_INDEX_LIST 16
  46. #define PragTyp_INTEGRITY_CHECK 17
  47. #define PragTyp_JOURNAL_MODE 18
  48. #define PragTyp_JOURNAL_SIZE_LIMIT 19
  49. #define PragTyp_LOCK_PROXY_FILE 20
  50. #define PragTyp_LOCKING_MODE 21
  51. #define PragTyp_PAGE_COUNT 22
  52. #define PragTyp_MMAP_SIZE 23
  53. #define PragTyp_PAGE_SIZE 24
  54. #define PragTyp_SECURE_DELETE 25
  55. #define PragTyp_SHRINK_MEMORY 26
  56. #define PragTyp_SOFT_HEAP_LIMIT 27
  57. #define PragTyp_STATS 28
  58. #define PragTyp_SYNCHRONOUS 29
  59. #define PragTyp_TABLE_INFO 30
  60. #define PragTyp_TEMP_STORE 31
  61. #define PragTyp_TEMP_STORE_DIRECTORY 32
  62. #define PragTyp_WAL_AUTOCHECKPOINT 33
  63. #define PragTyp_WAL_CHECKPOINT 34
  64. #define PragTyp_ACTIVATE_EXTENSIONS 35
  65. #define PragTyp_HEXKEY 36
  66. #define PragTyp_KEY 37
  67. #define PragTyp_REKEY 38
  68. #define PragTyp_LOCK_STATUS 39
  69. #define PragTyp_PARSER_TRACE 40
  70. #define PragFlag_NeedSchema 0x01
  71. static const struct sPragmaNames {
  72. const char *const zName; /* Name of pragma */
  73. u8 ePragTyp; /* PragTyp_XXX value */
  74. u8 mPragFlag; /* Zero or more PragFlag_XXX values */
  75. u32 iArg; /* Extra argument */
  76. } aPragmaNames[] = {
  77. #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
  78. { /* zName: */ "activate_extensions",
  79. /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
  80. /* ePragFlag: */ 0,
  81. /* iArg: */ 0 },
  82. #endif
  83. #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
  84. { /* zName: */ "application_id",
  85. /* ePragTyp: */ PragTyp_HEADER_VALUE,
  86. /* ePragFlag: */ 0,
  87. /* iArg: */ 0 },
  88. #endif
  89. #if !defined(SQLITE_OMIT_AUTOVACUUM)
  90. { /* zName: */ "auto_vacuum",
  91. /* ePragTyp: */ PragTyp_AUTO_VACUUM,
  92. /* ePragFlag: */ PragFlag_NeedSchema,
  93. /* iArg: */ 0 },
  94. #endif
  95. #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
  96. { /* zName: */ "automatic_index",
  97. /* ePragTyp: */ PragTyp_FLAG,
  98. /* ePragFlag: */ 0,
  99. /* iArg: */ SQLITE_AutoIndex },
  100. #endif
  101. { /* zName: */ "busy_timeout",
  102. /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
  103. /* ePragFlag: */ 0,
  104. /* iArg: */ 0 },
  105. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  106. { /* zName: */ "cache_size",
  107. /* ePragTyp: */ PragTyp_CACHE_SIZE,
  108. /* ePragFlag: */ PragFlag_NeedSchema,
  109. /* iArg: */ 0 },
  110. #endif
  111. { /* zName: */ "cache_spill",
  112. /* ePragTyp: */ PragTyp_FLAG,
  113. /* ePragFlag: */ 0,
  114. /* iArg: */ SQLITE_CacheSpill },
  115. { /* zName: */ "case_sensitive_like",
  116. /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
  117. /* ePragFlag: */ 0,
  118. /* iArg: */ 0 },
  119. { /* zName: */ "checkpoint_fullfsync",
  120. /* ePragTyp: */ PragTyp_FLAG,
  121. /* ePragFlag: */ 0,
  122. /* iArg: */ SQLITE_CkptFullFSync },
  123. #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
  124. { /* zName: */ "collation_list",
  125. /* ePragTyp: */ PragTyp_COLLATION_LIST,
  126. /* ePragFlag: */ 0,
  127. /* iArg: */ 0 },
  128. #endif
  129. #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
  130. { /* zName: */ "compile_options",
  131. /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
  132. /* ePragFlag: */ 0,
  133. /* iArg: */ 0 },
  134. #endif
  135. { /* zName: */ "count_changes",
  136. /* ePragTyp: */ PragTyp_FLAG,
  137. /* ePragFlag: */ 0,
  138. /* iArg: */ SQLITE_CountRows },
  139. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
  140. { /* zName: */ "data_store_directory",
  141. /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
  142. /* ePragFlag: */ 0,
  143. /* iArg: */ 0 },
  144. #endif
  145. #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
  146. { /* zName: */ "database_list",
  147. /* ePragTyp: */ PragTyp_DATABASE_LIST,
  148. /* ePragFlag: */ PragFlag_NeedSchema,
  149. /* iArg: */ 0 },
  150. #endif
  151. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
  152. { /* zName: */ "default_cache_size",
  153. /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
  154. /* ePragFlag: */ PragFlag_NeedSchema,
  155. /* iArg: */ 0 },
  156. #endif
  157. #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  158. { /* zName: */ "defer_foreign_keys",
  159. /* ePragTyp: */ PragTyp_FLAG,
  160. /* ePragFlag: */ 0,
  161. /* iArg: */ SQLITE_DeferFKs },
  162. #endif
  163. { /* zName: */ "empty_result_callbacks",
  164. /* ePragTyp: */ PragTyp_FLAG,
  165. /* ePragFlag: */ 0,
  166. /* iArg: */ SQLITE_NullCallback },
  167. #if !defined(SQLITE_OMIT_UTF16)
  168. { /* zName: */ "encoding",
  169. /* ePragTyp: */ PragTyp_ENCODING,
  170. /* ePragFlag: */ 0,
  171. /* iArg: */ 0 },
  172. #endif
  173. #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  174. { /* zName: */ "foreign_key_check",
  175. /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
  176. /* ePragFlag: */ PragFlag_NeedSchema,
  177. /* iArg: */ 0 },
  178. #endif
  179. #if !defined(SQLITE_OMIT_FOREIGN_KEY)
  180. { /* zName: */ "foreign_key_list",
  181. /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
  182. /* ePragFlag: */ PragFlag_NeedSchema,
  183. /* iArg: */ 0 },
  184. #endif
  185. #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  186. { /* zName: */ "foreign_keys",
  187. /* ePragTyp: */ PragTyp_FLAG,
  188. /* ePragFlag: */ 0,
  189. /* iArg: */ SQLITE_ForeignKeys },
  190. #endif
  191. #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
  192. { /* zName: */ "freelist_count",
  193. /* ePragTyp: */ PragTyp_HEADER_VALUE,
  194. /* ePragFlag: */ 0,
  195. /* iArg: */ 0 },
  196. #endif
  197. { /* zName: */ "full_column_names",
  198. /* ePragTyp: */ PragTyp_FLAG,
  199. /* ePragFlag: */ 0,
  200. /* iArg: */ SQLITE_FullColNames },
  201. { /* zName: */ "fullfsync",
  202. /* ePragTyp: */ PragTyp_FLAG,
  203. /* ePragFlag: */ 0,
  204. /* iArg: */ SQLITE_FullFSync },
  205. #if defined(SQLITE_HAS_CODEC)
  206. { /* zName: */ "hexkey",
  207. /* ePragTyp: */ PragTyp_HEXKEY,
  208. /* ePragFlag: */ 0,
  209. /* iArg: */ 0 },
  210. { /* zName: */ "hexrekey",
  211. /* ePragTyp: */ PragTyp_HEXKEY,
  212. /* ePragFlag: */ 0,
  213. /* iArg: */ 0 },
  214. #endif
  215. #if !defined(SQLITE_OMIT_CHECK)
  216. { /* zName: */ "ignore_check_constraints",
  217. /* ePragTyp: */ PragTyp_FLAG,
  218. /* ePragFlag: */ 0,
  219. /* iArg: */ SQLITE_IgnoreChecks },
  220. #endif
  221. #if !defined(SQLITE_OMIT_AUTOVACUUM)
  222. { /* zName: */ "incremental_vacuum",
  223. /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
  224. /* ePragFlag: */ PragFlag_NeedSchema,
  225. /* iArg: */ 0 },
  226. #endif
  227. #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
  228. { /* zName: */ "index_info",
  229. /* ePragTyp: */ PragTyp_INDEX_INFO,
  230. /* ePragFlag: */ PragFlag_NeedSchema,
  231. /* iArg: */ 0 },
  232. { /* zName: */ "index_list",
  233. /* ePragTyp: */ PragTyp_INDEX_LIST,
  234. /* ePragFlag: */ PragFlag_NeedSchema,
  235. /* iArg: */ 0 },
  236. #endif
  237. #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
  238. { /* zName: */ "integrity_check",
  239. /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
  240. /* ePragFlag: */ PragFlag_NeedSchema,
  241. /* iArg: */ 0 },
  242. #endif
  243. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  244. { /* zName: */ "journal_mode",
  245. /* ePragTyp: */ PragTyp_JOURNAL_MODE,
  246. /* ePragFlag: */ PragFlag_NeedSchema,
  247. /* iArg: */ 0 },
  248. { /* zName: */ "journal_size_limit",
  249. /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
  250. /* ePragFlag: */ 0,
  251. /* iArg: */ 0 },
  252. #endif
  253. #if defined(SQLITE_HAS_CODEC)
  254. { /* zName: */ "key",
  255. /* ePragTyp: */ PragTyp_KEY,
  256. /* ePragFlag: */ 0,
  257. /* iArg: */ 0 },
  258. #endif
  259. { /* zName: */ "legacy_file_format",
  260. /* ePragTyp: */ PragTyp_FLAG,
  261. /* ePragFlag: */ 0,
  262. /* iArg: */ SQLITE_LegacyFileFmt },
  263. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
  264. { /* zName: */ "lock_proxy_file",
  265. /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
  266. /* ePragFlag: */ 0,
  267. /* iArg: */ 0 },
  268. #endif
  269. #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  270. { /* zName: */ "lock_status",
  271. /* ePragTyp: */ PragTyp_LOCK_STATUS,
  272. /* ePragFlag: */ 0,
  273. /* iArg: */ 0 },
  274. #endif
  275. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  276. { /* zName: */ "locking_mode",
  277. /* ePragTyp: */ PragTyp_LOCKING_MODE,
  278. /* ePragFlag: */ 0,
  279. /* iArg: */ 0 },
  280. { /* zName: */ "max_page_count",
  281. /* ePragTyp: */ PragTyp_PAGE_COUNT,
  282. /* ePragFlag: */ PragFlag_NeedSchema,
  283. /* iArg: */ 0 },
  284. { /* zName: */ "mmap_size",
  285. /* ePragTyp: */ PragTyp_MMAP_SIZE,
  286. /* ePragFlag: */ 0,
  287. /* iArg: */ 0 },
  288. { /* zName: */ "page_count",
  289. /* ePragTyp: */ PragTyp_PAGE_COUNT,
  290. /* ePragFlag: */ PragFlag_NeedSchema,
  291. /* iArg: */ 0 },
  292. { /* zName: */ "page_size",
  293. /* ePragTyp: */ PragTyp_PAGE_SIZE,
  294. /* ePragFlag: */ 0,
  295. /* iArg: */ 0 },
  296. #endif
  297. #if defined(SQLITE_DEBUG)
  298. { /* zName: */ "parser_trace",
  299. /* ePragTyp: */ PragTyp_PARSER_TRACE,
  300. /* ePragFlag: */ 0,
  301. /* iArg: */ 0 },
  302. #endif
  303. { /* zName: */ "query_only",
  304. /* ePragTyp: */ PragTyp_FLAG,
  305. /* ePragFlag: */ 0,
  306. /* iArg: */ SQLITE_QueryOnly },
  307. #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
  308. { /* zName: */ "quick_check",
  309. /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
  310. /* ePragFlag: */ PragFlag_NeedSchema,
  311. /* iArg: */ 0 },
  312. #endif
  313. { /* zName: */ "read_uncommitted",
  314. /* ePragTyp: */ PragTyp_FLAG,
  315. /* ePragFlag: */ 0,
  316. /* iArg: */ SQLITE_ReadUncommitted },
  317. { /* zName: */ "recursive_triggers",
  318. /* ePragTyp: */ PragTyp_FLAG,
  319. /* ePragFlag: */ 0,
  320. /* iArg: */ SQLITE_RecTriggers },
  321. #if defined(SQLITE_HAS_CODEC)
  322. { /* zName: */ "rekey",
  323. /* ePragTyp: */ PragTyp_REKEY,
  324. /* ePragFlag: */ 0,
  325. /* iArg: */ 0 },
  326. #endif
  327. { /* zName: */ "reverse_unordered_selects",
  328. /* ePragTyp: */ PragTyp_FLAG,
  329. /* ePragFlag: */ 0,
  330. /* iArg: */ SQLITE_ReverseOrder },
  331. #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
  332. { /* zName: */ "schema_version",
  333. /* ePragTyp: */ PragTyp_HEADER_VALUE,
  334. /* ePragFlag: */ 0,
  335. /* iArg: */ 0 },
  336. #endif
  337. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  338. { /* zName: */ "secure_delete",
  339. /* ePragTyp: */ PragTyp_SECURE_DELETE,
  340. /* ePragFlag: */ 0,
  341. /* iArg: */ 0 },
  342. #endif
  343. { /* zName: */ "short_column_names",
  344. /* ePragTyp: */ PragTyp_FLAG,
  345. /* ePragFlag: */ 0,
  346. /* iArg: */ SQLITE_ShortColNames },
  347. { /* zName: */ "shrink_memory",
  348. /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
  349. /* ePragFlag: */ 0,
  350. /* iArg: */ 0 },
  351. { /* zName: */ "soft_heap_limit",
  352. /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
  353. /* ePragFlag: */ 0,
  354. /* iArg: */ 0 },
  355. #if defined(SQLITE_DEBUG)
  356. { /* zName: */ "sql_trace",
  357. /* ePragTyp: */ PragTyp_FLAG,
  358. /* ePragFlag: */ 0,
  359. /* iArg: */ SQLITE_SqlTrace },
  360. #endif
  361. #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
  362. { /* zName: */ "stats",
  363. /* ePragTyp: */ PragTyp_STATS,
  364. /* ePragFlag: */ PragFlag_NeedSchema,
  365. /* iArg: */ 0 },
  366. #endif
  367. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  368. { /* zName: */ "synchronous",
  369. /* ePragTyp: */ PragTyp_SYNCHRONOUS,
  370. /* ePragFlag: */ PragFlag_NeedSchema,
  371. /* iArg: */ 0 },
  372. #endif
  373. #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
  374. { /* zName: */ "table_info",
  375. /* ePragTyp: */ PragTyp_TABLE_INFO,
  376. /* ePragFlag: */ PragFlag_NeedSchema,
  377. /* iArg: */ 0 },
  378. #endif
  379. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  380. { /* zName: */ "temp_store",
  381. /* ePragTyp: */ PragTyp_TEMP_STORE,
  382. /* ePragFlag: */ 0,
  383. /* iArg: */ 0 },
  384. { /* zName: */ "temp_store_directory",
  385. /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
  386. /* ePragFlag: */ 0,
  387. /* iArg: */ 0 },
  388. #endif
  389. #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
  390. { /* zName: */ "user_version",
  391. /* ePragTyp: */ PragTyp_HEADER_VALUE,
  392. /* ePragFlag: */ 0,
  393. /* iArg: */ 0 },
  394. #endif
  395. #if defined(SQLITE_DEBUG)
  396. { /* zName: */ "vdbe_addoptrace",
  397. /* ePragTyp: */ PragTyp_FLAG,
  398. /* ePragFlag: */ 0,
  399. /* iArg: */ SQLITE_VdbeAddopTrace },
  400. { /* zName: */ "vdbe_debug",
  401. /* ePragTyp: */ PragTyp_FLAG,
  402. /* ePragFlag: */ 0,
  403. /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
  404. { /* zName: */ "vdbe_listing",
  405. /* ePragTyp: */ PragTyp_FLAG,
  406. /* ePragFlag: */ 0,
  407. /* iArg: */ SQLITE_VdbeListing },
  408. { /* zName: */ "vdbe_trace",
  409. /* ePragTyp: */ PragTyp_FLAG,
  410. /* ePragFlag: */ 0,
  411. /* iArg: */ SQLITE_VdbeTrace },
  412. #endif
  413. #if !defined(SQLITE_OMIT_WAL)
  414. { /* zName: */ "wal_autocheckpoint",
  415. /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
  416. /* ePragFlag: */ 0,
  417. /* iArg: */ 0 },
  418. { /* zName: */ "wal_checkpoint",
  419. /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
  420. /* ePragFlag: */ PragFlag_NeedSchema,
  421. /* iArg: */ 0 },
  422. #endif
  423. { /* zName: */ "writable_schema",
  424. /* ePragTyp: */ PragTyp_FLAG,
  425. /* ePragFlag: */ 0,
  426. /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
  427. };
  428. /* Number of pragmas: 56 on by default, 68 total. */
  429. /* End of the automatically generated pragma table.
  430. ***************************************************************************/
  431. /*
  432. ** Interpret the given string as a safety level. Return 0 for OFF,
  433. ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
  434. ** unrecognized string argument. The FULL option is disallowed
  435. ** if the omitFull parameter it 1.
  436. **
  437. ** Note that the values returned are one less that the values that
  438. ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
  439. ** to support legacy SQL code. The safety level used to be boolean
  440. ** and older scripts may have used numbers 0 for OFF and 1 for ON.
  441. */
  442. static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
  443. /* 123456789 123456789 */
  444. static const char zText[] = "onoffalseyestruefull";
  445. static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
  446. static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
  447. static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
  448. int i, n;
  449. if( sqlite3Isdigit(*z) ){
  450. return (u8)sqlite3Atoi(z);
  451. }
  452. n = sqlite3Strlen30(z);
  453. for(i=0; i<ArraySize(iLength)-omitFull; i++){
  454. if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
  455. return iValue[i];
  456. }
  457. }
  458. return dflt;
  459. }
  460. /*
  461. ** Interpret the given string as a boolean value.
  462. */
  463. u8 sqlite3GetBoolean(const char *z, int dflt){
  464. return getSafetyLevel(z,1,dflt)!=0;
  465. }
  466. /* The sqlite3GetBoolean() function is used by other modules but the
  467. ** remainder of this file is specific to PRAGMA processing. So omit
  468. ** the rest of the file if PRAGMAs are omitted from the build.
  469. */
  470. #if !defined(SQLITE_OMIT_PRAGMA)
  471. /*
  472. ** Interpret the given string as a locking mode value.
  473. */
  474. static int getLockingMode(const char *z){
  475. if( z ){
  476. if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
  477. if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
  478. }
  479. return PAGER_LOCKINGMODE_QUERY;
  480. }
  481. #ifndef SQLITE_OMIT_AUTOVACUUM
  482. /*
  483. ** Interpret the given string as an auto-vacuum mode value.
  484. **
  485. ** The following strings, "none", "full" and "incremental" are
  486. ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
  487. */
  488. static int getAutoVacuum(const char *z){
  489. int i;
  490. if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
  491. if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
  492. if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
  493. i = sqlite3Atoi(z);
  494. return (u8)((i>=0&&i<=2)?i:0);
  495. }
  496. #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
  497. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  498. /*
  499. ** Interpret the given string as a temp db location. Return 1 for file
  500. ** backed temporary databases, 2 for the Red-Black tree in memory database
  501. ** and 0 to use the compile-time default.
  502. */
  503. static int getTempStore(const char *z){
  504. if( z[0]>='0' && z[0]<='2' ){
  505. return z[0] - '0';
  506. }else if( sqlite3StrICmp(z, "file")==0 ){
  507. return 1;
  508. }else if( sqlite3StrICmp(z, "memory")==0 ){
  509. return 2;
  510. }else{
  511. return 0;
  512. }
  513. }
  514. #endif /* SQLITE_PAGER_PRAGMAS */
  515. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  516. /*
  517. ** Invalidate temp storage, either when the temp storage is changed
  518. ** from default, or when 'file' and the temp_store_directory has changed
  519. */
  520. static int invalidateTempStorage(Parse *pParse){
  521. sqlite3 *db = pParse->db;
  522. if( db->aDb[1].pBt!=0 ){
  523. if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
  524. sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
  525. "from within a transaction");
  526. return SQLITE_ERROR;
  527. }
  528. sqlite3BtreeClose(db->aDb[1].pBt);
  529. db->aDb[1].pBt = 0;
  530. sqlite3ResetAllSchemasOfConnection(db);
  531. }
  532. return SQLITE_OK;
  533. }
  534. #endif /* SQLITE_PAGER_PRAGMAS */
  535. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  536. /*
  537. ** If the TEMP database is open, close it and mark the database schema
  538. ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
  539. ** or DEFAULT_TEMP_STORE pragmas.
  540. */
  541. static int changeTempStorage(Parse *pParse, const char *zStorageType){
  542. int ts = getTempStore(zStorageType);
  543. sqlite3 *db = pParse->db;
  544. if( db->temp_store==ts ) return SQLITE_OK;
  545. if( invalidateTempStorage( pParse ) != SQLITE_OK ){
  546. return SQLITE_ERROR;
  547. }
  548. db->temp_store = (u8)ts;
  549. return SQLITE_OK;
  550. }
  551. #endif /* SQLITE_PAGER_PRAGMAS */
  552. /*
  553. ** Generate code to return a single integer value.
  554. */
  555. static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
  556. Vdbe *v = sqlite3GetVdbe(pParse);
  557. int mem = ++pParse->nMem;
  558. i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
  559. if( pI64 ){
  560. memcpy(pI64, &value, sizeof(value));
  561. }
  562. sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
  563. sqlite3VdbeSetNumCols(v, 1);
  564. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
  565. sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
  566. }
  567. /*
  568. ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
  569. ** set these values for all pagers.
  570. */
  571. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  572. static void setAllPagerFlags(sqlite3 *db){
  573. if( db->autoCommit ){
  574. Db *pDb = db->aDb;
  575. int n = db->nDb;
  576. assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
  577. assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
  578. assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
  579. assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
  580. == PAGER_FLAGS_MASK );
  581. assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
  582. while( (n--) > 0 ){
  583. if( pDb->pBt ){
  584. sqlite3BtreeSetPagerFlags(pDb->pBt,
  585. pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
  586. }
  587. pDb++;
  588. }
  589. }
  590. }
  591. #else
  592. # define setAllPagerFlags(X) /* no-op */
  593. #endif
  594. /*
  595. ** Return a human-readable name for a constraint resolution action.
  596. */
  597. #ifndef SQLITE_OMIT_FOREIGN_KEY
  598. static const char *actionName(u8 action){
  599. const char *zName;
  600. switch( action ){
  601. case OE_SetNull: zName = "SET NULL"; break;
  602. case OE_SetDflt: zName = "SET DEFAULT"; break;
  603. case OE_Cascade: zName = "CASCADE"; break;
  604. case OE_Restrict: zName = "RESTRICT"; break;
  605. default: zName = "NO ACTION";
  606. assert( action==OE_None ); break;
  607. }
  608. return zName;
  609. }
  610. #endif
  611. /*
  612. ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
  613. ** defined in pager.h. This function returns the associated lowercase
  614. ** journal-mode name.
  615. */
  616. const char *sqlite3JournalModename(int eMode){
  617. static char * const azModeName[] = {
  618. "delete", "persist", "off", "truncate", "memory"
  619. #ifndef SQLITE_OMIT_WAL
  620. , "wal"
  621. #endif
  622. };
  623. assert( PAGER_JOURNALMODE_DELETE==0 );
  624. assert( PAGER_JOURNALMODE_PERSIST==1 );
  625. assert( PAGER_JOURNALMODE_OFF==2 );
  626. assert( PAGER_JOURNALMODE_TRUNCATE==3 );
  627. assert( PAGER_JOURNALMODE_MEMORY==4 );
  628. assert( PAGER_JOURNALMODE_WAL==5 );
  629. assert( eMode>=0 && eMode<=ArraySize(azModeName) );
  630. if( eMode==ArraySize(azModeName) ) return 0;
  631. return azModeName[eMode];
  632. }
  633. /*
  634. ** Process a pragma statement.
  635. **
  636. ** Pragmas are of this form:
  637. **
  638. ** PRAGMA [database.]id [= value]
  639. **
  640. ** The identifier might also be a string. The value is a string, and
  641. ** identifier, or a number. If minusFlag is true, then the value is
  642. ** a number that was preceded by a minus sign.
  643. **
  644. ** If the left side is "database.id" then pId1 is the database name
  645. ** and pId2 is the id. If the left side is just "id" then pId1 is the
  646. ** id and pId2 is any empty string.
  647. */
  648. void sqlite3Pragma(
  649. Parse *pParse,
  650. Token *pId1, /* First part of [database.]id field */
  651. Token *pId2, /* Second part of [database.]id field, or NULL */
  652. Token *pValue, /* Token for <value>, or NULL */
  653. int minusFlag /* True if a '-' sign preceded <value> */
  654. ){
  655. char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
  656. char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
  657. const char *zDb = 0; /* The database name */
  658. Token *pId; /* Pointer to <id> token */
  659. char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
  660. int iDb; /* Database index for <database> */
  661. int lwr, upr, mid; /* Binary search bounds */
  662. int rc; /* return value form SQLITE_FCNTL_PRAGMA */
  663. sqlite3 *db = pParse->db; /* The database connection */
  664. Db *pDb; /* The specific database being pragmaed */
  665. Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
  666. if( v==0 ) return;
  667. sqlite3VdbeRunOnlyOnce(v);
  668. pParse->nMem = 2;
  669. /* Interpret the [database.] part of the pragma statement. iDb is the
  670. ** index of the database this pragma is being applied to in db.aDb[]. */
  671. iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
  672. if( iDb<0 ) return;
  673. pDb = &db->aDb[iDb];
  674. /* If the temp database has been explicitly named as part of the
  675. ** pragma, make sure it is open.
  676. */
  677. if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
  678. return;
  679. }
  680. zLeft = sqlite3NameFromToken(db, pId);
  681. if( !zLeft ) return;
  682. if( minusFlag ){
  683. zRight = sqlite3MPrintf(db, "-%T", pValue);
  684. }else{
  685. zRight = sqlite3NameFromToken(db, pValue);
  686. }
  687. assert( pId2 );
  688. zDb = pId2->n>0 ? pDb->zName : 0;
  689. if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
  690. goto pragma_out;
  691. }
  692. /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
  693. ** connection. If it returns SQLITE_OK, then assume that the VFS
  694. ** handled the pragma and generate a no-op prepared statement.
  695. */
  696. aFcntl[0] = 0;
  697. aFcntl[1] = zLeft;
  698. aFcntl[2] = zRight;
  699. aFcntl[3] = 0;
  700. db->busyHandler.nBusy = 0;
  701. rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
  702. if( rc==SQLITE_OK ){
  703. if( aFcntl[0] ){
  704. int mem = ++pParse->nMem;
  705. sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
  706. sqlite3VdbeSetNumCols(v, 1);
  707. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
  708. sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
  709. sqlite3_free(aFcntl[0]);
  710. }
  711. goto pragma_out;
  712. }
  713. if( rc!=SQLITE_NOTFOUND ){
  714. if( aFcntl[0] ){
  715. sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
  716. sqlite3_free(aFcntl[0]);
  717. }
  718. pParse->nErr++;
  719. pParse->rc = rc;
  720. goto pragma_out;
  721. }
  722. /* Locate the pragma in the lookup table */
  723. lwr = 0;
  724. upr = ArraySize(aPragmaNames)-1;
  725. while( lwr<=upr ){
  726. mid = (lwr+upr)/2;
  727. rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
  728. if( rc==0 ) break;
  729. if( rc<0 ){
  730. upr = mid - 1;
  731. }else{
  732. lwr = mid + 1;
  733. }
  734. }
  735. if( lwr>upr ) goto pragma_out;
  736. /* Make sure the database schema is loaded if the pragma requires that */
  737. if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
  738. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  739. }
  740. /* Jump to the appropriate pragma handler */
  741. switch( aPragmaNames[mid].ePragTyp ){
  742. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
  743. /*
  744. ** PRAGMA [database.]default_cache_size
  745. ** PRAGMA [database.]default_cache_size=N
  746. **
  747. ** The first form reports the current persistent setting for the
  748. ** page cache size. The value returned is the maximum number of
  749. ** pages in the page cache. The second form sets both the current
  750. ** page cache size value and the persistent page cache size value
  751. ** stored in the database file.
  752. **
  753. ** Older versions of SQLite would set the default cache size to a
  754. ** negative number to indicate synchronous=OFF. These days, synchronous
  755. ** is always on by default regardless of the sign of the default cache
  756. ** size. But continue to take the absolute value of the default cache
  757. ** size of historical compatibility.
  758. */
  759. case PragTyp_DEFAULT_CACHE_SIZE: {
  760. static const VdbeOpList getCacheSize[] = {
  761. { OP_Transaction, 0, 0, 0}, /* 0 */
  762. { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
  763. { OP_IfPos, 1, 8, 0},
  764. { OP_Integer, 0, 2, 0},
  765. { OP_Subtract, 1, 2, 1},
  766. { OP_IfPos, 1, 8, 0},
  767. { OP_Integer, 0, 1, 0}, /* 6 */
  768. { OP_Noop, 0, 0, 0},
  769. { OP_ResultRow, 1, 1, 0},
  770. };
  771. int addr;
  772. sqlite3VdbeUsesBtree(v, iDb);
  773. if( !zRight ){
  774. sqlite3VdbeSetNumCols(v, 1);
  775. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
  776. pParse->nMem += 2;
  777. addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
  778. sqlite3VdbeChangeP1(v, addr, iDb);
  779. sqlite3VdbeChangeP1(v, addr+1, iDb);
  780. sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
  781. }else{
  782. int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
  783. sqlite3BeginWriteOperation(pParse, 0, iDb);
  784. sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
  785. sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
  786. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  787. pDb->pSchema->cache_size = size;
  788. sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
  789. }
  790. break;
  791. }
  792. #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
  793. #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  794. /*
  795. ** PRAGMA [database.]page_size
  796. ** PRAGMA [database.]page_size=N
  797. **
  798. ** The first form reports the current setting for the
  799. ** database page size in bytes. The second form sets the
  800. ** database page size value. The value can only be set if
  801. ** the database has not yet been created.
  802. */
  803. case PragTyp_PAGE_SIZE: {
  804. Btree *pBt = pDb->pBt;
  805. assert( pBt!=0 );
  806. if( !zRight ){
  807. int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
  808. returnSingleInt(pParse, "page_size", size);
  809. }else{
  810. /* Malloc may fail when setting the page-size, as there is an internal
  811. ** buffer that the pager module resizes using sqlite3_realloc().
  812. */
  813. db->nextPagesize = sqlite3Atoi(zRight);
  814. if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
  815. db->mallocFailed = 1;
  816. }
  817. }
  818. break;
  819. }
  820. /*
  821. ** PRAGMA [database.]secure_delete
  822. ** PRAGMA [database.]secure_delete=ON/OFF
  823. **
  824. ** The first form reports the current setting for the
  825. ** secure_delete flag. The second form changes the secure_delete
  826. ** flag setting and reports thenew value.
  827. */
  828. case PragTyp_SECURE_DELETE: {
  829. Btree *pBt = pDb->pBt;
  830. int b = -1;
  831. assert( pBt!=0 );
  832. if( zRight ){
  833. b = sqlite3GetBoolean(zRight, 0);
  834. }
  835. if( pId2->n==0 && b>=0 ){
  836. int ii;
  837. for(ii=0; ii<db->nDb; ii++){
  838. sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
  839. }
  840. }
  841. b = sqlite3BtreeSecureDelete(pBt, b);
  842. returnSingleInt(pParse, "secure_delete", b);
  843. break;
  844. }
  845. /*
  846. ** PRAGMA [database.]max_page_count
  847. ** PRAGMA [database.]max_page_count=N
  848. **
  849. ** The first form reports the current setting for the
  850. ** maximum number of pages in the database file. The
  851. ** second form attempts to change this setting. Both
  852. ** forms return the current setting.
  853. **
  854. ** The absolute value of N is used. This is undocumented and might
  855. ** change. The only purpose is to provide an easy way to test
  856. ** the sqlite3AbsInt32() function.
  857. **
  858. ** PRAGMA [database.]page_count
  859. **
  860. ** Return the number of pages in the specified database.
  861. */
  862. case PragTyp_PAGE_COUNT: {
  863. int iReg;
  864. sqlite3CodeVerifySchema(pParse, iDb);
  865. iReg = ++pParse->nMem;
  866. if( sqlite3Tolower(zLeft[0])=='p' ){
  867. sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
  868. }else{
  869. sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
  870. sqlite3AbsInt32(sqlite3Atoi(zRight)));
  871. }
  872. sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
  873. sqlite3VdbeSetNumCols(v, 1);
  874. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
  875. break;
  876. }
  877. /*
  878. ** PRAGMA [database.]locking_mode
  879. ** PRAGMA [database.]locking_mode = (normal|exclusive)
  880. */
  881. case PragTyp_LOCKING_MODE: {
  882. const char *zRet = "normal";
  883. int eMode = getLockingMode(zRight);
  884. if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
  885. /* Simple "PRAGMA locking_mode;" statement. This is a query for
  886. ** the current default locking mode (which may be different to
  887. ** the locking-mode of the main database).
  888. */
  889. eMode = db->dfltLockMode;
  890. }else{
  891. Pager *pPager;
  892. if( pId2->n==0 ){
  893. /* This indicates that no database name was specified as part
  894. ** of the PRAGMA command. In this case the locking-mode must be
  895. ** set on all attached databases, as well as the main db file.
  896. **
  897. ** Also, the sqlite3.dfltLockMode variable is set so that
  898. ** any subsequently attached databases also use the specified
  899. ** locking mode.
  900. */
  901. int ii;
  902. assert(pDb==&db->aDb[0]);
  903. for(ii=2; ii<db->nDb; ii++){
  904. pPager = sqlite3BtreePager(db->aDb[ii].pBt);
  905. sqlite3PagerLockingMode(pPager, eMode);
  906. }
  907. db->dfltLockMode = (u8)eMode;
  908. }
  909. pPager = sqlite3BtreePager(pDb->pBt);
  910. eMode = sqlite3PagerLockingMode(pPager, eMode);
  911. }
  912. assert( eMode==PAGER_LOCKINGMODE_NORMAL
  913. || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
  914. if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
  915. zRet = "exclusive";
  916. }
  917. sqlite3VdbeSetNumCols(v, 1);
  918. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
  919. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
  920. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  921. break;
  922. }
  923. /*
  924. ** PRAGMA [database.]journal_mode
  925. ** PRAGMA [database.]journal_mode =
  926. ** (delete|persist|off|truncate|memory|wal|off)
  927. */
  928. case PragTyp_JOURNAL_MODE: {
  929. int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
  930. int ii; /* Loop counter */
  931. sqlite3VdbeSetNumCols(v, 1);
  932. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
  933. if( zRight==0 ){
  934. /* If there is no "=MODE" part of the pragma, do a query for the
  935. ** current mode */
  936. eMode = PAGER_JOURNALMODE_QUERY;
  937. }else{
  938. const char *zMode;
  939. int n = sqlite3Strlen30(zRight);
  940. for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
  941. if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
  942. }
  943. if( !zMode ){
  944. /* If the "=MODE" part does not match any known journal mode,
  945. ** then do a query */
  946. eMode = PAGER_JOURNALMODE_QUERY;
  947. }
  948. }
  949. if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
  950. /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
  951. iDb = 0;
  952. pId2->n = 1;
  953. }
  954. for(ii=db->nDb-1; ii>=0; ii--){
  955. if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
  956. sqlite3VdbeUsesBtree(v, ii);
  957. sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
  958. }
  959. }
  960. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  961. break;
  962. }
  963. /*
  964. ** PRAGMA [database.]journal_size_limit
  965. ** PRAGMA [database.]journal_size_limit=N
  966. **
  967. ** Get or set the size limit on rollback journal files.
  968. */
  969. case PragTyp_JOURNAL_SIZE_LIMIT: {
  970. Pager *pPager = sqlite3BtreePager(pDb->pBt);
  971. i64 iLimit = -2;
  972. if( zRight ){
  973. sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
  974. if( iLimit<-1 ) iLimit = -1;
  975. }
  976. iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
  977. returnSingleInt(pParse, "journal_size_limit", iLimit);
  978. break;
  979. }
  980. #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
  981. /*
  982. ** PRAGMA [database.]auto_vacuum
  983. ** PRAGMA [database.]auto_vacuum=N
  984. **
  985. ** Get or set the value of the database 'auto-vacuum' parameter.
  986. ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
  987. */
  988. #ifndef SQLITE_OMIT_AUTOVACUUM
  989. case PragTyp_AUTO_VACUUM: {
  990. Btree *pBt = pDb->pBt;
  991. assert( pBt!=0 );
  992. if( !zRight ){
  993. returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
  994. }else{
  995. int eAuto = getAutoVacuum(zRight);
  996. assert( eAuto>=0 && eAuto<=2 );
  997. db->nextAutovac = (u8)eAuto;
  998. /* Call SetAutoVacuum() to set initialize the internal auto and
  999. ** incr-vacuum flags. This is required in case this connection
  1000. ** creates the database file. It is important that it is created
  1001. ** as an auto-vacuum capable db.
  1002. */
  1003. rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
  1004. if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
  1005. /* When setting the auto_vacuum mode to either "full" or
  1006. ** "incremental", write the value of meta[6] in the database
  1007. ** file. Before writing to meta[6], check that meta[3] indicates
  1008. ** that this really is an auto-vacuum capable database.
  1009. */
  1010. static const VdbeOpList setMeta6[] = {
  1011. { OP_Transaction, 0, 1, 0}, /* 0 */
  1012. { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
  1013. { OP_If, 1, 0, 0}, /* 2 */
  1014. { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
  1015. { OP_Integer, 0, 1, 0}, /* 4 */
  1016. { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
  1017. };
  1018. int iAddr;
  1019. iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
  1020. sqlite3VdbeChangeP1(v, iAddr, iDb);
  1021. sqlite3VdbeChangeP1(v, iAddr+1, iDb);
  1022. sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
  1023. sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
  1024. sqlite3VdbeChangeP1(v, iAddr+5, iDb);
  1025. sqlite3VdbeUsesBtree(v, iDb);
  1026. }
  1027. }
  1028. break;
  1029. }
  1030. #endif
  1031. /*
  1032. ** PRAGMA [database.]incremental_vacuum(N)
  1033. **
  1034. ** Do N steps of incremental vacuuming on a database.
  1035. */
  1036. #ifndef SQLITE_OMIT_AUTOVACUUM
  1037. case PragTyp_INCREMENTAL_VACUUM: {
  1038. int iLimit, addr;
  1039. if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
  1040. iLimit = 0x7fffffff;
  1041. }
  1042. sqlite3BeginWriteOperation(pParse, 0, iDb);
  1043. sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
  1044. addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
  1045. sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
  1046. sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
  1047. sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
  1048. sqlite3VdbeJumpHere(v, addr);
  1049. break;
  1050. }
  1051. #endif
  1052. #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1053. /*
  1054. ** PRAGMA [database.]cache_size
  1055. ** PRAGMA [database.]cache_size=N
  1056. **
  1057. ** The first form reports the current local setting for the
  1058. ** page cache size. The second form sets the local
  1059. ** page cache size value. If N is positive then that is the
  1060. ** number of pages in the cache. If N is negative, then the
  1061. ** number of pages is adjusted so that the cache uses -N kibibytes
  1062. ** of memory.
  1063. */
  1064. case PragTyp_CACHE_SIZE: {
  1065. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1066. if( !zRight ){
  1067. returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
  1068. }else{
  1069. int size = sqlite3Atoi(zRight);
  1070. pDb->pSchema->cache_size = size;
  1071. sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
  1072. }
  1073. break;
  1074. }
  1075. /*
  1076. ** PRAGMA [database.]mmap_size(N)
  1077. **
  1078. ** Used to set mapping size limit. The mapping size limit is
  1079. ** used to limit the aggregate size of all memory mapped regions of the
  1080. ** database file. If this parameter is set to zero, then memory mapping
  1081. ** is not used at all. If N is negative, then the default memory map
  1082. ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
  1083. ** The parameter N is measured in bytes.
  1084. **
  1085. ** This value is advisory. The underlying VFS is free to memory map
  1086. ** as little or as much as it wants. Except, if N is set to 0 then the
  1087. ** upper layers will never invoke the xFetch interfaces to the VFS.
  1088. */
  1089. case PragTyp_MMAP_SIZE: {
  1090. sqlite3_int64 sz;
  1091. #if SQLITE_MAX_MMAP_SIZE>0
  1092. assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1093. if( zRight ){
  1094. int ii;
  1095. sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
  1096. if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
  1097. if( pId2->n==0 ) db->szMmap = sz;
  1098. for(ii=db->nDb-1; ii>=0; ii--){
  1099. if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
  1100. sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
  1101. }
  1102. }
  1103. }
  1104. sz = -1;
  1105. rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
  1106. #else
  1107. sz = 0;
  1108. rc = SQLITE_OK;
  1109. #endif
  1110. if( rc==SQLITE_OK ){
  1111. returnSingleInt(pParse, "mmap_size", sz);
  1112. }else if( rc!=SQLITE_NOTFOUND ){
  1113. pParse->nErr++;
  1114. pParse->rc = rc;
  1115. }
  1116. break;
  1117. }
  1118. /*
  1119. ** PRAGMA temp_store
  1120. ** PRAGMA temp_store = "default"|"memory"|"file"
  1121. **
  1122. ** Return or set the local value of the temp_store flag. Changing
  1123. ** the local value does not make changes to the disk file and the default
  1124. ** value will be restored the next time the database is opened.
  1125. **
  1126. ** Note that it is possible for the library compile-time options to
  1127. ** override this setting
  1128. */
  1129. case PragTyp_TEMP_STORE: {
  1130. if( !zRight ){
  1131. returnSingleInt(pParse, "temp_store", db->temp_store);
  1132. }else{
  1133. changeTempStorage(pParse, zRight);
  1134. }
  1135. break;
  1136. }
  1137. /*
  1138. ** PRAGMA temp_store_directory
  1139. ** PRAGMA temp_store_directory = ""|"directory_name"
  1140. **
  1141. ** Return or set the local value of the temp_store_directory flag. Changing
  1142. ** the value sets a specific directory to be used for temporary files.
  1143. ** Setting to a null string reverts to the default temporary directory search.
  1144. ** If temporary directory is changed, then invalidateTempStorage.
  1145. **
  1146. */
  1147. case PragTyp_TEMP_STORE_DIRECTORY: {
  1148. if( !zRight ){
  1149. if( sqlite3_temp_directory ){
  1150. sqlite3VdbeSetNumCols(v, 1);
  1151. sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
  1152. "temp_store_directory", SQLITE_STATIC);
  1153. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
  1154. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1155. }
  1156. }else{
  1157. #ifndef SQLITE_OMIT_WSD
  1158. if( zRight[0] ){
  1159. int res;
  1160. rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
  1161. if( rc!=SQLITE_OK || res==0 ){
  1162. sqlite3ErrorMsg(pParse, "not a writable directory");
  1163. goto pragma_out;
  1164. }
  1165. }
  1166. if( SQLITE_TEMP_STORE==0
  1167. || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
  1168. || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
  1169. ){
  1170. invalidateTempStorage(pParse);
  1171. }
  1172. sqlite3_free(sqlite3_temp_directory);
  1173. if( zRight[0] ){
  1174. sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
  1175. }else{
  1176. sqlite3_temp_directory = 0;
  1177. }
  1178. #endif /* SQLITE_OMIT_WSD */
  1179. }
  1180. break;
  1181. }
  1182. #if SQLITE_OS_WIN
  1183. /*
  1184. ** PRAGMA data_store_directory
  1185. ** PRAGMA data_store_directory = ""|"directory_name"
  1186. **
  1187. ** Return or set the local value of the data_store_directory flag. Changing
  1188. ** the value sets a specific directory to be used for database files that
  1189. ** were specified with a relative pathname. Setting to a null string reverts
  1190. ** to the default database directory, which for database files specified with
  1191. ** a relative path will probably be based on the current directory for the
  1192. ** process. Database file specified with an absolute path are not impacted
  1193. ** by this setting, regardless of its value.
  1194. **
  1195. */
  1196. case PragTyp_DATA_STORE_DIRECTORY: {
  1197. if( !zRight ){
  1198. if( sqlite3_data_directory ){
  1199. sqlite3VdbeSetNumCols(v, 1);
  1200. sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
  1201. "data_store_directory", SQLITE_STATIC);
  1202. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
  1203. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1204. }
  1205. }else{
  1206. #ifndef SQLITE_OMIT_WSD
  1207. if( zRight[0] ){
  1208. int res;
  1209. rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
  1210. if( rc!=SQLITE_OK || res==0 ){
  1211. sqlite3ErrorMsg(pParse, "not a writable directory");
  1212. goto pragma_out;
  1213. }
  1214. }
  1215. sqlite3_free(sqlite3_data_directory);
  1216. if( zRight[0] ){
  1217. sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
  1218. }else{
  1219. sqlite3_data_directory = 0;
  1220. }
  1221. #endif /* SQLITE_OMIT_WSD */
  1222. }
  1223. break;
  1224. }
  1225. #endif
  1226. #if SQLITE_ENABLE_LOCKING_STYLE
  1227. /*
  1228. ** PRAGMA [database.]lock_proxy_file
  1229. ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
  1230. **
  1231. ** Return or set the value of the lock_proxy_file flag. Changing
  1232. ** the value sets a specific file to be used for database access locks.
  1233. **
  1234. */
  1235. case PragTyp_LOCK_PROXY_FILE: {
  1236. if( !zRight ){
  1237. Pager *pPager = sqlite3BtreePager(pDb->pBt);
  1238. char *proxy_file_path = NULL;
  1239. sqlite3_file *pFile = sqlite3PagerFile(pPager);
  1240. sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
  1241. &proxy_file_path);
  1242. if( proxy_file_path ){
  1243. sqlite3VdbeSetNumCols(v, 1);
  1244. sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
  1245. "lock_proxy_file", SQLITE_STATIC);
  1246. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
  1247. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1248. }
  1249. }else{
  1250. Pager *pPager = sqlite3BtreePager(pDb->pBt);
  1251. sqlite3_file *pFile = sqlite3PagerFile(pPager);
  1252. int res;
  1253. if( zRight[0] ){
  1254. res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
  1255. zRight);
  1256. } else {
  1257. res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
  1258. NULL);
  1259. }
  1260. if( res!=SQLITE_OK ){
  1261. sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
  1262. goto pragma_out;
  1263. }
  1264. }
  1265. break;
  1266. }
  1267. #endif /* SQLITE_ENABLE_LOCKING_STYLE */
  1268. /*
  1269. ** PRAGMA [database.]synchronous
  1270. ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
  1271. **
  1272. ** Return or set the local value of the synchronous flag. Changing
  1273. ** the local value does not make changes to the disk file and the
  1274. ** default value will be restored the next time the database is
  1275. ** opened.
  1276. */
  1277. case PragTyp_SYNCHRONOUS: {
  1278. if( !zRight ){
  1279. returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
  1280. }else{
  1281. if( !db->autoCommit ){
  1282. sqlite3ErrorMsg(pParse,
  1283. "Safety level may not be changed inside a transaction");
  1284. }else{
  1285. pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
  1286. setAllPagerFlags(db);
  1287. }
  1288. }
  1289. break;
  1290. }
  1291. #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
  1292. #ifndef SQLITE_OMIT_FLAG_PRAGMAS
  1293. case PragTyp_FLAG: {
  1294. if( zRight==0 ){
  1295. returnSingleInt(pParse, aPragmaNames[mid].zName,
  1296. (db->flags & aPragmaNames[mid].iArg)!=0 );
  1297. }else{
  1298. int mask = aPragmaNames[mid].iArg; /* Mask of bits to set or clear. */
  1299. if( db->autoCommit==0 ){
  1300. /* Foreign key support may not be enabled or disabled while not
  1301. ** in auto-commit mode. */
  1302. mask &= ~(SQLITE_ForeignKeys);
  1303. }
  1304. if( sqlite3GetBoolean(zRight, 0) ){
  1305. db->flags |= mask;
  1306. }else{
  1307. db->flags &= ~mask;
  1308. if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
  1309. }
  1310. /* Many of the flag-pragmas modify the code generated by the SQL
  1311. ** compiler (eg. count_changes). So add an opcode to expire all
  1312. ** compiled SQL statements after modifying a pragma value.
  1313. */
  1314. sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
  1315. setAllPagerFlags(db);
  1316. }
  1317. break;
  1318. }
  1319. #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
  1320. #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
  1321. /*
  1322. ** PRAGMA table_info(<table>)
  1323. **
  1324. ** Return a single row for each column of the named table. The columns of
  1325. ** the returned data set are:
  1326. **
  1327. ** cid: Column id (numbered from left to right, starting at 0)
  1328. ** name: Column name
  1329. ** type: Column declaration type.
  1330. ** notnull: True if 'NOT NULL' is part of column declaration
  1331. ** dflt_value: The default value for the column, if any.
  1332. */
  1333. case PragTyp_TABLE_INFO: if( zRight ){
  1334. Table *pTab;
  1335. pTab = sqlite3FindTable(db, zRight, zDb);
  1336. if( pTab ){
  1337. int i, k;
  1338. int nHidden = 0;
  1339. Column *pCol;
  1340. Index *pPk;
  1341. for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){}
  1342. sqlite3VdbeSetNumCols(v, 6);
  1343. pParse->nMem = 6;
  1344. sqlite3CodeVerifySchema(pParse, iDb);
  1345. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
  1346. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1347. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
  1348. sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
  1349. sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
  1350. sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
  1351. sqlite3ViewGetColumnNames(pParse, pTab);
  1352. for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
  1353. if( IsHiddenColumn(pCol) ){
  1354. nHidden++;
  1355. continue;
  1356. }
  1357. sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
  1358. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
  1359. sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1360. pCol->zType ? pCol->zType : "", 0);
  1361. sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
  1362. if( pCol->zDflt ){
  1363. sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
  1364. }else{
  1365. sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
  1366. }
  1367. if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
  1368. k = 0;
  1369. }else if( pPk==0 ){
  1370. k = 1;
  1371. }else{
  1372. for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
  1373. }
  1374. sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
  1375. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
  1376. }
  1377. }
  1378. }
  1379. break;
  1380. case PragTyp_STATS: {
  1381. Index *pIdx;
  1382. HashElem *i;
  1383. v = sqlite3GetVdbe(pParse);
  1384. sqlite3VdbeSetNumCols(v, 4);
  1385. pParse->nMem = 4;
  1386. sqlite3CodeVerifySchema(pParse, iDb);
  1387. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
  1388. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
  1389. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
  1390. sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
  1391. for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
  1392. Table *pTab = sqliteHashData(i);
  1393. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
  1394. sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
  1395. sqlite3VdbeAddOp2(v, OP_Integer,
  1396. (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
  1397. sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
  1398. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
  1399. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1400. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
  1401. sqlite3VdbeAddOp2(v, OP_Integer,
  1402. (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
  1403. sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
  1404. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
  1405. }
  1406. }
  1407. }
  1408. break;
  1409. case PragTyp_INDEX_INFO: if( zRight ){
  1410. Index *pIdx;
  1411. Table *pTab;
  1412. pIdx = sqlite3FindIndex(db, zRight, zDb);
  1413. if( pIdx ){
  1414. int i;
  1415. pTab = pIdx->pTable;
  1416. sqlite3VdbeSetNumCols(v, 3);
  1417. pParse->nMem = 3;
  1418. sqlite3CodeVerifySchema(pParse, iDb);
  1419. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
  1420. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
  1421. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
  1422. for(i=0; i<pIdx->nColumn; i++){
  1423. int cnum = pIdx->aiColumn[i];
  1424. sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1425. sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
  1426. assert( pTab->nCol>cnum );
  1427. sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
  1428. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  1429. }
  1430. }
  1431. }
  1432. break;
  1433. case PragTyp_INDEX_LIST: if( zRight ){
  1434. Index *pIdx;
  1435. Table *pTab;
  1436. int i;
  1437. pTab = sqlite3FindTable(db, zRight, zDb);
  1438. if( pTab ){
  1439. v = sqlite3GetVdbe(pParse);
  1440. sqlite3VdbeSetNumCols(v, 3);
  1441. pParse->nMem = 3;
  1442. sqlite3CodeVerifySchema(pParse, iDb);
  1443. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
  1444. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1445. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
  1446. for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
  1447. sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1448. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
  1449. sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
  1450. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  1451. }
  1452. }
  1453. }
  1454. break;
  1455. case PragTyp_DATABASE_LIST: {
  1456. int i;
  1457. sqlite3VdbeSetNumCols(v, 3);
  1458. pParse->nMem = 3;
  1459. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
  1460. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1461. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
  1462. for(i=0; i<db->nDb; i++){
  1463. if( db->aDb[i].pBt==0 ) continue;
  1464. assert( db->aDb[i].zName!=0 );
  1465. sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1466. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
  1467. sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1468. sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
  1469. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  1470. }
  1471. }
  1472. break;
  1473. case PragTyp_COLLATION_LIST: {
  1474. int i = 0;
  1475. HashElem *p;
  1476. sqlite3VdbeSetNumCols(v, 2);
  1477. pParse->nMem = 2;
  1478. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
  1479. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1480. for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
  1481. CollSeq *pColl = (CollSeq *)sqliteHashData(p);
  1482. sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
  1483. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
  1484. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
  1485. }
  1486. }
  1487. break;
  1488. #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
  1489. #ifndef SQLITE_OMIT_FOREIGN_KEY
  1490. case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
  1491. FKey *pFK;
  1492. Table *pTab;
  1493. pTab = sqlite3FindTable(db, zRight, zDb);
  1494. if( pTab ){
  1495. v = sqlite3GetVdbe(pParse);
  1496. pFK = pTab->pFKey;
  1497. if( pFK ){
  1498. int i = 0;
  1499. sqlite3VdbeSetNumCols(v, 8);
  1500. pParse->nMem = 8;
  1501. sqlite3CodeVerifySchema(pParse, iDb);
  1502. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
  1503. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
  1504. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
  1505. sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
  1506. sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
  1507. sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
  1508. sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
  1509. sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
  1510. while(pFK){
  1511. int j;
  1512. for(j=0; j<pFK->nCol; j++){
  1513. char *zCol = pFK->aCol[j].zCol;
  1514. char *zOnDelete = (char *)actionName(pFK->aAction[0]);
  1515. char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
  1516. sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1517. sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
  1518. sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
  1519. sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
  1520. pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
  1521. sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
  1522. sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
  1523. sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
  1524. sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
  1525. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
  1526. }
  1527. ++i;
  1528. pFK = pFK->pNextFrom;
  1529. }
  1530. }
  1531. }
  1532. }
  1533. break;
  1534. #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
  1535. #ifndef SQLITE_OMIT_FOREIGN_KEY
  1536. #ifndef SQLITE_OMIT_TRIGGER
  1537. case PragTyp_FOREIGN_KEY_CHECK: {
  1538. FKey *pFK; /* A foreign key constraint */
  1539. Table *pTab; /* Child table contain "REFERENCES" keyword */
  1540. Table *pParent; /* Parent table that child points to */
  1541. Index *pIdx; /* Index in the parent table */
  1542. int i; /* Loop counter: Foreign key number for pTab */
  1543. int j; /* Loop counter: Field of the foreign key */
  1544. HashElem *k; /* Loop counter: Next table in schema */
  1545. int x; /* result variable */
  1546. int regResult; /* 3 registers to hold a result row */
  1547. int regKey; /* Register to hold key for checking the FK */
  1548. int regRow; /* Registers to hold a row from pTab */
  1549. int addrTop; /* Top of a loop checking foreign keys */
  1550. int addrOk; /* Jump here if the key is OK */
  1551. int *aiCols; /* child to parent column mapping */
  1552. regResult = pParse->nMem+1;
  1553. pParse->nMem += 4;
  1554. regKey = ++pParse->nMem;
  1555. regRow = ++pParse->nMem;
  1556. v = sqlite3GetVdbe(pParse);
  1557. sqlite3VdbeSetNumCols(v, 4);
  1558. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
  1559. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
  1560. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
  1561. sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
  1562. sqlite3CodeVerifySchema(pParse, iDb);
  1563. k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
  1564. while( k ){
  1565. if( zRight ){
  1566. pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
  1567. k = 0;
  1568. }else{
  1569. pTab = (Table*)sqliteHashData(k);
  1570. k = sqliteHashNext(k);
  1571. }
  1572. if( pTab==0 || pTab->pFKey==0 ) continue;
  1573. sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  1574. if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
  1575. sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
  1576. sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
  1577. P4_TRANSIENT);
  1578. for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
  1579. pParent = sqlite3FindTable(db, pFK->zTo, zDb);
  1580. if( pParent==0 ) continue;
  1581. pIdx = 0;
  1582. sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
  1583. x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
  1584. if( x==0 ){
  1585. if( pIdx==0 ){
  1586. sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
  1587. }else{
  1588. KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  1589. sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
  1590. sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
  1591. }
  1592. }else{
  1593. k = 0;
  1594. break;
  1595. }
  1596. }
  1597. assert( pParse->nErr>0 || pFK==0 );
  1598. if( pFK ) break;
  1599. if( pParse->nTab<i ) pParse->nTab = i;
  1600. addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
  1601. for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
  1602. pParent = sqlite3FindTable(db, pFK->zTo, zDb);
  1603. pIdx = 0;
  1604. aiCols = 0;
  1605. if( pParent ){
  1606. x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
  1607. assert( x==0 );
  1608. }
  1609. addrOk = sqlite3VdbeMakeLabel(v);
  1610. if( pParent && pIdx==0 ){
  1611. int iKey = pFK->aCol[0].iFrom;
  1612. assert( iKey>=0 && iKey<pTab->nCol );
  1613. if( iKey!=pTab->iPKey ){
  1614. sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
  1615. sqlite3ColumnDefault(v, pTab, iKey, regRow);
  1616. sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
  1617. sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
  1618. sqlite3VdbeCurrentAddr(v)+3);
  1619. }else{
  1620. sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
  1621. }
  1622. sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
  1623. sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
  1624. sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
  1625. }else{
  1626. for(j=0; j<pFK->nCol; j++){
  1627. sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
  1628. aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
  1629. sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
  1630. }
  1631. if( pParent ){
  1632. sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
  1633. sqlite3VdbeChangeP4(v, -1,
  1634. sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
  1635. sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
  1636. }
  1637. }
  1638. sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
  1639. sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
  1640. pFK->zTo, P4_TRANSIENT);
  1641. sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
  1642. sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
  1643. sqlite3VdbeResolveLabel(v, addrOk);
  1644. sqlite3DbFree(db, aiCols);
  1645. }
  1646. sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
  1647. sqlite3VdbeJumpHere(v, addrTop);
  1648. }
  1649. }
  1650. break;
  1651. #endif /* !defined(SQLITE_OMIT_TRIGGER) */
  1652. #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
  1653. #ifndef NDEBUG
  1654. case PragTyp_PARSER_TRACE: {
  1655. if( zRight ){
  1656. if( sqlite3GetBoolean(zRight, 0) ){
  1657. sqlite3ParserTrace(stderr, "parser: ");
  1658. }else{
  1659. sqlite3ParserTrace(0, 0);
  1660. }
  1661. }
  1662. }
  1663. break;
  1664. #endif
  1665. /* Reinstall the LIKE and GLOB functions. The variant of LIKE
  1666. ** used will be case sensitive or not depending on the RHS.
  1667. */
  1668. case PragTyp_CASE_SENSITIVE_LIKE: {
  1669. if( zRight ){
  1670. sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
  1671. }
  1672. }
  1673. break;
  1674. #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
  1675. # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
  1676. #endif
  1677. #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  1678. /* Pragma "quick_check" is reduced version of
  1679. ** integrity_check designed to detect most database corruption
  1680. ** without most of the overhead of a full integrity-check.
  1681. */
  1682. case PragTyp_INTEGRITY_CHECK: {
  1683. int i, j, addr, mxErr;
  1684. /* Code that appears at the end of the integrity check. If no error
  1685. ** messages have been generated, output OK. Otherwise output the
  1686. ** error message
  1687. */
  1688. static const VdbeOpList endCode[] = {
  1689. { OP_AddImm, 1, 0, 0}, /* 0 */
  1690. { OP_IfNeg, 1, 0, 0}, /* 1 */
  1691. { OP_String8, 0, 3, 0}, /* 2 */
  1692. { OP_ResultRow, 3, 1, 0},
  1693. };
  1694. int isQuick = (sqlite3Tolower(zLeft[0])=='q');
  1695. /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
  1696. ** then iDb is set to the index of the database identified by <db>.
  1697. ** In this case, the integrity of database iDb only is verified by
  1698. ** the VDBE created below.
  1699. **
  1700. ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
  1701. ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
  1702. ** to -1 here, to indicate that the VDBE should verify the integrity
  1703. ** of all attached databases. */
  1704. assert( iDb>=0 );
  1705. assert( iDb==0 || pId2->z );
  1706. if( pId2->z==0 ) iDb = -1;
  1707. /* Initialize the VDBE program */
  1708. pParse->nMem = 6;
  1709. sqlite3VdbeSetNumCols(v, 1);
  1710. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
  1711. /* Set the maximum error count */
  1712. mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  1713. if( zRight ){
  1714. sqlite3GetInt32(zRight, &mxErr);
  1715. if( mxErr<=0 ){
  1716. mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
  1717. }
  1718. }
  1719. sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */
  1720. /* Do an integrity check on each database file */
  1721. for(i=0; i<db->nDb; i++){
  1722. HashElem *x;
  1723. Hash *pTbls;
  1724. int cnt = 0;
  1725. if( OMIT_TEMPDB && i==1 ) continue;
  1726. if( iDb>=0 && i!=iDb ) continue;
  1727. sqlite3CodeVerifySchema(pParse, i);
  1728. addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
  1729. sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1730. sqlite3VdbeJumpHere(v, addr);
  1731. /* Do an integrity check of the B-Tree
  1732. **
  1733. ** Begin by filling registers 2, 3, ... with the root pages numbers
  1734. ** for all tables and indices in the database.
  1735. */
  1736. assert( sqlite3SchemaMutexHeld(db, i, 0) );
  1737. pTbls = &db->aDb[i].pSchema->tblHash;
  1738. for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
  1739. Table *pTab = sqliteHashData(x);
  1740. Index *pIdx;
  1741. sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
  1742. cnt++;
  1743. for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1744. sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
  1745. cnt++;
  1746. }
  1747. }
  1748. /* Make sure sufficient number of registers have been allocated */
  1749. pParse->nMem = MAX( pParse->nMem, cnt+7 );
  1750. /* Do the b-tree integrity checks */
  1751. sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1752. sqlite3VdbeChangeP5(v, (u8)i);
  1753. addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
  1754. sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1755. sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
  1756. P4_DYNAMIC);
  1757. sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
  1758. sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1759. sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
  1760. sqlite3VdbeJumpHere(v, addr);
  1761. /* Make sure all the indices are constructed correctly.
  1762. */
  1763. for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
  1764. Table *pTab = sqliteHashData(x);
  1765. Index *pIdx;
  1766. int loopTop;
  1767. if( pTab->pIndex==0 ) continue;
  1768. addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */
  1769. sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1770. sqlite3VdbeJumpHere(v, addr);
  1771. sqlite3ExprCacheClear(pParse);
  1772. sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
  1773. for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1774. sqlite3VdbeAddOp2(v, OP_Integer, 0, 7+j); /* index entries counter */
  1775. }
  1776. pParse->nMem = MAX(pParse->nMem, 7+j);
  1777. loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0) + 1;
  1778. for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1779. int jmp2, jmp3;
  1780. int r1;
  1781. static const VdbeOpList idxErr[] = {
  1782. { OP_AddImm, 1, -1, 0},
  1783. { OP_String8, 0, 3, 0}, /* 1 */
  1784. { OP_Rowid, 1, 4, 0},
  1785. { OP_String8, 0, 5, 0}, /* 3 */
  1786. { OP_String8, 0, 6, 0}, /* 4 */
  1787. { OP_Concat, 4, 3, 3},
  1788. { OP_Concat, 5, 3, 3},
  1789. { OP_Concat, 6, 3, 3},
  1790. { OP_ResultRow, 3, 1, 0},
  1791. { OP_IfPos, 1, 0, 0}, /* 9 */
  1792. { OP_Halt, 0, 0, 0},
  1793. };
  1794. r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0, &jmp3);
  1795. sqlite3VdbeAddOp2(v, OP_AddImm, 7+j, 1); /* increment entry count */
  1796. jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
  1797. addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
  1798. sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
  1799. sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
  1800. sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
  1801. sqlite3VdbeJumpHere(v, addr+9);
  1802. sqlite3VdbeJumpHere(v, jmp2);
  1803. sqlite3VdbeResolveLabel(v, jmp3);
  1804. }
  1805. sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop);
  1806. sqlite3VdbeJumpHere(v, loopTop-1);
  1807. #ifndef SQLITE_OMIT_BTREECOUNT
  1808. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
  1809. "wrong # of entries in index ", P4_STATIC);
  1810. for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1811. addr = sqlite3VdbeCurrentAddr(v);
  1812. sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
  1813. sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1814. sqlite3VdbeAddOp2(v, OP_Count, j+2, 3);
  1815. sqlite3VdbeAddOp3(v, OP_Eq, 7+j, addr+8, 3);
  1816. sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
  1817. sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
  1818. sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1819. sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
  1820. }
  1821. #endif /* SQLITE_OMIT_BTREECOUNT */
  1822. }
  1823. }
  1824. addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
  1825. sqlite3VdbeChangeP2(v, addr, -mxErr);
  1826. sqlite3VdbeJumpHere(v, addr+1);
  1827. sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
  1828. }
  1829. break;
  1830. #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  1831. #ifndef SQLITE_OMIT_UTF16
  1832. /*
  1833. ** PRAGMA encoding
  1834. ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
  1835. **
  1836. ** In its first form, this pragma returns the encoding of the main
  1837. ** database. If the database is not initialized, it is initialized now.
  1838. **
  1839. ** The second form of this pragma is a no-op if the main database file
  1840. ** has not already been initialized. In this case it sets the default
  1841. ** encoding that will be used for the main database file if a new file
  1842. ** is created. If an existing main database file is opened, then the
  1843. ** default text encoding for the existing database is used.
  1844. **
  1845. ** In all cases new databases created using the ATTACH command are
  1846. ** created to use the same default text encoding as the main database. If
  1847. ** the main database has not been initialized and/or created when ATTACH
  1848. ** is executed, this is done before the ATTACH operation.
  1849. **
  1850. ** In the second form this pragma sets the text encoding to be used in
  1851. ** new database files created using this database handle. It is only
  1852. ** useful if invoked immediately after the main database i
  1853. */
  1854. case PragTyp_ENCODING: {
  1855. static const struct EncName {
  1856. char *zName;
  1857. u8 enc;
  1858. } encnames[] = {
  1859. { "UTF8", SQLITE_UTF8 },
  1860. { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
  1861. { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
  1862. { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
  1863. { "UTF16le", SQLITE_UTF16LE },
  1864. { "UTF16be", SQLITE_UTF16BE },
  1865. { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
  1866. { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
  1867. { 0, 0 }
  1868. };
  1869. const struct EncName *pEnc;
  1870. if( !zRight ){ /* "PRAGMA encoding" */
  1871. if( sqlite3ReadSchema(pParse) ) goto pragma_out;
  1872. sqlite3VdbeSetNumCols(v, 1);
  1873. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
  1874. sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
  1875. assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
  1876. assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
  1877. assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
  1878. sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
  1879. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1880. }else{ /* "PRAGMA encoding = XXX" */
  1881. /* Only change the value of sqlite.enc if the database handle is not
  1882. ** initialized. If the main database exists, the new sqlite.enc value
  1883. ** will be overwritten when the schema is next loaded. If it does not
  1884. ** already exists, it will be created to use the new encoding value.
  1885. */
  1886. if(
  1887. !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
  1888. DbHasProperty(db, 0, DB_Empty)
  1889. ){
  1890. for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
  1891. if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
  1892. ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
  1893. break;
  1894. }
  1895. }
  1896. if( !pEnc->zName ){
  1897. sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
  1898. }
  1899. }
  1900. }
  1901. }
  1902. break;
  1903. #endif /* SQLITE_OMIT_UTF16 */
  1904. #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
  1905. /*
  1906. ** PRAGMA [database.]schema_version
  1907. ** PRAGMA [database.]schema_version = <integer>
  1908. **
  1909. ** PRAGMA [database.]user_version
  1910. ** PRAGMA [database.]user_version = <integer>
  1911. **
  1912. ** PRAGMA [database.]freelist_count = <integer>
  1913. **
  1914. ** PRAGMA [database.]application_id
  1915. ** PRAGMA [database.]application_id = <integer>
  1916. **
  1917. ** The pragma's schema_version and user_version are used to set or get
  1918. ** the value of the schema-version and user-version, respectively. Both
  1919. ** the schema-version and the user-version are 32-bit signed integers
  1920. ** stored in the database header.
  1921. **
  1922. ** The schema-cookie is usually only manipulated internally by SQLite. It
  1923. ** is incremented by SQLite whenever the database schema is modified (by
  1924. ** creating or dropping a table or index). The schema version is used by
  1925. ** SQLite each time a query is executed to ensure that the internal cache
  1926. ** of the schema used when compiling the SQL query matches the schema of
  1927. ** the database against which the compiled query is actually executed.
  1928. ** Subverting this mechanism by using "PRAGMA schema_version" to modify
  1929. ** the schema-version is potentially dangerous and may lead to program
  1930. ** crashes or database corruption. Use with caution!
  1931. **
  1932. ** The user-version is not used internally by SQLite. It may be used by
  1933. ** applications for any purpose.
  1934. */
  1935. case PragTyp_HEADER_VALUE: {
  1936. int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
  1937. sqlite3VdbeUsesBtree(v, iDb);
  1938. switch( zLeft[0] ){
  1939. case 'a': case 'A':
  1940. iCookie = BTREE_APPLICATION_ID;
  1941. break;
  1942. case 'f': case 'F':
  1943. iCookie = BTREE_FREE_PAGE_COUNT;
  1944. break;
  1945. case 's': case 'S':
  1946. iCookie = BTREE_SCHEMA_VERSION;
  1947. break;
  1948. default:
  1949. iCookie = BTREE_USER_VERSION;
  1950. break;
  1951. }
  1952. if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
  1953. /* Write the specified cookie value */
  1954. static const VdbeOpList setCookie[] = {
  1955. { OP_Transaction, 0, 1, 0}, /* 0 */
  1956. { OP_Integer, 0, 1, 0}, /* 1 */
  1957. { OP_SetCookie, 0, 0, 1}, /* 2 */
  1958. };
  1959. int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
  1960. sqlite3VdbeChangeP1(v, addr, iDb);
  1961. sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
  1962. sqlite3VdbeChangeP1(v, addr+2, iDb);
  1963. sqlite3VdbeChangeP2(v, addr+2, iCookie);
  1964. }else{
  1965. /* Read the specified cookie value */
  1966. static const VdbeOpList readCookie[] = {
  1967. { OP_Transaction, 0, 0, 0}, /* 0 */
  1968. { OP_ReadCookie, 0, 1, 0}, /* 1 */
  1969. { OP_ResultRow, 1, 1, 0}
  1970. };
  1971. int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
  1972. sqlite3VdbeChangeP1(v, addr, iDb);
  1973. sqlite3VdbeChangeP1(v, addr+1, iDb);
  1974. sqlite3VdbeChangeP3(v, addr+1, iCookie);
  1975. sqlite3VdbeSetNumCols(v, 1);
  1976. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
  1977. }
  1978. }
  1979. break;
  1980. #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
  1981. #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1982. /*
  1983. ** PRAGMA compile_options
  1984. **
  1985. ** Return the names of all compile-time options used in this build,
  1986. ** one option per row.
  1987. */
  1988. case PragTyp_COMPILE_OPTIONS: {
  1989. int i = 0;
  1990. const char *zOpt;
  1991. sqlite3VdbeSetNumCols(v, 1);
  1992. pParse->nMem = 1;
  1993. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
  1994. while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
  1995. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
  1996. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
  1997. }
  1998. }
  1999. break;
  2000. #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  2001. #ifndef SQLITE_OMIT_WAL
  2002. /*
  2003. ** PRAGMA [database.]wal_checkpoint = passive|full|restart
  2004. **
  2005. ** Checkpoint the database.
  2006. */
  2007. case PragTyp_WAL_CHECKPOINT: {
  2008. int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
  2009. int eMode = SQLITE_CHECKPOINT_PASSIVE;
  2010. if( zRight ){
  2011. if( sqlite3StrICmp(zRight, "full")==0 ){
  2012. eMode = SQLITE_CHECKPOINT_FULL;
  2013. }else if( sqlite3StrICmp(zRight, "restart")==0 ){
  2014. eMode = SQLITE_CHECKPOINT_RESTART;
  2015. }
  2016. }
  2017. sqlite3VdbeSetNumCols(v, 3);
  2018. pParse->nMem = 3;
  2019. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
  2020. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
  2021. sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
  2022. sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
  2023. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  2024. }
  2025. break;
  2026. /*
  2027. ** PRAGMA wal_autocheckpoint
  2028. ** PRAGMA wal_autocheckpoint = N
  2029. **
  2030. ** Configure a database connection to automatically checkpoint a database
  2031. ** after accumulating N frames in the log. Or query for the current value
  2032. ** of N.
  2033. */
  2034. case PragTyp_WAL_AUTOCHECKPOINT: {
  2035. if( zRight ){
  2036. sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
  2037. }
  2038. returnSingleInt(pParse, "wal_autocheckpoint",
  2039. db->xWalCallback==sqlite3WalDefaultHook ?
  2040. SQLITE_PTR_TO_INT(db->pWalArg) : 0);
  2041. }
  2042. break;
  2043. #endif
  2044. /*
  2045. ** PRAGMA shrink_memory
  2046. **
  2047. ** This pragma attempts to free as much memory as possible from the
  2048. ** current database connection.
  2049. */
  2050. case PragTyp_SHRINK_MEMORY: {
  2051. sqlite3_db_release_memory(db);
  2052. break;
  2053. }
  2054. /*
  2055. ** PRAGMA busy_timeout
  2056. ** PRAGMA busy_timeout = N
  2057. **
  2058. ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
  2059. ** if one is set. If no busy handler or a different busy handler is set
  2060. ** then 0 is returned. Setting the busy_timeout to 0 or negative
  2061. ** disables the timeout.
  2062. */
  2063. /*case PragTyp_BUSY_TIMEOUT*/ default: {
  2064. assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
  2065. if( zRight ){
  2066. sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
  2067. }
  2068. returnSingleInt(pParse, "timeout", db->busyTimeout);
  2069. break;
  2070. }
  2071. /*
  2072. ** PRAGMA soft_heap_limit
  2073. ** PRAGMA soft_heap_limit = N
  2074. **
  2075. ** Call sqlite3_soft_heap_limit64(N). Return the result. If N is omitted,
  2076. ** use -1.
  2077. */
  2078. case PragTyp_SOFT_HEAP_LIMIT: {
  2079. sqlite3_int64 N;
  2080. if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
  2081. sqlite3_soft_heap_limit64(N);
  2082. }
  2083. returnSingleInt(pParse, "soft_heap_limit", sqlite3_soft_heap_limit64(-1));
  2084. break;
  2085. }
  2086. #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  2087. /*
  2088. ** Report the current state of file logs for all databases
  2089. */
  2090. case PragTyp_LOCK_STATUS: {
  2091. static const char *const azLockName[] = {
  2092. "unlocked", "shared", "reserved", "pending", "exclusive"
  2093. };
  2094. int i;
  2095. sqlite3VdbeSetNumCols(v, 2);
  2096. pParse->nMem = 2;
  2097. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
  2098. sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
  2099. for(i=0; i<db->nDb; i++){
  2100. Btree *pBt;
  2101. const char *zState = "unknown";
  2102. int j;
  2103. if( db->aDb[i].zName==0 ) continue;
  2104. sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
  2105. pBt = db->aDb[i].pBt;
  2106. if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
  2107. zState = "closed";
  2108. }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
  2109. SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
  2110. zState = azLockName[j];
  2111. }
  2112. sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
  2113. sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
  2114. }
  2115. break;
  2116. }
  2117. #endif
  2118. #ifdef SQLITE_HAS_CODEC
  2119. case PragTyp_KEY: {
  2120. if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
  2121. break;
  2122. }
  2123. case PragTyp_REKEY: {
  2124. if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
  2125. break;
  2126. }
  2127. case PragTyp_HEXKEY: {
  2128. if( zRight ){
  2129. u8 iByte;
  2130. int i;
  2131. char zKey[40];
  2132. for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
  2133. iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
  2134. if( (i&1)!=0 ) zKey[i/2] = iByte;
  2135. }
  2136. if( (zLeft[3] & 0xf)==0xb ){
  2137. sqlite3_key_v2(db, zDb, zKey, i/2);
  2138. }else{
  2139. sqlite3_rekey_v2(db, zDb, zKey, i/2);
  2140. }
  2141. }
  2142. break;
  2143. }
  2144. #endif
  2145. #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
  2146. case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
  2147. #ifdef SQLITE_HAS_CODEC
  2148. if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
  2149. sqlite3_activate_see(&zRight[4]);
  2150. }
  2151. #endif
  2152. #ifdef SQLITE_ENABLE_CEROD
  2153. if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
  2154. sqlite3_activate_cerod(&zRight[6]);
  2155. }
  2156. #endif
  2157. }
  2158. break;
  2159. #endif
  2160. } /* End of the PRAGMA switch */
  2161. pragma_out:
  2162. sqlite3DbFree(db, zLeft);
  2163. sqlite3DbFree(db, zRight);
  2164. }
  2165. #endif /* SQLITE_OMIT_PRAGMA */