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