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. |
| 13 | ** |
drh | 1086172 | 2009-04-07 00:49:16 +0000 | [diff] [blame^] | 14 | ** $Id: pragma.c,v 1.208 2009/04/07 00:49:16 drh Exp $ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
| 17 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 18 | /* Ignore this whole file if pragmas are disabled |
| 19 | */ |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 20 | #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER) |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 21 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 22 | /* |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 23 | ** Interpret the given string as a safety level. Return 0 for OFF, |
jplyon | b1639ff | 2004-01-19 04:52:29 +0000 | [diff] [blame] | 24 | ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or |
| 25 | ** unrecognized string argument. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 26 | ** |
| 27 | ** Note that the values returned are one less that the values that |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 28 | ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 29 | ** to support legacy SQL code. The safety level used to be boolean |
| 30 | ** and older scripts may have used numbers 0 for OFF and 1 for ON. |
| 31 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 32 | static u8 getSafetyLevel(const char *z){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 33 | /* 123456789 123456789 */ |
| 34 | static const char zText[] = "onoffalseyestruefull"; |
| 35 | static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16}; |
| 36 | static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4}; |
| 37 | static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2}; |
| 38 | int i, n; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 39 | if( sqlite3Isdigit(*z) ){ |
drh | 3abbd39 | 2008-12-10 23:04:13 +0000 | [diff] [blame] | 40 | return (u8)atoi(z); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 41 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 42 | n = sqlite3Strlen30(z); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 43 | for(i=0; i<ArraySize(iLength); i++){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 44 | if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){ |
| 45 | return iValue[i]; |
| 46 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 47 | } |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | /* |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 52 | ** Interpret the given string as a boolean value. |
| 53 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 54 | static u8 getBoolean(const char *z){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 55 | return getSafetyLevel(z)&1; |
| 56 | } |
| 57 | |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 58 | /* |
| 59 | ** Interpret the given string as a locking mode value. |
| 60 | */ |
| 61 | static int getLockingMode(const char *z){ |
| 62 | if( z ){ |
| 63 | if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE; |
| 64 | if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL; |
| 65 | } |
| 66 | return PAGER_LOCKINGMODE_QUERY; |
| 67 | } |
| 68 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 69 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 70 | /* |
| 71 | ** Interpret the given string as an auto-vacuum mode value. |
| 72 | ** |
| 73 | ** The following strings, "none", "full" and "incremental" are |
| 74 | ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. |
| 75 | */ |
| 76 | static int getAutoVacuum(const char *z){ |
| 77 | int i; |
| 78 | if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE; |
| 79 | if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL; |
| 80 | if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR; |
| 81 | i = atoi(z); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 82 | return (u8)((i>=0&&i<=2)?i:0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 83 | } |
| 84 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 85 | |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 86 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 87 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 88 | ** Interpret the given string as a temp db location. Return 1 for file |
| 89 | ** backed temporary databases, 2 for the Red-Black tree in memory database |
| 90 | ** and 0 to use the compile-time default. |
| 91 | */ |
| 92 | static int getTempStore(const char *z){ |
| 93 | if( z[0]>='0' && z[0]<='2' ){ |
| 94 | return z[0] - '0'; |
| 95 | }else if( sqlite3StrICmp(z, "file")==0 ){ |
| 96 | return 1; |
| 97 | }else if( sqlite3StrICmp(z, "memory")==0 ){ |
| 98 | return 2; |
| 99 | }else{ |
| 100 | return 0; |
| 101 | } |
| 102 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 103 | #endif /* SQLITE_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 104 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 105 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 106 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 107 | ** Invalidate temp storage, either when the temp storage is changed |
| 108 | ** from default, or when 'file' and the temp_store_directory has changed |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 109 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 110 | static int invalidateTempStorage(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 111 | sqlite3 *db = pParse->db; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 112 | if( db->aDb[1].pBt!=0 ){ |
danielk1977 | 983e230 | 2008-07-08 07:35:51 +0000 | [diff] [blame] | 113 | if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 114 | sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " |
| 115 | "from within a transaction"); |
| 116 | return SQLITE_ERROR; |
| 117 | } |
| 118 | sqlite3BtreeClose(db->aDb[1].pBt); |
| 119 | db->aDb[1].pBt = 0; |
| 120 | sqlite3ResetInternalSchema(db, 0); |
| 121 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 122 | return SQLITE_OK; |
| 123 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 124 | #endif /* SQLITE_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 125 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 126 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 127 | /* |
| 128 | ** If the TEMP database is open, close it and mark the database schema |
danielk1977 | b06a0b6 | 2008-06-26 10:54:12 +0000 | [diff] [blame] | 129 | ** as needing reloading. This must be done when using the SQLITE_TEMP_STORE |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 130 | ** or DEFAULT_TEMP_STORE pragmas. |
| 131 | */ |
| 132 | static int changeTempStorage(Parse *pParse, const char *zStorageType){ |
| 133 | int ts = getTempStore(zStorageType); |
| 134 | sqlite3 *db = pParse->db; |
| 135 | if( db->temp_store==ts ) return SQLITE_OK; |
| 136 | if( invalidateTempStorage( pParse ) != SQLITE_OK ){ |
| 137 | return SQLITE_ERROR; |
| 138 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 139 | db->temp_store = (u8)ts; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 140 | return SQLITE_OK; |
| 141 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 142 | #endif /* SQLITE_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | ** Generate code to return a single integer value. |
| 146 | */ |
drh | 3c71364 | 2009-04-04 16:02:32 +0000 | [diff] [blame] | 147 | static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){ |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 148 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 149 | int mem = ++pParse->nMem; |
drh | 3c71364 | 2009-04-04 16:02:32 +0000 | [diff] [blame] | 150 | i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value)); |
| 151 | if( pI64 ){ |
| 152 | memcpy(pI64, &value, sizeof(value)); |
| 153 | } |
| 154 | sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64); |
drh | 1086172 | 2009-04-07 00:49:16 +0000 | [diff] [blame^] | 155 | sqlite3VdbeSetNumCols(v, 1); |
| 156 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 157 | sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 158 | } |
| 159 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 160 | #ifndef SQLITE_OMIT_FLAG_PRAGMAS |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 161 | /* |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 162 | ** Check to see if zRight and zLeft refer to a pragma that queries |
| 163 | ** or changes one of the flags in db->flags. Return 1 if so and 0 if not. |
| 164 | ** Also, implement the pragma. |
| 165 | */ |
| 166 | static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 167 | static const struct sPragmaType { |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 168 | const char *zName; /* Name of the pragma */ |
| 169 | int mask; /* Mask for the db->flags value */ |
| 170 | } aPragma[] = { |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 171 | { "full_column_names", SQLITE_FullColNames }, |
| 172 | { "short_column_names", SQLITE_ShortColNames }, |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 173 | { "count_changes", SQLITE_CountRows }, |
| 174 | { "empty_result_callbacks", SQLITE_NullCallback }, |
drh | e321c29 | 2006-01-12 01:56:43 +0000 | [diff] [blame] | 175 | { "legacy_file_format", SQLITE_LegacyFileFmt }, |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 176 | { "fullfsync", SQLITE_FullFSync }, |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 177 | { "reverse_unordered_selects", SQLITE_ReverseOrder }, |
drh | cf1023c | 2007-05-08 20:59:49 +0000 | [diff] [blame] | 178 | #ifdef SQLITE_DEBUG |
| 179 | { "sql_trace", SQLITE_SqlTrace }, |
| 180 | { "vdbe_listing", SQLITE_VdbeListing }, |
| 181 | { "vdbe_trace", SQLITE_VdbeTrace }, |
| 182 | #endif |
drh | 0cd2d4c | 2005-11-03 02:15:02 +0000 | [diff] [blame] | 183 | #ifndef SQLITE_OMIT_CHECK |
| 184 | { "ignore_check_constraints", SQLITE_IgnoreChecks }, |
| 185 | #endif |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 186 | /* The following is VERY experimental */ |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 187 | { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode }, |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 188 | { "omit_readlock", SQLITE_NoReadlock }, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 189 | |
| 190 | /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted |
| 191 | ** flag if there are any active statements. */ |
| 192 | { "read_uncommitted", SQLITE_ReadUncommitted }, |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 193 | }; |
| 194 | int i; |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 195 | const struct sPragmaType *p; |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 196 | for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 197 | if( sqlite3StrICmp(zLeft, p->zName)==0 ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 198 | sqlite3 *db = pParse->db; |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 199 | Vdbe *v; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 200 | v = sqlite3GetVdbe(pParse); |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 201 | assert( v!=0 ); /* Already allocated by sqlite3Pragma() */ |
| 202 | if( ALWAYS(v) ){ |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 203 | if( zRight==0 ){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 204 | returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 ); |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 205 | }else{ |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 206 | if( getBoolean(zRight) ){ |
| 207 | db->flags |= p->mask; |
| 208 | }else{ |
| 209 | db->flags &= ~p->mask; |
| 210 | } |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 211 | |
| 212 | /* Many of the flag-pragmas modify the code generated by the SQL |
| 213 | ** compiler (eg. count_changes). So add an opcode to expire all |
| 214 | ** compiled SQL statements after modifying a pragma value. |
| 215 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 216 | sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 217 | } |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 218 | } |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 219 | |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 220 | return 1; |
| 221 | } |
| 222 | } |
| 223 | return 0; |
| 224 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 225 | #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 226 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 227 | /* |
| 228 | ** Return a human-readable name for a constraint resolution action. |
| 229 | */ |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 230 | static const char *actionName(u8 action){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 231 | const char *zName; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 232 | switch( action ){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 233 | case OE_SetNull: zName = "SET NULL"; break; |
| 234 | case OE_SetDflt: zName = "SET DEFAULT"; break; |
| 235 | case OE_Cascade: zName = "CASCADE"; break; |
| 236 | default: zName = "RESTRICT"; |
| 237 | assert( action==OE_Restrict ); break; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 238 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 239 | return zName; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 240 | } |
| 241 | |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 242 | /* |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 243 | ** Process a pragma statement. |
| 244 | ** |
| 245 | ** Pragmas are of this form: |
| 246 | ** |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 247 | ** PRAGMA [database.]id [= value] |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 248 | ** |
| 249 | ** The identifier might also be a string. The value is a string, and |
| 250 | ** identifier, or a number. If minusFlag is true, then the value is |
| 251 | ** a number that was preceded by a minus sign. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 252 | ** |
| 253 | ** If the left side is "database.id" then pId1 is the database name |
| 254 | ** and pId2 is the id. If the left side is just "id" then pId1 is the |
| 255 | ** id and pId2 is any empty string. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 256 | */ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 257 | void sqlite3Pragma( |
| 258 | Parse *pParse, |
| 259 | Token *pId1, /* First part of [database.]id field */ |
| 260 | Token *pId2, /* Second part of [database.]id field, or NULL */ |
| 261 | Token *pValue, /* Token for <value>, or NULL */ |
| 262 | int minusFlag /* True if a '-' sign preceded <value> */ |
| 263 | ){ |
| 264 | char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ |
| 265 | char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ |
| 266 | const char *zDb = 0; /* The database name */ |
| 267 | Token *pId; /* Pointer to <id> token */ |
| 268 | int iDb; /* Database index for <database> */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 269 | sqlite3 *db = pParse->db; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 270 | Db *pDb; |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame] | 271 | Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 272 | if( v==0 ) return; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 273 | pParse->nMem = 2; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 274 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 275 | /* Interpret the [database.] part of the pragma statement. iDb is the |
| 276 | ** index of the database this pragma is being applied to in db.aDb[]. */ |
| 277 | iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); |
| 278 | if( iDb<0 ) return; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 279 | pDb = &db->aDb[iDb]; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 280 | |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 281 | /* If the temp database has been explicitly named as part of the |
| 282 | ** pragma, make sure it is open. |
| 283 | */ |
| 284 | if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ |
| 285 | return; |
| 286 | } |
| 287 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 288 | zLeft = sqlite3NameFromToken(db, pId); |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 289 | if( !zLeft ) return; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 290 | if( minusFlag ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 291 | zRight = sqlite3MPrintf(db, "-%T", pValue); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 292 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 293 | zRight = sqlite3NameFromToken(db, pValue); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 294 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 295 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 296 | assert( pId2 ); |
| 297 | zDb = pId2->n>0 ? pDb->zName : 0; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 298 | if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 299 | goto pragma_out; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 302 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 303 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 304 | ** PRAGMA [database.]default_cache_size |
| 305 | ** PRAGMA [database.]default_cache_size=N |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 306 | ** |
| 307 | ** The first form reports the current persistent setting for the |
| 308 | ** page cache size. The value returned is the maximum number of |
| 309 | ** pages in the page cache. The second form sets both the current |
| 310 | ** page cache size value and the persistent page cache size value |
| 311 | ** stored in the database file. |
| 312 | ** |
| 313 | ** The default cache size is stored in meta-value 2 of page 1 of the |
| 314 | ** database file. The cache size is actually the absolute value of |
| 315 | ** this memory location. The sign of meta-value 2 determines the |
| 316 | ** synchronous setting. A negative value means synchronous is off |
| 317 | ** and a positive value means synchronous is on. |
| 318 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 319 | if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 320 | static const VdbeOpList getCacheSize[] = { |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 321 | { OP_ReadCookie, 0, 1, 2}, /* 0 */ |
| 322 | { OP_IfPos, 1, 6, 0}, |
| 323 | { OP_Integer, 0, 2, 0}, |
| 324 | { OP_Subtract, 1, 2, 1}, |
| 325 | { OP_IfPos, 1, 6, 0}, |
| 326 | { OP_Integer, 0, 1, 0}, /* 5 */ |
| 327 | { OP_ResultRow, 1, 1, 0}, |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 328 | }; |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 329 | int addr; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 330 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 331 | sqlite3VdbeUsesBtree(v, iDb); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 332 | if( !zRight ){ |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 333 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 334 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 335 | pParse->nMem += 2; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 336 | addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 337 | sqlite3VdbeChangeP1(v, addr, iDb); |
drh | c797d4d | 2007-05-08 01:08:49 +0000 | [diff] [blame] | 338 | sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 339 | }else{ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 340 | int size = atoi(zRight); |
| 341 | if( size<0 ) size = -size; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 342 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 343 | sqlite3VdbeAddOp2(v, OP_Integer, size, 1); |
| 344 | sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2); |
| 345 | addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0); |
| 346 | sqlite3VdbeAddOp2(v, OP_Integer, -size, 1); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 347 | sqlite3VdbeJumpHere(v, addr); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 348 | sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 349 | pDb->pSchema->cache_size = size; |
| 350 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 351 | } |
| 352 | }else |
| 353 | |
| 354 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 355 | ** PRAGMA [database.]page_size |
| 356 | ** PRAGMA [database.]page_size=N |
| 357 | ** |
| 358 | ** The first form reports the current setting for the |
| 359 | ** database page size in bytes. The second form sets the |
| 360 | ** database page size value. The value can only be set if |
| 361 | ** the database has not yet been created. |
| 362 | */ |
| 363 | if( sqlite3StrICmp(zLeft,"page_size")==0 ){ |
| 364 | Btree *pBt = pDb->pBt; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 365 | assert( pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 366 | if( !zRight ){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 367 | int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 368 | returnSingleInt(pParse, "page_size", size); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 369 | }else{ |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 370 | /* Malloc may fail when setting the page-size, as there is an internal |
| 371 | ** buffer that the pager module resizes using sqlite3_realloc(). |
| 372 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 373 | db->nextPagesize = atoi(zRight); |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 374 | if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 375 | db->mallocFailed = 1; |
| 376 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 377 | } |
| 378 | }else |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 379 | |
| 380 | /* |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 381 | ** PRAGMA [database.]max_page_count |
| 382 | ** PRAGMA [database.]max_page_count=N |
| 383 | ** |
| 384 | ** The first form reports the current setting for the |
| 385 | ** maximum number of pages in the database file. The |
| 386 | ** second form attempts to change this setting. Both |
| 387 | ** forms return the current setting. |
| 388 | */ |
| 389 | if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){ |
| 390 | Btree *pBt = pDb->pBt; |
| 391 | int newMax = 0; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 392 | assert( pBt!=0 ); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 393 | if( zRight ){ |
| 394 | newMax = atoi(zRight); |
| 395 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 396 | if( ALWAYS(pBt) ){ |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 397 | newMax = sqlite3BtreeMaxPageCount(pBt, newMax); |
| 398 | } |
| 399 | returnSingleInt(pParse, "max_page_count", newMax); |
| 400 | }else |
| 401 | |
| 402 | /* |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 403 | ** PRAGMA [database.]page_count |
| 404 | ** |
| 405 | ** Return the number of pages in the specified database. |
| 406 | */ |
| 407 | if( sqlite3StrICmp(zLeft,"page_count")==0 ){ |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 408 | int iReg; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 409 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 410 | sqlite3CodeVerifySchema(pParse, iDb); |
| 411 | iReg = ++pParse->nMem; |
| 412 | sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); |
| 413 | sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); |
| 414 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 415 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "page_count", SQLITE_STATIC); |
danielk1977 | 59a9379 | 2008-05-15 17:48:20 +0000 | [diff] [blame] | 416 | }else |
| 417 | |
| 418 | /* |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 419 | ** PRAGMA [database.]locking_mode |
| 420 | ** PRAGMA [database.]locking_mode = (normal|exclusive) |
| 421 | */ |
| 422 | if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){ |
| 423 | const char *zRet = "normal"; |
| 424 | int eMode = getLockingMode(zRight); |
| 425 | |
| 426 | if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ |
| 427 | /* Simple "PRAGMA locking_mode;" statement. This is a query for |
| 428 | ** the current default locking mode (which may be different to |
| 429 | ** the locking-mode of the main database). |
| 430 | */ |
| 431 | eMode = db->dfltLockMode; |
| 432 | }else{ |
| 433 | Pager *pPager; |
| 434 | if( pId2->n==0 ){ |
| 435 | /* This indicates that no database name was specified as part |
| 436 | ** of the PRAGMA command. In this case the locking-mode must be |
| 437 | ** set on all attached databases, as well as the main db file. |
| 438 | ** |
| 439 | ** Also, the sqlite3.dfltLockMode variable is set so that |
| 440 | ** any subsequently attached databases also use the specified |
| 441 | ** locking mode. |
| 442 | */ |
| 443 | int ii; |
| 444 | assert(pDb==&db->aDb[0]); |
| 445 | for(ii=2; ii<db->nDb; ii++){ |
| 446 | pPager = sqlite3BtreePager(db->aDb[ii].pBt); |
| 447 | sqlite3PagerLockingMode(pPager, eMode); |
| 448 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 449 | db->dfltLockMode = (u8)eMode; |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 450 | } |
| 451 | pPager = sqlite3BtreePager(pDb->pBt); |
| 452 | eMode = sqlite3PagerLockingMode(pPager, eMode); |
| 453 | } |
| 454 | |
| 455 | assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE); |
| 456 | if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ |
| 457 | zRet = "exclusive"; |
| 458 | } |
| 459 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 460 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 461 | sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0); |
| 462 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
danielk1977 | 4148346 | 2007-03-24 16:45:04 +0000 | [diff] [blame] | 463 | }else |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | ** PRAGMA [database.]journal_mode |
shane | c782f69 | 2008-11-10 19:24:38 +0000 | [diff] [blame] | 467 | ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory) |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 468 | */ |
| 469 | if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){ |
| 470 | int eMode; |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 471 | static char * const azModeName[] = { |
| 472 | "delete", "persist", "off", "truncate", "memory" |
| 473 | }; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 474 | |
| 475 | if( zRight==0 ){ |
| 476 | eMode = PAGER_JOURNALMODE_QUERY; |
| 477 | }else{ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 478 | int n = sqlite3Strlen30(zRight); |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 479 | eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 480 | while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){ |
| 481 | eMode--; |
| 482 | } |
| 483 | } |
| 484 | if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){ |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 485 | /* Simple "PRAGMA journal_mode;" statement. This is a query for |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 486 | ** the current default journal mode (which may be different to |
| 487 | ** the journal-mode of the main database). |
| 488 | */ |
| 489 | eMode = db->dfltJournalMode; |
| 490 | }else{ |
| 491 | Pager *pPager; |
| 492 | if( pId2->n==0 ){ |
| 493 | /* This indicates that no database name was specified as part |
| 494 | ** of the PRAGMA command. In this case the journal-mode must be |
| 495 | ** set on all attached databases, as well as the main db file. |
| 496 | ** |
| 497 | ** Also, the sqlite3.dfltJournalMode variable is set so that |
| 498 | ** any subsequently attached databases also use the specified |
| 499 | ** journal mode. |
| 500 | */ |
| 501 | int ii; |
| 502 | assert(pDb==&db->aDb[0]); |
drh | fdc40e9 | 2008-04-17 20:59:37 +0000 | [diff] [blame] | 503 | for(ii=1; ii<db->nDb; ii++){ |
| 504 | if( db->aDb[ii].pBt ){ |
| 505 | pPager = sqlite3BtreePager(db->aDb[ii].pBt); |
| 506 | sqlite3PagerJournalMode(pPager, eMode); |
| 507 | } |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 508 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 509 | db->dfltJournalMode = (u8)eMode; |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 510 | } |
| 511 | pPager = sqlite3BtreePager(pDb->pBt); |
| 512 | eMode = sqlite3PagerJournalMode(pPager, eMode); |
| 513 | } |
| 514 | assert( eMode==PAGER_JOURNALMODE_DELETE |
drh | 0433588 | 2008-09-26 21:08:08 +0000 | [diff] [blame] | 515 | || eMode==PAGER_JOURNALMODE_TRUNCATE |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 516 | || eMode==PAGER_JOURNALMODE_PERSIST |
danielk1977 | b317538 | 2008-10-17 18:51:52 +0000 | [diff] [blame] | 517 | || eMode==PAGER_JOURNALMODE_OFF |
| 518 | || eMode==PAGER_JOURNALMODE_MEMORY ); |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 519 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 520 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC); |
drh | 3b02013 | 2008-04-17 17:02:01 +0000 | [diff] [blame] | 521 | sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, |
| 522 | azModeName[eMode], P4_STATIC); |
| 523 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 524 | }else |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | ** PRAGMA [database.]journal_size_limit |
| 528 | ** PRAGMA [database.]journal_size_limit=N |
| 529 | ** |
drh | a9e364f | 2009-01-13 20:14:15 +0000 | [diff] [blame] | 530 | ** Get or set the size limit on rollback journal files. |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 531 | */ |
| 532 | if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){ |
| 533 | Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 534 | i64 iLimit = -2; |
| 535 | if( zRight ){ |
drh | 3c71364 | 2009-04-04 16:02:32 +0000 | [diff] [blame] | 536 | sqlite3Atoi64(zRight, &iLimit); |
| 537 | if( iLimit<-1 ) iLimit = -1; |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 538 | } |
| 539 | iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); |
drh | 3c71364 | 2009-04-04 16:02:32 +0000 | [diff] [blame] | 540 | returnSingleInt(pParse, "journal_size_limit", iLimit); |
danielk1977 | b53e496 | 2008-06-04 06:45:59 +0000 | [diff] [blame] | 541 | }else |
| 542 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 543 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 544 | |
| 545 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 546 | ** PRAGMA [database.]auto_vacuum |
| 547 | ** PRAGMA [database.]auto_vacuum=N |
| 548 | ** |
drh | a9e364f | 2009-01-13 20:14:15 +0000 | [diff] [blame] | 549 | ** Get or set the value of the database 'auto-vacuum' parameter. |
| 550 | ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 551 | */ |
| 552 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 553 | if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){ |
| 554 | Btree *pBt = pDb->pBt; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 555 | assert( pBt!=0 ); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 556 | if( sqlite3ReadSchema(pParse) ){ |
| 557 | goto pragma_out; |
| 558 | } |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 559 | if( !zRight ){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 560 | int auto_vacuum; |
| 561 | if( ALWAYS(pBt) ){ |
| 562 | auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt); |
| 563 | }else{ |
| 564 | auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM; |
| 565 | } |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 566 | returnSingleInt(pParse, "auto_vacuum", auto_vacuum); |
| 567 | }else{ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 568 | int eAuto = getAutoVacuum(zRight); |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 569 | assert( eAuto>=0 && eAuto<=2 ); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 570 | db->nextAutovac = (u8)eAuto; |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 571 | if( ALWAYS(eAuto>=0) ){ |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 572 | /* Call SetAutoVacuum() to set initialize the internal auto and |
| 573 | ** incr-vacuum flags. This is required in case this connection |
| 574 | ** creates the database file. It is important that it is created |
| 575 | ** as an auto-vacuum capable db. |
| 576 | */ |
| 577 | int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); |
| 578 | if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ |
| 579 | /* When setting the auto_vacuum mode to either "full" or |
| 580 | ** "incremental", write the value of meta[6] in the database |
| 581 | ** file. Before writing to meta[6], check that meta[3] indicates |
| 582 | ** that this really is an auto-vacuum capable database. |
| 583 | */ |
| 584 | static const VdbeOpList setMeta6[] = { |
| 585 | { OP_Transaction, 0, 1, 0}, /* 0 */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 586 | { OP_ReadCookie, 0, 1, 3}, /* 1 */ |
| 587 | { OP_If, 1, 0, 0}, /* 2 */ |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 588 | { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 589 | { OP_Integer, 0, 1, 0}, /* 4 */ |
| 590 | { OP_SetCookie, 0, 6, 1}, /* 5 */ |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 591 | }; |
| 592 | int iAddr; |
| 593 | iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6); |
| 594 | sqlite3VdbeChangeP1(v, iAddr, iDb); |
| 595 | sqlite3VdbeChangeP1(v, iAddr+1, iDb); |
| 596 | sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4); |
| 597 | sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1); |
| 598 | sqlite3VdbeChangeP1(v, iAddr+5, iDb); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 599 | sqlite3VdbeUsesBtree(v, iDb); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 600 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 601 | } |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 602 | } |
| 603 | }else |
| 604 | #endif |
| 605 | |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 606 | /* |
| 607 | ** PRAGMA [database.]incremental_vacuum(N) |
| 608 | ** |
| 609 | ** Do N steps of incremental vacuuming on a database. |
| 610 | */ |
| 611 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 612 | if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){ |
| 613 | int iLimit, addr; |
danielk1977 | 17a240a | 2007-05-23 13:50:23 +0000 | [diff] [blame] | 614 | if( sqlite3ReadSchema(pParse) ){ |
| 615 | goto pragma_out; |
| 616 | } |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 617 | if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ |
| 618 | iLimit = 0x7fffffff; |
| 619 | } |
| 620 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 621 | sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 622 | addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 623 | sqlite3VdbeAddOp1(v, OP_ResultRow, 1); |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 624 | sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 625 | sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); |
drh | ca5557f | 2007-05-04 18:30:40 +0000 | [diff] [blame] | 626 | sqlite3VdbeJumpHere(v, addr); |
| 627 | }else |
| 628 | #endif |
| 629 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 630 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 631 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 632 | ** PRAGMA [database.]cache_size |
| 633 | ** PRAGMA [database.]cache_size=N |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 634 | ** |
| 635 | ** The first form reports the current local setting for the |
| 636 | ** page cache size. The local setting can be different from |
| 637 | ** the persistent cache size value that is stored in the database |
| 638 | ** file itself. The value returned is the maximum number of |
| 639 | ** pages in the page cache. The second form sets the local |
| 640 | ** page cache size value. It does not change the persistent |
| 641 | ** cache size stored on the disk so the cache size will revert |
| 642 | ** to its default value when the database is closed and reopened. |
| 643 | ** N should be a positive integer. |
| 644 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 645 | if( sqlite3StrICmp(zLeft,"cache_size")==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 646 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 647 | if( !zRight ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 648 | returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 649 | }else{ |
| 650 | int size = atoi(zRight); |
| 651 | if( size<0 ) size = -size; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 652 | pDb->pSchema->cache_size = size; |
| 653 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 654 | } |
| 655 | }else |
| 656 | |
| 657 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 658 | ** PRAGMA temp_store |
| 659 | ** PRAGMA temp_store = "default"|"memory"|"file" |
| 660 | ** |
| 661 | ** Return or set the local value of the temp_store flag. Changing |
| 662 | ** the local value does not make changes to the disk file and the default |
| 663 | ** value will be restored the next time the database is opened. |
| 664 | ** |
| 665 | ** Note that it is possible for the library compile-time options to |
| 666 | ** override this setting |
| 667 | */ |
| 668 | if( sqlite3StrICmp(zLeft, "temp_store")==0 ){ |
| 669 | if( !zRight ){ |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 670 | returnSingleInt(pParse, "temp_store", db->temp_store); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 671 | }else{ |
| 672 | changeTempStorage(pParse, zRight); |
| 673 | } |
| 674 | }else |
| 675 | |
| 676 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 677 | ** PRAGMA temp_store_directory |
| 678 | ** PRAGMA temp_store_directory = ""|"directory_name" |
| 679 | ** |
| 680 | ** Return or set the local value of the temp_store_directory flag. Changing |
| 681 | ** the value sets a specific directory to be used for temporary files. |
| 682 | ** Setting to a null string reverts to the default temporary directory search. |
| 683 | ** If temporary directory is changed, then invalidateTempStorage. |
| 684 | ** |
| 685 | */ |
| 686 | if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){ |
| 687 | if( !zRight ){ |
| 688 | if( sqlite3_temp_directory ){ |
| 689 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 690 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 691 | "temp_store_directory", SQLITE_STATIC); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 692 | sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0); |
| 693 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 694 | } |
| 695 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 696 | #ifndef SQLITE_OMIT_WSD |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 697 | if( zRight[0] ){ |
danielk1977 | fab1127 | 2008-09-16 14:38:02 +0000 | [diff] [blame] | 698 | int rc; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 699 | int res; |
danielk1977 | fab1127 | 2008-09-16 14:38:02 +0000 | [diff] [blame] | 700 | rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); |
| 701 | if( rc!=SQLITE_OK || res==0 ){ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 702 | sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 703 | goto pragma_out; |
| 704 | } |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 705 | } |
danielk1977 | b06a0b6 | 2008-06-26 10:54:12 +0000 | [diff] [blame] | 706 | if( SQLITE_TEMP_STORE==0 |
| 707 | || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) |
| 708 | || (SQLITE_TEMP_STORE==2 && db->temp_store==1) |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 709 | ){ |
| 710 | invalidateTempStorage(pParse); |
| 711 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 712 | sqlite3_free(sqlite3_temp_directory); |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 713 | if( zRight[0] ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 714 | sqlite3_temp_directory = sqlite3DbStrDup(0, zRight); |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 715 | }else{ |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 716 | sqlite3_temp_directory = 0; |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 717 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 718 | #endif /* SQLITE_OMIT_WSD */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 719 | } |
| 720 | }else |
| 721 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 722 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 723 | # if defined(__APPLE__) |
| 724 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 725 | # else |
| 726 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 727 | # endif |
| 728 | #endif |
| 729 | #if SQLITE_ENABLE_LOCKING_STYLE |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 730 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 731 | ** PRAGMA [database.]lock_proxy_file |
| 732 | ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path" |
| 733 | ** |
| 734 | ** Return or set the value of the lock_proxy_file flag. Changing |
| 735 | ** the value sets a specific file to be used for database access locks. |
| 736 | ** |
| 737 | */ |
| 738 | if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){ |
| 739 | if( !zRight ){ |
| 740 | Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 741 | char *proxy_file_path = NULL; |
| 742 | sqlite3_file *pFile = sqlite3PagerFile(pPager); |
| 743 | sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE, |
| 744 | &proxy_file_path); |
| 745 | |
| 746 | if( proxy_file_path ){ |
| 747 | sqlite3VdbeSetNumCols(v, 1); |
| 748 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, |
| 749 | "lock_proxy_file", SQLITE_STATIC); |
| 750 | sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0); |
| 751 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 752 | } |
| 753 | }else{ |
| 754 | Pager *pPager = sqlite3BtreePager(pDb->pBt); |
| 755 | sqlite3_file *pFile = sqlite3PagerFile(pPager); |
| 756 | int res; |
| 757 | if( zRight[0] ){ |
| 758 | res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, |
| 759 | zRight); |
| 760 | } else { |
| 761 | res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, |
| 762 | NULL); |
| 763 | } |
| 764 | if( res!=SQLITE_OK ){ |
| 765 | sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); |
| 766 | goto pragma_out; |
| 767 | } |
| 768 | } |
| 769 | }else |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 770 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 771 | |
| 772 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 773 | ** PRAGMA [database.]synchronous |
| 774 | ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 775 | ** |
| 776 | ** Return or set the local value of the synchronous flag. Changing |
| 777 | ** the local value does not make changes to the disk file and the |
| 778 | ** default value will be restored the next time the database is |
| 779 | ** opened. |
| 780 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 781 | if( sqlite3StrICmp(zLeft,"synchronous")==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 782 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 783 | if( !zRight ){ |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 784 | returnSingleInt(pParse, "synchronous", pDb->safety_level-1); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 785 | }else{ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 786 | if( !db->autoCommit ){ |
| 787 | sqlite3ErrorMsg(pParse, |
| 788 | "Safety level may not be changed inside a transaction"); |
| 789 | }else{ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 790 | pDb->safety_level = getSafetyLevel(zRight)+1; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 791 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 792 | } |
| 793 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 794 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 795 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 796 | #ifndef SQLITE_OMIT_FLAG_PRAGMAS |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 797 | if( flagPragma(pParse, zLeft, zRight) ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 798 | /* The flagPragma() subroutine also generates any necessary code |
| 799 | ** there is nothing more to do here */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 800 | }else |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 801 | #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 802 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 803 | #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 804 | /* |
| 805 | ** PRAGMA table_info(<table>) |
| 806 | ** |
| 807 | ** Return a single row for each column of the named table. The columns of |
| 808 | ** the returned data set are: |
| 809 | ** |
| 810 | ** cid: Column id (numbered from left to right, starting at 0) |
| 811 | ** name: Column name |
| 812 | ** type: Column declaration type. |
| 813 | ** notnull: True if 'NOT NULL' is part of column declaration |
| 814 | ** dflt_value: The default value for the column, if any. |
| 815 | */ |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 816 | if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 817 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 818 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 819 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 820 | if( pTab ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 821 | int i; |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 822 | int nHidden = 0; |
drh | f7eece6 | 2006-02-06 21:34:27 +0000 | [diff] [blame] | 823 | Column *pCol; |
drh | 9f6696a | 2006-02-09 16:52:23 +0000 | [diff] [blame] | 824 | sqlite3VdbeSetNumCols(v, 6); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 825 | pParse->nMem = 6; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 826 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC); |
| 827 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
| 828 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC); |
| 829 | sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC); |
| 830 | sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC); |
| 831 | sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 832 | sqlite3ViewGetColumnNames(pParse, pTab); |
drh | f7eece6 | 2006-02-06 21:34:27 +0000 | [diff] [blame] | 833 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
danielk1977 | 034ca14 | 2007-06-26 10:38:54 +0000 | [diff] [blame] | 834 | if( IsHiddenColumn(pCol) ){ |
| 835 | nHidden++; |
| 836 | continue; |
| 837 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 838 | sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1); |
| 839 | sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0); |
| 840 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
drh | bfa8b10 | 2006-03-03 21:20:16 +0000 | [diff] [blame] | 841 | pCol->zType ? pCol->zType : "", 0); |
danielk1977 | f96a377 | 2008-10-23 05:45:07 +0000 | [diff] [blame] | 842 | sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4); |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 843 | if( pCol->pDflt ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 844 | const Token *p = &pCol->pDflt->span; |
| 845 | assert( p->z ); |
| 846 | sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)p->z, p->n); |
drh | 6f83598 | 2006-09-25 13:48:30 +0000 | [diff] [blame] | 847 | }else{ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 848 | sqlite3VdbeAddOp2(v, OP_Null, 0, 5); |
drh | 6f83598 | 2006-09-25 13:48:30 +0000 | [diff] [blame] | 849 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 850 | sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6); |
| 851 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | }else |
| 855 | |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 856 | if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 857 | Index *pIdx; |
| 858 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 859 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 860 | pIdx = sqlite3FindIndex(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 861 | if( pIdx ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 862 | int i; |
| 863 | pTab = pIdx->pTable; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 864 | sqlite3VdbeSetNumCols(v, 3); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 865 | pParse->nMem = 3; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 866 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC); |
| 867 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC); |
| 868 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 869 | for(i=0; i<pIdx->nColumn; i++){ |
| 870 | int cnum = pIdx->aiColumn[i]; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 871 | sqlite3VdbeAddOp2(v, OP_Integer, i, 1); |
| 872 | sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 873 | assert( pTab->nCol>cnum ); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 874 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0); |
| 875 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | }else |
| 879 | |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 880 | if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 881 | Index *pIdx; |
| 882 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 883 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 884 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 885 | if( pTab ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 886 | v = sqlite3GetVdbe(pParse); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 887 | pIdx = pTab->pIndex; |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 888 | if( pIdx ){ |
| 889 | int i = 0; |
| 890 | sqlite3VdbeSetNumCols(v, 3); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 891 | pParse->nMem = 3; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 892 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); |
| 893 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
| 894 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 895 | while(pIdx){ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 896 | sqlite3VdbeAddOp2(v, OP_Integer, i, 1); |
| 897 | sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0); |
| 898 | sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3); |
| 899 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 900 | ++i; |
| 901 | pIdx = pIdx->pNext; |
| 902 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | }else |
| 906 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 907 | if( sqlite3StrICmp(zLeft, "database_list")==0 ){ |
| 908 | int i; |
| 909 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 910 | sqlite3VdbeSetNumCols(v, 3); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 911 | pParse->nMem = 3; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 912 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); |
| 913 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
| 914 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC); |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 915 | for(i=0; i<db->nDb; i++){ |
| 916 | if( db->aDb[i].pBt==0 ) continue; |
| 917 | assert( db->aDb[i].zName!=0 ); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 918 | sqlite3VdbeAddOp2(v, OP_Integer, i, 1); |
| 919 | sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0); |
| 920 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 921 | sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 922 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 923 | } |
| 924 | }else |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 925 | |
| 926 | if( sqlite3StrICmp(zLeft, "collation_list")==0 ){ |
| 927 | int i = 0; |
| 928 | HashElem *p; |
| 929 | sqlite3VdbeSetNumCols(v, 2); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 930 | pParse->nMem = 2; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 931 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); |
| 932 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 933 | for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ |
| 934 | CollSeq *pColl = (CollSeq *)sqliteHashData(p); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 935 | sqlite3VdbeAddOp2(v, OP_Integer, i++, 1); |
| 936 | sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0); |
| 937 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 938 | } |
| 939 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 940 | #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ |
| 941 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 942 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 943 | if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 944 | FKey *pFK; |
| 945 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 946 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 947 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 948 | if( pTab ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 949 | v = sqlite3GetVdbe(pParse); |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 950 | pFK = pTab->pFKey; |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 951 | if( pFK ){ |
| 952 | int i = 0; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 953 | sqlite3VdbeSetNumCols(v, 8); |
| 954 | pParse->nMem = 8; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 955 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC); |
| 956 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC); |
| 957 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC); |
| 958 | sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC); |
| 959 | sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC); |
| 960 | sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC); |
| 961 | sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC); |
| 962 | sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 963 | while(pFK){ |
| 964 | int j; |
| 965 | for(j=0; j<pFK->nCol; j++){ |
drh | 2f47149 | 2005-06-23 03:15:07 +0000 | [diff] [blame] | 966 | char *zCol = pFK->aCol[j].zCol; |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 967 | char *zOnUpdate = (char *)actionName(pFK->updateConf); |
| 968 | char *zOnDelete = (char *)actionName(pFK->deleteConf); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 969 | sqlite3VdbeAddOp2(v, OP_Integer, i, 1); |
| 970 | sqlite3VdbeAddOp2(v, OP_Integer, j, 2); |
| 971 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0); |
| 972 | sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 973 | pTab->aCol[pFK->aCol[j].iFrom].zName, 0); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 974 | sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0); |
danielk1977 | 50af3e1 | 2008-10-10 17:47:21 +0000 | [diff] [blame] | 975 | sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0); |
| 976 | sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0); |
| 977 | sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0); |
| 978 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 979 | } |
| 980 | ++i; |
| 981 | pFK = pFK->pNextFrom; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 982 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | }else |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 986 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 987 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 988 | #ifndef NDEBUG |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 989 | if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 990 | if( zRight ){ |
| 991 | if( getBoolean(zRight) ){ |
| 992 | sqlite3ParserTrace(stderr, "parser: "); |
| 993 | }else{ |
| 994 | sqlite3ParserTrace(0, 0); |
| 995 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 996 | } |
| 997 | }else |
| 998 | #endif |
| 999 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1000 | /* Reinstall the LIKE and GLOB functions. The variant of LIKE |
| 1001 | ** used will be case sensitive or not depending on the RHS. |
| 1002 | */ |
| 1003 | if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){ |
| 1004 | if( zRight ){ |
| 1005 | sqlite3RegisterLikeFunctions(db, getBoolean(zRight)); |
| 1006 | } |
| 1007 | }else |
| 1008 | |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1009 | #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 1010 | # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 1011 | #endif |
| 1012 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1013 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 1014 | /* Pragma "quick_check" is an experimental reduced version of |
| 1015 | ** integrity_check designed to detect most database corruption |
| 1016 | ** without most of the overhead of a full integrity-check. |
| 1017 | */ |
| 1018 | if( sqlite3StrICmp(zLeft, "integrity_check")==0 |
| 1019 | || sqlite3StrICmp(zLeft, "quick_check")==0 |
| 1020 | ){ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1021 | int i, j, addr, mxErr; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1022 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1023 | /* Code that appears at the end of the integrity check. If no error |
| 1024 | ** messages have been generated, output OK. Otherwise output the |
| 1025 | ** error message |
| 1026 | */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1027 | static const VdbeOpList endCode[] = { |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1028 | { OP_AddImm, 1, 0, 0}, /* 0 */ |
| 1029 | { OP_IfNeg, 1, 0, 0}, /* 1 */ |
| 1030 | { OP_String8, 0, 3, 0}, /* 2 */ |
| 1031 | { OP_ResultRow, 3, 1, 0}, |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1032 | }; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1033 | |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 1034 | int isQuick = (zLeft[0]=='q'); |
| 1035 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1036 | /* Initialize the VDBE program */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 1037 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1038 | pParse->nMem = 6; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1039 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1040 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* Set the maximum error count */ |
| 1043 | mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; |
| 1044 | if( zRight ){ |
| 1045 | mxErr = atoi(zRight); |
| 1046 | if( mxErr<=0 ){ |
| 1047 | mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; |
| 1048 | } |
| 1049 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1050 | sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1051 | |
| 1052 | /* Do an integrity check on each database file */ |
| 1053 | for(i=0; i<db->nDb; i++){ |
| 1054 | HashElem *x; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1055 | Hash *pTbls; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1056 | int cnt = 0; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1057 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 1058 | if( OMIT_TEMPDB && i==1 ) continue; |
| 1059 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 1060 | sqlite3CodeVerifySchema(pParse, i); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1061 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1062 | sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1063 | sqlite3VdbeJumpHere(v, addr); |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 1064 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1065 | /* Do an integrity check of the B-Tree |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1066 | ** |
| 1067 | ** Begin by filling registers 2, 3, ... with the root pages numbers |
| 1068 | ** for all tables and indices in the database. |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1069 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1070 | pTbls = &db->aDb[i].pSchema->tblHash; |
| 1071 | for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1072 | Table *pTab = sqliteHashData(x); |
| 1073 | Index *pIdx; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1074 | sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt); |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1075 | cnt++; |
| 1076 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1077 | sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt); |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 1078 | cnt++; |
| 1079 | } |
| 1080 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1081 | if( cnt==0 ) continue; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1082 | |
| 1083 | /* Make sure sufficient number of registers have been allocated */ |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1084 | if( pParse->nMem < cnt+4 ){ |
| 1085 | pParse->nMem = cnt+4; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1086 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1087 | |
| 1088 | /* Do the b-tree integrity checks */ |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1089 | sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1090 | sqlite3VdbeChangeP5(v, (u8)i); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1091 | addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); |
| 1092 | sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1093 | sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1094 | P4_DYNAMIC); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1095 | sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1096 | sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 1097 | sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1098 | sqlite3VdbeJumpHere(v, addr); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1099 | |
| 1100 | /* Make sure all the indices are constructed correctly. |
| 1101 | */ |
danielk1977 | 41c58b7 | 2007-12-29 13:39:19 +0000 | [diff] [blame] | 1102 | for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1103 | Table *pTab = sqliteHashData(x); |
| 1104 | Index *pIdx; |
| 1105 | int loopTop; |
| 1106 | |
| 1107 | if( pTab->pIndex==0 ) continue; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1108 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1109 | sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1110 | sqlite3VdbeJumpHere(v, addr); |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 1111 | sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1112 | sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1113 | loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1114 | sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1115 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1116 | int jmp2; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1117 | static const VdbeOpList idxErr[] = { |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1118 | { OP_AddImm, 1, -1, 0}, |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1119 | { OP_String8, 0, 3, 0}, /* 1 */ |
| 1120 | { OP_Rowid, 1, 4, 0}, |
| 1121 | { OP_String8, 0, 5, 0}, /* 3 */ |
| 1122 | { OP_String8, 0, 6, 0}, /* 4 */ |
| 1123 | { OP_Concat, 4, 3, 3}, |
| 1124 | { OP_Concat, 5, 3, 3}, |
| 1125 | { OP_Concat, 6, 3, 3}, |
| 1126 | { OP_ResultRow, 3, 1, 0}, |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 1127 | { OP_IfPos, 1, 0, 0}, /* 9 */ |
| 1128 | { OP_Halt, 0, 0, 0}, |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1129 | }; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 1130 | sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 1); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1131 | jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1132 | addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); |
drh | 2400345 | 2008-01-03 01:28:59 +0000 | [diff] [blame] | 1133 | sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC); |
| 1134 | sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1135 | sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC); |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 1136 | sqlite3VdbeJumpHere(v, addr+9); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1137 | sqlite3VdbeJumpHere(v, jmp2); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1138 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1139 | sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1140 | sqlite3VdbeJumpHere(v, loopTop); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1141 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1142 | static const VdbeOpList cntIdx[] = { |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1143 | { OP_Integer, 0, 3, 0}, |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1144 | { OP_Rewind, 0, 0, 0}, /* 1 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1145 | { OP_AddImm, 3, 1, 0}, |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1146 | { OP_Next, 0, 0, 0}, /* 3 */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1147 | { OP_Eq, 2, 0, 3}, /* 4 */ |
drh | 8558cde | 2008-01-05 05:20:10 +0000 | [diff] [blame] | 1148 | { OP_AddImm, 1, -1, 0}, |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1149 | { OP_String8, 0, 2, 0}, /* 6 */ |
| 1150 | { OP_String8, 0, 3, 0}, /* 7 */ |
| 1151 | { OP_Concat, 3, 2, 2}, |
| 1152 | { OP_ResultRow, 2, 1, 0}, |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1153 | }; |
| 1154 | if( pIdx->tnum==0 ) continue; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1155 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1156 | sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 1157 | sqlite3VdbeJumpHere(v, addr); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1158 | addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 1159 | sqlite3VdbeChangeP1(v, addr+1, j+2); |
| 1160 | sqlite3VdbeChangeP2(v, addr+1, addr+4); |
| 1161 | sqlite3VdbeChangeP1(v, addr+3, j+2); |
| 1162 | sqlite3VdbeChangeP2(v, addr+3, addr+2); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1163 | sqlite3VdbeJumpHere(v, addr+4); |
| 1164 | sqlite3VdbeChangeP4(v, addr+6, |
drh | 2400345 | 2008-01-03 01:28:59 +0000 | [diff] [blame] | 1165 | "wrong # of entries in index ", P4_STATIC); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1166 | sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1170 | addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1171 | sqlite3VdbeChangeP2(v, addr, -mxErr); |
| 1172 | sqlite3VdbeJumpHere(v, addr+1); |
| 1173 | sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1174 | }else |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 1175 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 1176 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1177 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1178 | /* |
| 1179 | ** PRAGMA encoding |
| 1180 | ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" |
| 1181 | ** |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1182 | ** In its first form, this pragma returns the encoding of the main |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1183 | ** database. If the database is not initialized, it is initialized now. |
| 1184 | ** |
| 1185 | ** The second form of this pragma is a no-op if the main database file |
| 1186 | ** has not already been initialized. In this case it sets the default |
| 1187 | ** encoding that will be used for the main database file if a new file |
| 1188 | ** is created. If an existing main database file is opened, then the |
| 1189 | ** default text encoding for the existing database is used. |
| 1190 | ** |
| 1191 | ** In all cases new databases created using the ATTACH command are |
| 1192 | ** created to use the same default text encoding as the main database. If |
| 1193 | ** the main database has not been initialized and/or created when ATTACH |
| 1194 | ** is executed, this is done before the ATTACH operation. |
| 1195 | ** |
| 1196 | ** In the second form this pragma sets the text encoding to be used in |
| 1197 | ** new database files created using this database handle. It is only |
| 1198 | ** useful if invoked immediately after the main database i |
| 1199 | */ |
| 1200 | if( sqlite3StrICmp(zLeft, "encoding")==0 ){ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1201 | static const struct EncName { |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1202 | char *zName; |
| 1203 | u8 enc; |
| 1204 | } encnames[] = { |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 1205 | { "UTF8", SQLITE_UTF8 }, |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1206 | { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ |
| 1207 | { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ |
| 1208 | { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 1209 | { "UTF16le", SQLITE_UTF16LE }, |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 1210 | { "UTF16be", SQLITE_UTF16BE }, |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1211 | { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ |
| 1212 | { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1213 | { 0, 0 } |
| 1214 | }; |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1215 | const struct EncName *pEnc; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1216 | if( !zRight ){ /* "PRAGMA encoding" */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 1217 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1218 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1219 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1220 | sqlite3VdbeAddOp2(v, OP_String8, 0, 1); |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1221 | assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); |
| 1222 | assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); |
| 1223 | assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); |
| 1224 | sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1225 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1226 | }else{ /* "PRAGMA encoding = XXX" */ |
| 1227 | /* Only change the value of sqlite.enc if the database handle is not |
| 1228 | ** initialized. If the main database exists, the new sqlite.enc value |
| 1229 | ** will be overwritten when the schema is next loaded. If it does not |
| 1230 | ** already exists, it will be created to use the new encoding value. |
| 1231 | */ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 1232 | if( |
| 1233 | !(DbHasProperty(db, 0, DB_SchemaLoaded)) || |
| 1234 | DbHasProperty(db, 0, DB_Empty) |
| 1235 | ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1236 | for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
| 1237 | if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 1238 | ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1239 | break; |
| 1240 | } |
| 1241 | } |
| 1242 | if( !pEnc->zName ){ |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 1243 | sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1248 | #endif /* SQLITE_OMIT_UTF16 */ |
| 1249 | |
| 1250 | #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1251 | /* |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1252 | ** PRAGMA [database.]schema_version |
| 1253 | ** PRAGMA [database.]schema_version = <integer> |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1254 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1255 | ** PRAGMA [database.]user_version |
| 1256 | ** PRAGMA [database.]user_version = <integer> |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1257 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1258 | ** The pragma's schema_version and user_version are used to set or get |
| 1259 | ** the value of the schema-version and user-version, respectively. Both |
| 1260 | ** the schema-version and the user-version are 32-bit signed integers |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1261 | ** stored in the database header. |
| 1262 | ** |
| 1263 | ** The schema-cookie is usually only manipulated internally by SQLite. It |
| 1264 | ** is incremented by SQLite whenever the database schema is modified (by |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1265 | ** creating or dropping a table or index). The schema version is used by |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1266 | ** SQLite each time a query is executed to ensure that the internal cache |
| 1267 | ** of the schema used when compiling the SQL query matches the schema of |
| 1268 | ** the database against which the compiled query is actually executed. |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1269 | ** Subverting this mechanism by using "PRAGMA schema_version" to modify |
| 1270 | ** the schema-version is potentially dangerous and may lead to program |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1271 | ** crashes or database corruption. Use with caution! |
| 1272 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 1273 | ** 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] | 1274 | ** applications for any purpose. |
| 1275 | */ |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 1276 | if( sqlite3StrICmp(zLeft, "schema_version")==0 |
| 1277 | || sqlite3StrICmp(zLeft, "user_version")==0 |
| 1278 | || sqlite3StrICmp(zLeft, "freelist_count")==0 |
| 1279 | ){ |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1280 | int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 1281 | sqlite3VdbeUsesBtree(v, iDb); |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 1282 | switch( zLeft[0] ){ |
| 1283 | case 's': case 'S': |
| 1284 | iCookie = 0; |
| 1285 | break; |
| 1286 | case 'f': case 'F': |
| 1287 | iCookie = 1; |
| 1288 | iDb = (-1*(iDb+1)); |
| 1289 | assert(iDb<=0); |
| 1290 | break; |
| 1291 | default: |
| 1292 | iCookie = 5; |
| 1293 | break; |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
danielk1977 | 180b56a | 2007-06-24 08:00:42 +0000 | [diff] [blame] | 1296 | if( zRight && iDb>=0 ){ |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1297 | /* Write the specified cookie value */ |
| 1298 | static const VdbeOpList setCookie[] = { |
| 1299 | { OP_Transaction, 0, 1, 0}, /* 0 */ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1300 | { OP_Integer, 0, 1, 0}, /* 1 */ |
| 1301 | { OP_SetCookie, 0, 0, 1}, /* 2 */ |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1302 | }; |
| 1303 | int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); |
| 1304 | sqlite3VdbeChangeP1(v, addr, iDb); |
| 1305 | sqlite3VdbeChangeP1(v, addr+1, atoi(zRight)); |
| 1306 | sqlite3VdbeChangeP1(v, addr+2, iDb); |
| 1307 | sqlite3VdbeChangeP2(v, addr+2, iCookie); |
| 1308 | }else{ |
| 1309 | /* Read the specified cookie value */ |
| 1310 | static const VdbeOpList readCookie[] = { |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1311 | { OP_ReadCookie, 0, 1, 0}, /* 0 */ |
| 1312 | { OP_ResultRow, 1, 1, 0} |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1313 | }; |
| 1314 | int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie); |
| 1315 | sqlite3VdbeChangeP1(v, addr, iDb); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1316 | sqlite3VdbeChangeP3(v, addr, iCookie); |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1317 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1318 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 1319 | } |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 1320 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1321 | #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1322 | |
dougcurrie | 81c95ef | 2004-06-18 23:21:47 +0000 | [diff] [blame] | 1323 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1324 | /* |
| 1325 | ** Report the current state of file logs for all databases |
| 1326 | */ |
| 1327 | if( sqlite3StrICmp(zLeft, "lock_status")==0 ){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 1328 | static const char *const azLockName[] = { |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1329 | "unlocked", "shared", "reserved", "pending", "exclusive" |
| 1330 | }; |
| 1331 | int i; |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1332 | sqlite3VdbeSetNumCols(v, 2); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1333 | pParse->nMem = 2; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 1334 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC); |
| 1335 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1336 | for(i=0; i<db->nDb; i++){ |
| 1337 | Btree *pBt; |
| 1338 | Pager *pPager; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 1339 | const char *zState = "unknown"; |
| 1340 | int j; |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1341 | if( db->aDb[i].zName==0 ) continue; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1342 | sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1343 | pBt = db->aDb[i].pBt; |
| 1344 | if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 1345 | zState = "closed"; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 1346 | }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 1347 | SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ |
| 1348 | zState = azLockName[j]; |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1349 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1350 | sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC); |
| 1351 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1352 | } |
danielk1977 | a4de453 | 2008-09-02 15:44:08 +0000 | [diff] [blame] | 1353 | |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 1354 | }else |
| 1355 | #endif |
| 1356 | |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 1357 | #ifdef SQLITE_SSE |
| 1358 | /* |
| 1359 | ** Check to see if the sqlite_statements table exists. Create it |
| 1360 | ** if it does not. |
| 1361 | */ |
| 1362 | if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){ |
danielk1977 | 3a3f38e | 2005-05-22 06:49:56 +0000 | [diff] [blame] | 1363 | extern int sqlite3CreateStatementsTable(Parse*); |
| 1364 | sqlite3CreateStatementsTable(pParse); |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 1365 | }else |
| 1366 | #endif |
| 1367 | |
drh | 3c4f2a4 | 2005-12-08 18:12:56 +0000 | [diff] [blame] | 1368 | #if SQLITE_HAS_CODEC |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1369 | if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1370 | sqlite3_key(db, zRight, sqlite3Strlen30(zRight)); |
drh | 3c4f2a4 | 2005-12-08 18:12:56 +0000 | [diff] [blame] | 1371 | }else |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1372 | if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){ |
| 1373 | sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight)); |
| 1374 | }else |
| 1375 | if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 || |
| 1376 | sqlite3StrICmp(zLeft, "hexrekey")==0) ){ |
| 1377 | int i, h1, h2; |
| 1378 | char zKey[40]; |
| 1379 | for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){ |
| 1380 | h1 += 9*(1&(h1>>6)); |
| 1381 | h2 += 9*(1&(h2>>6)); |
| 1382 | zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4); |
| 1383 | } |
| 1384 | if( (zLeft[3] & 0xf)==0xb ){ |
| 1385 | sqlite3_key(db, zKey, i/2); |
| 1386 | }else{ |
| 1387 | sqlite3_rekey(db, zKey, i/2); |
| 1388 | } |
| 1389 | }else |
drh | 3c4f2a4 | 2005-12-08 18:12:56 +0000 | [diff] [blame] | 1390 | #endif |
drh | 21e2cab | 2006-09-25 18:01:57 +0000 | [diff] [blame] | 1391 | #if SQLITE_HAS_CODEC || defined(SQLITE_ENABLE_CEROD) |
| 1392 | if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){ |
| 1393 | #if SQLITE_HAS_CODEC |
| 1394 | if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ |
| 1395 | extern void sqlite3_activate_see(const char*); |
| 1396 | sqlite3_activate_see(&zRight[4]); |
| 1397 | } |
| 1398 | #endif |
| 1399 | #ifdef SQLITE_ENABLE_CEROD |
| 1400 | if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ |
| 1401 | extern void sqlite3_activate_cerod(const char*); |
| 1402 | sqlite3_activate_cerod(&zRight[6]); |
| 1403 | } |
| 1404 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1405 | }else |
drh | 21e2cab | 2006-09-25 18:01:57 +0000 | [diff] [blame] | 1406 | #endif |
drh | 3c4f2a4 | 2005-12-08 18:12:56 +0000 | [diff] [blame] | 1407 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1408 | |
| 1409 | {/* Empty ELSE clause */} |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1410 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1411 | /* Code an OP_Expire at the end of each PRAGMA program to cause |
| 1412 | ** the VDBE implementing the pragma to expire. Most (all?) pragmas |
| 1413 | ** are only valid for a single execution. |
| 1414 | */ |
| 1415 | sqlite3VdbeAddOp2(v, OP_Expire, 1, 0); |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1416 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1417 | /* |
| 1418 | ** Reset the safety level, in case the fullfsync flag or synchronous |
| 1419 | ** setting changed. |
| 1420 | */ |
danielk1977 | ddfb2f0 | 2006-02-17 12:25:14 +0000 | [diff] [blame] | 1421 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1422 | if( db->autoCommit ){ |
| 1423 | sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level, |
| 1424 | (db->flags&SQLITE_FullFSync)!=0); |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 1425 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 1426 | #endif |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 1427 | pragma_out: |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1428 | sqlite3DbFree(db, zLeft); |
| 1429 | sqlite3DbFree(db, zRight); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 1430 | } |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 1431 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1432 | #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */ |