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