danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2005 May 25 |
| 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 the implementation of the sqlite3_prepare() |
| 13 | ** interface, and routines that contribute to loading the database schema |
| 14 | ** from disk. |
| 15 | ** |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame^] | 16 | ** $Id: prepare.c,v 1.58 2007/08/22 02:56:44 drh Exp $ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | |
| 21 | /* |
| 22 | ** Fill the InitData structure with an error message that indicates |
| 23 | ** that the database is corrupt. |
| 24 | */ |
| 25 | static void corruptSchema(InitData *pData, const char *zExtra){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 26 | if( !pData->db->mallocFailed ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 27 | sqlite3SetString(pData->pzErrMsg, "malformed database schema", |
| 28 | zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
| 29 | } |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 30 | pData->rc = SQLITE_CORRUPT; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* |
| 34 | ** This is the callback routine for the code that initializes the |
| 35 | ** database. See sqlite3Init() below for additional information. |
| 36 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 37 | ** |
| 38 | ** Each callback contains the following information: |
| 39 | ** |
| 40 | ** argv[0] = name of thing being created |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 41 | ** argv[1] = root page number for table or index. 0 for trigger or view. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 42 | ** argv[2] = SQL text for the CREATE statement. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 43 | ** |
| 44 | */ |
| 45 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ |
| 46 | InitData *pData = (InitData*)pInit; |
| 47 | sqlite3 *db = pData->db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 48 | int iDb = pData->iDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 49 | |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 50 | pData->rc = SQLITE_OK; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 51 | DbClearProperty(db, iDb, DB_Empty); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 52 | if( db->mallocFailed ){ |
drh | fe5a816 | 2006-08-12 13:28:23 +0000 | [diff] [blame] | 53 | corruptSchema(pData, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 54 | return SQLITE_NOMEM; |
| 55 | } |
| 56 | |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 57 | assert( argc==3 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 58 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 59 | if( argv[1]==0 ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 60 | corruptSchema(pData, 0); |
| 61 | return 1; |
| 62 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 63 | assert( iDb>=0 && iDb<db->nDb ); |
| 64 | if( argv[2] && argv[2][0] ){ |
| 65 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 66 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 67 | ** or executed. All the parser does is build the internal data |
| 68 | ** structures that describe the table, index, or view. |
| 69 | */ |
| 70 | char *zErr; |
| 71 | int rc; |
| 72 | assert( db->init.busy ); |
| 73 | db->init.iDb = iDb; |
| 74 | db->init.newTnum = atoi(argv[1]); |
| 75 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 76 | db->init.iDb = 0; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 77 | assert( rc!=SQLITE_OK || zErr==0 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 78 | if( SQLITE_OK!=rc ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 79 | pData->rc = rc; |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 80 | if( rc==SQLITE_NOMEM ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 81 | db->mallocFailed = 1; |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 82 | }else if( rc!=SQLITE_INTERRUPT ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 83 | corruptSchema(pData, zErr); |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 84 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 85 | sqlite3_free(zErr); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 86 | return 1; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 87 | } |
| 88 | }else{ |
| 89 | /* If the SQL column is blank it means this is an index that |
| 90 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 91 | ** constraint for a CREATE TABLE. The index should have already |
| 92 | ** been created when we processed the CREATE TABLE. All we have |
| 93 | ** to do here is record the root page number for that index. |
| 94 | */ |
| 95 | Index *pIndex; |
| 96 | pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); |
| 97 | if( pIndex==0 || pIndex->tnum!=0 ){ |
| 98 | /* This can occur if there exists an index on a TEMP table which |
| 99 | ** has the same name as another index on a permanent index. Since |
| 100 | ** the permanent table is hidden by the TEMP table, we can also |
| 101 | ** safely ignore the index on the permanent table. |
| 102 | */ |
| 103 | /* Do Nothing */; |
| 104 | }else{ |
| 105 | pIndex->tnum = atoi(argv[1]); |
| 106 | } |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | ** Attempt to read the database schema and initialize internal |
| 113 | ** data structures for a single database file. The index of the |
| 114 | ** database file is given by iDb. iDb==0 is used for the main |
| 115 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 116 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
| 117 | ** indicate success or failure. |
| 118 | */ |
| 119 | static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ |
| 120 | int rc; |
| 121 | BtCursor *curMain; |
| 122 | int size; |
| 123 | Table *pTab; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 124 | Db *pDb; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 125 | char const *azArg[4]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 126 | int meta[10]; |
| 127 | InitData initData; |
| 128 | char const *zMasterSchema; |
| 129 | char const *zMasterName = SCHEMA_TABLE(iDb); |
| 130 | |
| 131 | /* |
| 132 | ** The master database table has a structure like this |
| 133 | */ |
| 134 | static const char master_schema[] = |
| 135 | "CREATE TABLE sqlite_master(\n" |
| 136 | " type text,\n" |
| 137 | " name text,\n" |
| 138 | " tbl_name text,\n" |
| 139 | " rootpage integer,\n" |
| 140 | " sql text\n" |
| 141 | ")" |
| 142 | ; |
| 143 | #ifndef SQLITE_OMIT_TEMPDB |
| 144 | static const char temp_master_schema[] = |
| 145 | "CREATE TEMP TABLE sqlite_temp_master(\n" |
| 146 | " type text,\n" |
| 147 | " name text,\n" |
| 148 | " tbl_name text,\n" |
| 149 | " rootpage integer,\n" |
| 150 | " sql text\n" |
| 151 | ")" |
| 152 | ; |
| 153 | #else |
| 154 | #define temp_master_schema 0 |
| 155 | #endif |
| 156 | |
| 157 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 158 | assert( db->aDb[iDb].pSchema ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 159 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 160 | /* zMasterSchema and zInitScript are set to point at the master schema |
| 161 | ** and initialisation script appropriate for the database being |
| 162 | ** initialised. zMasterName is the name of the master table. |
| 163 | */ |
| 164 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 165 | zMasterSchema = temp_master_schema; |
| 166 | }else{ |
| 167 | zMasterSchema = master_schema; |
| 168 | } |
| 169 | zMasterName = SCHEMA_TABLE(iDb); |
| 170 | |
| 171 | /* Construct the schema tables. */ |
| 172 | sqlite3SafetyOff(db); |
| 173 | azArg[0] = zMasterName; |
| 174 | azArg[1] = "1"; |
| 175 | azArg[2] = zMasterSchema; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 176 | azArg[3] = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 177 | initData.db = db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 178 | initData.iDb = iDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 179 | initData.pzErrMsg = pzErrMsg; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 180 | rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 181 | if( rc ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 182 | sqlite3SafetyOn(db); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 183 | return initData.rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 184 | } |
| 185 | pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); |
| 186 | if( pTab ){ |
| 187 | pTab->readOnly = 1; |
| 188 | } |
| 189 | sqlite3SafetyOn(db); |
| 190 | |
| 191 | /* Create a cursor to hold the database open |
| 192 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 193 | pDb = &db->aDb[iDb]; |
| 194 | if( pDb->pBt==0 ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 195 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 196 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 197 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 198 | return SQLITE_OK; |
| 199 | } |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 200 | rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 201 | if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ |
| 202 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | /* Get the database meta information. |
| 207 | ** |
| 208 | ** Meta values are as follows: |
| 209 | ** meta[0] Schema cookie. Changes with each schema change. |
| 210 | ** meta[1] File format of schema layer. |
| 211 | ** meta[2] Size of the page cache. |
| 212 | ** meta[3] Use freelist if 0. Autovacuum if greater than zero. |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 213 | ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 214 | ** meta[5] The user cookie. Used by the application. |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 215 | ** meta[6] Incremental-vacuum flag. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 216 | ** meta[7] |
| 217 | ** meta[8] |
| 218 | ** meta[9] |
| 219 | ** |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 220 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 221 | ** the possible values of meta[4]. |
| 222 | */ |
| 223 | if( rc==SQLITE_OK ){ |
| 224 | int i; |
| 225 | for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 226 | rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 227 | } |
| 228 | if( rc ){ |
| 229 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
| 230 | sqlite3BtreeCloseCursor(curMain); |
| 231 | return rc; |
| 232 | } |
| 233 | }else{ |
| 234 | memset(meta, 0, sizeof(meta)); |
| 235 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 236 | pDb->pSchema->schema_cookie = meta[0]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 237 | |
| 238 | /* If opening a non-empty database, check the text encoding. For the |
| 239 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 240 | ** For an attached db, it is an error if the encoding is not the same |
| 241 | ** as sqlite3.enc. |
| 242 | */ |
| 243 | if( meta[4] ){ /* text encoding */ |
| 244 | if( iDb==0 ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 245 | /* If opening the main database, set ENC(db). */ |
| 246 | ENC(db) = (u8)meta[4]; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 247 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 248 | }else{ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 249 | /* If opening an attached database, the encoding much match ENC(db) */ |
| 250 | if( meta[4]!=ENC(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 251 | sqlite3BtreeCloseCursor(curMain); |
| 252 | sqlite3SetString(pzErrMsg, "attached databases must use the same" |
| 253 | " text encoding as main database", (char*)0); |
| 254 | return SQLITE_ERROR; |
| 255 | } |
| 256 | } |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 257 | }else{ |
| 258 | DbSetProperty(db, iDb, DB_Empty); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 259 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 260 | pDb->pSchema->enc = ENC(db); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 261 | |
| 262 | size = meta[2]; |
drh | c797d4d | 2007-05-08 01:08:49 +0000 | [diff] [blame] | 263 | if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 264 | pDb->pSchema->cache_size = size; |
| 265 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | ** file_format==1 Version 3.0.0. |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 269 | ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
| 270 | ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 271 | ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 272 | */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 273 | pDb->pSchema->file_format = meta[1]; |
| 274 | if( pDb->pSchema->file_format==0 ){ |
| 275 | pDb->pSchema->file_format = 1; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 276 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 277 | if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 278 | sqlite3BtreeCloseCursor(curMain); |
| 279 | sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0); |
| 280 | return SQLITE_ERROR; |
| 281 | } |
| 282 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 283 | |
| 284 | /* Read the schema information out of the schema tables |
| 285 | */ |
| 286 | assert( db->init.busy ); |
| 287 | if( rc==SQLITE_EMPTY ){ |
| 288 | /* For an empty database, there is nothing to read */ |
| 289 | rc = SQLITE_OK; |
| 290 | }else{ |
| 291 | char *zSql; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 292 | zSql = sqlite3MPrintf(db, |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 293 | "SELECT name, rootpage, sql FROM '%q'.%s", |
| 294 | db->aDb[iDb].zName, zMasterName); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 295 | sqlite3SafetyOff(db); |
| 296 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 297 | if( rc==SQLITE_ABORT ) rc = initData.rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 298 | sqlite3SafetyOn(db); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 299 | sqlite3_free(zSql); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 300 | #ifndef SQLITE_OMIT_ANALYZE |
| 301 | if( rc==SQLITE_OK ){ |
| 302 | sqlite3AnalysisLoad(db, iDb); |
| 303 | } |
| 304 | #endif |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 305 | sqlite3BtreeCloseCursor(curMain); |
| 306 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 307 | if( db->mallocFailed ){ |
danielk1977 | 9e12800 | 2006-01-18 16:51:35 +0000 | [diff] [blame] | 308 | /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 309 | rc = SQLITE_NOMEM; |
| 310 | sqlite3ResetInternalSchema(db, 0); |
| 311 | } |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 312 | if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ |
| 313 | /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider |
| 314 | ** the schema loaded, even if errors occured. In this situation the |
| 315 | ** current sqlite3_prepare() operation will fail, but the following one |
| 316 | ** will attempt to compile the supplied statement against whatever subset |
| 317 | ** of the schema was loaded before the error occured. The primary |
| 318 | ** purpose of this is to allow access to the sqlite_master table |
| 319 | ** even when it's contents have been corrupted. |
| 320 | */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 321 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 322 | rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 323 | } |
| 324 | return rc; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | ** Initialize all database files - the main database file, the file |
| 329 | ** used to store temporary tables, and any additional database files |
| 330 | ** created using ATTACH statements. Return a success code. If an |
| 331 | ** error occurs, write an error message into *pzErrMsg. |
| 332 | ** |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 333 | ** After a database is initialized, the DB_SchemaLoaded bit is set |
| 334 | ** bit is set in the flags field of the Db structure. If the database |
| 335 | ** file was of zero-length, then the DB_Empty flag is also set. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 336 | */ |
| 337 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 338 | int i, rc; |
danielk1977 | 5814c1a | 2007-08-13 14:41:19 +0000 | [diff] [blame] | 339 | int commit_internal = !(db->flags&SQLITE_InternChanges); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 340 | |
| 341 | if( db->init.busy ) return SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 342 | rc = SQLITE_OK; |
| 343 | db->init.busy = 1; |
| 344 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 345 | if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
| 346 | rc = sqlite3InitOne(db, i, pzErrMsg); |
| 347 | if( rc ){ |
| 348 | sqlite3ResetInternalSchema(db, i); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /* Once all the other databases have been initialised, load the schema |
| 353 | ** for the TEMP database. This is loaded last, as the TEMP database |
| 354 | ** schema may contain references to objects in other databases. |
| 355 | */ |
| 356 | #ifndef SQLITE_OMIT_TEMPDB |
| 357 | if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
| 358 | rc = sqlite3InitOne(db, 1, pzErrMsg); |
| 359 | if( rc ){ |
| 360 | sqlite3ResetInternalSchema(db, 1); |
| 361 | } |
| 362 | } |
| 363 | #endif |
| 364 | |
| 365 | db->init.busy = 0; |
danielk1977 | 5814c1a | 2007-08-13 14:41:19 +0000 | [diff] [blame] | 366 | if( rc==SQLITE_OK && commit_internal ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 367 | sqlite3CommitInternalChanges(db); |
| 368 | } |
| 369 | |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 370 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* |
| 374 | ** This routine is a no-op if the database schema is already initialised. |
| 375 | ** Otherwise, the schema is loaded. An error code is returned. |
| 376 | */ |
| 377 | int sqlite3ReadSchema(Parse *pParse){ |
| 378 | int rc = SQLITE_OK; |
| 379 | sqlite3 *db = pParse->db; |
| 380 | if( !db->init.busy ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 381 | rc = sqlite3Init(db, &pParse->zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 382 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 383 | if( rc!=SQLITE_OK ){ |
| 384 | pParse->rc = rc; |
| 385 | pParse->nErr++; |
| 386 | } |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* |
| 392 | ** Check schema cookies in all databases. If any cookie is out |
| 393 | ** of date, return 0. If all schema cookies are current, return 1. |
| 394 | */ |
| 395 | static int schemaIsValid(sqlite3 *db){ |
| 396 | int iDb; |
| 397 | int rc; |
| 398 | BtCursor *curTemp; |
| 399 | int cookie; |
| 400 | int allOk = 1; |
| 401 | |
| 402 | for(iDb=0; allOk && iDb<db->nDb; iDb++){ |
| 403 | Btree *pBt; |
| 404 | pBt = db->aDb[iDb].pBt; |
| 405 | if( pBt==0 ) continue; |
| 406 | rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp); |
| 407 | if( rc==SQLITE_OK ){ |
| 408 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 409 | if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 410 | allOk = 0; |
| 411 | } |
| 412 | sqlite3BtreeCloseCursor(curTemp); |
| 413 | } |
| 414 | } |
| 415 | return allOk; |
| 416 | } |
| 417 | |
| 418 | /* |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 419 | ** Convert a schema pointer into the iDb index that indicates |
| 420 | ** which database file in db->aDb[] the schema refers to. |
| 421 | ** |
| 422 | ** If the same database is attached more than once, the first |
| 423 | ** attached database is returned. |
| 424 | */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 425 | int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 426 | int i = -1000000; |
| 427 | |
| 428 | /* If pSchema is NULL, then return -1000000. This happens when code in |
| 429 | ** expr.c is trying to resolve a reference to a transient table (i.e. one |
| 430 | ** created by a sub-select). In this case the return value of this |
| 431 | ** function should never be used. |
| 432 | ** |
| 433 | ** We return -1000000 instead of the more usual -1 simply because using |
| 434 | ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much |
| 435 | ** more likely to cause a segfault than -1 (of course there are assert() |
| 436 | ** statements too, but it never hurts to play the odds). |
| 437 | */ |
| 438 | if( pSchema ){ |
| 439 | for(i=0; i<db->nDb; i++){ |
| 440 | if( db->aDb[i].pSchema==pSchema ){ |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | assert( i>=0 &&i>=0 && i<db->nDb ); |
| 445 | } |
| 446 | return i; |
| 447 | } |
| 448 | |
| 449 | /* |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 450 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 451 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 452 | int sqlite3Prepare( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 453 | sqlite3 *db, /* Database handle. */ |
| 454 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 455 | int nBytes, /* Length of zSql in bytes. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 456 | int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 457 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 458 | const char **pzTail /* OUT: End of parsed string */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 459 | ){ |
| 460 | Parse sParse; |
| 461 | char *zErrMsg = 0; |
| 462 | int rc = SQLITE_OK; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 463 | int i; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 464 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 465 | assert( ppStmt ); |
| 466 | *ppStmt = 0; |
| 467 | if( sqlite3SafetyOn(db) ){ |
| 468 | return SQLITE_MISUSE; |
| 469 | } |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 470 | assert( !db->mallocFailed ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 471 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 472 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 473 | /* If any attached database schemas are locked, do not proceed with |
| 474 | ** compilation. Instead return SQLITE_LOCKED immediately. |
| 475 | */ |
| 476 | for(i=0; i<db->nDb; i++) { |
| 477 | Btree *pBt = db->aDb[i].pBt; |
| 478 | if( pBt && sqlite3BtreeSchemaLocked(pBt) ){ |
| 479 | const char *zDb = db->aDb[i].zName; |
| 480 | sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb); |
| 481 | sqlite3SafetyOff(db); |
| 482 | return SQLITE_LOCKED; |
| 483 | } |
| 484 | } |
| 485 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 486 | memset(&sParse, 0, sizeof(sParse)); |
| 487 | sParse.db = db; |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 488 | if( nBytes>=0 && zSql[nBytes]!=0 ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 489 | char *zSqlCopy; |
| 490 | if( nBytes>SQLITE_MAX_SQL_LENGTH ){ |
| 491 | return SQLITE_TOOBIG; |
| 492 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 493 | zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 494 | if( zSqlCopy ){ |
| 495 | sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 496 | sqlite3_free(zSqlCopy); |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 497 | } |
| 498 | sParse.zTail = &zSql[nBytes]; |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 499 | }else{ |
| 500 | sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 501 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 502 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 503 | if( db->mallocFailed ){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 504 | sParse.rc = SQLITE_NOMEM; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 505 | } |
| 506 | if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
drh | 4d91a70 | 2006-01-04 15:54:36 +0000 | [diff] [blame] | 507 | if( sParse.checkSchema && !schemaIsValid(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 508 | sParse.rc = SQLITE_SCHEMA; |
| 509 | } |
| 510 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 511 | sqlite3ResetInternalSchema(db, 0); |
| 512 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 513 | if( db->mallocFailed ){ |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 514 | sParse.rc = SQLITE_NOMEM; |
| 515 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 516 | if( pzTail ){ |
| 517 | *pzTail = sParse.zTail; |
| 518 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 519 | rc = sParse.rc; |
| 520 | |
| 521 | #ifndef SQLITE_OMIT_EXPLAIN |
| 522 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 523 | if( sParse.explain==2 ){ |
| 524 | sqlite3VdbeSetNumCols(sParse.pVdbe, 3); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 525 | sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC); |
| 526 | sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC); |
| 527 | sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 528 | }else{ |
| 529 | sqlite3VdbeSetNumCols(sParse.pVdbe, 5); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 530 | sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC); |
| 531 | sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC); |
| 532 | sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC); |
| 533 | sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC); |
| 534 | sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 535 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 536 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 537 | #endif |
| 538 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 539 | if( sqlite3SafetyOff(db) ){ |
| 540 | rc = SQLITE_MISUSE; |
| 541 | } |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 542 | |
| 543 | if( saveSqlFlag ){ |
| 544 | sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql); |
| 545 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 546 | if( rc!=SQLITE_OK || db->mallocFailed ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 547 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 548 | assert(!(*ppStmt)); |
| 549 | }else{ |
| 550 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | if( zErrMsg ){ |
| 554 | sqlite3Error(db, rc, "%s", zErrMsg); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 555 | sqlite3_free(zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 556 | }else{ |
| 557 | sqlite3Error(db, rc, 0); |
| 558 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 559 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 560 | rc = sqlite3ApiExit(db, rc); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 561 | /* sqlite3ReleaseThreadData(); */ |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 562 | assert( (rc&db->errMask)==rc ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 563 | return rc; |
| 564 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 565 | static int sqlite3LockAndPrepare( |
| 566 | sqlite3 *db, /* Database handle. */ |
| 567 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 568 | int nBytes, /* Length of zSql in bytes. */ |
| 569 | int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
| 570 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 571 | const char **pzTail /* OUT: End of parsed string */ |
| 572 | ){ |
| 573 | int rc; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame^] | 574 | if( sqlite3SafetyCheck(db) ){ |
| 575 | return SQLITE_MISUSE; |
| 576 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 577 | sqlite3_mutex_enter(db->mutex); |
| 578 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail); |
| 579 | sqlite3_mutex_leave(db->mutex); |
| 580 | return rc; |
| 581 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 582 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 583 | /* |
| 584 | ** Rerun the compilation of a statement after a schema change. |
| 585 | ** Return true if the statement was recompiled successfully. |
| 586 | ** Return false if there is an error of some kind. |
| 587 | */ |
| 588 | int sqlite3Reprepare(Vdbe *p){ |
| 589 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 590 | sqlite3_stmt *pNew; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 591 | const char *zSql; |
| 592 | sqlite3 *db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 593 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 594 | zSql = sqlite3VdbeGetSql(p); |
| 595 | if( zSql==0 ){ |
| 596 | return 0; |
| 597 | } |
| 598 | db = sqlite3VdbeDb(p); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 599 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 600 | rc = sqlite3Prepare(db, zSql, -1, 0, &pNew, 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 601 | if( rc ){ |
| 602 | assert( pNew==0 ); |
| 603 | return 0; |
| 604 | }else{ |
| 605 | assert( pNew!=0 ); |
| 606 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 607 | sqlite3VdbeSwap((Vdbe*)pNew, p); |
| 608 | sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p); |
| 609 | sqlite3VdbeResetStepResult((Vdbe*)pNew); |
| 610 | sqlite3VdbeFinalize((Vdbe*)pNew); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 611 | return 1; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | /* |
| 616 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 617 | ** version, the original SQL text is not saved in the prepared statement |
| 618 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 619 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 620 | ** and the statement is automatically recompiled if an schema change |
| 621 | ** occurs. |
| 622 | */ |
| 623 | int sqlite3_prepare( |
| 624 | sqlite3 *db, /* Database handle. */ |
| 625 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 626 | int nBytes, /* Length of zSql in bytes. */ |
| 627 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 628 | const char **pzTail /* OUT: End of parsed string */ |
| 629 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 630 | return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 631 | } |
| 632 | int sqlite3_prepare_v2( |
| 633 | sqlite3 *db, /* Database handle. */ |
| 634 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 635 | int nBytes, /* Length of zSql in bytes. */ |
| 636 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 637 | const char **pzTail /* OUT: End of parsed string */ |
| 638 | ){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 639 | return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 643 | #ifndef SQLITE_OMIT_UTF16 |
| 644 | /* |
| 645 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 646 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 647 | static int sqlite3Prepare16( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 648 | sqlite3 *db, /* Database handle. */ |
| 649 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 650 | int nBytes, /* Length of zSql in bytes. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 651 | int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 652 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 653 | const void **pzTail /* OUT: End of parsed string */ |
| 654 | ){ |
| 655 | /* This function currently works by first transforming the UTF-16 |
| 656 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 657 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 658 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 659 | char *zSql8; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 660 | const char *zTail8 = 0; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 661 | int rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 662 | |
| 663 | if( sqlite3SafetyCheck(db) ){ |
| 664 | return SQLITE_MISUSE; |
| 665 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 666 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 667 | zSql8 = sqlite3Utf16to8(db, zSql, nBytes); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 668 | if( zSql8 ){ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 669 | rc = sqlite3Prepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 670 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 671 | |
| 672 | if( zTail8 && pzTail ){ |
| 673 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 674 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 675 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 676 | ** the same number of characters into the UTF-16 string. |
| 677 | */ |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 678 | int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8); |
| 679 | *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 680 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 681 | sqlite3_free(zSql8); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 682 | rc = sqlite3ApiExit(db, rc); |
| 683 | sqlite3_mutex_leave(db->mutex); |
| 684 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 685 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 686 | |
| 687 | /* |
| 688 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 689 | ** version, the original SQL text is not saved in the prepared statement |
| 690 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 691 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 692 | ** and the statement is automatically recompiled if an schema change |
| 693 | ** occurs. |
| 694 | */ |
| 695 | int sqlite3_prepare16( |
| 696 | sqlite3 *db, /* Database handle. */ |
| 697 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 698 | int nBytes, /* Length of zSql in bytes. */ |
| 699 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 700 | const void **pzTail /* OUT: End of parsed string */ |
| 701 | ){ |
| 702 | return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); |
| 703 | } |
| 704 | int sqlite3_prepare16_v2( |
| 705 | sqlite3 *db, /* Database handle. */ |
| 706 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 707 | int nBytes, /* Length of zSql in bytes. */ |
| 708 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 709 | const void **pzTail /* OUT: End of parsed string */ |
| 710 | ){ |
| 711 | return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); |
| 712 | } |
| 713 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 714 | #endif /* SQLITE_OMIT_UTF16 */ |