drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 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. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 13 | */ |
| 14 | #include "sqliteInt.h" |
| 15 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 16 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 17 | # if defined(__APPLE__) |
| 18 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 19 | # else |
| 20 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 21 | # endif |
| 22 | #endif |
| 23 | |
| 24 | /*************************************************************************** |
drh | 67e65e5 | 2015-02-02 21:34:54 +0000 | [diff] [blame] | 25 | ** The "pragma.h" include file is an automatically generated file that |
| 26 | ** that includes the PragType_XXXX macro definitions and the aPragmaName[] |
| 27 | ** object. This ensures that the aPragmaName[] table is arranged in |
| 28 | ** lexicographical order to facility a binary search of the pragma name. |
| 29 | ** Do not edit pragma.h directly. Edit and rerun the script in at |
| 30 | ** ../tool/mkpragmatab.tcl. */ |
| 31 | #include "pragma.h" |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 32 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 33 | /* |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 34 | ** Interpret the given string as a safety level. Return 0 for OFF, |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 35 | ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or |
| 36 | ** unrecognized string argument. The FULL and EXTRA option is disallowed |
drh | 908c005 | 2012-01-30 18:40:55 +0000 | [diff] [blame] | 37 | ** if the omitFull parameter it 1. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 38 | ** |
| 39 | ** Note that the values returned are one less that the values that |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 40 | ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 41 | ** to support legacy SQL code. The safety level used to be boolean |
| 42 | ** and older scripts may have used numbers 0 for OFF and 1 for ON. |
| 43 | */ |
drh | eac5bd7 | 2014-07-25 21:35:39 +0000 | [diff] [blame] | 44 | static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){ |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 45 | /* 123456789 123456789 123 */ |
| 46 | static const char zText[] = "onoffalseyestruextrafull"; |
| 47 | static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20}; |
| 48 | static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4}; |
| 49 | static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2}; |
| 50 | /* on no off false yes true extra full */ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 51 | int i, n; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 52 | if( sqlite3Isdigit(*z) ){ |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 53 | return (u8)sqlite3Atoi(z); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 54 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 55 | n = sqlite3Strlen30(z); |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 56 | for(i=0; i<ArraySize(iLength); i++){ |
| 57 | if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 |
| 58 | && (!omitFull || iValue[i]<=1) |
| 59 | ){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 60 | return iValue[i]; |
| 61 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 62 | } |
drh | 908c005 | 2012-01-30 18:40:55 +0000 | [diff] [blame] | 63 | return dflt; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 67 | ** Interpret the given string as a boolean value. |
| 68 | */ |
drh | eac5bd7 | 2014-07-25 21:35:39 +0000 | [diff] [blame] | 69 | u8 sqlite3GetBoolean(const char *z, u8 dflt){ |
drh | 38d9c61 | 2012-01-31 14:24:47 +0000 | [diff] [blame] | 70 | return getSafetyLevel(z,1,dflt)!=0; |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
drh | c7dc9bf | 2011-06-03 13:02:57 +0000 | [diff] [blame] | 73 | /* The sqlite3GetBoolean() function is used by other modules but the |
| 74 | ** remainder of this file is specific to PRAGMA processing. So omit |
| 75 | ** the rest of the file if PRAGMAs are omitted from the build. |
| 76 | */ |
| 77 | #if !defined(SQLITE_OMIT_PRAGMA) |
| 78 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 79 | /* |
| 80 | ** Interpret the given string as a locking mode value. |
| 81 | */ |
| 82 | static int getLockingMode(const char *z){ |
| 83 | if( z ){ |
| 84 | if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE; |
| 85 | if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL; |
| 86 | } |
| 87 | return PAGER_LOCKINGMODE_QUERY; |
| 88 | } |
| 89 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 90 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 91 | /* |
| 92 | ** Interpret the given string as an auto-vacuum mode value. |
| 93 | ** |
| 94 | ** The following strings, "none", "full" and "incremental" are |
| 95 | ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. |
| 96 | */ |
| 97 | static int getAutoVacuum(const char *z){ |
| 98 | int i; |
| 99 | if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE; |
| 100 | if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL; |
| 101 | if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR; |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 102 | i = sqlite3Atoi(z); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 103 | return (u8)((i>=0&&i<=2)?i:0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 104 | } |
| 105 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 106 | |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 107 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 108 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 109 | ** Interpret the given string as a temp db location. Return 1 for file |
| 110 | ** backed temporary databases, 2 for the Red-Black tree in memory database |
| 111 | ** and 0 to use the compile-time default. |
| 112 | */ |
| 113 | static int getTempStore(const char *z){ |
| 114 | if( z[0]>='0' && z[0]<='2' ){ |
| 115 | return z[0] - '0'; |
| 116 | }else if( sqlite3StrICmp(z, "file")==0 ){ |
| 117 | return 1; |
| 118 | }else if( sqlite3StrICmp(z, "memory")==0 ){ |
| 119 | return 2; |
| 120 | }else{ |
| 121 | return 0; |
| 122 | } |
| 123 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 124 | #endif /* SQLITE_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 125 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 126 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 127 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 128 | ** Invalidate temp storage, either when the temp storage is changed |
| 129 | ** from default, or when 'file' and the temp_store_directory has changed |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 130 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 131 | static int invalidateTempStorage(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 132 | sqlite3 *db = pParse->db; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 133 | if( db->aDb[1].pBt!=0 ){ |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 134 | if( !db->autoCommit |
| 135 | || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE |
| 136 | ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 137 | sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " |
| 138 | "from within a transaction"); |
| 139 | return SQLITE_ERROR; |
| 140 | } |
| 141 | sqlite3BtreeClose(db->aDb[1].pBt); |
| 142 | db->aDb[1].pBt = 0; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 143 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 144 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 145 | return SQLITE_OK; |
| 146 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 147 | #endif /* SQLITE_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 148 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 149 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 150 | /* |
| 151 | ** If the TEMP database is open, close it and mark the database schema |
danielk1977 | b06a0b6 | 2008-06-26 10:54:12 +0000 | [diff] [blame] | 152 | ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 153 | ** or DEFAULT_TEMP_STORE pragmas. |
| 154 | */ |
| 155 | static int changeTempStorage(Parse *pParse, const char *zStorageType){ |
| 156 | int ts = getTempStore(zStorageType); |
| 157 | sqlite3 *db = pParse->db; |
| 158 | if( db->temp_store==ts ) return SQLITE_OK; |
| 159 | if( invalidateTempStorage( pParse ) != SQLITE_OK ){ |
| 160 | return SQLITE_ERROR; |
| 161 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 162 | db->temp_store = (u8)ts; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 163 | return SQLITE_OK; |
| 164 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 165 | #endif /* SQLITE_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 166 | |
| 167 | /* |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 168 | ** Set result column names for a pragma. |
drh | b460e52 | 2015-09-03 03:29:51 +0000 | [diff] [blame] | 169 | */ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 170 | static void setPragmaResultColumnNames( |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 171 | Vdbe *v, /* The query under construction */ |
| 172 | const PragmaName *pPragma /* The pragma */ |
drh | b460e52 | 2015-09-03 03:29:51 +0000 | [diff] [blame] | 173 | ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 174 | u8 n = pPragma->nPragCName; |
| 175 | sqlite3VdbeSetNumCols(v, n==0 ? 1 : n); |
| 176 | if( n==0 ){ |
| 177 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC); |
| 178 | }else{ |
| 179 | int i, j; |
| 180 | for(i=0, j=pPragma->iPragCName; i<n; i++, j++){ |
| 181 | sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC); |
| 182 | } |
drh | b460e52 | 2015-09-03 03:29:51 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
drh | b460e52 | 2015-09-03 03:29:51 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 187 | ** Generate code to return a single integer value. |
| 188 | */ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 189 | static void returnSingleInt(Vdbe *v, i64 value){ |
drh | 7cc023c | 2015-09-03 04:28:25 +0000 | [diff] [blame] | 190 | sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64); |
drh | 7cc023c | 2015-09-03 04:28:25 +0000 | [diff] [blame] | 191 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | ** Generate code to return a single text value. |
| 196 | */ |
| 197 | static void returnSingleText( |
| 198 | Vdbe *v, /* Prepared statement under construction */ |
drh | 7cc023c | 2015-09-03 04:28:25 +0000 | [diff] [blame] | 199 | const char *zValue /* Value to be returned */ |
| 200 | ){ |
| 201 | if( zValue ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 202 | sqlite3VdbeLoadString(v, 1, (const char*)zValue); |
drh | 7cc023c | 2015-09-03 04:28:25 +0000 | [diff] [blame] | 203 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 204 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 205 | } |
| 206 | |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | ** Set the safety_level and pager flags for pager iDb. Or if iDb<0 |
| 210 | ** set these values for all pagers. |
| 211 | */ |
| 212 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 213 | static void setAllPagerFlags(sqlite3 *db){ |
| 214 | if( db->autoCommit ){ |
| 215 | Db *pDb = db->aDb; |
| 216 | int n = db->nDb; |
| 217 | assert( SQLITE_FullFSync==PAGER_FULLFSYNC ); |
| 218 | assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC ); |
| 219 | assert( SQLITE_CacheSpill==PAGER_CACHESPILL ); |
| 220 | assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL) |
| 221 | == PAGER_FLAGS_MASK ); |
| 222 | assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level ); |
| 223 | while( (n--) > 0 ){ |
| 224 | if( pDb->pBt ){ |
| 225 | sqlite3BtreeSetPagerFlags(pDb->pBt, |
| 226 | pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) ); |
| 227 | } |
| 228 | pDb++; |
| 229 | } |
| 230 | } |
| 231 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 232 | #else |
| 233 | # define setAllPagerFlags(X) /* no-op */ |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
| 236 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 237 | /* |
| 238 | ** Return a human-readable name for a constraint resolution action. |
| 239 | */ |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 240 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 241 | static const char *actionName(u8 action){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 242 | const char *zName; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 243 | switch( action ){ |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 244 | case OE_SetNull: zName = "SET NULL"; break; |
| 245 | case OE_SetDflt: zName = "SET DEFAULT"; break; |
| 246 | case OE_Cascade: zName = "CASCADE"; break; |
| 247 | case OE_Restrict: zName = "RESTRICT"; break; |
| 248 | default: zName = "NO ACTION"; |
| 249 | assert( action==OE_None ); break; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 250 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 251 | return zName; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 252 | } |
dan | ba9108b | 2009-09-22 07:13:42 +0000 | [diff] [blame] | 253 | #endif |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 254 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants |
| 258 | ** defined in pager.h. This function returns the associated lowercase |
| 259 | ** journal-mode name. |
| 260 | */ |
| 261 | const char *sqlite3JournalModename(int eMode){ |
| 262 | static char * const azModeName[] = { |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 263 | "delete", "persist", "off", "truncate", "memory" |
| 264 | #ifndef SQLITE_OMIT_WAL |
| 265 | , "wal" |
| 266 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 267 | }; |
| 268 | assert( PAGER_JOURNALMODE_DELETE==0 ); |
| 269 | assert( PAGER_JOURNALMODE_PERSIST==1 ); |
| 270 | assert( PAGER_JOURNALMODE_OFF==2 ); |
| 271 | assert( PAGER_JOURNALMODE_TRUNCATE==3 ); |
| 272 | assert( PAGER_JOURNALMODE_MEMORY==4 ); |
| 273 | assert( PAGER_JOURNALMODE_WAL==5 ); |
| 274 | assert( eMode>=0 && eMode<=ArraySize(azModeName) ); |
| 275 | |
| 276 | if( eMode==ArraySize(azModeName) ) return 0; |
| 277 | return azModeName[eMode]; |
| 278 | } |
| 279 | |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 280 | /* |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 281 | ** Locate a pragma in the aPragmaName[] array. |
| 282 | */ |
| 283 | static const PragmaName *pragmaLocate(const char *zName){ |
mistachkin | 2e52532 | 2017-02-01 22:43:08 +0000 | [diff] [blame] | 284 | int upr, lwr, mid = 0, rc; |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 285 | lwr = 0; |
| 286 | upr = ArraySize(aPragmaName)-1; |
| 287 | while( lwr<=upr ){ |
| 288 | mid = (lwr+upr)/2; |
| 289 | rc = sqlite3_stricmp(zName, aPragmaName[mid].zName); |
| 290 | if( rc==0 ) break; |
| 291 | if( rc<0 ){ |
| 292 | upr = mid - 1; |
| 293 | }else{ |
| 294 | lwr = mid + 1; |
| 295 | } |
| 296 | } |
| 297 | return lwr>upr ? 0 : &aPragmaName[mid]; |
| 298 | } |
| 299 | |
| 300 | /* |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 301 | ** Create zero or more entries in the output for the SQL functions |
| 302 | ** defined by FuncDef p. |
| 303 | */ |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 304 | static void pragmaFunclistLine( |
| 305 | Vdbe *v, /* The prepared statement being created */ |
| 306 | FuncDef *p, /* A particular function definition */ |
| 307 | int isBuiltin, /* True if this is a built-in function */ |
| 308 | int showInternFuncs /* True if showing internal functions */ |
| 309 | ){ |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 310 | for(; p; p=p->pNext){ |
| 311 | const char *zType; |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 312 | static const u32 mask = |
| 313 | SQLITE_DETERMINISTIC | |
| 314 | SQLITE_DIRECTONLY | |
| 315 | SQLITE_SUBTYPE | |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 316 | SQLITE_INNOCUOUS | |
| 317 | SQLITE_FUNC_INTERNAL |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 318 | ; |
drh | b84fda3 | 2020-01-09 16:28:50 +0000 | [diff] [blame] | 319 | static const char *azEnc[] = { 0, "utf8", "utf16le", "utf16be" }; |
| 320 | |
| 321 | assert( SQLITE_FUNC_ENCMASK==0x3 ); |
| 322 | assert( strcmp(azEnc[SQLITE_UTF8],"utf8")==0 ); |
| 323 | assert( strcmp(azEnc[SQLITE_UTF16LE],"utf16le")==0 ); |
| 324 | assert( strcmp(azEnc[SQLITE_UTF16BE],"utf16be")==0 ); |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 325 | |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 326 | if( p->xSFunc==0 ) continue; |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 327 | if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0 |
| 328 | && showInternFuncs==0 |
| 329 | ){ |
| 330 | continue; |
| 331 | } |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 332 | if( p->xValue!=0 ){ |
| 333 | zType = "w"; |
| 334 | }else if( p->xFinalize!=0 ){ |
| 335 | zType = "a"; |
| 336 | }else{ |
| 337 | zType = "s"; |
| 338 | } |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 339 | sqlite3VdbeMultiLoad(v, 1, "sissii", |
| 340 | p->zName, isBuiltin, |
drh | b84fda3 | 2020-01-09 16:28:50 +0000 | [diff] [blame] | 341 | zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK], |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 342 | p->nArg, |
| 343 | (p->funcFlags & mask) ^ SQLITE_INNOCUOUS |
| 344 | ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | /* |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 350 | ** Helper subroutine for PRAGMA integrity_check: |
| 351 | ** |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 352 | ** Generate code to output a single-column result row with a value of the |
| 353 | ** string held in register 3. Decrement the result count in register 1 |
| 354 | ** and halt if the maximum number of result rows have been issued. |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 355 | */ |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 356 | static int integrityCheckResultRow(Vdbe *v){ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 357 | int addr; |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 358 | sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1); |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 359 | addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1); |
| 360 | VdbeCoverage(v); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 361 | sqlite3VdbeAddOp0(v, OP_Halt); |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 362 | return addr; |
| 363 | } |
| 364 | |
| 365 | /* |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 366 | ** Process a pragma statement. |
| 367 | ** |
| 368 | ** Pragmas are of this form: |
| 369 | ** |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 370 | ** PRAGMA [schema.]id [= value] |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 371 | ** |
| 372 | ** The identifier might also be a string. The value is a string, and |
| 373 | ** identifier, or a number. If minusFlag is true, then the value is |
| 374 | ** a number that was preceded by a minus sign. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 375 | ** |
| 376 | ** If the left side is "database.id" then pId1 is the database name |
| 377 | ** and pId2 is the id. If the left side is just "id" then pId1 is the |
| 378 | ** id and pId2 is any empty string. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 379 | */ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 380 | void sqlite3Pragma( |
| 381 | Parse *pParse, |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 382 | Token *pId1, /* First part of [schema.]id field */ |
| 383 | Token *pId2, /* Second part of [schema.]id field, or NULL */ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 384 | Token *pValue, /* Token for <value>, or NULL */ |
| 385 | int minusFlag /* True if a '-' sign preceded <value> */ |
| 386 | ){ |
| 387 | char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ |
| 388 | char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ |
| 389 | const char *zDb = 0; /* The database name */ |
| 390 | Token *pId; /* Pointer to <id> token */ |
drh | 3fa9730 | 2012-02-22 16:58:36 +0000 | [diff] [blame] | 391 | char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 392 | int iDb; /* Database index for <database> */ |
drh | 06fd5d6 | 2012-02-22 14:45:19 +0000 | [diff] [blame] | 393 | int rc; /* return value form SQLITE_FCNTL_PRAGMA */ |
| 394 | sqlite3 *db = pParse->db; /* The database connection */ |
| 395 | Db *pDb; /* The specific database being pragmaed */ |
drh | ef8e986 | 2013-04-11 13:26:18 +0000 | [diff] [blame] | 396 | Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 397 | const PragmaName *pPragma; /* The pragma */ |
drh | 06fd5d6 | 2012-02-22 14:45:19 +0000 | [diff] [blame] | 398 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 399 | if( v==0 ) return; |
drh | 4611d92 | 2010-02-25 14:47:01 +0000 | [diff] [blame] | 400 | sqlite3VdbeRunOnlyOnce(v); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 401 | pParse->nMem = 2; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 402 | |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 403 | /* Interpret the [schema.] part of the pragma statement. iDb is the |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 404 | ** index of the database this pragma is being applied to in db.aDb[]. */ |
| 405 | iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); |
| 406 | if( iDb<0 ) return; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 407 | pDb = &db->aDb[iDb]; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 408 | |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 409 | /* If the temp database has been explicitly named as part of the |
| 410 | ** pragma, make sure it is open. |
| 411 | */ |
| 412 | if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ |
| 413 | return; |
| 414 | } |
| 415 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 416 | zLeft = sqlite3NameFromToken(db, pId); |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 417 | if( !zLeft ) return; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 418 | if( minusFlag ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 419 | zRight = sqlite3MPrintf(db, "-%T", pValue); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 420 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 421 | zRight = sqlite3NameFromToken(db, pValue); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 422 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 423 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 424 | assert( pId2 ); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 425 | zDb = pId2->n>0 ? pDb->zDbSName : 0; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 426 | if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 427 | goto pragma_out; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 428 | } |
drh | 06fd5d6 | 2012-02-22 14:45:19 +0000 | [diff] [blame] | 429 | |
| 430 | /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS |
| 431 | ** connection. If it returns SQLITE_OK, then assume that the VFS |
| 432 | ** handled the pragma and generate a no-op prepared statement. |
drh | 8dd7a6a | 2015-03-06 04:37:26 +0000 | [diff] [blame] | 433 | ** |
| 434 | ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, |
| 435 | ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file |
| 436 | ** object corresponding to the database file to which the pragma |
| 437 | ** statement refers. |
| 438 | ** |
| 439 | ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA |
| 440 | ** file control is an array of pointers to strings (char**) in which the |
| 441 | ** second element of the array is the name of the pragma and the third |
| 442 | ** element is the argument to the pragma or NULL if the pragma has no |
| 443 | ** argument. |
drh | 06fd5d6 | 2012-02-22 14:45:19 +0000 | [diff] [blame] | 444 | */ |
drh | 3fa9730 | 2012-02-22 16:58:36 +0000 | [diff] [blame] | 445 | aFcntl[0] = 0; |
| 446 | aFcntl[1] = zLeft; |
| 447 | aFcntl[2] = zRight; |
| 448 | aFcntl[3] = 0; |
dan | 80bb6f8 | 2012-10-01 18:44:33 +0000 | [diff] [blame] | 449 | db->busyHandler.nBusy = 0; |
drh | 06fd5d6 | 2012-02-22 14:45:19 +0000 | [diff] [blame] | 450 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); |
| 451 | if( rc==SQLITE_OK ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 452 | sqlite3VdbeSetNumCols(v, 1); |
| 453 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT); |
| 454 | returnSingleText(v, aFcntl[0]); |
drh | 7cc023c | 2015-09-03 04:28:25 +0000 | [diff] [blame] | 455 | sqlite3_free(aFcntl[0]); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 456 | goto pragma_out; |
| 457 | } |
| 458 | if( rc!=SQLITE_NOTFOUND ){ |
drh | 92c700d | 2012-02-22 19:56:17 +0000 | [diff] [blame] | 459 | if( aFcntl[0] ){ |
| 460 | sqlite3ErrorMsg(pParse, "%s", aFcntl[0]); |
| 461 | sqlite3_free(aFcntl[0]); |
| 462 | } |
| 463 | pParse->nErr++; |
| 464 | pParse->rc = rc; |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 465 | goto pragma_out; |
| 466 | } |
| 467 | |
| 468 | /* Locate the pragma in the lookup table */ |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 469 | pPragma = pragmaLocate(zLeft); |
| 470 | if( pPragma==0 ) goto pragma_out; |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 471 | |
drh | f63936e | 2013-10-03 14:08:07 +0000 | [diff] [blame] | 472 | /* Make sure the database schema is loaded if the pragma requires that */ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 473 | if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){ |
drh | f63936e | 2013-10-03 14:08:07 +0000 | [diff] [blame] | 474 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 475 | } |
| 476 | |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 477 | /* Register the result column names for pragmas that return results */ |
dan | 9e1ab1a | 2017-01-05 19:32:48 +0000 | [diff] [blame] | 478 | if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 |
| 479 | && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0) |
| 480 | ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 481 | setPragmaResultColumnNames(v, pPragma); |
| 482 | } |
| 483 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 484 | /* Jump to the appropriate pragma handler */ |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 485 | switch( pPragma->ePragTyp ){ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 486 | |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 487 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED) |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 488 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 489 | ** PRAGMA [schema.]default_cache_size |
| 490 | ** PRAGMA [schema.]default_cache_size=N |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 491 | ** |
| 492 | ** The first form reports the current persistent setting for the |
| 493 | ** page cache size. The value returned is the maximum number of |
| 494 | ** pages in the page cache. The second form sets both the current |
| 495 | ** page cache size value and the persistent page cache size value |
| 496 | ** stored in the database file. |
| 497 | ** |
drh | 93791ea | 2010-04-26 17:36:35 +0000 | [diff] [blame] | 498 | ** Older versions of SQLite would set the default cache size to a |
| 499 | ** negative number to indicate synchronous=OFF. These days, synchronous |
| 500 | ** is always on by default regardless of the sign of the default cache |
| 501 | ** size. But continue to take the absolute value of the default cache |
| 502 | ** size of historical compatibility. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 503 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 504 | case PragTyp_DEFAULT_CACHE_SIZE: { |
drh | b06a4ec | 2014-03-10 18:03:09 +0000 | [diff] [blame] | 505 | static const int iLn = VDBE_OFFSET_LINENO(2); |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 506 | static const VdbeOpList getCacheSize[] = { |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 507 | { OP_Transaction, 0, 0, 0}, /* 0 */ |
| 508 | { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ |
drh | ef8e986 | 2013-04-11 13:26:18 +0000 | [diff] [blame] | 509 | { OP_IfPos, 1, 8, 0}, |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 510 | { OP_Integer, 0, 2, 0}, |
| 511 | { OP_Subtract, 1, 2, 1}, |
drh | ef8e986 | 2013-04-11 13:26:18 +0000 | [diff] [blame] | 512 | { OP_IfPos, 1, 8, 0}, |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 513 | { OP_Integer, 0, 1, 0}, /* 6 */ |
drh | ef8e986 | 2013-04-11 13:26:18 +0000 | [diff] [blame] | 514 | { OP_Noop, 0, 0, 0}, |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 515 | { OP_ResultRow, 1, 1, 0}, |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 516 | }; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 517 | VdbeOp *aOp; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 518 | sqlite3VdbeUsesBtree(v, iDb); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 519 | if( !zRight ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 520 | pParse->nMem += 2; |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 521 | sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize)); |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 522 | aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn); |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 523 | if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 524 | aOp[0].p1 = iDb; |
| 525 | aOp[1].p1 = iDb; |
| 526 | aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 527 | }else{ |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 528 | int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 529 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 530 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 531 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 532 | pDb->pSchema->cache_size = size; |
| 533 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 534 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 535 | break; |
| 536 | } |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 537 | #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 538 | |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 539 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 540 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 541 | ** PRAGMA [schema.]page_size |
| 542 | ** PRAGMA [schema.]page_size=N |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 543 | ** |
| 544 | ** The first form reports the current setting for the |
| 545 | ** database page size in bytes. The second form sets the |
| 546 | ** database page size value. The value can only be set if |
| 547 | ** the database has not yet been created. |
| 548 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 549 | case PragTyp_PAGE_SIZE: { |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 550 | Btree *pBt = pDb->pBt; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 551 | assert( pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 552 | if( !zRight ){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 553 | int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 554 | returnSingleInt(v, size); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 555 | }else{ |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 556 | /* Malloc may fail when setting the page-size, as there is an internal |
| 557 | ** buffer that the pager module resizes using sqlite3_realloc(). |
| 558 | */ |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 559 | db->nextPagesize = sqlite3Atoi(zRight); |
drh | e937df8 | 2020-05-07 01:56:57 +0000 | [diff] [blame] | 560 | if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 561 | sqlite3OomFault(db); |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 562 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 563 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 566 | |
| 567 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 568 | ** PRAGMA [schema.]secure_delete |
drh | a5907a8 | 2017-06-19 11:44:22 +0000 | [diff] [blame] | 569 | ** PRAGMA [schema.]secure_delete=ON/OFF/FAST |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 570 | ** |
| 571 | ** The first form reports the current setting for the |
| 572 | ** secure_delete flag. The second form changes the secure_delete |
drh | a5907a8 | 2017-06-19 11:44:22 +0000 | [diff] [blame] | 573 | ** flag setting and reports the new value. |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 574 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 575 | case PragTyp_SECURE_DELETE: { |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 576 | Btree *pBt = pDb->pBt; |
| 577 | int b = -1; |
| 578 | assert( pBt!=0 ); |
| 579 | if( zRight ){ |
drh | a5907a8 | 2017-06-19 11:44:22 +0000 | [diff] [blame] | 580 | if( sqlite3_stricmp(zRight, "fast")==0 ){ |
| 581 | b = 2; |
| 582 | }else{ |
| 583 | b = sqlite3GetBoolean(zRight, 0); |
| 584 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 585 | } |
drh | af034ed | 2010-02-12 19:46:26 +0000 | [diff] [blame] | 586 | if( pId2->n==0 && b>=0 ){ |
| 587 | int ii; |
| 588 | for(ii=0; ii<db->nDb; ii++){ |
| 589 | sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); |
| 590 | } |
| 591 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 592 | b = sqlite3BtreeSecureDelete(pBt, b); |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 593 | returnSingleInt(v, b); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 594 | break; |
| 595 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 596 | |
| 597 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 598 | ** PRAGMA [schema.]max_page_count |
| 599 | ** PRAGMA [schema.]max_page_count=N |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 600 | ** |
| 601 | ** The first form reports the current setting for the |
| 602 | ** maximum number of pages in the database file. The |
| 603 | ** second form attempts to change this setting. Both |
| 604 | ** forms return the current setting. |
| 605 | ** |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 606 | ** The absolute value of N is used. This is undocumented and might |
| 607 | ** change. The only purpose is to provide an easy way to test |
| 608 | ** the sqlite3AbsInt32() function. |
| 609 | ** |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 610 | ** PRAGMA [schema.]page_count |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 611 | ** |
| 612 | ** Return the number of pages in the specified database. |
| 613 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 614 | case PragTyp_PAGE_COUNT: { |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 615 | int iReg; |
drh | e9261db | 2020-07-20 12:47:32 +0000 | [diff] [blame] | 616 | i64 x = 0; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 617 | sqlite3CodeVerifySchema(pParse, iDb); |
| 618 | iReg = ++pParse->nMem; |
drh | c522731 | 2011-10-13 17:09:01 +0000 | [diff] [blame] | 619 | if( sqlite3Tolower(zLeft[0])=='p' ){ |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 620 | sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); |
| 621 | }else{ |
drh | e9261db | 2020-07-20 12:47:32 +0000 | [diff] [blame] | 622 | if( zRight && sqlite3DecOrHexToI64(zRight,&x)==0 ){ |
| 623 | if( x<0 ) x = 0; |
| 624 | else if( x>0xfffffffe ) x = 0xfffffffe; |
| 625 | }else{ |
| 626 | x = 0; |
| 627 | } |
| 628 | sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, (int)x); |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 629 | } |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 630 | sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 631 | break; |
| 632 | } |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 633 | |
| 634 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 635 | ** PRAGMA [schema.]locking_mode |
| 636 | ** PRAGMA [schema.]locking_mode = (normal|exclusive) |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 637 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 638 | case PragTyp_LOCKING_MODE: { |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 639 | const char *zRet = "normal"; |
| 640 | int eMode = getLockingMode(zRight); |
| 641 | |
| 642 | if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ |
| 643 | /* Simple "PRAGMA locking_mode;" statement. This is a query for |
| 644 | ** the current default locking mode (which may be different to |
| 645 | ** the locking-mode of the main database). |
| 646 | */ |
| 647 | eMode = db->dfltLockMode; |
| 648 | }else{ |
| 649 | Pager *pPager; |
| 650 | if( pId2->n==0 ){ |
| 651 | /* This indicates that no database name was specified as part |
| 652 | ** of the PRAGMA command. In this case the locking-mode must be |
| 653 | ** set on all attached databases, as well as the main db file. |
| 654 | ** |
| 655 | ** Also, the sqlite3.dfltLockMode variable is set so that |
| 656 | ** any subsequently attached databases also use the specified |
| 657 | ** locking mode. |
| 658 | */ |
| 659 | int ii; |
| 660 | assert(pDb==&db->aDb[0]); |
| 661 | for(ii=2; ii<db->nDb; ii++){ |
| 662 | pPager = sqlite3BtreePager(db->aDb[ii].pBt); |
| 663 | sqlite3PagerLockingMode(pPager, eMode); |
| 664 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 665 | db->dfltLockMode = (u8)eMode; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 666 | } |
| 667 | pPager = sqlite3BtreePager(pDb->pBt); |
| 668 | eMode = sqlite3PagerLockingMode(pPager, eMode); |
| 669 | } |
| 670 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 671 | assert( eMode==PAGER_LOCKINGMODE_NORMAL |
| 672 | || eMode==PAGER_LOCKINGMODE_EXCLUSIVE ); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 673 | if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ |
| 674 | zRet = "exclusive"; |
| 675 | } |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 676 | returnSingleText(v, zRet); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 677 | break; |
| 678 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 679 | |
| 680 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 681 | ** PRAGMA [schema.]journal_mode |
| 682 | ** PRAGMA [schema.]journal_mode = |
drh | 3ebaee9 | 2010-05-06 21:37:22 +0000 | [diff] [blame] | 683 | ** (delete|persist|off|truncate|memory|wal|off) |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 684 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 685 | case PragTyp_JOURNAL_MODE: { |
drh | c6b2a0f | 2010-07-08 17:40:37 +0000 | [diff] [blame] | 686 | int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ |
| 687 | int ii; /* Loop counter */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 688 | |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 689 | if( zRight==0 ){ |
drh | c6b2a0f | 2010-07-08 17:40:37 +0000 | [diff] [blame] | 690 | /* If there is no "=MODE" part of the pragma, do a query for the |
| 691 | ** current mode */ |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 692 | eMode = PAGER_JOURNALMODE_QUERY; |
| 693 | }else{ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 694 | const char *zMode; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 695 | int n = sqlite3Strlen30(zRight); |
drh | f77e2ac | 2010-07-07 14:33:09 +0000 | [diff] [blame] | 696 | for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 697 | if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; |
| 698 | } |
| 699 | if( !zMode ){ |
drh | c6b2a0f | 2010-07-08 17:40:37 +0000 | [diff] [blame] | 700 | /* If the "=MODE" part does not match any known journal mode, |
| 701 | ** then do a query */ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 702 | eMode = PAGER_JOURNALMODE_QUERY; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 703 | } |
drh | 6c35b30 | 2019-05-17 20:37:17 +0000 | [diff] [blame] | 704 | if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){ |
| 705 | /* Do not allow journal-mode "OFF" in defensive since the database |
| 706 | ** can become corrupted using ordinary SQL when the journal is off */ |
| 707 | eMode = PAGER_JOURNALMODE_QUERY; |
| 708 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 709 | } |
drh | c6b2a0f | 2010-07-08 17:40:37 +0000 | [diff] [blame] | 710 | if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ |
| 711 | /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ |
| 712 | iDb = 0; |
| 713 | pId2->n = 1; |
| 714 | } |
| 715 | for(ii=db->nDb-1; ii>=0; ii--){ |
| 716 | if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ |
| 717 | sqlite3VdbeUsesBtree(v, ii); |
| 718 | sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 719 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 720 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 721 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 722 | break; |
| 723 | } |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 724 | |
| 725 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 726 | ** PRAGMA [schema.]journal_size_limit |
| 727 | ** PRAGMA [schema.]journal_size_limit=N |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 728 | ** |
drh | a9e364f | 2009-01-13 20:14:15 +0000 | [diff] [blame] | 729 | ** Get or set the size limit on rollback journal files. |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 730 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 731 | case PragTyp_JOURNAL_SIZE_LIMIT: { |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 732 | Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 733 | i64 iLimit = -2; |
| 734 | if( zRight ){ |
drh | 9296c18 | 2014-07-23 13:40:49 +0000 | [diff] [blame] | 735 | sqlite3DecOrHexToI64(zRight, &iLimit); |
drh | 3c71364 | 2009-04-04 16:02:32 +0000 | [diff] [blame] | 736 | if( iLimit<-1 ) iLimit = -1; |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 737 | } |
| 738 | iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 739 | returnSingleInt(v, iLimit); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 740 | break; |
| 741 | } |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 742 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 743 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 744 | |
| 745 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 746 | ** PRAGMA [schema.]auto_vacuum |
| 747 | ** PRAGMA [schema.]auto_vacuum=N |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 748 | ** |
drh | a9e364f | 2009-01-13 20:14:15 +0000 | [diff] [blame] | 749 | ** Get or set the value of the database 'auto-vacuum' parameter. |
| 750 | ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 751 | */ |
| 752 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 753 | case PragTyp_AUTO_VACUUM: { |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 754 | Btree *pBt = pDb->pBt; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 755 | assert( pBt!=0 ); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 756 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 757 | returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt)); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 758 | }else{ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 759 | int eAuto = getAutoVacuum(zRight); |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 760 | assert( eAuto>=0 && eAuto<=2 ); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 761 | db->nextAutovac = (u8)eAuto; |
drh | f63936e | 2013-10-03 14:08:07 +0000 | [diff] [blame] | 762 | /* Call SetAutoVacuum() to set initialize the internal auto and |
| 763 | ** incr-vacuum flags. This is required in case this connection |
| 764 | ** creates the database file. It is important that it is created |
| 765 | ** as an auto-vacuum capable db. |
| 766 | */ |
| 767 | rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); |
| 768 | if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ |
| 769 | /* When setting the auto_vacuum mode to either "full" or |
| 770 | ** "incremental", write the value of meta[6] in the database |
| 771 | ** file. Before writing to meta[6], check that meta[3] indicates |
| 772 | ** that this really is an auto-vacuum capable database. |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 773 | */ |
drh | b06a4ec | 2014-03-10 18:03:09 +0000 | [diff] [blame] | 774 | static const int iLn = VDBE_OFFSET_LINENO(2); |
drh | f63936e | 2013-10-03 14:08:07 +0000 | [diff] [blame] | 775 | static const VdbeOpList setMeta6[] = { |
| 776 | { OP_Transaction, 0, 1, 0}, /* 0 */ |
| 777 | { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, |
| 778 | { OP_If, 1, 0, 0}, /* 2 */ |
| 779 | { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 780 | { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */ |
drh | f63936e | 2013-10-03 14:08:07 +0000 | [diff] [blame] | 781 | }; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 782 | VdbeOp *aOp; |
| 783 | int iAddr = sqlite3VdbeCurrentAddr(v); |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 784 | sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6)); |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 785 | aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn); |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 786 | if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 787 | aOp[0].p1 = iDb; |
| 788 | aOp[1].p1 = iDb; |
| 789 | aOp[2].p2 = iAddr+4; |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 790 | aOp[4].p1 = iDb; |
| 791 | aOp[4].p3 = eAuto - 1; |
drh | f63936e | 2013-10-03 14:08:07 +0000 | [diff] [blame] | 792 | sqlite3VdbeUsesBtree(v, iDb); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 793 | } |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 794 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 795 | break; |
| 796 | } |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 797 | #endif |
| 798 | |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 799 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 800 | ** PRAGMA [schema.]incremental_vacuum(N) |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 801 | ** |
| 802 | ** Do N steps of incremental vacuuming on a database. |
| 803 | */ |
| 804 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 805 | case PragTyp_INCREMENTAL_VACUUM: { |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 806 | int iLimit, addr; |
| 807 | if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ |
| 808 | iLimit = 0x7fffffff; |
| 809 | } |
| 810 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 811 | sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 812 | addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 813 | sqlite3VdbeAddOp1(v, OP_ResultRow, 1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 814 | sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 815 | sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 816 | sqlite3VdbeJumpHere(v, addr); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 817 | break; |
| 818 | } |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 819 | #endif |
| 820 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 821 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 822 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 823 | ** PRAGMA [schema.]cache_size |
| 824 | ** PRAGMA [schema.]cache_size=N |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 825 | ** |
| 826 | ** The first form reports the current local setting for the |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 827 | ** page cache size. The second form sets the local |
| 828 | ** page cache size value. If N is positive then that is the |
| 829 | ** number of pages in the cache. If N is negative, then the |
| 830 | ** number of pages is adjusted so that the cache uses -N kibibytes |
| 831 | ** of memory. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 832 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 833 | case PragTyp_CACHE_SIZE: { |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 834 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 835 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 836 | returnSingleInt(v, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 837 | }else{ |
drh | 3b42abb | 2011-11-09 14:23:04 +0000 | [diff] [blame] | 838 | int size = sqlite3Atoi(zRight); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 839 | pDb->pSchema->cache_size = size; |
| 840 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 841 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 842 | break; |
| 843 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 844 | |
| 845 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 846 | ** PRAGMA [schema.]cache_spill |
| 847 | ** PRAGMA cache_spill=BOOLEAN |
| 848 | ** PRAGMA [schema.]cache_spill=N |
| 849 | ** |
| 850 | ** The first form reports the current local setting for the |
| 851 | ** page cache spill size. The second form turns cache spill on |
| 852 | ** or off. When turnning cache spill on, the size is set to the |
| 853 | ** current cache_size. The third form sets a spill size that |
| 854 | ** may be different form the cache size. |
| 855 | ** If N is positive then that is the |
| 856 | ** number of pages in the cache. If N is negative, then the |
| 857 | ** number of pages is adjusted so that the cache uses -N kibibytes |
| 858 | ** of memory. |
| 859 | ** |
| 860 | ** If the number of cache_spill pages is less then the number of |
| 861 | ** cache_size pages, no spilling occurs until the page count exceeds |
| 862 | ** the number of cache_size pages. |
| 863 | ** |
| 864 | ** The cache_spill=BOOLEAN setting applies to all attached schemas, |
| 865 | ** not just the schema specified. |
| 866 | */ |
| 867 | case PragTyp_CACHE_SPILL: { |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 868 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
| 869 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 870 | returnSingleInt(v, |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 871 | (db->flags & SQLITE_CacheSpill)==0 ? 0 : |
| 872 | sqlite3BtreeSetSpillSize(pDb->pBt,0)); |
| 873 | }else{ |
drh | 4f9c8ec | 2015-11-12 15:47:48 +0000 | [diff] [blame] | 874 | int size = 1; |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 875 | if( sqlite3GetInt32(zRight, &size) ){ |
| 876 | sqlite3BtreeSetSpillSize(pDb->pBt, size); |
| 877 | } |
drh | 4f9c8ec | 2015-11-12 15:47:48 +0000 | [diff] [blame] | 878 | if( sqlite3GetBoolean(zRight, size!=0) ){ |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 879 | db->flags |= SQLITE_CacheSpill; |
| 880 | }else{ |
drh | d5b44d6 | 2018-12-06 17:06:02 +0000 | [diff] [blame] | 881 | db->flags &= ~(u64)SQLITE_CacheSpill; |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 882 | } |
drh | 4f9c8ec | 2015-11-12 15:47:48 +0000 | [diff] [blame] | 883 | setAllPagerFlags(db); |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 884 | } |
| 885 | break; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | ** PRAGMA [schema.]mmap_size(N) |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 890 | ** |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 891 | ** Used to set mapping size limit. The mapping size limit is |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 892 | ** used to limit the aggregate size of all memory mapped regions of the |
| 893 | ** database file. If this parameter is set to zero, then memory mapping |
drh | a1f42c7 | 2013-04-01 22:38:06 +0000 | [diff] [blame] | 894 | ** is not used at all. If N is negative, then the default memory map |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 895 | ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set. |
drh | a1f42c7 | 2013-04-01 22:38:06 +0000 | [diff] [blame] | 896 | ** The parameter N is measured in bytes. |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 897 | ** |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 898 | ** This value is advisory. The underlying VFS is free to memory map |
| 899 | ** as little or as much as it wants. Except, if N is set to 0 then the |
| 900 | ** upper layers will never invoke the xFetch interfaces to the VFS. |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 901 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 902 | case PragTyp_MMAP_SIZE: { |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 903 | sqlite3_int64 sz; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 904 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 905 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 906 | if( zRight ){ |
drh | a1f42c7 | 2013-04-01 22:38:06 +0000 | [diff] [blame] | 907 | int ii; |
drh | 9296c18 | 2014-07-23 13:40:49 +0000 | [diff] [blame] | 908 | sqlite3DecOrHexToI64(zRight, &sz); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 909 | if( sz<0 ) sz = sqlite3GlobalConfig.szMmap; |
| 910 | if( pId2->n==0 ) db->szMmap = sz; |
drh | a1f42c7 | 2013-04-01 22:38:06 +0000 | [diff] [blame] | 911 | for(ii=db->nDb-1; ii>=0; ii--){ |
| 912 | if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 913 | sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz); |
drh | a1f42c7 | 2013-04-01 22:38:06 +0000 | [diff] [blame] | 914 | } |
| 915 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 916 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 917 | sz = -1; |
dan | 3719f5f | 2013-05-23 10:13:18 +0000 | [diff] [blame] | 918 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 919 | #else |
dan | 3719f5f | 2013-05-23 10:13:18 +0000 | [diff] [blame] | 920 | sz = 0; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 921 | rc = SQLITE_OK; |
drh | 188d488 | 2013-04-08 20:47:49 +0000 | [diff] [blame] | 922 | #endif |
dan | 3719f5f | 2013-05-23 10:13:18 +0000 | [diff] [blame] | 923 | if( rc==SQLITE_OK ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 924 | returnSingleInt(v, sz); |
dan | 3719f5f | 2013-05-23 10:13:18 +0000 | [diff] [blame] | 925 | }else if( rc!=SQLITE_NOTFOUND ){ |
| 926 | pParse->nErr++; |
| 927 | pParse->rc = rc; |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 928 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 929 | break; |
| 930 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 931 | |
| 932 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 933 | ** PRAGMA temp_store |
| 934 | ** PRAGMA temp_store = "default"|"memory"|"file" |
| 935 | ** |
| 936 | ** Return or set the local value of the temp_store flag. Changing |
| 937 | ** the local value does not make changes to the disk file and the default |
| 938 | ** value will be restored the next time the database is opened. |
| 939 | ** |
| 940 | ** Note that it is possible for the library compile-time options to |
| 941 | ** override this setting |
| 942 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 943 | case PragTyp_TEMP_STORE: { |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 944 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 945 | returnSingleInt(v, db->temp_store); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 946 | }else{ |
| 947 | changeTempStorage(pParse, zRight); |
| 948 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 949 | break; |
| 950 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 951 | |
| 952 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 953 | ** PRAGMA temp_store_directory |
| 954 | ** PRAGMA temp_store_directory = ""|"directory_name" |
| 955 | ** |
| 956 | ** Return or set the local value of the temp_store_directory flag. Changing |
| 957 | ** the value sets a specific directory to be used for temporary files. |
| 958 | ** Setting to a null string reverts to the default temporary directory search. |
| 959 | ** If temporary directory is changed, then invalidateTempStorage. |
| 960 | ** |
| 961 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 962 | case PragTyp_TEMP_STORE_DIRECTORY: { |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 963 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 964 | returnSingleText(v, sqlite3_temp_directory); |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 965 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 966 | #ifndef SQLITE_OMIT_WSD |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 967 | if( zRight[0] ){ |
| 968 | int res; |
danielk1977 | fab1127 | 2008-09-16 14:38:02 +0000 | [diff] [blame] | 969 | rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 970 | if( rc!=SQLITE_OK || res==0 ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 971 | sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 972 | goto pragma_out; |
| 973 | } |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 974 | } |
danielk1977 | b06a0b6 | 2008-06-26 10:54:12 +0000 | [diff] [blame] | 975 | if( SQLITE_TEMP_STORE==0 |
| 976 | || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) |
| 977 | || (SQLITE_TEMP_STORE==2 && db->temp_store==1) |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 978 | ){ |
| 979 | invalidateTempStorage(pParse); |
| 980 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 981 | sqlite3_free(sqlite3_temp_directory); |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 982 | if( zRight[0] ){ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 983 | sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 984 | }else{ |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 985 | sqlite3_temp_directory = 0; |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 986 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 987 | #endif /* SQLITE_OMIT_WSD */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 988 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 989 | break; |
| 990 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 991 | |
drh | cc71645 | 2012-06-06 23:23:23 +0000 | [diff] [blame] | 992 | #if SQLITE_OS_WIN |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 993 | /* |
| 994 | ** PRAGMA data_store_directory |
| 995 | ** PRAGMA data_store_directory = ""|"directory_name" |
| 996 | ** |
| 997 | ** Return or set the local value of the data_store_directory flag. Changing |
| 998 | ** the value sets a specific directory to be used for database files that |
| 999 | ** were specified with a relative pathname. Setting to a null string reverts |
| 1000 | ** to the default database directory, which for database files specified with |
| 1001 | ** a relative path will probably be based on the current directory for the |
| 1002 | ** process. Database file specified with an absolute path are not impacted |
| 1003 | ** by this setting, regardless of its value. |
| 1004 | ** |
| 1005 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1006 | case PragTyp_DATA_STORE_DIRECTORY: { |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 1007 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1008 | returnSingleText(v, sqlite3_data_directory); |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 1009 | }else{ |
| 1010 | #ifndef SQLITE_OMIT_WSD |
| 1011 | if( zRight[0] ){ |
| 1012 | int res; |
| 1013 | rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 1014 | if( rc!=SQLITE_OK || res==0 ){ |
| 1015 | sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 1016 | goto pragma_out; |
| 1017 | } |
| 1018 | } |
| 1019 | sqlite3_free(sqlite3_data_directory); |
| 1020 | if( zRight[0] ){ |
| 1021 | sqlite3_data_directory = sqlite3_mprintf("%s", zRight); |
| 1022 | }else{ |
| 1023 | sqlite3_data_directory = 0; |
| 1024 | } |
| 1025 | #endif /* SQLITE_OMIT_WSD */ |
| 1026 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } |
drh | cc71645 | 2012-06-06 23:23:23 +0000 | [diff] [blame] | 1029 | #endif |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 1030 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1031 | #if SQLITE_ENABLE_LOCKING_STYLE |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 1032 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 1033 | ** PRAGMA [schema.]lock_proxy_file |
| 1034 | ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path" |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1035 | ** |
| 1036 | ** Return or set the value of the lock_proxy_file flag. Changing |
| 1037 | ** the value sets a specific file to be used for database access locks. |
| 1038 | ** |
| 1039 | */ |
| 1040 | case PragTyp_LOCK_PROXY_FILE: { |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1041 | if( !zRight ){ |
| 1042 | Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 1043 | char *proxy_file_path = NULL; |
| 1044 | sqlite3_file *pFile = sqlite3PagerFile(pPager); |
drh | c02372c | 2012-01-10 17:59:59 +0000 | [diff] [blame] | 1045 | sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1046 | &proxy_file_path); |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1047 | returnSingleText(v, proxy_file_path); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1048 | }else{ |
| 1049 | Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 1050 | sqlite3_file *pFile = sqlite3PagerFile(pPager); |
| 1051 | int res; |
| 1052 | if( zRight[0] ){ |
| 1053 | res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, |
| 1054 | zRight); |
| 1055 | } else { |
| 1056 | res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, |
| 1057 | NULL); |
| 1058 | } |
| 1059 | if( res!=SQLITE_OK ){ |
| 1060 | sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); |
| 1061 | goto pragma_out; |
| 1062 | } |
| 1063 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1066 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 1069 | ** PRAGMA [schema.]synchronous |
drh | 6841b1c | 2016-02-03 19:20:15 +0000 | [diff] [blame] | 1070 | ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1071 | ** |
| 1072 | ** Return or set the local value of the synchronous flag. Changing |
| 1073 | ** the local value does not make changes to the disk file and the |
| 1074 | ** default value will be restored the next time the database is |
| 1075 | ** opened. |
| 1076 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1077 | case PragTyp_SYNCHRONOUS: { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1078 | if( !zRight ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1079 | returnSingleInt(v, pDb->safety_level-1); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1080 | }else{ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1081 | if( !db->autoCommit ){ |
| 1082 | sqlite3ErrorMsg(pParse, |
| 1083 | "Safety level may not be changed inside a transaction"); |
drh | 7553c82 | 2017-03-15 14:04:03 +0000 | [diff] [blame] | 1084 | }else if( iDb!=1 ){ |
drh | d99d283 | 2015-04-17 15:58:33 +0000 | [diff] [blame] | 1085 | int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; |
| 1086 | if( iLevel==0 ) iLevel = 1; |
| 1087 | pDb->safety_level = iLevel; |
drh | 50a1a5a | 2016-03-08 14:40:11 +0000 | [diff] [blame] | 1088 | pDb->bSyncSet = 1; |
drh | d3605a4 | 2013-08-17 15:42:29 +0000 | [diff] [blame] | 1089 | setAllPagerFlags(db); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1090 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1091 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1092 | break; |
| 1093 | } |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1094 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1095 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 1096 | #ifndef SQLITE_OMIT_FLAG_PRAGMAS |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1097 | case PragTyp_FLAG: { |
| 1098 | if( zRight==0 ){ |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1099 | setPragmaResultColumnNames(v, pPragma); |
| 1100 | returnSingleInt(v, (db->flags & pPragma->iArg)!=0 ); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1101 | }else{ |
drh | fd748c6 | 2018-10-30 16:25:35 +0000 | [diff] [blame] | 1102 | u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1103 | if( db->autoCommit==0 ){ |
| 1104 | /* Foreign key support may not be enabled or disabled while not |
| 1105 | ** in auto-commit mode. */ |
| 1106 | mask &= ~(SQLITE_ForeignKeys); |
| 1107 | } |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 1108 | #if SQLITE_USER_AUTHENTICATION |
drh | b2445d5 | 2014-09-11 14:01:41 +0000 | [diff] [blame] | 1109 | if( db->auth.authLevel==UAUTH_User ){ |
drh | d453097 | 2014-09-09 14:47:53 +0000 | [diff] [blame] | 1110 | /* Do not allow non-admin users to modify the schema arbitrarily */ |
| 1111 | mask &= ~(SQLITE_WriteSchema); |
| 1112 | } |
| 1113 | #endif |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1114 | |
| 1115 | if( sqlite3GetBoolean(zRight, 0) ){ |
drh | 2eeca20 | 2020-01-08 20:37:45 +0000 | [diff] [blame] | 1116 | db->flags |= mask; |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1117 | }else{ |
| 1118 | db->flags &= ~mask; |
| 1119 | if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0; |
| 1120 | } |
| 1121 | |
| 1122 | /* Many of the flag-pragmas modify the code generated by the SQL |
| 1123 | ** compiler (eg. count_changes). So add an opcode to expire all |
| 1124 | ** compiled SQL statements after modifying a pragma value. |
| 1125 | */ |
drh | 01c736d | 2016-05-20 15:15:07 +0000 | [diff] [blame] | 1126 | sqlite3VdbeAddOp0(v, OP_Expire); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1127 | setAllPagerFlags(db); |
| 1128 | } |
| 1129 | break; |
| 1130 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 1131 | #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1132 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1133 | #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1134 | /* |
| 1135 | ** PRAGMA table_info(<table>) |
| 1136 | ** |
| 1137 | ** Return a single row for each column of the named table. The columns of |
| 1138 | ** the returned data set are: |
| 1139 | ** |
| 1140 | ** cid: Column id (numbered from left to right, starting at 0) |
| 1141 | ** name: Column name |
| 1142 | ** type: Column declaration type. |
| 1143 | ** notnull: True if 'NOT NULL' is part of column declaration |
| 1144 | ** dflt_value: The default value for the column, if any. |
dan | 3e6ac16 | 2016-02-11 21:01:16 +0000 | [diff] [blame] | 1145 | ** pk: Non-zero for PK fields. |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1146 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1147 | case PragTyp_TABLE_INFO: if( zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1148 | Table *pTab; |
drh | a78d2c0 | 2020-07-04 20:29:56 +0000 | [diff] [blame] | 1149 | sqlite3CodeVerifyNamedSchema(pParse, zDb); |
drh | 4d249e6 | 2016-06-10 22:49:01 +0000 | [diff] [blame] | 1150 | pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1151 | if( pTab ){ |
drh | 384b7fe | 2013-01-01 13:55:31 +0000 | [diff] [blame] | 1152 | int i, k; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 1153 | int nHidden = 0; |
drh | f7eece6 | 2006-02-06 21:34:27 +0000 | [diff] [blame] | 1154 | Column *pCol; |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1155 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
drh | d7dc0a3 | 2018-10-01 18:28:42 +0000 | [diff] [blame] | 1156 | pParse->nMem = 7; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1157 | sqlite3ViewGetColumnNames(pParse, pTab); |
drh | f7eece6 | 2006-02-06 21:34:27 +0000 | [diff] [blame] | 1158 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
drh | ab3c5f2 | 2019-10-17 13:15:40 +0000 | [diff] [blame] | 1159 | int isHidden = 0; |
| 1160 | if( pCol->colFlags & COLFLAG_NOINSERT ){ |
drh | 676fa25 | 2019-10-17 14:21:07 +0000 | [diff] [blame] | 1161 | if( pPragma->iArg==0 ){ |
| 1162 | nHidden++; |
| 1163 | continue; |
| 1164 | } |
drh | ab3c5f2 | 2019-10-17 13:15:40 +0000 | [diff] [blame] | 1165 | if( pCol->colFlags & COLFLAG_VIRTUAL ){ |
| 1166 | isHidden = 2; /* GENERATED ALWAYS AS ... VIRTUAL */ |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 1167 | }else if( pCol->colFlags & COLFLAG_STORED ){ |
drh | ab3c5f2 | 2019-10-17 13:15:40 +0000 | [diff] [blame] | 1168 | isHidden = 3; /* GENERATED ALWAYS AS ... STORED */ |
drh | c143114 | 2019-10-17 17:54:05 +0000 | [diff] [blame] | 1169 | }else{ assert( pCol->colFlags & COLFLAG_HIDDEN ); |
drh | ab3c5f2 | 2019-10-17 13:15:40 +0000 | [diff] [blame] | 1170 | isHidden = 1; /* HIDDEN */ |
| 1171 | } |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 1172 | } |
drh | 384b7fe | 2013-01-01 13:55:31 +0000 | [diff] [blame] | 1173 | if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ |
| 1174 | k = 0; |
| 1175 | }else if( pPk==0 ){ |
| 1176 | k = 1; |
| 1177 | }else{ |
drh | 1b67896 | 2015-04-15 07:19:27 +0000 | [diff] [blame] | 1178 | for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} |
drh | 384b7fe | 2013-01-01 13:55:31 +0000 | [diff] [blame] | 1179 | } |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1180 | assert( sqlite3ColumnExpr(pTab,pCol)==0 |
| 1181 | || sqlite3ColumnExpr(pTab,pCol)->op==TK_SPAN |
| 1182 | || isHidden>=2 ); |
drh | d7dc0a3 | 2018-10-01 18:28:42 +0000 | [diff] [blame] | 1183 | sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi", |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1184 | i-nHidden, |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1185 | pCol->zName, |
| 1186 | sqlite3ColumnType(pCol,""), |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1187 | pCol->notNull ? 1 : 0, |
drh | 79cf2b7 | 2021-07-31 20:30:41 +0000 | [diff] [blame] | 1188 | isHidden>=2 || sqlite3ColumnExpr(pTab,pCol)==0 ? 0 : |
| 1189 | sqlite3ColumnExpr(pTab,pCol)->u.zToken, |
drh | d7dc0a3 | 2018-10-01 18:28:42 +0000 | [diff] [blame] | 1190 | k, |
| 1191 | isHidden); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1192 | } |
| 1193 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1194 | } |
| 1195 | break; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1196 | |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 1197 | #ifdef SQLITE_DEBUG |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1198 | case PragTyp_STATS: { |
| 1199 | Index *pIdx; |
| 1200 | HashElem *i; |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 1201 | pParse->nMem = 5; |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1202 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1203 | for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){ |
| 1204 | Table *pTab = sqliteHashData(i); |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 1205 | sqlite3VdbeMultiLoad(v, 1, "ssiii", |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1206 | pTab->zName, |
| 1207 | 0, |
drh | d566c95 | 2016-02-25 21:19:03 +0000 | [diff] [blame] | 1208 | pTab->szTabRow, |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 1209 | pTab->nRowLogEst, |
| 1210 | pTab->tabFlags); |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1211 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 40cf27c | 2017-07-07 16:00:53 +0000 | [diff] [blame] | 1212 | sqlite3VdbeMultiLoad(v, 2, "siiiX", |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1213 | pIdx->zName, |
drh | d566c95 | 2016-02-25 21:19:03 +0000 | [diff] [blame] | 1214 | pIdx->szIdxRow, |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 1215 | pIdx->aiRowLogEst[0], |
| 1216 | pIdx->hasStat1); |
| 1217 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | break; |
drh | 33bec3f | 2017-02-17 13:38:15 +0000 | [diff] [blame] | 1222 | #endif |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1223 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1224 | case PragTyp_INDEX_INFO: if( zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1225 | Index *pIdx; |
| 1226 | Table *pTab; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 1227 | pIdx = sqlite3FindIndex(db, zRight, zDb); |
drh | 7b1904e | 2019-07-17 11:01:11 +0000 | [diff] [blame] | 1228 | if( pIdx==0 ){ |
| 1229 | /* If there is no index named zRight, check to see if there is a |
| 1230 | ** WITHOUT ROWID table named zRight, and if there is, show the |
| 1231 | ** structure of the PRIMARY KEY index for that table. */ |
| 1232 | pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb); |
| 1233 | if( pTab && !HasRowid(pTab) ){ |
| 1234 | pIdx = sqlite3PrimaryKeyIndex(pTab); |
| 1235 | } |
| 1236 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1237 | if( pIdx ){ |
dan | 3c42548 | 2018-11-20 18:09:59 +0000 | [diff] [blame] | 1238 | int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1239 | int i; |
drh | 5e7028c | 2015-03-05 14:29:02 +0000 | [diff] [blame] | 1240 | int mx; |
| 1241 | if( pPragma->iArg ){ |
| 1242 | /* PRAGMA index_xinfo (newer version with more rows and columns) */ |
| 1243 | mx = pIdx->nColumn; |
| 1244 | pParse->nMem = 6; |
| 1245 | }else{ |
| 1246 | /* PRAGMA index_info (legacy version) */ |
| 1247 | mx = pIdx->nKeyCol; |
| 1248 | pParse->nMem = 3; |
| 1249 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1250 | pTab = pIdx->pTable; |
dan | 3c42548 | 2018-11-20 18:09:59 +0000 | [diff] [blame] | 1251 | sqlite3CodeVerifySchema(pParse, iIdxDb); |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1252 | assert( pParse->nMem<=pPragma->nPragCName ); |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1253 | for(i=0; i<mx; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1254 | i16 cnum = pIdx->aiColumn[i]; |
drh | 40cf27c | 2017-07-07 16:00:53 +0000 | [diff] [blame] | 1255 | sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum, |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1256 | cnum<0 ? 0 : pTab->aCol[cnum].zName); |
drh | 5e7028c | 2015-03-05 14:29:02 +0000 | [diff] [blame] | 1257 | if( pPragma->iArg ){ |
drh | 40cf27c | 2017-07-07 16:00:53 +0000 | [diff] [blame] | 1258 | sqlite3VdbeMultiLoad(v, 4, "isiX", |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1259 | pIdx->aSortOrder[i], |
| 1260 | pIdx->azColl[i], |
| 1261 | i<pIdx->nKeyCol); |
drh | 5e7028c | 2015-03-05 14:29:02 +0000 | [diff] [blame] | 1262 | } |
| 1263 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1266 | } |
| 1267 | break; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1268 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1269 | case PragTyp_INDEX_LIST: if( zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1270 | Index *pIdx; |
| 1271 | Table *pTab; |
drh | e13e9f5 | 2013-10-05 19:18:00 +0000 | [diff] [blame] | 1272 | int i; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 1273 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1274 | if( pTab ){ |
dan | 3c42548 | 2018-11-20 18:09:59 +0000 | [diff] [blame] | 1275 | int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1276 | pParse->nMem = 5; |
dan | 3c42548 | 2018-11-20 18:09:59 +0000 | [diff] [blame] | 1277 | sqlite3CodeVerifySchema(pParse, iTabDb); |
drh | 3ef2615 | 2013-10-12 20:22:00 +0000 | [diff] [blame] | 1278 | for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){ |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1279 | const char *azOrigin[] = { "c", "u", "pk" }; |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1280 | sqlite3VdbeMultiLoad(v, 1, "isisi", |
| 1281 | i, |
| 1282 | pIdx->zName, |
| 1283 | IsUniqueIndex(pIdx), |
| 1284 | azOrigin[pIdx->idxType], |
| 1285 | pIdx->pPartIdxWhere!=0); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1286 | } |
| 1287 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1288 | } |
| 1289 | break; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1290 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1291 | case PragTyp_DATABASE_LIST: { |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1292 | int i; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1293 | pParse->nMem = 3; |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1294 | for(i=0; i<db->nDb; i++){ |
| 1295 | if( db->aDb[i].pBt==0 ) continue; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1296 | assert( db->aDb[i].zDbSName!=0 ); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1297 | sqlite3VdbeMultiLoad(v, 1, "iss", |
| 1298 | i, |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1299 | db->aDb[i].zDbSName, |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1300 | sqlite3BtreeGetFilename(db->aDb[i].pBt)); |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1301 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1302 | } |
| 1303 | break; |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1304 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1305 | case PragTyp_COLLATION_LIST: { |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1306 | int i = 0; |
| 1307 | HashElem *p; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1308 | pParse->nMem = 2; |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1309 | for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ |
| 1310 | CollSeq *pColl = (CollSeq *)sqliteHashData(p); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1311 | sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName); |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 1312 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1313 | } |
| 1314 | break; |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1315 | |
drh | cc3f3d1 | 2019-08-17 15:27:58 +0000 | [diff] [blame] | 1316 | #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1317 | case PragTyp_FUNCTION_LIST: { |
| 1318 | int i; |
| 1319 | HashElem *j; |
| 1320 | FuncDef *p; |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 1321 | int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0; |
drh | 79d5bc8 | 2020-01-04 01:43:02 +0000 | [diff] [blame] | 1322 | pParse->nMem = 6; |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1323 | for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){ |
| 1324 | for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){ |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 1325 | pragmaFunclistLine(v, p, 1, showInternFunc); |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){ |
| 1329 | p = (FuncDef*)sqliteHashData(j); |
drh | 337ca51 | 2020-01-04 19:58:28 +0000 | [diff] [blame] | 1330 | pragmaFunclistLine(v, p, 0, showInternFunc); |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | break; |
| 1334 | |
| 1335 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1336 | case PragTyp_MODULE_LIST: { |
| 1337 | HashElem *j; |
| 1338 | pParse->nMem = 1; |
| 1339 | for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){ |
| 1340 | Module *pMod = (Module*)sqliteHashData(j); |
| 1341 | sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName); |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | break; |
| 1345 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1346 | |
drh | 8ae11aa | 2017-07-07 17:33:07 +0000 | [diff] [blame] | 1347 | case PragTyp_PRAGMA_LIST: { |
| 1348 | int i; |
| 1349 | for(i=0; i<ArraySize(aPragmaName); i++){ |
| 1350 | sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName); |
drh | 8ae11aa | 2017-07-07 17:33:07 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | break; |
| 1354 | #endif /* SQLITE_INTROSPECTION_PRAGMAS */ |
drh | ab53bb6 | 2017-07-07 15:43:22 +0000 | [diff] [blame] | 1355 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1356 | #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ |
| 1357 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1358 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1359 | case PragTyp_FOREIGN_KEY_LIST: if( zRight ){ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1360 | FKey *pFK; |
| 1361 | Table *pTab; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 1362 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame^] | 1363 | if( pTab && !IsVirtual(pTab) ){ |
| 1364 | pFK = pTab->u.tab.pFKey; |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 1365 | if( pFK ){ |
dan | 3c42548 | 2018-11-20 18:09:59 +0000 | [diff] [blame] | 1366 | int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 1367 | int i = 0; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 1368 | pParse->nMem = 8; |
dan | 3c42548 | 2018-11-20 18:09:59 +0000 | [diff] [blame] | 1369 | sqlite3CodeVerifySchema(pParse, iTabDb); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 1370 | while(pFK){ |
| 1371 | int j; |
| 1372 | for(j=0; j<pFK->nCol; j++){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1373 | sqlite3VdbeMultiLoad(v, 1, "iissssss", |
| 1374 | i, |
| 1375 | j, |
| 1376 | pFK->zTo, |
| 1377 | pTab->aCol[pFK->aCol[j].iFrom].zName, |
| 1378 | pFK->aCol[j].zCol, |
| 1379 | actionName(pFK->aAction[1]), /* ON UPDATE */ |
| 1380 | actionName(pFK->aAction[0]), /* ON DELETE */ |
| 1381 | "NONE"); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 1382 | } |
| 1383 | ++i; |
| 1384 | pFK = pFK->pNextFrom; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1385 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1386 | } |
| 1387 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1388 | } |
| 1389 | break; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1390 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 1391 | |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1392 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
dan | 09ff9e1 | 2013-03-11 11:49:03 +0000 | [diff] [blame] | 1393 | #ifndef SQLITE_OMIT_TRIGGER |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1394 | case PragTyp_FOREIGN_KEY_CHECK: { |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1395 | FKey *pFK; /* A foreign key constraint */ |
| 1396 | Table *pTab; /* Child table contain "REFERENCES" keyword */ |
| 1397 | Table *pParent; /* Parent table that child points to */ |
| 1398 | Index *pIdx; /* Index in the parent table */ |
| 1399 | int i; /* Loop counter: Foreign key number for pTab */ |
| 1400 | int j; /* Loop counter: Field of the foreign key */ |
| 1401 | HashElem *k; /* Loop counter: Next table in schema */ |
| 1402 | int x; /* result variable */ |
| 1403 | int regResult; /* 3 registers to hold a result row */ |
| 1404 | int regKey; /* Register to hold key for checking the FK */ |
| 1405 | int regRow; /* Registers to hold a row from pTab */ |
| 1406 | int addrTop; /* Top of a loop checking foreign keys */ |
| 1407 | int addrOk; /* Jump here if the key is OK */ |
drh | 7d22a4d | 2012-12-17 22:32:14 +0000 | [diff] [blame] | 1408 | int *aiCols; /* child to parent column mapping */ |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1409 | |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1410 | regResult = pParse->nMem+1; |
drh | 4b4b473 | 2012-12-17 20:57:15 +0000 | [diff] [blame] | 1411 | pParse->nMem += 4; |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1412 | regKey = ++pParse->nMem; |
| 1413 | regRow = ++pParse->nMem; |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1414 | k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash); |
| 1415 | while( k ){ |
| 1416 | if( zRight ){ |
| 1417 | pTab = sqlite3LocateTable(pParse, 0, zRight, zDb); |
| 1418 | k = 0; |
| 1419 | }else{ |
| 1420 | pTab = (Table*)sqliteHashData(k); |
| 1421 | k = sqliteHashNext(k); |
| 1422 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame^] | 1423 | if( pTab==0 || IsVirtual(pTab) || pTab->u.tab.pFKey==0 ) continue; |
drh | ec1650a | 2020-07-03 12:15:59 +0000 | [diff] [blame] | 1424 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 1425 | zDb = db->aDb[iDb].zDbSName; |
| 1426 | sqlite3CodeVerifySchema(pParse, iDb); |
| 1427 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1428 | if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow; |
drh | ec1650a | 2020-07-03 12:15:59 +0000 | [diff] [blame] | 1429 | sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1430 | sqlite3VdbeLoadString(v, regResult, pTab->zName); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame^] | 1431 | assert( !IsVirtual(pTab) ); |
| 1432 | for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ |
dan | 5e87830 | 2013-10-12 19:06:48 +0000 | [diff] [blame] | 1433 | pParent = sqlite3FindTable(db, pFK->zTo, zDb); |
| 1434 | if( pParent==0 ) continue; |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1435 | pIdx = 0; |
drh | ec1650a | 2020-07-03 12:15:59 +0000 | [diff] [blame] | 1436 | sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName); |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1437 | x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0); |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1438 | if( x==0 ){ |
| 1439 | if( pIdx==0 ){ |
drh | ec1650a | 2020-07-03 12:15:59 +0000 | [diff] [blame] | 1440 | sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead); |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1441 | }else{ |
drh | ec1650a | 2020-07-03 12:15:59 +0000 | [diff] [blame] | 1442 | sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1443 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1444 | } |
| 1445 | }else{ |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1446 | k = 0; |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1447 | break; |
| 1448 | } |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1449 | } |
dan | 5e87830 | 2013-10-12 19:06:48 +0000 | [diff] [blame] | 1450 | assert( pParse->nErr>0 || pFK==0 ); |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1451 | if( pFK ) break; |
| 1452 | if( pParse->nTab<i ) pParse->nTab = i; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1453 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame^] | 1454 | assert( !IsVirtual(pTab) ); |
| 1455 | for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){ |
dan | 5e87830 | 2013-10-12 19:06:48 +0000 | [diff] [blame] | 1456 | pParent = sqlite3FindTable(db, pFK->zTo, zDb); |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1457 | pIdx = 0; |
drh | 7d22a4d | 2012-12-17 22:32:14 +0000 | [diff] [blame] | 1458 | aiCols = 0; |
dan | 5e87830 | 2013-10-12 19:06:48 +0000 | [diff] [blame] | 1459 | if( pParent ){ |
| 1460 | x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols); |
drh | d96e382 | 2020-09-16 19:48:23 +0000 | [diff] [blame] | 1461 | assert( x==0 || db->mallocFailed ); |
dan | 5e87830 | 2013-10-12 19:06:48 +0000 | [diff] [blame] | 1462 | } |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1463 | addrOk = sqlite3VdbeMakeLabel(pParse); |
dan | 4bee559 | 2017-04-17 18:42:33 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* Generate code to read the child key values into registers |
| 1466 | ** regRow..regRow+n. If any of the child key values are NULL, this |
| 1467 | ** row cannot cause an FK violation. Jump directly to addrOk in |
| 1468 | ** this case. */ |
drh | 4f16ff9 | 2021-07-07 16:48:24 +0000 | [diff] [blame] | 1469 | if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol; |
dan | 4bee559 | 2017-04-17 18:42:33 +0000 | [diff] [blame] | 1470 | for(j=0; j<pFK->nCol; j++){ |
| 1471 | int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom; |
| 1472 | sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j); |
| 1473 | sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v); |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1474 | } |
dan | 4bee559 | 2017-04-17 18:42:33 +0000 | [diff] [blame] | 1475 | |
| 1476 | /* Generate code to query the parent index for a matching parent |
| 1477 | ** key. If a match is found, jump to addrOk. */ |
| 1478 | if( pIdx ){ |
| 1479 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey, |
| 1480 | sqlite3IndexAffinityStr(db,pIdx), pFK->nCol); |
| 1481 | sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0); |
| 1482 | VdbeCoverage(v); |
| 1483 | }else if( pParent ){ |
| 1484 | int jmp = sqlite3VdbeCurrentAddr(v)+2; |
| 1485 | sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v); |
| 1486 | sqlite3VdbeGoto(v, addrOk); |
drh | d96e382 | 2020-09-16 19:48:23 +0000 | [diff] [blame] | 1487 | assert( pFK->nCol==1 || db->mallocFailed ); |
dan | 4bee559 | 2017-04-17 18:42:33 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* Generate code to report an FK violation to the caller. */ |
dan | 940464b | 2017-04-17 18:02:41 +0000 | [diff] [blame] | 1491 | if( HasRowid(pTab) ){ |
| 1492 | sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1); |
| 1493 | }else{ |
| 1494 | sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1); |
| 1495 | } |
drh | 40cf27c | 2017-07-07 16:00:53 +0000 | [diff] [blame] | 1496 | sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1); |
drh | 4b4b473 | 2012-12-17 20:57:15 +0000 | [diff] [blame] | 1497 | sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4); |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1498 | sqlite3VdbeResolveLabel(v, addrOk); |
drh | 7d22a4d | 2012-12-17 22:32:14 +0000 | [diff] [blame] | 1499 | sqlite3DbFree(db, aiCols); |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1500 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1501 | sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v); |
drh | 613028b | 2012-12-17 18:43:02 +0000 | [diff] [blame] | 1502 | sqlite3VdbeJumpHere(v, addrTop); |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1503 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1504 | } |
| 1505 | break; |
dan | 09ff9e1 | 2013-03-11 11:49:03 +0000 | [diff] [blame] | 1506 | #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
drh | 6c5b915 | 2012-12-17 16:46:37 +0000 | [diff] [blame] | 1507 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
| 1508 | |
drh | 08652b5 | 2019-05-08 17:27:18 +0000 | [diff] [blame] | 1509 | #ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1510 | /* Reinstall the LIKE and GLOB functions. The variant of LIKE |
| 1511 | ** used will be case sensitive or not depending on the RHS. |
| 1512 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1513 | case PragTyp_CASE_SENSITIVE_LIKE: { |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1514 | if( zRight ){ |
drh | 38d9c61 | 2012-01-31 14:24:47 +0000 | [diff] [blame] | 1515 | sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0)); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1516 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1517 | } |
| 1518 | break; |
drh | 08652b5 | 2019-05-08 17:27:18 +0000 | [diff] [blame] | 1519 | #endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1520 | |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1521 | #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 1522 | # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 1523 | #endif |
| 1524 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1525 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1526 | /* PRAGMA integrity_check |
| 1527 | ** PRAGMA integrity_check(N) |
| 1528 | ** PRAGMA quick_check |
| 1529 | ** PRAGMA quick_check(N) |
| 1530 | ** |
| 1531 | ** Verify the integrity of the database. |
| 1532 | ** |
| 1533 | ** The "quick_check" is reduced version of |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 1534 | ** integrity_check designed to detect most database corruption |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1535 | ** without the overhead of cross-checking indexes. Quick_check |
| 1536 | ** is linear time wherease integrity_check is O(NlogN). |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1537 | ** |
| 1538 | ** The maximum nubmer of errors is 100 by default. A different default |
| 1539 | ** can be specified using a numeric parameter N. |
| 1540 | ** |
| 1541 | ** Or, the parameter N can be the name of a table. In that case, only |
| 1542 | ** the one table named is verified. The freelist is only verified if |
| 1543 | ** the named table is "sqlite_schema" (or one of its aliases). |
| 1544 | ** |
| 1545 | ** All schemas are checked by default. To check just a single |
| 1546 | ** schema, use the form: |
| 1547 | ** |
| 1548 | ** PRAGMA schema.integrity_check; |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 1549 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1550 | case PragTyp_INTEGRITY_CHECK: { |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1551 | int i, j, addr, mxErr; |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1552 | Table *pObjTab = 0; /* Check only this one table, if not NULL */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1553 | |
drh | c522731 | 2011-10-13 17:09:01 +0000 | [diff] [blame] | 1554 | int isQuick = (sqlite3Tolower(zLeft[0])=='q'); |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 1555 | |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 1556 | /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check", |
| 1557 | ** then iDb is set to the index of the database identified by <db>. |
| 1558 | ** In this case, the integrity of database iDb only is verified by |
| 1559 | ** the VDBE created below. |
| 1560 | ** |
| 1561 | ** Otherwise, if the command was simply "PRAGMA integrity_check" (or |
| 1562 | ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb |
| 1563 | ** to -1 here, to indicate that the VDBE should verify the integrity |
| 1564 | ** of all attached databases. */ |
| 1565 | assert( iDb>=0 ); |
| 1566 | assert( iDb==0 || pId2->z ); |
| 1567 | if( pId2->z==0 ) iDb = -1; |
| 1568 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1569 | /* Initialize the VDBE program */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1570 | pParse->nMem = 6; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1571 | |
| 1572 | /* Set the maximum error count */ |
| 1573 | mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; |
| 1574 | if( zRight ){ |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1575 | if( sqlite3GetInt32(zRight, &mxErr) ){ |
| 1576 | if( mxErr<=0 ){ |
| 1577 | mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; |
| 1578 | } |
| 1579 | }else{ |
| 1580 | pObjTab = sqlite3LocateTable(pParse, 0, zRight, |
| 1581 | iDb>=0 ? db->aDb[iDb].zDbSName : 0); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1582 | } |
| 1583 | } |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 1584 | sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1585 | |
| 1586 | /* Do an integrity check on each database file */ |
| 1587 | for(i=0; i<db->nDb; i++){ |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1588 | HashElem *x; /* For looping over tables in the schema */ |
| 1589 | Hash *pTbls; /* Set of all tables in the schema */ |
| 1590 | int *aRoot; /* Array of root page numbers of all btrees */ |
| 1591 | int cnt = 0; /* Number of entries in aRoot[] */ |
| 1592 | int mxIdx = 0; /* Maximum number of indexes for any table */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1593 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 1594 | if( OMIT_TEMPDB && i==1 ) continue; |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 1595 | if( iDb>=0 && i!=iDb ) continue; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 1596 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 1597 | sqlite3CodeVerifySchema(pParse, i); |
| 1598 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1599 | /* Do an integrity check of the B-Tree |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1600 | ** |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1601 | ** Begin by finding the root pages numbers |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1602 | ** for all tables and indices in the database. |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1603 | */ |
dan | 5885e76 | 2012-07-16 10:06:12 +0000 | [diff] [blame] | 1604 | assert( sqlite3SchemaMutexHeld(db, i, 0) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1605 | pTbls = &db->aDb[i].pSchema->tblHash; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1606 | for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1607 | Table *pTab = sqliteHashData(x); /* Current table */ |
| 1608 | Index *pIdx; /* An index on pTab */ |
| 1609 | int nIdx; /* Number of indexes on pTab */ |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1610 | if( pObjTab && pObjTab!=pTab ) continue; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1611 | if( HasRowid(pTab) ) cnt++; |
drh | bb9b5f2 | 2016-03-19 00:35:02 +0000 | [diff] [blame] | 1612 | for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; } |
| 1613 | if( nIdx>mxIdx ) mxIdx = nIdx; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1614 | } |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1615 | if( cnt==0 ) continue; |
| 1616 | if( pObjTab ) cnt++; |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1617 | aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1)); |
| 1618 | if( aRoot==0 ) break; |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1619 | cnt = 0; |
| 1620 | if( pObjTab ) aRoot[++cnt] = 0; |
| 1621 | for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1622 | Table *pTab = sqliteHashData(x); |
| 1623 | Index *pIdx; |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1624 | if( pObjTab && pObjTab!=pTab ) continue; |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 1625 | if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1626 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 1627 | aRoot[++cnt] = pIdx->tnum; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
drh | b5c1063 | 2017-09-21 00:49:15 +0000 | [diff] [blame] | 1630 | aRoot[0] = cnt; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1631 | |
| 1632 | /* Make sure sufficient number of registers have been allocated */ |
drh | bb9b5f2 | 2016-03-19 00:35:02 +0000 | [diff] [blame] | 1633 | pParse->nMem = MAX( pParse->nMem, 8+mxIdx ); |
drh | 3963e58 | 2017-07-15 20:33:19 +0000 | [diff] [blame] | 1634 | sqlite3ClearTempRegCache(pParse); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1635 | |
| 1636 | /* Do the b-tree integrity checks */ |
drh | 98968b2 | 2016-03-15 22:00:39 +0000 | [diff] [blame] | 1637 | sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1638 | sqlite3VdbeChangeP5(v, (u8)i); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1639 | addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1640 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 1641 | sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName), |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1642 | P4_DYNAMIC); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1643 | sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3); |
| 1644 | integrityCheckResultRow(v); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1645 | sqlite3VdbeJumpHere(v, addr); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1646 | |
| 1647 | /* Make sure all the indices are constructed correctly. |
| 1648 | */ |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1649 | for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1650 | Table *pTab = sqliteHashData(x); |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1651 | Index *pIdx, *pPk; |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 1652 | Index *pPrior = 0; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1653 | int loopTop; |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1654 | int iDataCur, iIdxCur; |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 1655 | int r1 = -1; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1656 | |
drh | a3b2da9 | 2017-03-17 03:21:14 +0000 | [diff] [blame] | 1657 | if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */ |
drh | 17d2d59 | 2020-07-23 00:45:06 +0000 | [diff] [blame] | 1658 | if( pObjTab && pObjTab!=pTab ) continue; |
drh | 26198bb | 2013-10-31 11:15:09 +0000 | [diff] [blame] | 1659 | pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab); |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 1660 | sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0, |
drh | 6a53499 | 2013-11-16 20:13:39 +0000 | [diff] [blame] | 1661 | 1, 0, &iDataCur, &iIdxCur); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1662 | /* reg[7] counts the number of entries in the table. |
| 1663 | ** reg[8+i] counts the number of entries in the i-th index |
| 1664 | */ |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1665 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 7); |
drh | 8a9789b | 2013-08-01 03:36:59 +0000 | [diff] [blame] | 1666 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1667 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */ |
drh | 8a9789b | 2013-08-01 03:36:59 +0000 | [diff] [blame] | 1668 | } |
drh | bb9b5f2 | 2016-03-19 00:35:02 +0000 | [diff] [blame] | 1669 | assert( pParse->nMem>=8+j ); |
| 1670 | assert( sqlite3NoTempsInRange(pParse,1,7+j) ); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1671 | sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v); |
drh | 6fbe41a | 2013-10-30 20:22:55 +0000 | [diff] [blame] | 1672 | loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1); |
drh | 4011c44 | 2018-06-06 19:48:19 +0000 | [diff] [blame] | 1673 | if( !isQuick ){ |
| 1674 | /* Sanity check on record header decoding */ |
drh | 0b0b3a9 | 2019-10-17 18:35:57 +0000 | [diff] [blame] | 1675 | sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3); |
drh | 4011c44 | 2018-06-06 19:48:19 +0000 | [diff] [blame] | 1676 | sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 1677 | } |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1678 | /* Verify that all NOT NULL columns really are NOT NULL */ |
| 1679 | for(j=0; j<pTab->nCol; j++){ |
| 1680 | char *zErr; |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 1681 | int jmp2; |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1682 | if( j==pTab->iPKey ) continue; |
| 1683 | if( pTab->aCol[j].notNull==0 ) continue; |
| 1684 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3); |
drh | ebd70ee | 2019-12-09 15:52:07 +0000 | [diff] [blame] | 1685 | if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){ |
| 1686 | sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
| 1687 | } |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1688 | jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v); |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1689 | zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName, |
| 1690 | pTab->aCol[j].zName); |
| 1691 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1692 | integrityCheckResultRow(v); |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1693 | sqlite3VdbeJumpHere(v, jmp2); |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1694 | } |
drh | 8a284dc | 2017-02-22 14:15:37 +0000 | [diff] [blame] | 1695 | /* Verify CHECK constraints */ |
| 1696 | if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){ |
dan | 75f9558 | 2017-04-04 19:58:54 +0000 | [diff] [blame] | 1697 | ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0); |
| 1698 | if( db->mallocFailed==0 ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1699 | int addrCkFault = sqlite3VdbeMakeLabel(pParse); |
| 1700 | int addrCkOk = sqlite3VdbeMakeLabel(pParse); |
dan | 75f9558 | 2017-04-04 19:58:54 +0000 | [diff] [blame] | 1701 | char *zErr; |
| 1702 | int k; |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 1703 | pParse->iSelfTab = iDataCur + 1; |
dan | 75f9558 | 2017-04-04 19:58:54 +0000 | [diff] [blame] | 1704 | for(k=pCheck->nExpr-1; k>0; k--){ |
| 1705 | sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0); |
| 1706 | } |
| 1707 | sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, |
| 1708 | SQLITE_JUMPIFNULL); |
| 1709 | sqlite3VdbeResolveLabel(v, addrCkFault); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 1710 | pParse->iSelfTab = 0; |
dan | 75f9558 | 2017-04-04 19:58:54 +0000 | [diff] [blame] | 1711 | zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s", |
| 1712 | pTab->zName); |
| 1713 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1714 | integrityCheckResultRow(v); |
dan | 75f9558 | 2017-04-04 19:58:54 +0000 | [diff] [blame] | 1715 | sqlite3VdbeResolveLabel(v, addrCkOk); |
drh | 8a284dc | 2017-02-22 14:15:37 +0000 | [diff] [blame] | 1716 | } |
dan | 75f9558 | 2017-04-04 19:58:54 +0000 | [diff] [blame] | 1717 | sqlite3ExprListDelete(db, pCheck); |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1718 | } |
drh | 226cef4 | 2017-09-09 20:38:49 +0000 | [diff] [blame] | 1719 | if( !isQuick ){ /* Omit the remaining tests for quick_check */ |
drh | 226cef4 | 2017-09-09 20:38:49 +0000 | [diff] [blame] | 1720 | /* Validate index entries for the current row */ |
| 1721 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 1722 | int jmp2, jmp3, jmp4, jmp5; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1723 | int ckUniq = sqlite3VdbeMakeLabel(pParse); |
drh | 226cef4 | 2017-09-09 20:38:49 +0000 | [diff] [blame] | 1724 | if( pPk==pIdx ) continue; |
| 1725 | r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3, |
| 1726 | pPrior, r1); |
| 1727 | pPrior = pIdx; |
| 1728 | sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */ |
| 1729 | /* Verify that an index entry exists for the current table row */ |
| 1730 | jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1, |
| 1731 | pIdx->nColumn); VdbeCoverage(v); |
| 1732 | sqlite3VdbeLoadString(v, 3, "row "); |
| 1733 | sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3); |
| 1734 | sqlite3VdbeLoadString(v, 4, " missing from index "); |
| 1735 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); |
| 1736 | jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName); |
| 1737 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1738 | jmp4 = integrityCheckResultRow(v); |
drh | 226cef4 | 2017-09-09 20:38:49 +0000 | [diff] [blame] | 1739 | sqlite3VdbeJumpHere(v, jmp2); |
| 1740 | /* For UNIQUE indexes, verify that only one entry exists with the |
| 1741 | ** current key. The entry is unique if (1) any column is NULL |
| 1742 | ** or (2) the next entry has a different key */ |
| 1743 | if( IsUniqueIndex(pIdx) ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1744 | int uniqOk = sqlite3VdbeMakeLabel(pParse); |
drh | 226cef4 | 2017-09-09 20:38:49 +0000 | [diff] [blame] | 1745 | int jmp6; |
| 1746 | int kk; |
| 1747 | for(kk=0; kk<pIdx->nKeyCol; kk++){ |
| 1748 | int iCol = pIdx->aiColumn[kk]; |
| 1749 | assert( iCol!=XN_ROWID && iCol<pTab->nCol ); |
| 1750 | if( iCol>=0 && pTab->aCol[iCol].notNull ) continue; |
| 1751 | sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk); |
| 1752 | VdbeCoverage(v); |
| 1753 | } |
| 1754 | jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v); |
| 1755 | sqlite3VdbeGoto(v, uniqOk); |
| 1756 | sqlite3VdbeJumpHere(v, jmp6); |
| 1757 | sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1, |
| 1758 | pIdx->nKeyCol); VdbeCoverage(v); |
| 1759 | sqlite3VdbeLoadString(v, 3, "non-unique entry in index "); |
| 1760 | sqlite3VdbeGoto(v, jmp5); |
| 1761 | sqlite3VdbeResolveLabel(v, uniqOk); |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1762 | } |
drh | 226cef4 | 2017-09-09 20:38:49 +0000 | [diff] [blame] | 1763 | sqlite3VdbeJumpHere(v, jmp4); |
| 1764 | sqlite3ResolvePartIdxLabel(pParse, jmp3); |
drh | cefc87f | 2014-08-01 01:40:33 +0000 | [diff] [blame] | 1765 | } |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1766 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1767 | sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v); |
drh | 8a9789b | 2013-08-01 03:36:59 +0000 | [diff] [blame] | 1768 | sqlite3VdbeJumpHere(v, loopTop-1); |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1769 | if( !isQuick ){ |
| 1770 | sqlite3VdbeLoadString(v, 2, "wrong # of entries in index "); |
| 1771 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 1772 | if( pPk==pIdx ) continue; |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1773 | sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3); |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 1774 | addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v); |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1775 | sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); |
drh | 9ecd708 | 2017-09-10 01:06:05 +0000 | [diff] [blame] | 1776 | sqlite3VdbeLoadString(v, 4, pIdx->zName); |
| 1777 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3); |
| 1778 | integrityCheckResultRow(v); |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 1779 | sqlite3VdbeJumpHere(v, addr); |
drh | 8b174f2 | 2017-02-22 15:11:36 +0000 | [diff] [blame] | 1780 | } |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | } |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1784 | { |
| 1785 | static const int iLn = VDBE_OFFSET_LINENO(2); |
| 1786 | static const VdbeOpList endCode[] = { |
| 1787 | { OP_AddImm, 1, 0, 0}, /* 0 */ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 1788 | { OP_IfNotZero, 1, 4, 0}, /* 1 */ |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1789 | { OP_String8, 0, 3, 0}, /* 2 */ |
drh | 1b32554 | 2016-02-03 01:55:44 +0000 | [diff] [blame] | 1790 | { OP_ResultRow, 3, 1, 0}, /* 3 */ |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 1791 | { OP_Halt, 0, 0, 0}, /* 4 */ |
| 1792 | { OP_String8, 0, 3, 0}, /* 5 */ |
| 1793 | { OP_Goto, 0, 3, 0}, /* 6 */ |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1794 | }; |
| 1795 | VdbeOp *aOp; |
| 1796 | |
| 1797 | aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn); |
| 1798 | if( aOp ){ |
drh | 66accfc | 2017-02-22 18:04:42 +0000 | [diff] [blame] | 1799 | aOp[0].p2 = 1-mxErr; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1800 | aOp[2].p4type = P4_STATIC; |
| 1801 | aOp[2].p4.z = "ok"; |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 1802 | aOp[5].p4type = P4_STATIC; |
| 1803 | aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT); |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1804 | } |
drh | 74588ce | 2017-09-13 00:13:05 +0000 | [diff] [blame] | 1805 | sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2); |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1806 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1807 | } |
| 1808 | break; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1809 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 1810 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1811 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1812 | /* |
| 1813 | ** PRAGMA encoding |
| 1814 | ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" |
| 1815 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1816 | ** In its first form, this pragma returns the encoding of the main |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1817 | ** database. If the database is not initialized, it is initialized now. |
| 1818 | ** |
| 1819 | ** The second form of this pragma is a no-op if the main database file |
| 1820 | ** has not already been initialized. In this case it sets the default |
| 1821 | ** encoding that will be used for the main database file if a new file |
| 1822 | ** is created. If an existing main database file is opened, then the |
| 1823 | ** default text encoding for the existing database is used. |
| 1824 | ** |
| 1825 | ** In all cases new databases created using the ATTACH command are |
| 1826 | ** created to use the same default text encoding as the main database. If |
| 1827 | ** the main database has not been initialized and/or created when ATTACH |
| 1828 | ** is executed, this is done before the ATTACH operation. |
| 1829 | ** |
| 1830 | ** In the second form this pragma sets the text encoding to be used in |
| 1831 | ** new database files created using this database handle. It is only |
| 1832 | ** useful if invoked immediately after the main database i |
| 1833 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1834 | case PragTyp_ENCODING: { |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1835 | static const struct EncName { |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1836 | char *zName; |
| 1837 | u8 enc; |
| 1838 | } encnames[] = { |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 1839 | { "UTF8", SQLITE_UTF8 }, |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1840 | { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ |
| 1841 | { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ |
| 1842 | { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 1843 | { "UTF16le", SQLITE_UTF16LE }, |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 1844 | { "UTF16be", SQLITE_UTF16BE }, |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1845 | { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ |
| 1846 | { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1847 | { 0, 0 } |
| 1848 | }; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1849 | const struct EncName *pEnc; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1850 | if( !zRight ){ /* "PRAGMA encoding" */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 1851 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1852 | assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); |
| 1853 | assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); |
| 1854 | assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1855 | returnSingleText(v, encnames[ENC(pParse->db)].zName); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1856 | }else{ /* "PRAGMA encoding = XXX" */ |
| 1857 | /* Only change the value of sqlite.enc if the database handle is not |
| 1858 | ** initialized. If the main database exists, the new sqlite.enc value |
| 1859 | ** will be overwritten when the schema is next loaded. If it does not |
| 1860 | ** already exists, it will be created to use the new encoding value. |
| 1861 | */ |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 1862 | if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1863 | for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
| 1864 | if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ |
drh | 42a630b | 2020-03-05 16:13:24 +0000 | [diff] [blame] | 1865 | u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; |
| 1866 | SCHEMA_ENC(db) = enc; |
| 1867 | sqlite3SetTextEncoding(db, enc); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | } |
| 1870 | } |
| 1871 | if( !pEnc->zName ){ |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 1872 | sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1876 | } |
| 1877 | break; |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1878 | #endif /* SQLITE_OMIT_UTF16 */ |
| 1879 | |
| 1880 | #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1881 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 1882 | ** PRAGMA [schema.]schema_version |
| 1883 | ** PRAGMA [schema.]schema_version = <integer> |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1884 | ** |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 1885 | ** PRAGMA [schema.]user_version |
| 1886 | ** PRAGMA [schema.]user_version = <integer> |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1887 | ** |
drh | e459bd4 | 2016-03-16 20:05:57 +0000 | [diff] [blame] | 1888 | ** PRAGMA [schema.]freelist_count |
| 1889 | ** |
| 1890 | ** PRAGMA [schema.]data_version |
drh | 4ee09b4 | 2013-05-01 19:49:27 +0000 | [diff] [blame] | 1891 | ** |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 1892 | ** PRAGMA [schema.]application_id |
| 1893 | ** PRAGMA [schema.]application_id = <integer> |
drh | 4ee09b4 | 2013-05-01 19:49:27 +0000 | [diff] [blame] | 1894 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1895 | ** The pragma's schema_version and user_version are used to set or get |
| 1896 | ** the value of the schema-version and user-version, respectively. Both |
| 1897 | ** the schema-version and the user-version are 32-bit signed integers |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1898 | ** stored in the database header. |
| 1899 | ** |
| 1900 | ** The schema-cookie is usually only manipulated internally by SQLite. It |
| 1901 | ** is incremented by SQLite whenever the database schema is modified (by |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1902 | ** creating or dropping a table or index). The schema version is used by |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1903 | ** SQLite each time a query is executed to ensure that the internal cache |
| 1904 | ** of the schema used when compiling the SQL query matches the schema of |
| 1905 | ** the database against which the compiled query is actually executed. |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1906 | ** Subverting this mechanism by using "PRAGMA schema_version" to modify |
| 1907 | ** the schema-version is potentially dangerous and may lead to program |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1908 | ** crashes or database corruption. Use with caution! |
| 1909 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1910 | ** The user-version is not used internally by SQLite. It may be used by |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1911 | ** applications for any purpose. |
| 1912 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1913 | case PragTyp_HEADER_VALUE: { |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 1914 | int iCookie = pPragma->iArg; /* Which cookie to read or write */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1915 | sqlite3VdbeUsesBtree(v, iDb); |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 1916 | if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){ |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1917 | /* Write the specified cookie value */ |
| 1918 | static const VdbeOpList setCookie[] = { |
| 1919 | { OP_Transaction, 0, 1, 0}, /* 0 */ |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 1920 | { OP_SetCookie, 0, 0, 0}, /* 1 */ |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1921 | }; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1922 | VdbeOp *aOp; |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 1923 | sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie)); |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1924 | aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0); |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 1925 | if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1926 | aOp[0].p1 = iDb; |
drh | 1861afc | 2016-02-01 21:48:34 +0000 | [diff] [blame] | 1927 | aOp[1].p1 = iDb; |
| 1928 | aOp[1].p2 = iCookie; |
| 1929 | aOp[1].p3 = sqlite3Atoi(zRight); |
drh | e3863b5 | 2020-07-01 16:19:14 +0000 | [diff] [blame] | 1930 | aOp[1].p5 = 1; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1931 | }else{ |
| 1932 | /* Read the specified cookie value */ |
| 1933 | static const VdbeOpList readCookie[] = { |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 1934 | { OP_Transaction, 0, 0, 0}, /* 0 */ |
| 1935 | { OP_ReadCookie, 0, 1, 0}, /* 1 */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1936 | { OP_ResultRow, 1, 1, 0} |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1937 | }; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1938 | VdbeOp *aOp; |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 1939 | sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie)); |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1940 | aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0); |
drh | dad300d | 2016-01-18 00:20:26 +0000 | [diff] [blame] | 1941 | if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break; |
drh | 2ce1865 | 2016-01-16 20:50:21 +0000 | [diff] [blame] | 1942 | aOp[0].p1 = iDb; |
| 1943 | aOp[1].p1 = iDb; |
| 1944 | aOp[1].p3 = iCookie; |
drh | f71a366 | 2016-03-16 20:44:45 +0000 | [diff] [blame] | 1945 | sqlite3VdbeReusable(v); |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1946 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1947 | } |
| 1948 | break; |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1949 | #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1950 | |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1951 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 1952 | /* |
| 1953 | ** PRAGMA compile_options |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1954 | ** |
drh | 71caabf | 2010-02-26 15:39:24 +0000 | [diff] [blame] | 1955 | ** Return the names of all compile-time options used in this build, |
| 1956 | ** one option per row. |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1957 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1958 | case PragTyp_COMPILE_OPTIONS: { |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1959 | int i = 0; |
| 1960 | const char *zOpt; |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1961 | pParse->nMem = 1; |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1962 | while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1963 | sqlite3VdbeLoadString(v, 1, zOpt); |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1964 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 1965 | } |
drh | f71a366 | 2016-03-16 20:44:45 +0000 | [diff] [blame] | 1966 | sqlite3VdbeReusable(v); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1967 | } |
| 1968 | break; |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 1969 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 1970 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 1971 | #ifndef SQLITE_OMIT_WAL |
| 1972 | /* |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 1973 | ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 1974 | ** |
| 1975 | ** Checkpoint the database. |
| 1976 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1977 | case PragTyp_WAL_CHECKPOINT: { |
drh | 099b385 | 2021-03-10 16:35:37 +0000 | [diff] [blame] | 1978 | int iBt = (pId2->z?iDb:SQLITE_MAX_DB); |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1979 | int eMode = SQLITE_CHECKPOINT_PASSIVE; |
| 1980 | if( zRight ){ |
| 1981 | if( sqlite3StrICmp(zRight, "full")==0 ){ |
| 1982 | eMode = SQLITE_CHECKPOINT_FULL; |
| 1983 | }else if( sqlite3StrICmp(zRight, "restart")==0 ){ |
| 1984 | eMode = SQLITE_CHECKPOINT_RESTART; |
dan | f26a154 | 2014-12-02 19:04:54 +0000 | [diff] [blame] | 1985 | }else if( sqlite3StrICmp(zRight, "truncate")==0 ){ |
| 1986 | eMode = SQLITE_CHECKPOINT_TRUNCATE; |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1987 | } |
| 1988 | } |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1989 | pParse->nMem = 3; |
drh | 30aa3b9 | 2011-02-07 23:56:01 +0000 | [diff] [blame] | 1990 | sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1991 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 1992 | } |
| 1993 | break; |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 1994 | |
| 1995 | /* |
| 1996 | ** PRAGMA wal_autocheckpoint |
| 1997 | ** PRAGMA wal_autocheckpoint = N |
| 1998 | ** |
| 1999 | ** Configure a database connection to automatically checkpoint a database |
| 2000 | ** after accumulating N frames in the log. Or query for the current value |
| 2001 | ** of N. |
| 2002 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2003 | case PragTyp_WAL_AUTOCHECKPOINT: { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 2004 | if( zRight ){ |
drh | 60ac3f4 | 2010-11-23 18:59:27 +0000 | [diff] [blame] | 2005 | sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 2006 | } |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 2007 | returnSingleInt(v, |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 2008 | db->xWalCallback==sqlite3WalDefaultHook ? |
| 2009 | SQLITE_PTR_TO_INT(db->pWalArg) : 0); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2010 | } |
| 2011 | break; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2012 | #endif |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2013 | |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 2014 | /* |
| 2015 | ** PRAGMA shrink_memory |
| 2016 | ** |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 2017 | ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database |
| 2018 | ** connection on which it is invoked to free up as much memory as it |
| 2019 | ** can, by calling sqlite3_db_release_memory(). |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 2020 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2021 | case PragTyp_SHRINK_MEMORY: { |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 2022 | sqlite3_db_release_memory(db); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2023 | break; |
| 2024 | } |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 2025 | |
drh | f360396 | 2012-09-07 16:46:59 +0000 | [diff] [blame] | 2026 | /* |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2027 | ** PRAGMA optimize |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2028 | ** PRAGMA optimize(MASK) |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2029 | ** PRAGMA schema.optimize |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2030 | ** PRAGMA schema.optimize(MASK) |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2031 | ** |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2032 | ** Attempt to optimize the database. All schemas are optimized in the first |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2033 | ** two forms, and only the specified schema is optimized in the latter two. |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2034 | ** |
drh | e1f1c08 | 2017-03-06 20:44:13 +0000 | [diff] [blame] | 2035 | ** The details of optimizations performed by this pragma are expected |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2036 | ** to change and improve over time. Applications should anticipate that |
| 2037 | ** this pragma will perform new optimizations in future releases. |
| 2038 | ** |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2039 | ** The optional argument is a bitmask of optimizations to perform: |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2040 | ** |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2041 | ** 0x0001 Debugging mode. Do not actually perform any optimizations |
| 2042 | ** but instead return one line of text for each optimization |
| 2043 | ** that would have been done. Off by default. |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2044 | ** |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2045 | ** 0x0002 Run ANALYZE on tables that might benefit. On by default. |
| 2046 | ** See below for additional information. |
| 2047 | ** |
| 2048 | ** 0x0004 (Not yet implemented) Record usage and performance |
| 2049 | ** information from the current session in the |
| 2050 | ** database file so that it will be available to "optimize" |
| 2051 | ** pragmas run by future database connections. |
| 2052 | ** |
| 2053 | ** 0x0008 (Not yet implemented) Create indexes that might have |
| 2054 | ** been helpful to recent queries |
| 2055 | ** |
drh | e7c6f97 | 2017-07-15 20:25:22 +0000 | [diff] [blame] | 2056 | ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all |
| 2057 | ** of the optimizations listed above except Debug Mode, including new |
drh | 59dbe3a | 2017-03-06 23:51:16 +0000 | [diff] [blame] | 2058 | ** optimizations that have not yet been invented. If new optimizations are |
| 2059 | ** ever added that should be off by default, those off-by-default |
| 2060 | ** optimizations will have bitmasks of 0x10000 or larger. |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2061 | ** |
| 2062 | ** DETERMINATION OF WHEN TO RUN ANALYZE |
| 2063 | ** |
| 2064 | ** In the current implementation, a table is analyzed if only if all of |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2065 | ** the following are true: |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2066 | ** |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2067 | ** (1) MASK bit 0x02 is set. |
| 2068 | ** |
| 2069 | ** (2) The query planner used sqlite_stat1-style statistics for one or |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2070 | ** more indexes of the table at some point during the lifetime of |
| 2071 | ** the current connection. |
| 2072 | ** |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2073 | ** (3) One or more indexes of the table are currently unanalyzed OR |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2074 | ** the number of rows in the table has increased by 25 times or more |
| 2075 | ** since the last time ANALYZE was run. |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2076 | ** |
| 2077 | ** The rules for when tables are analyzed are likely to change in |
| 2078 | ** future releases. |
drh | 72052a7 | 2017-02-17 16:26:34 +0000 | [diff] [blame] | 2079 | */ |
drh | 2ead47c | 2017-02-22 20:24:10 +0000 | [diff] [blame] | 2080 | case PragTyp_OPTIMIZE: { |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2081 | int iDbLast; /* Loop termination point for the schema loop */ |
| 2082 | int iTabCur; /* Cursor for a table whose size needs checking */ |
| 2083 | HashElem *k; /* Loop over tables of a schema */ |
| 2084 | Schema *pSchema; /* The current schema */ |
| 2085 | Table *pTab; /* A table in the schema */ |
| 2086 | Index *pIdx; /* An index of the table */ |
| 2087 | LogEst szThreshold; /* Size threshold above which reanalysis is needd */ |
| 2088 | char *zSubSql; /* SQL statement for the OP_SqlExec opcode */ |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2089 | u32 opMask; /* Mask of operations to perform */ |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2090 | |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2091 | if( zRight ){ |
| 2092 | opMask = (u32)sqlite3Atoi(zRight); |
| 2093 | if( (opMask & 0x02)==0 ) break; |
| 2094 | }else{ |
drh | 59dbe3a | 2017-03-06 23:51:16 +0000 | [diff] [blame] | 2095 | opMask = 0xfffe; |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2096 | } |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2097 | iTabCur = pParse->nTab++; |
| 2098 | for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){ |
| 2099 | if( iDb==1 ) continue; |
| 2100 | sqlite3CodeVerifySchema(pParse, iDb); |
| 2101 | pSchema = db->aDb[iDb].pSchema; |
| 2102 | for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 2103 | pTab = (Table*)sqliteHashData(k); |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2104 | |
| 2105 | /* If table pTab has not been used in a way that would benefit from |
| 2106 | ** having analysis statistics during the current session, then skip it. |
| 2107 | ** This also has the effect of skipping virtual tables and views */ |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2108 | if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue; |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2109 | |
| 2110 | /* Reanalyze if the table is 25 times larger than the last analysis */ |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2111 | szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 ); |
| 2112 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2113 | if( !pIdx->hasStat1 ){ |
drh | 99d5b4c | 2017-02-18 22:52:40 +0000 | [diff] [blame] | 2114 | szThreshold = 0; /* Always analyze if any index lacks statistics */ |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | } |
| 2117 | } |
| 2118 | if( szThreshold ){ |
| 2119 | sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); |
| 2120 | sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2121 | sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold); |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2122 | VdbeCoverage(v); |
| 2123 | } |
| 2124 | zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"", |
| 2125 | db->aDb[iDb].zDbSName, pTab->zName); |
drh | 1cfaf8e | 2017-03-02 14:17:21 +0000 | [diff] [blame] | 2126 | if( opMask & 0x01 ){ |
| 2127 | int r1 = sqlite3GetTempReg(pParse); |
| 2128 | sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC); |
| 2129 | sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1); |
| 2130 | }else{ |
| 2131 | sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC); |
| 2132 | } |
drh | 4a54bb5 | 2017-02-18 15:58:52 +0000 | [diff] [blame] | 2133 | } |
| 2134 | } |
drh | bce0414 | 2017-02-23 00:58:36 +0000 | [diff] [blame] | 2135 | sqlite3VdbeAddOp0(v, OP_Expire); |
drh | 72052a7 | 2017-02-17 16:26:34 +0000 | [diff] [blame] | 2136 | break; |
| 2137 | } |
| 2138 | |
| 2139 | /* |
drh | f360396 | 2012-09-07 16:46:59 +0000 | [diff] [blame] | 2140 | ** PRAGMA busy_timeout |
| 2141 | ** PRAGMA busy_timeout = N |
| 2142 | ** |
| 2143 | ** Call sqlite3_busy_timeout(db, N). Return the current timeout value |
drh | c0c7b5e | 2012-09-07 18:49:57 +0000 | [diff] [blame] | 2144 | ** if one is set. If no busy handler or a different busy handler is set |
| 2145 | ** then 0 is returned. Setting the busy_timeout to 0 or negative |
| 2146 | ** disables the timeout. |
drh | f360396 | 2012-09-07 16:46:59 +0000 | [diff] [blame] | 2147 | */ |
drh | d49c358 | 2013-09-13 19:00:06 +0000 | [diff] [blame] | 2148 | /*case PragTyp_BUSY_TIMEOUT*/ default: { |
drh | c228be5 | 2015-01-31 02:00:01 +0000 | [diff] [blame] | 2149 | assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT ); |
drh | f360396 | 2012-09-07 16:46:59 +0000 | [diff] [blame] | 2150 | if( zRight ){ |
| 2151 | sqlite3_busy_timeout(db, sqlite3Atoi(zRight)); |
| 2152 | } |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 2153 | returnSingleInt(v, db->busyTimeout); |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2154 | break; |
| 2155 | } |
drh | f360396 | 2012-09-07 16:46:59 +0000 | [diff] [blame] | 2156 | |
drh | 55e85ca | 2013-09-13 21:01:56 +0000 | [diff] [blame] | 2157 | /* |
| 2158 | ** PRAGMA soft_heap_limit |
| 2159 | ** PRAGMA soft_heap_limit = N |
| 2160 | ** |
drh | 51a74d4 | 2015-02-28 01:04:27 +0000 | [diff] [blame] | 2161 | ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the |
| 2162 | ** sqlite3_soft_heap_limit64() interface with the argument N, if N is |
| 2163 | ** specified and is a non-negative integer. |
| 2164 | ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always |
| 2165 | ** returns the same integer that would be returned by the |
| 2166 | ** sqlite3_soft_heap_limit64(-1) C-language function. |
drh | 55e85ca | 2013-09-13 21:01:56 +0000 | [diff] [blame] | 2167 | */ |
| 2168 | case PragTyp_SOFT_HEAP_LIMIT: { |
| 2169 | sqlite3_int64 N; |
drh | 9296c18 | 2014-07-23 13:40:49 +0000 | [diff] [blame] | 2170 | if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ |
drh | 55e85ca | 2013-09-13 21:01:56 +0000 | [diff] [blame] | 2171 | sqlite3_soft_heap_limit64(N); |
| 2172 | } |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 2173 | returnSingleInt(v, sqlite3_soft_heap_limit64(-1)); |
drh | 55e85ca | 2013-09-13 21:01:56 +0000 | [diff] [blame] | 2174 | break; |
| 2175 | } |
| 2176 | |
drh | 0345961 | 2014-08-25 15:13:22 +0000 | [diff] [blame] | 2177 | /* |
drh | 10c0e71 | 2019-04-25 18:15:38 +0000 | [diff] [blame] | 2178 | ** PRAGMA hard_heap_limit |
| 2179 | ** PRAGMA hard_heap_limit = N |
| 2180 | ** |
| 2181 | ** Invoke sqlite3_hard_heap_limit64() to query or set the hard heap |
| 2182 | ** limit. The hard heap limit can be activated or lowered by this |
| 2183 | ** pragma, but not raised or deactivated. Only the |
| 2184 | ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate |
| 2185 | ** the hard heap limit. This allows an application to set a heap limit |
| 2186 | ** constraint that cannot be relaxed by an untrusted SQL script. |
| 2187 | */ |
| 2188 | case PragTyp_HARD_HEAP_LIMIT: { |
| 2189 | sqlite3_int64 N; |
| 2190 | if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){ |
| 2191 | sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1); |
| 2192 | if( N>0 && (iPrior==0 || iPrior>N) ) sqlite3_hard_heap_limit64(N); |
| 2193 | } |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 2194 | returnSingleInt(v, sqlite3_hard_heap_limit64(-1)); |
drh | 10c0e71 | 2019-04-25 18:15:38 +0000 | [diff] [blame] | 2195 | break; |
| 2196 | } |
| 2197 | |
| 2198 | /* |
drh | 0345961 | 2014-08-25 15:13:22 +0000 | [diff] [blame] | 2199 | ** PRAGMA threads |
| 2200 | ** PRAGMA threads = N |
| 2201 | ** |
| 2202 | ** Configure the maximum number of worker threads. Return the new |
| 2203 | ** maximum, which might be less than requested. |
| 2204 | */ |
| 2205 | case PragTyp_THREADS: { |
| 2206 | sqlite3_int64 N; |
drh | 111544c | 2014-08-29 16:20:47 +0000 | [diff] [blame] | 2207 | if( zRight |
drh | 0345961 | 2014-08-25 15:13:22 +0000 | [diff] [blame] | 2208 | && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK |
| 2209 | && N>=0 |
| 2210 | ){ |
drh | 111544c | 2014-08-29 16:20:47 +0000 | [diff] [blame] | 2211 | sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff)); |
drh | 0345961 | 2014-08-25 15:13:22 +0000 | [diff] [blame] | 2212 | } |
drh | c232aca | 2016-12-15 16:01:17 +0000 | [diff] [blame] | 2213 | returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1)); |
drh | 0345961 | 2014-08-25 15:13:22 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | } |
| 2216 | |
drh | 49a76a8 | 2020-03-31 20:57:06 +0000 | [diff] [blame] | 2217 | /* |
| 2218 | ** PRAGMA analysis_limit |
| 2219 | ** PRAGMA analysis_limit = N |
| 2220 | ** |
| 2221 | ** Configure the maximum number of rows that ANALYZE will examine |
| 2222 | ** in each index that it looks at. Return the new limit. |
| 2223 | */ |
| 2224 | case PragTyp_ANALYSIS_LIMIT: { |
| 2225 | sqlite3_int64 N; |
| 2226 | if( zRight |
| 2227 | && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK |
| 2228 | && N>=0 |
| 2229 | ){ |
| 2230 | db->nAnalysisLimit = (int)(N&0x7fffffff); |
| 2231 | } |
| 2232 | returnSingleInt(v, db->nAnalysisLimit); |
| 2233 | break; |
| 2234 | } |
| 2235 | |
dougcurrie | 81c95ef | 2004-06-18 23:21:47 +0000 | [diff] [blame] | 2236 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2237 | /* |
| 2238 | ** Report the current state of file logs for all databases |
| 2239 | */ |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2240 | case PragTyp_LOCK_STATUS: { |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 2241 | static const char *const azLockName[] = { |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2242 | "unlocked", "shared", "reserved", "pending", "exclusive" |
| 2243 | }; |
| 2244 | int i; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 2245 | pParse->nMem = 2; |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2246 | for(i=0; i<db->nDb; i++){ |
| 2247 | Btree *pBt; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 2248 | const char *zState = "unknown"; |
| 2249 | int j; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 2250 | if( db->aDb[i].zDbSName==0 ) continue; |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2251 | pBt = db->aDb[i].pBt; |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2252 | if( pBt==0 || sqlite3BtreePager(pBt)==0 ){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 2253 | zState = "closed"; |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 2254 | }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0, |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 2255 | SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ |
| 2256 | zState = azLockName[j]; |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2257 | } |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 2258 | sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2259 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2260 | break; |
| 2261 | } |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 2262 | #endif |
| 2263 | |
drh | b48c0d5 | 2020-02-07 01:12:53 +0000 | [diff] [blame] | 2264 | #if defined(SQLITE_ENABLE_CEROD) |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2265 | case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){ |
drh | 21e2cab | 2006-09-25 18:01:57 +0000 | [diff] [blame] | 2266 | if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ |
drh | 21e2cab | 2006-09-25 18:01:57 +0000 | [diff] [blame] | 2267 | sqlite3_activate_cerod(&zRight[6]); |
| 2268 | } |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2269 | } |
| 2270 | break; |
drh | 21e2cab | 2006-09-25 18:01:57 +0000 | [diff] [blame] | 2271 | #endif |
drh | 3c4f2a4 | 2005-12-08 18:12:56 +0000 | [diff] [blame] | 2272 | |
drh | 9ccd865 | 2013-09-13 16:36:46 +0000 | [diff] [blame] | 2273 | } /* End of the PRAGMA switch */ |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 2274 | |
dan | 9e1ab1a | 2017-01-05 19:32:48 +0000 | [diff] [blame] | 2275 | /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only |
| 2276 | ** purpose is to execute assert() statements to verify that if the |
| 2277 | ** PragFlg_NoColumns1 flag is set and the caller specified an argument |
| 2278 | ** to the PRAGMA, the implementation has not added any OP_ResultRow |
| 2279 | ** instructions to the VM. */ |
| 2280 | if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){ |
| 2281 | sqlite3VdbeVerifyNoResultRow(v); |
| 2282 | } |
| 2283 | |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 2284 | pragma_out: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2285 | sqlite3DbFree(db, zLeft); |
| 2286 | sqlite3DbFree(db, zRight); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 2287 | } |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2288 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2289 | /***************************************************************************** |
| 2290 | ** Implementation of an eponymous virtual table that runs a pragma. |
| 2291 | ** |
| 2292 | */ |
| 2293 | typedef struct PragmaVtab PragmaVtab; |
| 2294 | typedef struct PragmaVtabCursor PragmaVtabCursor; |
| 2295 | struct PragmaVtab { |
| 2296 | sqlite3_vtab base; /* Base class. Must be first */ |
| 2297 | sqlite3 *db; /* The database connection to which it belongs */ |
| 2298 | const PragmaName *pName; /* Name of the pragma */ |
| 2299 | u8 nHidden; /* Number of hidden columns */ |
| 2300 | u8 iHidden; /* Index of the first hidden column */ |
| 2301 | }; |
| 2302 | struct PragmaVtabCursor { |
| 2303 | sqlite3_vtab_cursor base; /* Base class. Must be first */ |
| 2304 | sqlite3_stmt *pPragma; /* The pragma statement to run */ |
| 2305 | sqlite_int64 iRowid; /* Current rowid */ |
| 2306 | char *azArg[2]; /* Value of the argument and schema */ |
| 2307 | }; |
| 2308 | |
| 2309 | /* |
| 2310 | ** Pragma virtual table module xConnect method. |
| 2311 | */ |
| 2312 | static int pragmaVtabConnect( |
| 2313 | sqlite3 *db, |
| 2314 | void *pAux, |
| 2315 | int argc, const char *const*argv, |
| 2316 | sqlite3_vtab **ppVtab, |
| 2317 | char **pzErr |
| 2318 | ){ |
| 2319 | const PragmaName *pPragma = (const PragmaName*)pAux; |
| 2320 | PragmaVtab *pTab = 0; |
| 2321 | int rc; |
| 2322 | int i, j; |
| 2323 | char cSep = '('; |
| 2324 | StrAccum acc; |
| 2325 | char zBuf[200]; |
| 2326 | |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 2327 | UNUSED_PARAMETER(argc); |
| 2328 | UNUSED_PARAMETER(argv); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2329 | sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2330 | sqlite3_str_appendall(&acc, "CREATE TABLE x"); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2331 | for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2332 | sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2333 | cSep = ','; |
| 2334 | } |
drh | 9a63f09 | 2016-12-16 02:14:15 +0000 | [diff] [blame] | 2335 | if( i==0 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2336 | sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName); |
drh | 9a63f09 | 2016-12-16 02:14:15 +0000 | [diff] [blame] | 2337 | i++; |
| 2338 | } |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2339 | j = 0; |
| 2340 | if( pPragma->mPragFlg & PragFlg_Result1 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2341 | sqlite3_str_appendall(&acc, ",arg HIDDEN"); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2342 | j++; |
| 2343 | } |
| 2344 | if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2345 | sqlite3_str_appendall(&acc, ",schema HIDDEN"); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2346 | j++; |
| 2347 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2348 | sqlite3_str_append(&acc, ")", 1); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2349 | sqlite3StrAccumFinish(&acc); |
| 2350 | assert( strlen(zBuf) < sizeof(zBuf)-1 ); |
| 2351 | rc = sqlite3_declare_vtab(db, zBuf); |
| 2352 | if( rc==SQLITE_OK ){ |
| 2353 | pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab)); |
| 2354 | if( pTab==0 ){ |
| 2355 | rc = SQLITE_NOMEM; |
| 2356 | }else{ |
| 2357 | memset(pTab, 0, sizeof(PragmaVtab)); |
| 2358 | pTab->pName = pPragma; |
| 2359 | pTab->db = db; |
| 2360 | pTab->iHidden = i; |
| 2361 | pTab->nHidden = j; |
| 2362 | } |
| 2363 | }else{ |
| 2364 | *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
| 2365 | } |
| 2366 | |
| 2367 | *ppVtab = (sqlite3_vtab*)pTab; |
| 2368 | return rc; |
| 2369 | } |
| 2370 | |
| 2371 | /* |
| 2372 | ** Pragma virtual table module xDisconnect method. |
| 2373 | */ |
| 2374 | static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){ |
| 2375 | PragmaVtab *pTab = (PragmaVtab*)pVtab; |
| 2376 | sqlite3_free(pTab); |
| 2377 | return SQLITE_OK; |
| 2378 | } |
| 2379 | |
| 2380 | /* Figure out the best index to use to search a pragma virtual table. |
| 2381 | ** |
| 2382 | ** There are not really any index choices. But we want to encourage the |
| 2383 | ** query planner to give == constraints on as many hidden parameters as |
| 2384 | ** possible, and especially on the first hidden parameter. So return a |
| 2385 | ** high cost if hidden parameters are unconstrained. |
| 2386 | */ |
| 2387 | static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 2388 | PragmaVtab *pTab = (PragmaVtab*)tab; |
| 2389 | const struct sqlite3_index_constraint *pConstraint; |
| 2390 | int i, j; |
| 2391 | int seen[2]; |
| 2392 | |
drh | ae7045c | 2016-12-15 21:33:55 +0000 | [diff] [blame] | 2393 | pIdxInfo->estimatedCost = (double)1; |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2394 | if( pTab->nHidden==0 ){ return SQLITE_OK; } |
| 2395 | pConstraint = pIdxInfo->aConstraint; |
| 2396 | seen[0] = 0; |
| 2397 | seen[1] = 0; |
| 2398 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 2399 | if( pConstraint->usable==0 ) continue; |
| 2400 | if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 2401 | if( pConstraint->iColumn < pTab->iHidden ) continue; |
| 2402 | j = pConstraint->iColumn - pTab->iHidden; |
| 2403 | assert( j < 2 ); |
drh | d7175eb | 2016-12-15 21:11:15 +0000 | [diff] [blame] | 2404 | seen[j] = i+1; |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2405 | } |
| 2406 | if( seen[0]==0 ){ |
| 2407 | pIdxInfo->estimatedCost = (double)2147483647; |
| 2408 | pIdxInfo->estimatedRows = 2147483647; |
| 2409 | return SQLITE_OK; |
| 2410 | } |
drh | d7175eb | 2016-12-15 21:11:15 +0000 | [diff] [blame] | 2411 | j = seen[0]-1; |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2412 | pIdxInfo->aConstraintUsage[j].argvIndex = 1; |
| 2413 | pIdxInfo->aConstraintUsage[j].omit = 1; |
| 2414 | if( seen[1]==0 ) return SQLITE_OK; |
| 2415 | pIdxInfo->estimatedCost = (double)20; |
| 2416 | pIdxInfo->estimatedRows = 20; |
drh | d7175eb | 2016-12-15 21:11:15 +0000 | [diff] [blame] | 2417 | j = seen[1]-1; |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2418 | pIdxInfo->aConstraintUsage[j].argvIndex = 2; |
| 2419 | pIdxInfo->aConstraintUsage[j].omit = 1; |
| 2420 | return SQLITE_OK; |
| 2421 | } |
| 2422 | |
| 2423 | /* Create a new cursor for the pragma virtual table */ |
| 2424 | static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){ |
| 2425 | PragmaVtabCursor *pCsr; |
| 2426 | pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr)); |
| 2427 | if( pCsr==0 ) return SQLITE_NOMEM; |
| 2428 | memset(pCsr, 0, sizeof(PragmaVtabCursor)); |
| 2429 | pCsr->base.pVtab = pVtab; |
| 2430 | *ppCursor = &pCsr->base; |
| 2431 | return SQLITE_OK; |
| 2432 | } |
| 2433 | |
| 2434 | /* Clear all content from pragma virtual table cursor. */ |
| 2435 | static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){ |
| 2436 | int i; |
| 2437 | sqlite3_finalize(pCsr->pPragma); |
| 2438 | pCsr->pPragma = 0; |
| 2439 | for(i=0; i<ArraySize(pCsr->azArg); i++){ |
| 2440 | sqlite3_free(pCsr->azArg[i]); |
| 2441 | pCsr->azArg[i] = 0; |
| 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | /* Close a pragma virtual table cursor */ |
| 2446 | static int pragmaVtabClose(sqlite3_vtab_cursor *cur){ |
| 2447 | PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur; |
| 2448 | pragmaVtabCursorClear(pCsr); |
drh | 9a63f09 | 2016-12-16 02:14:15 +0000 | [diff] [blame] | 2449 | sqlite3_free(pCsr); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2450 | return SQLITE_OK; |
| 2451 | } |
| 2452 | |
| 2453 | /* Advance the pragma virtual table cursor to the next row */ |
| 2454 | static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){ |
| 2455 | PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2456 | int rc = SQLITE_OK; |
| 2457 | |
| 2458 | /* Increment the xRowid value */ |
| 2459 | pCsr->iRowid++; |
drh | 9a63f09 | 2016-12-16 02:14:15 +0000 | [diff] [blame] | 2460 | assert( pCsr->pPragma ); |
| 2461 | if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){ |
| 2462 | rc = sqlite3_finalize(pCsr->pPragma); |
| 2463 | pCsr->pPragma = 0; |
| 2464 | pragmaVtabCursorClear(pCsr); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2465 | } |
| 2466 | return rc; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | ** Pragma virtual table module xFilter method. |
| 2471 | */ |
| 2472 | static int pragmaVtabFilter( |
| 2473 | sqlite3_vtab_cursor *pVtabCursor, |
| 2474 | int idxNum, const char *idxStr, |
| 2475 | int argc, sqlite3_value **argv |
| 2476 | ){ |
| 2477 | PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2478 | PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); |
| 2479 | int rc; |
drh | d8b7200 | 2016-12-16 04:20:27 +0000 | [diff] [blame] | 2480 | int i, j; |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2481 | StrAccum acc; |
| 2482 | char *zSql; |
| 2483 | |
drh | 344a1bf | 2016-12-22 14:53:25 +0000 | [diff] [blame] | 2484 | UNUSED_PARAMETER(idxNum); |
| 2485 | UNUSED_PARAMETER(idxStr); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2486 | pragmaVtabCursorClear(pCsr); |
drh | d8b7200 | 2016-12-16 04:20:27 +0000 | [diff] [blame] | 2487 | j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1; |
| 2488 | for(i=0; i<argc; i++, j++){ |
dan | d8ecefa | 2017-07-15 20:48:30 +0000 | [diff] [blame] | 2489 | const char *zText = (const char*)sqlite3_value_text(argv[i]); |
drh | d8b7200 | 2016-12-16 04:20:27 +0000 | [diff] [blame] | 2490 | assert( j<ArraySize(pCsr->azArg) ); |
dan | d8ecefa | 2017-07-15 20:48:30 +0000 | [diff] [blame] | 2491 | assert( pCsr->azArg[j]==0 ); |
| 2492 | if( zText ){ |
| 2493 | pCsr->azArg[j] = sqlite3_mprintf("%s", zText); |
| 2494 | if( pCsr->azArg[j]==0 ){ |
| 2495 | return SQLITE_NOMEM; |
| 2496 | } |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2497 | } |
| 2498 | } |
drh | d7175eb | 2016-12-15 21:11:15 +0000 | [diff] [blame] | 2499 | sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]); |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2500 | sqlite3_str_appendall(&acc, "PRAGMA "); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2501 | if( pCsr->azArg[1] ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2502 | sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2503 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2504 | sqlite3_str_appendall(&acc, pTab->pName->zName); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2505 | if( pCsr->azArg[0] ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 +0000 | [diff] [blame] | 2506 | sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2507 | } |
| 2508 | zSql = sqlite3StrAccumFinish(&acc); |
| 2509 | if( zSql==0 ) return SQLITE_NOMEM; |
| 2510 | rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0); |
| 2511 | sqlite3_free(zSql); |
| 2512 | if( rc!=SQLITE_OK ){ |
| 2513 | pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); |
| 2514 | return rc; |
| 2515 | } |
| 2516 | return pragmaVtabNext(pVtabCursor); |
| 2517 | } |
| 2518 | |
| 2519 | /* |
| 2520 | ** Pragma virtual table module xEof method. |
| 2521 | */ |
| 2522 | static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){ |
| 2523 | PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2524 | return (pCsr->pPragma==0); |
| 2525 | } |
| 2526 | |
| 2527 | /* The xColumn method simply returns the corresponding column from |
| 2528 | ** the PRAGMA. |
| 2529 | */ |
| 2530 | static int pragmaVtabColumn( |
| 2531 | sqlite3_vtab_cursor *pVtabCursor, |
| 2532 | sqlite3_context *ctx, |
| 2533 | int i |
| 2534 | ){ |
| 2535 | PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2536 | PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab); |
| 2537 | if( i<pTab->iHidden ){ |
| 2538 | sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i)); |
| 2539 | }else{ |
| 2540 | sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT); |
| 2541 | } |
| 2542 | return SQLITE_OK; |
| 2543 | } |
| 2544 | |
| 2545 | /* |
| 2546 | ** Pragma virtual table module xRowid method. |
| 2547 | */ |
| 2548 | static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){ |
| 2549 | PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor; |
| 2550 | *p = pCsr->iRowid; |
| 2551 | return SQLITE_OK; |
| 2552 | } |
| 2553 | |
| 2554 | /* The pragma virtual table object */ |
| 2555 | static const sqlite3_module pragmaVtabModule = { |
| 2556 | 0, /* iVersion */ |
| 2557 | 0, /* xCreate - create a table */ |
| 2558 | pragmaVtabConnect, /* xConnect - connect to an existing table */ |
| 2559 | pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */ |
| 2560 | pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */ |
| 2561 | 0, /* xDestroy - Drop a table */ |
| 2562 | pragmaVtabOpen, /* xOpen - open a cursor */ |
| 2563 | pragmaVtabClose, /* xClose - close a cursor */ |
| 2564 | pragmaVtabFilter, /* xFilter - configure scan constraints */ |
| 2565 | pragmaVtabNext, /* xNext - advance a cursor */ |
| 2566 | pragmaVtabEof, /* xEof */ |
| 2567 | pragmaVtabColumn, /* xColumn - read data */ |
| 2568 | pragmaVtabRowid, /* xRowid - read data */ |
| 2569 | 0, /* xUpdate - write data */ |
| 2570 | 0, /* xBegin - begin transaction */ |
| 2571 | 0, /* xSync - sync transaction */ |
| 2572 | 0, /* xCommit - commit transaction */ |
| 2573 | 0, /* xRollback - rollback transaction */ |
| 2574 | 0, /* xFindFunction - function overloading */ |
| 2575 | 0, /* xRename - rename the table */ |
| 2576 | 0, /* xSavepoint */ |
| 2577 | 0, /* xRelease */ |
drh | 84c501b | 2018-11-05 23:01:45 +0000 | [diff] [blame] | 2578 | 0, /* xRollbackTo */ |
| 2579 | 0 /* xShadowName */ |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2580 | }; |
| 2581 | |
| 2582 | /* |
| 2583 | ** Check to see if zTabName is really the name of a pragma. If it is, |
| 2584 | ** then register an eponymous virtual table for that pragma and return |
| 2585 | ** a pointer to the Module object for the new virtual table. |
| 2586 | */ |
| 2587 | Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){ |
| 2588 | const PragmaName *pName; |
| 2589 | assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 ); |
| 2590 | pName = pragmaLocate(zName+7); |
| 2591 | if( pName==0 ) return 0; |
| 2592 | if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0; |
| 2593 | assert( sqlite3HashFind(&db->aModule, zName)==0 ); |
drh | d7175eb | 2016-12-15 21:11:15 +0000 | [diff] [blame] | 2594 | return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0); |
drh | 2fcc159 | 2016-12-15 20:59:03 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 2598 | |
drh | 8bfdf72 | 2009-06-19 14:06:03 +0000 | [diff] [blame] | 2599 | #endif /* SQLITE_OMIT_PRAGMA */ |