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 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 14 | ** $Id: pragma.c,v 1.113 2006/01/11 14:09:32 danielk1977 Exp $ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 17 | #include "os.h" |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 19 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 20 | /* Ignore this whole file if pragmas are disabled |
| 21 | */ |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 22 | #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER) |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 23 | |
dougcurrie | 81c95ef | 2004-06-18 23:21:47 +0000 | [diff] [blame] | 24 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 25 | # include "pager.h" |
| 26 | # include "btree.h" |
| 27 | #endif |
| 28 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 29 | /* |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 30 | ** Interpret the given string as a safety level. Return 0 for OFF, |
jplyon | b1639ff | 2004-01-19 04:52:29 +0000 | [diff] [blame] | 31 | ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or |
| 32 | ** unrecognized string argument. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 33 | ** |
| 34 | ** Note that the values returned are one less that the values that |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 35 | ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 36 | ** to support legacy SQL code. The safety level used to be boolean |
| 37 | ** and older scripts may have used numbers 0 for OFF and 1 for ON. |
| 38 | */ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 39 | static int getSafetyLevel(const char *z){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 40 | /* 123456789 123456789 */ |
| 41 | static const char zText[] = "onoffalseyestruefull"; |
| 42 | static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16}; |
| 43 | static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4}; |
| 44 | static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2}; |
| 45 | int i, n; |
| 46 | if( isdigit(*z) ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 47 | return atoi(z); |
| 48 | } |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 49 | n = strlen(z); |
| 50 | for(i=0; i<sizeof(iLength); i++){ |
| 51 | if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){ |
| 52 | return iValue[i]; |
| 53 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 54 | } |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | /* |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 59 | ** Interpret the given string as a boolean value. |
| 60 | */ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 61 | static int getBoolean(const char *z){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 62 | return getSafetyLevel(z)&1; |
| 63 | } |
| 64 | |
danielk1977 | b84f96f | 2005-01-20 11:32:23 +0000 | [diff] [blame] | 65 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 66 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 67 | ** Interpret the given string as a temp db location. Return 1 for file |
| 68 | ** backed temporary databases, 2 for the Red-Black tree in memory database |
| 69 | ** and 0 to use the compile-time default. |
| 70 | */ |
| 71 | static int getTempStore(const char *z){ |
| 72 | if( z[0]>='0' && z[0]<='2' ){ |
| 73 | return z[0] - '0'; |
| 74 | }else if( sqlite3StrICmp(z, "file")==0 ){ |
| 75 | return 1; |
| 76 | }else if( sqlite3StrICmp(z, "memory")==0 ){ |
| 77 | return 2; |
| 78 | }else{ |
| 79 | return 0; |
| 80 | } |
| 81 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 82 | #endif /* SQLITE_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 83 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 84 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 85 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 86 | ** Invalidate temp storage, either when the temp storage is changed |
| 87 | ** from default, or when 'file' and the temp_store_directory has changed |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 88 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 89 | static int invalidateTempStorage(Parse *pParse){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 90 | sqlite3 *db = pParse->db; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 91 | if( db->aDb[1].pBt!=0 ){ |
| 92 | if( db->flags & SQLITE_InTrans ){ |
| 93 | sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " |
| 94 | "from within a transaction"); |
| 95 | return SQLITE_ERROR; |
| 96 | } |
| 97 | sqlite3BtreeClose(db->aDb[1].pBt); |
| 98 | db->aDb[1].pBt = 0; |
| 99 | sqlite3ResetInternalSchema(db, 0); |
| 100 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 101 | return SQLITE_OK; |
| 102 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 103 | #endif /* SQLITE_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 104 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 105 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 106 | /* |
| 107 | ** If the TEMP database is open, close it and mark the database schema |
| 108 | ** as needing reloading. This must be done when using the TEMP_STORE |
| 109 | ** or DEFAULT_TEMP_STORE pragmas. |
| 110 | */ |
| 111 | static int changeTempStorage(Parse *pParse, const char *zStorageType){ |
| 112 | int ts = getTempStore(zStorageType); |
| 113 | sqlite3 *db = pParse->db; |
| 114 | if( db->temp_store==ts ) return SQLITE_OK; |
| 115 | if( invalidateTempStorage( pParse ) != SQLITE_OK ){ |
| 116 | return SQLITE_ERROR; |
| 117 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 118 | db->temp_store = ts; |
| 119 | return SQLITE_OK; |
| 120 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 121 | #endif /* SQLITE_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | ** Generate code to return a single integer value. |
| 125 | */ |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 126 | static void returnSingleInt(Parse *pParse, const char *zLabel, int value){ |
| 127 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 128 | sqlite3VdbeAddOp(v, OP_Integer, value, 0); |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 129 | if( pParse->explain==0 ){ |
| 130 | sqlite3VdbeSetNumCols(v, 1); |
| 131 | sqlite3VdbeSetColName(v, 0, zLabel, P3_STATIC); |
| 132 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 133 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
| 134 | } |
| 135 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 136 | #ifndef SQLITE_OMIT_FLAG_PRAGMAS |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 137 | /* |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 138 | ** Check to see if zRight and zLeft refer to a pragma that queries |
| 139 | ** or changes one of the flags in db->flags. Return 1 if so and 0 if not. |
| 140 | ** Also, implement the pragma. |
| 141 | */ |
| 142 | static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 143 | static const struct sPragmaType { |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 144 | const char *zName; /* Name of the pragma */ |
| 145 | int mask; /* Mask for the db->flags value */ |
| 146 | } aPragma[] = { |
| 147 | { "vdbe_trace", SQLITE_VdbeTrace }, |
drh | 35d4c2f | 2004-06-10 01:30:59 +0000 | [diff] [blame] | 148 | { "sql_trace", SQLITE_SqlTrace }, |
| 149 | { "vdbe_listing", SQLITE_VdbeListing }, |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 150 | { "full_column_names", SQLITE_FullColNames }, |
| 151 | { "short_column_names", SQLITE_ShortColNames }, |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 152 | { "count_changes", SQLITE_CountRows }, |
| 153 | { "empty_result_callbacks", SQLITE_NullCallback }, |
drh | 0cd2d4c | 2005-11-03 02:15:02 +0000 | [diff] [blame] | 154 | #ifndef SQLITE_OMIT_CHECK |
| 155 | { "ignore_check_constraints", SQLITE_IgnoreChecks }, |
| 156 | #endif |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 157 | /* The following is VERY experimental */ |
drh | f404083 | 2004-10-22 20:29:21 +0000 | [diff] [blame] | 158 | { "writable_schema", SQLITE_WriteSchema }, |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 159 | { "omit_readlock", SQLITE_NoReadlock }, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 160 | |
| 161 | /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted |
| 162 | ** flag if there are any active statements. */ |
| 163 | { "read_uncommitted", SQLITE_ReadUncommitted }, |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 164 | }; |
| 165 | int i; |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 166 | const struct sPragmaType *p; |
| 167 | for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){ |
| 168 | if( sqlite3StrICmp(zLeft, p->zName)==0 ){ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 169 | sqlite3 *db = pParse->db; |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 170 | Vdbe *v; |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 171 | v = sqlite3GetVdbe(pParse); |
| 172 | if( v ){ |
| 173 | if( zRight==0 ){ |
drh | 722e95a | 2004-10-25 20:33:44 +0000 | [diff] [blame] | 174 | returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 ); |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 175 | }else{ |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 176 | if( getBoolean(zRight) ){ |
| 177 | db->flags |= p->mask; |
| 178 | }else{ |
| 179 | db->flags &= ~p->mask; |
| 180 | } |
drh | d89bd00 | 2005-01-22 03:03:54 +0000 | [diff] [blame] | 181 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 182 | /* If one of these pragmas is executed, any prepared statements |
| 183 | ** need to be recompiled. |
| 184 | */ |
| 185 | sqlite3VdbeAddOp(v, OP_Expire, 0, 0); |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 186 | } |
| 187 | return 1; |
| 188 | } |
| 189 | } |
| 190 | return 0; |
| 191 | } |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 192 | #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 193 | |
| 194 | /* |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 195 | ** Process a pragma statement. |
| 196 | ** |
| 197 | ** Pragmas are of this form: |
| 198 | ** |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 199 | ** PRAGMA [database.]id [= value] |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 200 | ** |
| 201 | ** The identifier might also be a string. The value is a string, and |
| 202 | ** identifier, or a number. If minusFlag is true, then the value is |
| 203 | ** a number that was preceded by a minus sign. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 204 | ** |
| 205 | ** If the left side is "database.id" then pId1 is the database name |
| 206 | ** and pId2 is the id. If the left side is just "id" then pId1 is the |
| 207 | ** id and pId2 is any empty string. |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 208 | */ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 209 | void sqlite3Pragma( |
| 210 | Parse *pParse, |
| 211 | Token *pId1, /* First part of [database.]id field */ |
| 212 | Token *pId2, /* Second part of [database.]id field, or NULL */ |
| 213 | Token *pValue, /* Token for <value>, or NULL */ |
| 214 | int minusFlag /* True if a '-' sign preceded <value> */ |
| 215 | ){ |
| 216 | char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */ |
| 217 | char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */ |
| 218 | const char *zDb = 0; /* The database name */ |
| 219 | Token *pId; /* Pointer to <id> token */ |
| 220 | int iDb; /* Database index for <database> */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 221 | sqlite3 *db = pParse->db; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 222 | Db *pDb; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 223 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 224 | if( v==0 ) return; |
| 225 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 226 | /* Interpret the [database.] part of the pragma statement. iDb is the |
| 227 | ** index of the database this pragma is being applied to in db.aDb[]. */ |
| 228 | iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); |
| 229 | if( iDb<0 ) return; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 230 | pDb = &db->aDb[iDb]; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 231 | |
| 232 | zLeft = sqlite3NameFromToken(pId); |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 233 | if( !zLeft ) return; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 234 | if( minusFlag ){ |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 235 | zRight = sqlite3MPrintf("-%T", pValue); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 236 | }else{ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 237 | zRight = sqlite3NameFromToken(pValue); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 238 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 239 | |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 240 | zDb = ((iDb>0)?pDb->zName:0); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 241 | if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 242 | goto pragma_out; |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 245 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 246 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 247 | ** PRAGMA [database.]default_cache_size |
| 248 | ** PRAGMA [database.]default_cache_size=N |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 249 | ** |
| 250 | ** The first form reports the current persistent setting for the |
| 251 | ** page cache size. The value returned is the maximum number of |
| 252 | ** pages in the page cache. The second form sets both the current |
| 253 | ** page cache size value and the persistent page cache size value |
| 254 | ** stored in the database file. |
| 255 | ** |
| 256 | ** The default cache size is stored in meta-value 2 of page 1 of the |
| 257 | ** database file. The cache size is actually the absolute value of |
| 258 | ** this memory location. The sign of meta-value 2 determines the |
| 259 | ** synchronous setting. A negative value means synchronous is off |
| 260 | ** and a positive value means synchronous is on. |
| 261 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 262 | if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 263 | static const VdbeOpList getCacheSize[] = { |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 264 | { OP_ReadCookie, 0, 2, 0}, /* 0 */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 265 | { OP_AbsValue, 0, 0, 0}, |
| 266 | { OP_Dup, 0, 0, 0}, |
| 267 | { OP_Integer, 0, 0, 0}, |
| 268 | { OP_Ne, 0, 6, 0}, |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 269 | { OP_Integer, 0, 0, 0}, /* 5 */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 270 | { OP_Callback, 1, 0, 0}, |
| 271 | }; |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 272 | int addr; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 273 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 274 | if( !zRight ){ |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 275 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 276 | sqlite3VdbeSetColName(v, 0, "cache_size", P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 277 | addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 278 | sqlite3VdbeChangeP1(v, addr, iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 279 | sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 280 | }else{ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 281 | int size = atoi(zRight); |
| 282 | if( size<0 ) size = -size; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 283 | sqlite3BeginWriteOperation(pParse, 0, iDb); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 284 | sqlite3VdbeAddOp(v, OP_Integer, size, 0); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 285 | sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 286 | addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
| 287 | sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3); |
| 288 | sqlite3VdbeAddOp(v, OP_Negative, 0, 0); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 289 | sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 290 | pDb->pSchema->cache_size = size; |
| 291 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 292 | } |
| 293 | }else |
| 294 | |
| 295 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 296 | ** PRAGMA [database.]page_size |
| 297 | ** PRAGMA [database.]page_size=N |
| 298 | ** |
| 299 | ** The first form reports the current setting for the |
| 300 | ** database page size in bytes. The second form sets the |
| 301 | ** database page size value. The value can only be set if |
| 302 | ** the database has not yet been created. |
| 303 | */ |
| 304 | if( sqlite3StrICmp(zLeft,"page_size")==0 ){ |
| 305 | Btree *pBt = pDb->pBt; |
| 306 | if( !zRight ){ |
| 307 | int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0; |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 308 | returnSingleInt(pParse, "page_size", size); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 309 | }else{ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 310 | sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 311 | } |
| 312 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 313 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 314 | |
| 315 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 316 | ** PRAGMA [database.]auto_vacuum |
| 317 | ** PRAGMA [database.]auto_vacuum=N |
| 318 | ** |
| 319 | ** Get or set the (boolean) value of the database 'auto-vacuum' parameter. |
| 320 | */ |
| 321 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 322 | if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){ |
| 323 | Btree *pBt = pDb->pBt; |
| 324 | if( !zRight ){ |
| 325 | int auto_vacuum = |
| 326 | pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM; |
| 327 | returnSingleInt(pParse, "auto_vacuum", auto_vacuum); |
| 328 | }else{ |
| 329 | sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight)); |
| 330 | } |
| 331 | }else |
| 332 | #endif |
| 333 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 334 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 335 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 336 | ** PRAGMA [database.]cache_size |
| 337 | ** PRAGMA [database.]cache_size=N |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 338 | ** |
| 339 | ** The first form reports the current local setting for the |
| 340 | ** page cache size. The local setting can be different from |
| 341 | ** the persistent cache size value that is stored in the database |
| 342 | ** file itself. The value returned is the maximum number of |
| 343 | ** pages in the page cache. The second form sets the local |
| 344 | ** page cache size value. It does not change the persistent |
| 345 | ** cache size stored on the disk so the cache size will revert |
| 346 | ** to its default value when the database is closed and reopened. |
| 347 | ** N should be a positive integer. |
| 348 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 349 | if( sqlite3StrICmp(zLeft,"cache_size")==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 350 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 351 | if( !zRight ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 352 | returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 353 | }else{ |
| 354 | int size = atoi(zRight); |
| 355 | if( size<0 ) size = -size; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 356 | pDb->pSchema->cache_size = size; |
| 357 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 358 | } |
| 359 | }else |
| 360 | |
| 361 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 362 | ** PRAGMA temp_store |
| 363 | ** PRAGMA temp_store = "default"|"memory"|"file" |
| 364 | ** |
| 365 | ** Return or set the local value of the temp_store flag. Changing |
| 366 | ** the local value does not make changes to the disk file and the default |
| 367 | ** value will be restored the next time the database is opened. |
| 368 | ** |
| 369 | ** Note that it is possible for the library compile-time options to |
| 370 | ** override this setting |
| 371 | */ |
| 372 | if( sqlite3StrICmp(zLeft, "temp_store")==0 ){ |
| 373 | if( !zRight ){ |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 374 | returnSingleInt(pParse, "temp_store", db->temp_store); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 375 | }else{ |
| 376 | changeTempStorage(pParse, zRight); |
| 377 | } |
| 378 | }else |
| 379 | |
| 380 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 381 | ** PRAGMA temp_store_directory |
| 382 | ** PRAGMA temp_store_directory = ""|"directory_name" |
| 383 | ** |
| 384 | ** Return or set the local value of the temp_store_directory flag. Changing |
| 385 | ** the value sets a specific directory to be used for temporary files. |
| 386 | ** Setting to a null string reverts to the default temporary directory search. |
| 387 | ** If temporary directory is changed, then invalidateTempStorage. |
| 388 | ** |
| 389 | */ |
| 390 | if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){ |
| 391 | if( !zRight ){ |
| 392 | if( sqlite3_temp_directory ){ |
| 393 | sqlite3VdbeSetNumCols(v, 1); |
| 394 | sqlite3VdbeSetColName(v, 0, "temp_store_directory", P3_STATIC); |
| 395 | sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0); |
| 396 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
| 397 | } |
| 398 | }else{ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 399 | if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){ |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 400 | sqlite3ErrorMsg(pParse, "not a writable directory"); |
| 401 | goto pragma_out; |
| 402 | } |
| 403 | if( TEMP_STORE==0 |
| 404 | || (TEMP_STORE==1 && db->temp_store<=1) |
| 405 | || (TEMP_STORE==2 && db->temp_store==1) |
| 406 | ){ |
| 407 | invalidateTempStorage(pParse); |
| 408 | } |
| 409 | sqliteFree(sqlite3_temp_directory); |
| 410 | if( zRight[0] ){ |
| 411 | sqlite3_temp_directory = zRight; |
| 412 | zRight = 0; |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 413 | }else{ |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 414 | sqlite3_temp_directory = 0; |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | }else |
| 418 | |
| 419 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 420 | ** PRAGMA [database.]synchronous |
| 421 | ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 422 | ** |
| 423 | ** Return or set the local value of the synchronous flag. Changing |
| 424 | ** the local value does not make changes to the disk file and the |
| 425 | ** default value will be restored the next time the database is |
| 426 | ** opened. |
| 427 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 428 | if( sqlite3StrICmp(zLeft,"synchronous")==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 429 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 430 | if( !zRight ){ |
drh | 5bb7ffe | 2004-09-02 15:14:00 +0000 | [diff] [blame] | 431 | returnSingleInt(pParse, "synchronous", pDb->safety_level-1); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 432 | }else{ |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 433 | if( !db->autoCommit ){ |
| 434 | sqlite3ErrorMsg(pParse, |
| 435 | "Safety level may not be changed inside a transaction"); |
| 436 | }else{ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 437 | pDb->safety_level = getSafetyLevel(zRight)+1; |
| 438 | sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level); |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 439 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 440 | } |
| 441 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 442 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 443 | |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 444 | #ifndef SQLITE_OMIT_FLAG_PRAGMAS |
drh | 8722318 | 2004-02-21 14:00:29 +0000 | [diff] [blame] | 445 | if( flagPragma(pParse, zLeft, zRight) ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 446 | /* The flagPragma() subroutine also generates any necessary code |
| 447 | ** there is nothing more to do here */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 448 | }else |
drh | bf21627 | 2005-02-26 18:10:44 +0000 | [diff] [blame] | 449 | #endif /* SQLITE_OMIT_FLAG_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 450 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 451 | #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 452 | /* |
| 453 | ** PRAGMA table_info(<table>) |
| 454 | ** |
| 455 | ** Return a single row for each column of the named table. The columns of |
| 456 | ** the returned data set are: |
| 457 | ** |
| 458 | ** cid: Column id (numbered from left to right, starting at 0) |
| 459 | ** name: Column name |
| 460 | ** type: Column declaration type. |
| 461 | ** notnull: True if 'NOT NULL' is part of column declaration |
| 462 | ** dflt_value: The default value for the column, if any. |
| 463 | */ |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 464 | if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 465 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 466 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 467 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 468 | if( pTab ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 469 | int i; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 470 | sqlite3VdbeSetNumCols(v, 6); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 471 | sqlite3VdbeSetColName(v, 0, "cid", P3_STATIC); |
| 472 | sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); |
| 473 | sqlite3VdbeSetColName(v, 2, "type", P3_STATIC); |
| 474 | sqlite3VdbeSetColName(v, 3, "notnull", P3_STATIC); |
| 475 | sqlite3VdbeSetColName(v, 4, "dflt_value", P3_STATIC); |
| 476 | sqlite3VdbeSetColName(v, 5, "pk", P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 477 | sqlite3ViewGetColumnNames(pParse, pTab); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 478 | for(i=0; i<pTab->nCol; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 479 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 480 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[i].zName, 0); |
| 481 | sqlite3VdbeOp3(v, OP_String8, 0, 0, |
drh | 701a0ae | 2004-02-22 20:05:00 +0000 | [diff] [blame] | 482 | pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 483 | sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0); |
danielk1977 | 7977a17 | 2004-11-09 12:44:37 +0000 | [diff] [blame] | 484 | sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 485 | sqlite3VdbeAddOp(v, OP_Integer, pTab->aCol[i].isPrimKey, 0); |
| 486 | sqlite3VdbeAddOp(v, OP_Callback, 6, 0); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | }else |
| 490 | |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 491 | if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 492 | Index *pIdx; |
| 493 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 494 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 495 | pIdx = sqlite3FindIndex(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 496 | if( pIdx ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 497 | int i; |
| 498 | pTab = pIdx->pTable; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 499 | sqlite3VdbeSetNumCols(v, 3); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 500 | sqlite3VdbeSetColName(v, 0, "seqno", P3_STATIC); |
| 501 | sqlite3VdbeSetColName(v, 1, "cid", P3_STATIC); |
| 502 | sqlite3VdbeSetColName(v, 2, "name", P3_STATIC); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 503 | for(i=0; i<pIdx->nColumn; i++){ |
| 504 | int cnum = pIdx->aiColumn[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 505 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
| 506 | sqlite3VdbeAddOp(v, OP_Integer, cnum, 0); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 507 | assert( pTab->nCol>cnum ); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 508 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 509 | sqlite3VdbeAddOp(v, OP_Callback, 3, 0); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | }else |
| 513 | |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 514 | if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 515 | Index *pIdx; |
| 516 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 517 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 518 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 519 | if( pTab ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 520 | v = sqlite3GetVdbe(pParse); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 521 | pIdx = pTab->pIndex; |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 522 | if( pIdx ){ |
| 523 | int i = 0; |
| 524 | sqlite3VdbeSetNumCols(v, 3); |
| 525 | sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC); |
| 526 | sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); |
| 527 | sqlite3VdbeSetColName(v, 2, "unique", P3_STATIC); |
| 528 | while(pIdx){ |
| 529 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
| 530 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); |
| 531 | sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); |
| 532 | sqlite3VdbeAddOp(v, OP_Callback, 3, 0); |
| 533 | ++i; |
| 534 | pIdx = pIdx->pNext; |
| 535 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | }else |
| 539 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 540 | if( sqlite3StrICmp(zLeft, "database_list")==0 ){ |
| 541 | int i; |
| 542 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 543 | sqlite3VdbeSetNumCols(v, 3); |
| 544 | sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC); |
| 545 | sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); |
| 546 | sqlite3VdbeSetColName(v, 2, "file", P3_STATIC); |
| 547 | for(i=0; i<db->nDb; i++){ |
| 548 | if( db->aDb[i].pBt==0 ) continue; |
| 549 | assert( db->aDb[i].zName!=0 ); |
| 550 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
| 551 | sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0); |
| 552 | sqlite3VdbeOp3(v, OP_String8, 0, 0, |
| 553 | sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); |
| 554 | sqlite3VdbeAddOp(v, OP_Callback, 3, 0); |
| 555 | } |
| 556 | }else |
danielk1977 | 48af65a | 2005-02-09 03:20:37 +0000 | [diff] [blame] | 557 | |
| 558 | if( sqlite3StrICmp(zLeft, "collation_list")==0 ){ |
| 559 | int i = 0; |
| 560 | HashElem *p; |
| 561 | sqlite3VdbeSetNumCols(v, 2); |
| 562 | sqlite3VdbeSetColName(v, 0, "seq", P3_STATIC); |
| 563 | sqlite3VdbeSetColName(v, 1, "name", P3_STATIC); |
| 564 | for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ |
| 565 | CollSeq *pColl = (CollSeq *)sqliteHashData(p); |
| 566 | sqlite3VdbeAddOp(v, OP_Integer, i++, 0); |
| 567 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0); |
| 568 | sqlite3VdbeAddOp(v, OP_Callback, 2, 0); |
| 569 | } |
| 570 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 571 | #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ |
| 572 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 573 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 574 | if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 575 | FKey *pFK; |
| 576 | Table *pTab; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 577 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
drh | 2783e4b | 2004-10-05 15:42:53 +0000 | [diff] [blame] | 578 | pTab = sqlite3FindTable(db, zRight, zDb); |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 579 | if( pTab ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 580 | v = sqlite3GetVdbe(pParse); |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 581 | pFK = pTab->pFKey; |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 582 | if( pFK ){ |
| 583 | int i = 0; |
| 584 | sqlite3VdbeSetNumCols(v, 5); |
| 585 | sqlite3VdbeSetColName(v, 0, "id", P3_STATIC); |
| 586 | sqlite3VdbeSetColName(v, 1, "seq", P3_STATIC); |
| 587 | sqlite3VdbeSetColName(v, 2, "table", P3_STATIC); |
| 588 | sqlite3VdbeSetColName(v, 3, "from", P3_STATIC); |
| 589 | sqlite3VdbeSetColName(v, 4, "to", P3_STATIC); |
| 590 | while(pFK){ |
| 591 | int j; |
| 592 | for(j=0; j<pFK->nCol; j++){ |
drh | 2f47149 | 2005-06-23 03:15:07 +0000 | [diff] [blame] | 593 | char *zCol = pFK->aCol[j].zCol; |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 594 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
| 595 | sqlite3VdbeAddOp(v, OP_Integer, j, 0); |
| 596 | sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0); |
| 597 | sqlite3VdbeOp3(v, OP_String8, 0, 0, |
| 598 | pTab->aCol[pFK->aCol[j].iFrom].zName, 0); |
drh | 2f47149 | 2005-06-23 03:15:07 +0000 | [diff] [blame] | 599 | sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0); |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 600 | sqlite3VdbeAddOp(v, OP_Callback, 5, 0); |
| 601 | } |
| 602 | ++i; |
| 603 | pFK = pFK->pNextFrom; |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 604 | } |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | }else |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 608 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
drh | 78100cc | 2003-08-23 22:40:53 +0000 | [diff] [blame] | 609 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 610 | #ifndef NDEBUG |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 611 | if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ |
| 612 | extern void sqlite3ParserTrace(FILE*, char *); |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 613 | if( zRight ){ |
| 614 | if( getBoolean(zRight) ){ |
| 615 | sqlite3ParserTrace(stderr, "parser: "); |
| 616 | }else{ |
| 617 | sqlite3ParserTrace(0, 0); |
| 618 | } |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 619 | } |
| 620 | }else |
| 621 | #endif |
| 622 | |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 623 | /* Reinstall the LIKE and GLOB functions. The variant of LIKE |
| 624 | ** used will be case sensitive or not depending on the RHS. |
| 625 | */ |
| 626 | if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){ |
| 627 | if( zRight ){ |
| 628 | sqlite3RegisterLikeFunctions(db, getBoolean(zRight)); |
| 629 | } |
| 630 | }else |
| 631 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 632 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 633 | if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 634 | int i, j, addr; |
| 635 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 636 | /* Code that appears at the end of the integrity check. If no error |
| 637 | ** messages have been generated, output OK. Otherwise output the |
| 638 | ** error message |
| 639 | */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 640 | static const VdbeOpList endCode[] = { |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 641 | { OP_MemLoad, 0, 0, 0}, |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 642 | { OP_Integer, 0, 0, 0}, |
| 643 | { OP_Ne, 0, 0, 0}, /* 2 */ |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 644 | { OP_String8, 0, 0, "ok"}, |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 645 | { OP_Callback, 1, 0, 0}, |
| 646 | }; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 647 | |
| 648 | /* Initialize the VDBE program */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 649 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 650 | sqlite3VdbeSetNumCols(v, 1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 651 | sqlite3VdbeSetColName(v, 0, "integrity_check", P3_STATIC); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 652 | sqlite3VdbeAddOp(v, OP_MemInt, 0, 0); /* Initialize error count to 0 */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 653 | |
| 654 | /* Do an integrity check on each database file */ |
| 655 | for(i=0; i<db->nDb; i++){ |
| 656 | HashElem *x; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 657 | Hash *pTbls; |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 658 | int cnt = 0; |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 659 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 660 | if( OMIT_TEMPDB && i==1 ) continue; |
| 661 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 662 | sqlite3CodeVerifySchema(pParse, i); |
| 663 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 664 | /* Do an integrity check of the B-Tree |
| 665 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 666 | pTbls = &db->aDb[i].pSchema->tblHash; |
| 667 | for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 668 | Table *pTab = sqliteHashData(x); |
| 669 | Index *pIdx; |
| 670 | sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0); |
| 671 | cnt++; |
| 672 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 673 | sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0); |
| 674 | cnt++; |
| 675 | } |
| 676 | } |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 677 | assert( cnt>0 ); |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 678 | sqlite3VdbeAddOp(v, OP_IntegrityCk, cnt, i); |
| 679 | sqlite3VdbeAddOp(v, OP_Dup, 0, 1); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 680 | addr = sqlite3VdbeOp3(v, OP_String8, 0, 0, "ok", P3_STATIC); |
drh | 23cc57f | 2005-10-06 13:59:26 +0000 | [diff] [blame] | 681 | sqlite3VdbeAddOp(v, OP_Eq, 0, addr+7); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 682 | sqlite3VdbeOp3(v, OP_String8, 0, 0, |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 683 | sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName), |
| 684 | P3_DYNAMIC); |
| 685 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 686 | sqlite3VdbeAddOp(v, OP_Concat, 0, 1); |
drh | 7906975 | 2004-05-22 21:30:40 +0000 | [diff] [blame] | 687 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 688 | sqlite3VdbeAddOp(v, OP_MemIncr, 1, 0); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 689 | |
| 690 | /* Make sure all the indices are constructed correctly. |
| 691 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 692 | sqlite3CodeVerifySchema(pParse, i); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 693 | for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 694 | Table *pTab = sqliteHashData(x); |
| 695 | Index *pIdx; |
| 696 | int loopTop; |
| 697 | |
| 698 | if( pTab->pIndex==0 ) continue; |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 699 | sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 700 | sqlite3VdbeAddOp(v, OP_MemInt, 0, 1); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 701 | loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0); |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 702 | sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 703 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 704 | int jmp2; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 705 | static const VdbeOpList idxErr[] = { |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 706 | { OP_MemIncr, 1, 0, 0}, |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 707 | { OP_String8, 0, 0, "rowid "}, |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 708 | { OP_Rowid, 1, 0, 0}, |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 709 | { OP_String8, 0, 0, " missing from index "}, |
| 710 | { OP_String8, 0, 0, 0}, /* 4 */ |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 711 | { OP_Concat, 2, 0, 0}, |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 712 | { OP_Callback, 1, 0, 0}, |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 713 | }; |
drh | 51846b5 | 2004-05-28 16:00:21 +0000 | [diff] [blame] | 714 | sqlite3GenerateIndexKey(v, pIdx, 1); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 715 | jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0); |
| 716 | addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); |
| 717 | sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 718 | sqlite3VdbeJumpHere(v, jmp2); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 719 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 720 | sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 721 | sqlite3VdbeJumpHere(v, loopTop); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 722 | for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 723 | static const VdbeOpList cntIdx[] = { |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 724 | { OP_MemInt, 0, 2, 0}, |
| 725 | { OP_Rewind, 0, 0, 0}, /* 1 */ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 726 | { OP_MemIncr, 1, 2, 0}, |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 727 | { OP_Next, 0, 0, 0}, /* 3 */ |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 728 | { OP_MemLoad, 1, 0, 0}, |
| 729 | { OP_MemLoad, 2, 0, 0}, |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 730 | { OP_Eq, 0, 0, 0}, /* 6 */ |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 731 | { OP_MemIncr, 1, 0, 0}, |
drh | 290c194 | 2004-08-21 17:54:45 +0000 | [diff] [blame] | 732 | { OP_String8, 0, 0, "wrong # of entries in index "}, |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 733 | { OP_String8, 0, 0, 0}, /* 9 */ |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 734 | { OP_Concat, 0, 0, 0}, |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 735 | { OP_Callback, 1, 0, 0}, |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 736 | }; |
| 737 | if( pIdx->tnum==0 ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 738 | addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 739 | sqlite3VdbeChangeP1(v, addr+1, j+2); |
| 740 | sqlite3VdbeChangeP2(v, addr+1, addr+4); |
| 741 | sqlite3VdbeChangeP1(v, addr+3, j+2); |
| 742 | sqlite3VdbeChangeP2(v, addr+3, addr+2); |
| 743 | sqlite3VdbeJumpHere(v, addr+6); |
| 744 | sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC); |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 748 | addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 749 | sqlite3VdbeJumpHere(v, addr+2); |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 750 | }else |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 751 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 752 | |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 753 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 754 | /* |
| 755 | ** PRAGMA encoding |
| 756 | ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" |
| 757 | ** |
| 758 | ** In it's first form, this pragma returns the encoding of the main |
| 759 | ** database. If the database is not initialized, it is initialized now. |
| 760 | ** |
| 761 | ** The second form of this pragma is a no-op if the main database file |
| 762 | ** has not already been initialized. In this case it sets the default |
| 763 | ** encoding that will be used for the main database file if a new file |
| 764 | ** is created. If an existing main database file is opened, then the |
| 765 | ** default text encoding for the existing database is used. |
| 766 | ** |
| 767 | ** In all cases new databases created using the ATTACH command are |
| 768 | ** created to use the same default text encoding as the main database. If |
| 769 | ** the main database has not been initialized and/or created when ATTACH |
| 770 | ** is executed, this is done before the ATTACH operation. |
| 771 | ** |
| 772 | ** In the second form this pragma sets the text encoding to be used in |
| 773 | ** new database files created using this database handle. It is only |
| 774 | ** useful if invoked immediately after the main database i |
| 775 | */ |
| 776 | if( sqlite3StrICmp(zLeft, "encoding")==0 ){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 777 | static struct EncName { |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 778 | char *zName; |
| 779 | u8 enc; |
| 780 | } encnames[] = { |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 781 | { "UTF-8", SQLITE_UTF8 }, |
| 782 | { "UTF8", SQLITE_UTF8 }, |
| 783 | { "UTF-16le", SQLITE_UTF16LE }, |
| 784 | { "UTF16le", SQLITE_UTF16LE }, |
| 785 | { "UTF-16be", SQLITE_UTF16BE }, |
| 786 | { "UTF16be", SQLITE_UTF16BE }, |
| 787 | { "UTF-16", 0 /* Filled in at run-time */ }, |
| 788 | { "UTF16", 0 /* Filled in at run-time */ }, |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 789 | { 0, 0 } |
| 790 | }; |
| 791 | struct EncName *pEnc; |
drh | 998da3a | 2004-06-19 15:22:56 +0000 | [diff] [blame] | 792 | encnames[6].enc = encnames[7].enc = SQLITE_UTF16NATIVE; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 793 | if( !zRight ){ /* "PRAGMA encoding" */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 794 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 795 | sqlite3VdbeSetNumCols(v, 1); |
| 796 | sqlite3VdbeSetColName(v, 0, "encoding", P3_STATIC); |
| 797 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
| 798 | for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 799 | if( pEnc->enc==ENC(pParse->db) ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 800 | sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC); |
| 801 | break; |
| 802 | } |
| 803 | } |
| 804 | sqlite3VdbeAddOp(v, OP_Callback, 1, 0); |
| 805 | }else{ /* "PRAGMA encoding = XXX" */ |
| 806 | /* Only change the value of sqlite.enc if the database handle is not |
| 807 | ** initialized. If the main database exists, the new sqlite.enc value |
| 808 | ** will be overwritten when the schema is next loaded. If it does not |
| 809 | ** already exists, it will be created to use the new encoding value. |
| 810 | */ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 811 | if( |
| 812 | !(DbHasProperty(db, 0, DB_SchemaLoaded)) || |
| 813 | DbHasProperty(db, 0, DB_Empty) |
| 814 | ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 815 | for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
| 816 | if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 817 | ENC(pParse->db) = pEnc->enc; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | } |
| 821 | if( !pEnc->zName ){ |
drh | 5260f7e | 2004-06-26 19:35:29 +0000 | [diff] [blame] | 822 | sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | } |
| 826 | }else |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 827 | #endif /* SQLITE_OMIT_UTF16 */ |
| 828 | |
| 829 | #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 830 | /* |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 831 | ** PRAGMA [database.]schema_version |
| 832 | ** PRAGMA [database.]schema_version = <integer> |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 833 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 834 | ** PRAGMA [database.]user_version |
| 835 | ** PRAGMA [database.]user_version = <integer> |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 836 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 837 | ** The pragma's schema_version and user_version are used to set or get |
| 838 | ** the value of the schema-version and user-version, respectively. Both |
| 839 | ** the schema-version and the user-version are 32-bit signed integers |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 840 | ** stored in the database header. |
| 841 | ** |
| 842 | ** The schema-cookie is usually only manipulated internally by SQLite. It |
| 843 | ** is incremented by SQLite whenever the database schema is modified (by |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 844 | ** creating or dropping a table or index). The schema version is used by |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 845 | ** SQLite each time a query is executed to ensure that the internal cache |
| 846 | ** of the schema used when compiling the SQL query matches the schema of |
| 847 | ** the database against which the compiled query is actually executed. |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 848 | ** Subverting this mechanism by using "PRAGMA schema_version" to modify |
| 849 | ** the schema-version is potentially dangerous and may lead to program |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 850 | ** crashes or database corruption. Use with caution! |
| 851 | ** |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 852 | ** 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] | 853 | ** applications for any purpose. |
| 854 | */ |
danielk1977 | b92b70b | 2004-11-12 16:11:59 +0000 | [diff] [blame] | 855 | if( sqlite3StrICmp(zLeft, "schema_version")==0 || |
| 856 | sqlite3StrICmp(zLeft, "user_version")==0 ){ |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 857 | |
| 858 | int iCookie; /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */ |
| 859 | if( zLeft[0]=='s' || zLeft[0]=='S' ){ |
| 860 | iCookie = 0; |
| 861 | }else{ |
| 862 | iCookie = 5; |
| 863 | } |
| 864 | |
| 865 | if( zRight ){ |
| 866 | /* Write the specified cookie value */ |
| 867 | static const VdbeOpList setCookie[] = { |
| 868 | { OP_Transaction, 0, 1, 0}, /* 0 */ |
| 869 | { OP_Integer, 0, 0, 0}, /* 1 */ |
| 870 | { OP_SetCookie, 0, 0, 0}, /* 2 */ |
| 871 | }; |
| 872 | int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); |
| 873 | sqlite3VdbeChangeP1(v, addr, iDb); |
| 874 | sqlite3VdbeChangeP1(v, addr+1, atoi(zRight)); |
| 875 | sqlite3VdbeChangeP1(v, addr+2, iDb); |
| 876 | sqlite3VdbeChangeP2(v, addr+2, iCookie); |
| 877 | }else{ |
| 878 | /* Read the specified cookie value */ |
| 879 | static const VdbeOpList readCookie[] = { |
| 880 | { OP_ReadCookie, 0, 0, 0}, /* 0 */ |
| 881 | { OP_Callback, 1, 0, 0} |
| 882 | }; |
| 883 | int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie); |
| 884 | sqlite3VdbeChangeP1(v, addr, iDb); |
| 885 | sqlite3VdbeChangeP2(v, addr, iCookie); |
| 886 | sqlite3VdbeSetNumCols(v, 1); |
| 887 | } |
| 888 | } |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 889 | #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 890 | |
dougcurrie | 81c95ef | 2004-06-18 23:21:47 +0000 | [diff] [blame] | 891 | #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 892 | /* |
| 893 | ** Report the current state of file logs for all databases |
| 894 | */ |
| 895 | if( sqlite3StrICmp(zLeft, "lock_status")==0 ){ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 896 | static const char *const azLockName[] = { |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 897 | "unlocked", "shared", "reserved", "pending", "exclusive" |
| 898 | }; |
| 899 | int i; |
| 900 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 901 | sqlite3VdbeSetNumCols(v, 2); |
| 902 | sqlite3VdbeSetColName(v, 0, "database", P3_STATIC); |
| 903 | sqlite3VdbeSetColName(v, 1, "status", P3_STATIC); |
| 904 | for(i=0; i<db->nDb; i++){ |
| 905 | Btree *pBt; |
| 906 | Pager *pPager; |
| 907 | if( db->aDb[i].zName==0 ) continue; |
drh | a67367e | 2005-09-17 16:36:55 +0000 | [diff] [blame] | 908 | sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 909 | pBt = db->aDb[i].pBt; |
| 910 | if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ |
drh | a67367e | 2005-09-17 16:36:55 +0000 | [diff] [blame] | 911 | sqlite3VdbeOp3(v, OP_String8, 0, 0, "closed", P3_STATIC); |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 912 | }else{ |
| 913 | int j = sqlite3pager_lockstate(pPager); |
drh | a67367e | 2005-09-17 16:36:55 +0000 | [diff] [blame] | 914 | sqlite3VdbeOp3(v, OP_String8, 0, 0, |
drh | 89ac8c1 | 2004-06-09 14:17:20 +0000 | [diff] [blame] | 915 | (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC); |
| 916 | } |
| 917 | sqlite3VdbeAddOp(v, OP_Callback, 2, 0); |
| 918 | } |
| 919 | }else |
| 920 | #endif |
| 921 | |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 922 | #ifdef SQLITE_SSE |
| 923 | /* |
| 924 | ** Check to see if the sqlite_statements table exists. Create it |
| 925 | ** if it does not. |
| 926 | */ |
| 927 | if( sqlite3StrICmp(zLeft, "create_sqlite_statement_table")==0 ){ |
danielk1977 | 3a3f38e | 2005-05-22 06:49:56 +0000 | [diff] [blame] | 928 | extern int sqlite3CreateStatementsTable(Parse*); |
| 929 | sqlite3CreateStatementsTable(pParse); |
drh | 89dec81 | 2005-04-28 19:03:37 +0000 | [diff] [blame] | 930 | }else |
| 931 | #endif |
| 932 | |
drh | 3c4f2a4 | 2005-12-08 18:12:56 +0000 | [diff] [blame] | 933 | #if SQLITE_HAS_CODEC |
| 934 | if( sqlite3StrICmp(zLeft, "key")==0 ){ |
| 935 | sqlite3_key(db, zRight, strlen(zRight)); |
| 936 | }else |
| 937 | #endif |
| 938 | |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 939 | {} |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 940 | |
| 941 | if( v ){ |
| 942 | /* Code an OP_Expire at the end of each PRAGMA program to cause |
| 943 | ** the VDBE implementing the pragma to expire. Most (all?) pragmas |
| 944 | ** are only valid for a single execution. |
| 945 | */ |
| 946 | sqlite3VdbeAddOp(v, OP_Expire, 1, 0); |
| 947 | } |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 948 | pragma_out: |
drh | c11d4f9 | 2003-04-06 21:08:24 +0000 | [diff] [blame] | 949 | sqliteFree(zLeft); |
| 950 | sqliteFree(zRight); |
| 951 | } |
drh | 13d7042 | 2004-11-13 15:59:14 +0000 | [diff] [blame] | 952 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 953 | #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */ |