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