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 | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 16 | ** $Id: prepare.c,v 1.131 2009/08/06 17:43:31 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 | |
| 20 | /* |
| 21 | ** Fill the InitData structure with an error message that indicates |
| 22 | ** that the database is corrupt. |
| 23 | */ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 24 | static void corruptSchema( |
| 25 | InitData *pData, /* Initialization context */ |
| 26 | const char *zObj, /* Object being parsed at the point of error */ |
| 27 | const char *zExtra /* Error information */ |
| 28 | ){ |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 29 | sqlite3 *db = pData->db; |
| 30 | if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 31 | if( zObj==0 ) zObj = "?"; |
drh | 35ccb3d | 2009-08-01 16:27:00 +0000 | [diff] [blame] | 32 | sqlite3SetString(pData->pzErrMsg, db, |
| 33 | "malformed database schema (%s)", zObj); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 34 | if( zExtra ){ |
drh | 35ccb3d | 2009-08-01 16:27:00 +0000 | [diff] [blame] | 35 | *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, |
| 36 | "%s - %s", *pData->pzErrMsg, zExtra); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 37 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 38 | } |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 39 | pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | ** This is the callback routine for the code that initializes the |
| 44 | ** database. See sqlite3Init() below for additional information. |
| 45 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 46 | ** |
| 47 | ** Each callback contains the following information: |
| 48 | ** |
| 49 | ** argv[0] = name of thing being created |
danielk1977 | 78efaba | 2006-06-12 06:09:17 +0000 | [diff] [blame] | 50 | ** 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] | 51 | ** argv[2] = SQL text for the CREATE statement. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 52 | ** |
| 53 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 54 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 55 | InitData *pData = (InitData*)pInit; |
| 56 | sqlite3 *db = pData->db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 57 | int iDb = pData->iDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 58 | |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 59 | assert( argc==3 ); |
| 60 | UNUSED_PARAMETER2(NotUsed, argc); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 61 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 62 | DbClearProperty(db, iDb, DB_Empty); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 63 | if( db->mallocFailed ){ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 64 | corruptSchema(pData, argv[0], 0); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 65 | return 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 66 | } |
| 67 | |
danielk1977 | ff9b2e7 | 2008-09-08 09:06:18 +0000 | [diff] [blame] | 68 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 69 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 70 | if( argv[1]==0 ){ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 71 | corruptSchema(pData, argv[0], 0); |
danielk1977 | ff9b2e7 | 2008-09-08 09:06:18 +0000 | [diff] [blame] | 72 | }else if( argv[2] && argv[2][0] ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 73 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 74 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 75 | ** or executed. All the parser does is build the internal data |
| 76 | ** structures that describe the table, index, or view. |
| 77 | */ |
| 78 | char *zErr; |
| 79 | int rc; |
| 80 | assert( db->init.busy ); |
| 81 | db->init.iDb = iDb; |
| 82 | db->init.newTnum = atoi(argv[1]); |
drh | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 83 | db->init.orphanTrigger = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 84 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 85 | db->init.iDb = 0; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 86 | assert( rc!=SQLITE_OK || zErr==0 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 87 | if( SQLITE_OK!=rc ){ |
drh | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 88 | if( db->init.orphanTrigger ){ |
| 89 | assert( iDb==1 ); |
| 90 | }else{ |
| 91 | pData->rc = rc; |
| 92 | if( rc==SQLITE_NOMEM ){ |
| 93 | db->mallocFailed = 1; |
| 94 | }else if( rc!=SQLITE_INTERRUPT && rc!=SQLITE_LOCKED ){ |
| 95 | corruptSchema(pData, argv[0], zErr); |
| 96 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 97 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 98 | sqlite3DbFree(db, zErr); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 99 | } |
drh | 02cd2b8 | 2008-01-22 19:34:27 +0000 | [diff] [blame] | 100 | }else if( argv[0]==0 ){ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 101 | corruptSchema(pData, 0, 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 102 | }else{ |
| 103 | /* If the SQL column is blank it means this is an index that |
| 104 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 105 | ** constraint for a CREATE TABLE. The index should have already |
| 106 | ** been created when we processed the CREATE TABLE. All we have |
| 107 | ** to do here is record the root page number for that index. |
| 108 | */ |
| 109 | Index *pIndex; |
| 110 | pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 111 | if( pIndex==0 ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 112 | /* This can occur if there exists an index on a TEMP table which |
| 113 | ** has the same name as another index on a permanent index. Since |
| 114 | ** the permanent table is hidden by the TEMP table, we can also |
| 115 | ** safely ignore the index on the permanent table. |
| 116 | */ |
| 117 | /* Do Nothing */; |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 118 | }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){ |
| 119 | corruptSchema(pData, argv[0], "invalid rootpage"); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | ** Attempt to read the database schema and initialize internal |
| 127 | ** data structures for a single database file. The index of the |
| 128 | ** database file is given by iDb. iDb==0 is used for the main |
| 129 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 130 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
| 131 | ** indicate success or failure. |
| 132 | */ |
| 133 | static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ |
| 134 | int rc; |
drh | 8a93919 | 2009-04-20 17:43:03 +0000 | [diff] [blame] | 135 | int i; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 136 | int size; |
| 137 | Table *pTab; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 138 | Db *pDb; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 139 | char const *azArg[4]; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 140 | int meta[5]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 141 | InitData initData; |
| 142 | char const *zMasterSchema; |
| 143 | char const *zMasterName = SCHEMA_TABLE(iDb); |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 144 | int openedTransaction = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | ** The master database table has a structure like this |
| 148 | */ |
| 149 | static const char master_schema[] = |
| 150 | "CREATE TABLE sqlite_master(\n" |
| 151 | " type text,\n" |
| 152 | " name text,\n" |
| 153 | " tbl_name text,\n" |
| 154 | " rootpage integer,\n" |
| 155 | " sql text\n" |
| 156 | ")" |
| 157 | ; |
| 158 | #ifndef SQLITE_OMIT_TEMPDB |
| 159 | static const char temp_master_schema[] = |
| 160 | "CREATE TEMP TABLE sqlite_temp_master(\n" |
| 161 | " type text,\n" |
| 162 | " name text,\n" |
| 163 | " tbl_name text,\n" |
| 164 | " rootpage integer,\n" |
| 165 | " sql text\n" |
| 166 | ")" |
| 167 | ; |
| 168 | #else |
| 169 | #define temp_master_schema 0 |
| 170 | #endif |
| 171 | |
| 172 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 173 | assert( db->aDb[iDb].pSchema ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 174 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 4eab8b7 | 2007-12-27 15:12:16 +0000 | [diff] [blame] | 175 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 176 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 177 | /* zMasterSchema and zInitScript are set to point at the master schema |
| 178 | ** and initialisation script appropriate for the database being |
| 179 | ** initialised. zMasterName is the name of the master table. |
| 180 | */ |
| 181 | if( !OMIT_TEMPDB && iDb==1 ){ |
| 182 | zMasterSchema = temp_master_schema; |
| 183 | }else{ |
| 184 | zMasterSchema = master_schema; |
| 185 | } |
| 186 | zMasterName = SCHEMA_TABLE(iDb); |
| 187 | |
| 188 | /* Construct the schema tables. */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 189 | azArg[0] = zMasterName; |
| 190 | azArg[1] = "1"; |
| 191 | azArg[2] = zMasterSchema; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 192 | azArg[3] = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 193 | initData.db = db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 194 | initData.iDb = iDb; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 195 | initData.rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 196 | initData.pzErrMsg = pzErrMsg; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 197 | (void)sqlite3SafetyOff(db); |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 198 | sqlite3InitCallback(&initData, 3, (char **)azArg, 0); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 199 | (void)sqlite3SafetyOn(db); |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 200 | if( initData.rc ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 201 | rc = initData.rc; |
| 202 | goto error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 203 | } |
| 204 | pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 205 | if( ALWAYS(pTab) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 206 | pTab->tabFlags |= TF_Readonly; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 207 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 208 | |
| 209 | /* Create a cursor to hold the database open |
| 210 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 211 | pDb = &db->aDb[iDb]; |
| 212 | if( pDb->pBt==0 ){ |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 213 | if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 214 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 215 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 216 | return SQLITE_OK; |
| 217 | } |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 218 | |
| 219 | /* If there is not already a read-only (or read-write) transaction opened |
| 220 | ** on the b-tree database, open one now. If a transaction is opened, it |
| 221 | ** will be closed before this function returns. */ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 222 | sqlite3BtreeEnter(pDb->pBt); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 223 | if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ |
| 224 | rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); |
| 225 | if( rc!=SQLITE_OK ){ |
| 226 | sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc)); |
| 227 | goto initone_error_out; |
| 228 | } |
| 229 | openedTransaction = 1; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 230 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 231 | |
| 232 | /* Get the database meta information. |
| 233 | ** |
| 234 | ** Meta values are as follows: |
| 235 | ** meta[0] Schema cookie. Changes with each schema change. |
| 236 | ** meta[1] File format of schema layer. |
| 237 | ** meta[2] Size of the page cache. |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 238 | ** meta[3] Largest rootpage (auto/incr_vacuum mode) |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 239 | ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 240 | ** meta[5] User version |
| 241 | ** meta[6] Incremental vacuum mode |
| 242 | ** meta[7] unused |
| 243 | ** meta[8] unused |
| 244 | ** meta[9] unused |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 245 | ** |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 246 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 247 | ** the possible values of meta[4]. |
| 248 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 249 | for(i=0; i<ArraySize(meta); i++){ |
| 250 | sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 251 | } |
| 252 | pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 253 | |
| 254 | /* If opening a non-empty database, check the text encoding. For the |
| 255 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 256 | ** For an attached db, it is an error if the encoding is not the same |
| 257 | ** as sqlite3.enc. |
| 258 | */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 259 | if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 260 | if( iDb==0 ){ |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 261 | u8 encoding; |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 262 | /* If opening the main database, set ENC(db). */ |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 263 | encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; |
| 264 | if( encoding==0 ) encoding = SQLITE_UTF8; |
| 265 | ENC(db) = encoding; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 266 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 267 | }else{ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 268 | /* If opening an attached database, the encoding much match ENC(db) */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 269 | if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 270 | sqlite3SetString(pzErrMsg, db, "attached databases must use the same" |
| 271 | " text encoding as main database"); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 272 | rc = SQLITE_ERROR; |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 273 | goto initone_error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 276 | }else{ |
| 277 | DbSetProperty(db, iDb, DB_Empty); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 278 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 279 | pDb->pSchema->enc = ENC(db); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 280 | |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 281 | if( pDb->pSchema->cache_size==0 ){ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 282 | size = meta[BTREE_DEFAULT_CACHE_SIZE-1]; |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 283 | if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } |
| 284 | if( size<0 ) size = -size; |
| 285 | pDb->pSchema->cache_size = size; |
| 286 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
| 287 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 288 | |
| 289 | /* |
| 290 | ** file_format==1 Version 3.0.0. |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 291 | ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
| 292 | ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 293 | ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 294 | */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 295 | pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 296 | if( pDb->pSchema->file_format==0 ){ |
| 297 | pDb->pSchema->file_format = 1; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 298 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 299 | if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 300 | sqlite3SetString(pzErrMsg, db, "unsupported file format"); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 301 | rc = SQLITE_ERROR; |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 302 | goto initone_error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 303 | } |
| 304 | |
drh | 4aa2bfe | 2007-11-28 13:43:16 +0000 | [diff] [blame] | 305 | /* Ticket #2804: When we open a database in the newer file format, |
| 306 | ** clear the legacy_file_format pragma flag so that a VACUUM will |
| 307 | ** not downgrade the database and thus invalidate any descending |
| 308 | ** indices that the user might have created. |
| 309 | */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 310 | if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ |
drh | 4aa2bfe | 2007-11-28 13:43:16 +0000 | [diff] [blame] | 311 | db->flags &= ~SQLITE_LegacyFileFmt; |
| 312 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 313 | |
| 314 | /* Read the schema information out of the schema tables |
| 315 | */ |
| 316 | assert( db->init.busy ); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 317 | { |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 318 | char *zSql; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 319 | zSql = sqlite3MPrintf(db, |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 320 | "SELECT name, rootpage, sql FROM '%q'.%s", |
| 321 | db->aDb[iDb].zName, zMasterName); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 322 | (void)sqlite3SafetyOff(db); |
drh | a6d0ffc | 2007-10-12 20:42:28 +0000 | [diff] [blame] | 323 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 324 | { |
| 325 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); |
| 326 | xAuth = db->xAuth; |
| 327 | db->xAuth = 0; |
| 328 | #endif |
| 329 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 330 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 331 | db->xAuth = xAuth; |
| 332 | } |
| 333 | #endif |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 334 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 335 | (void)sqlite3SafetyOn(db); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 336 | sqlite3DbFree(db, zSql); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 337 | #ifndef SQLITE_OMIT_ANALYZE |
| 338 | if( rc==SQLITE_OK ){ |
| 339 | sqlite3AnalysisLoad(db, iDb); |
| 340 | } |
| 341 | #endif |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 342 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 343 | if( db->mallocFailed ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 344 | rc = SQLITE_NOMEM; |
| 345 | sqlite3ResetInternalSchema(db, 0); |
| 346 | } |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 347 | if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ |
| 348 | /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 349 | ** the schema loaded, even if errors occurred. In this situation the |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 350 | ** current sqlite3_prepare() operation will fail, but the following one |
| 351 | ** will attempt to compile the supplied statement against whatever subset |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 352 | ** of the schema was loaded before the error occurred. The primary |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 353 | ** purpose of this is to allow access to the sqlite_master table |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 354 | ** even when its contents have been corrupted. |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 355 | */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 356 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 357 | rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 358 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 359 | |
| 360 | /* Jump here for an error that occurs after successfully allocating |
| 361 | ** curMain and calling sqlite3BtreeEnter(). For an error that occurs |
| 362 | ** before that point, jump to error_out. |
| 363 | */ |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 364 | initone_error_out: |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 365 | if( openedTransaction ){ |
| 366 | sqlite3BtreeCommit(pDb->pBt); |
| 367 | } |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 368 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 369 | |
| 370 | error_out: |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 371 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 372 | db->mallocFailed = 1; |
| 373 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 374 | return rc; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | ** Initialize all database files - the main database file, the file |
| 379 | ** used to store temporary tables, and any additional database files |
| 380 | ** created using ATTACH statements. Return a success code. If an |
| 381 | ** error occurs, write an error message into *pzErrMsg. |
| 382 | ** |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 383 | ** After a database is initialized, the DB_SchemaLoaded bit is set |
| 384 | ** bit is set in the flags field of the Db structure. If the database |
| 385 | ** file was of zero-length, then the DB_Empty flag is also set. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 386 | */ |
| 387 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 388 | int i, rc; |
danielk1977 | 5814c1a | 2007-08-13 14:41:19 +0000 | [diff] [blame] | 389 | int commit_internal = !(db->flags&SQLITE_InternChanges); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 390 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 391 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 392 | rc = SQLITE_OK; |
| 393 | db->init.busy = 1; |
| 394 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 395 | if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
| 396 | rc = sqlite3InitOne(db, i, pzErrMsg); |
| 397 | if( rc ){ |
| 398 | sqlite3ResetInternalSchema(db, i); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /* Once all the other databases have been initialised, load the schema |
| 403 | ** for the TEMP database. This is loaded last, as the TEMP database |
| 404 | ** schema may contain references to objects in other databases. |
| 405 | */ |
| 406 | #ifndef SQLITE_OMIT_TEMPDB |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 407 | if( rc==SQLITE_OK && ALWAYS(db->nDb>1) |
| 408 | && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 409 | rc = sqlite3InitOne(db, 1, pzErrMsg); |
| 410 | if( rc ){ |
| 411 | sqlite3ResetInternalSchema(db, 1); |
| 412 | } |
| 413 | } |
| 414 | #endif |
| 415 | |
| 416 | db->init.busy = 0; |
danielk1977 | 5814c1a | 2007-08-13 14:41:19 +0000 | [diff] [blame] | 417 | if( rc==SQLITE_OK && commit_internal ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 418 | sqlite3CommitInternalChanges(db); |
| 419 | } |
| 420 | |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 421 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* |
| 425 | ** This routine is a no-op if the database schema is already initialised. |
| 426 | ** Otherwise, the schema is loaded. An error code is returned. |
| 427 | */ |
| 428 | int sqlite3ReadSchema(Parse *pParse){ |
| 429 | int rc = SQLITE_OK; |
| 430 | sqlite3 *db = pParse->db; |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 431 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 432 | if( !db->init.busy ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 433 | rc = sqlite3Init(db, &pParse->zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 434 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 435 | if( rc!=SQLITE_OK ){ |
| 436 | pParse->rc = rc; |
| 437 | pParse->nErr++; |
| 438 | } |
| 439 | return rc; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /* |
| 444 | ** Check schema cookies in all databases. If any cookie is out |
drh | 1adecdf | 2009-07-03 19:19:50 +0000 | [diff] [blame] | 445 | ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies |
| 446 | ** make no changes to pParse->rc. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 447 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 448 | static void schemaIsValid(Parse *pParse){ |
| 449 | sqlite3 *db = pParse->db; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 450 | int iDb; |
| 451 | int rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 452 | int cookie; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 453 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 454 | assert( pParse->checkSchema ); |
| 455 | assert( sqlite3_mutex_held(db->mutex) ); |
| 456 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 457 | int openedTransaction = 0; /* True if a transaction is opened */ |
| 458 | Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */ |
| 459 | if( pBt==0 ) continue; |
| 460 | |
| 461 | /* If there is not already a read-only (or read-write) transaction opened |
| 462 | ** on the b-tree database, open one now. If a transaction is opened, it |
| 463 | ** will be closed immediately after reading the meta-value. */ |
| 464 | if( !sqlite3BtreeIsInReadTrans(pBt) ){ |
| 465 | rc = sqlite3BtreeBeginTrans(pBt, 0); |
drh | 1adecdf | 2009-07-03 19:19:50 +0000 | [diff] [blame] | 466 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 467 | db->mallocFailed = 1; |
| 468 | } |
| 469 | if( rc!=SQLITE_OK ) return; |
| 470 | openedTransaction = 1; |
| 471 | } |
| 472 | |
| 473 | /* Read the schema cookie from the database. If it does not match the |
| 474 | ** value stored as part of the in the in-memory schema representation, |
| 475 | ** set Parse.rc to SQLITE_SCHEMA. */ |
| 476 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); |
| 477 | if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
| 478 | pParse->rc = SQLITE_SCHEMA; |
| 479 | } |
| 480 | |
| 481 | /* Close the transaction, if one was opened. */ |
| 482 | if( openedTransaction ){ |
| 483 | sqlite3BtreeCommit(pBt); |
| 484 | } |
| 485 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 489 | ** Convert a schema pointer into the iDb index that indicates |
| 490 | ** which database file in db->aDb[] the schema refers to. |
| 491 | ** |
| 492 | ** If the same database is attached more than once, the first |
| 493 | ** attached database is returned. |
| 494 | */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 495 | int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 496 | int i = -1000000; |
| 497 | |
| 498 | /* If pSchema is NULL, then return -1000000. This happens when code in |
| 499 | ** expr.c is trying to resolve a reference to a transient table (i.e. one |
| 500 | ** created by a sub-select). In this case the return value of this |
| 501 | ** function should never be used. |
| 502 | ** |
| 503 | ** We return -1000000 instead of the more usual -1 simply because using |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 504 | ** -1000000 as the incorrect index into db->aDb[] is much |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 505 | ** more likely to cause a segfault than -1 (of course there are assert() |
| 506 | ** statements too, but it never hurts to play the odds). |
| 507 | */ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 508 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 509 | if( pSchema ){ |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 510 | for(i=0; ALWAYS(i<db->nDb); i++){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 511 | if( db->aDb[i].pSchema==pSchema ){ |
| 512 | break; |
| 513 | } |
| 514 | } |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 515 | assert( i>=0 && i<db->nDb ); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 516 | } |
| 517 | return i; |
| 518 | } |
| 519 | |
| 520 | /* |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 521 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 522 | */ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 523 | static int sqlite3Prepare( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 524 | sqlite3 *db, /* Database handle. */ |
| 525 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 526 | int nBytes, /* Length of zSql in bytes. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 527 | int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 528 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 529 | const char **pzTail /* OUT: End of parsed string */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 530 | ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 531 | Parse *pParse; /* Parsing context */ |
| 532 | char *zErrMsg = 0; /* Error message */ |
| 533 | int rc = SQLITE_OK; /* Result code */ |
| 534 | int i; /* Loop counter */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 535 | |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 536 | /* Allocate the parsing context */ |
| 537 | pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); |
| 538 | if( pParse==0 ){ |
| 539 | rc = SQLITE_NOMEM; |
| 540 | goto end_prepare; |
| 541 | } |
| 542 | |
| 543 | if( sqlite3SafetyOn(db) ){ |
| 544 | rc = SQLITE_MISUSE; |
| 545 | goto end_prepare; |
| 546 | } |
drh | 860e077 | 2009-04-02 18:32:26 +0000 | [diff] [blame] | 547 | assert( ppStmt && *ppStmt==0 ); |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 548 | assert( !db->mallocFailed ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 549 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 550 | |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 551 | /* Check to verify that it is possible to get a read lock on all |
| 552 | ** database schemas. The inability to get a read lock indicates that |
| 553 | ** some other database connection is holding a write-lock, which in |
| 554 | ** turn means that the other connection has made uncommitted changes |
| 555 | ** to the schema. |
| 556 | ** |
| 557 | ** Were we to proceed and prepare the statement against the uncommitted |
| 558 | ** schema changes and if those schema changes are subsequently rolled |
| 559 | ** back and different changes are made in their place, then when this |
| 560 | ** prepared statement goes to run the schema cookie would fail to detect |
| 561 | ** the schema change. Disaster would follow. |
| 562 | ** |
| 563 | ** This thread is currently holding mutexes on all Btrees (because |
| 564 | ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it |
| 565 | ** is not possible for another thread to start a new schema change |
| 566 | ** while this routine is running. Hence, we do not need to hold |
| 567 | ** locks on the schema, we just need to make sure nobody else is |
| 568 | ** holding them. |
| 569 | ** |
| 570 | ** Note that setting READ_UNCOMMITTED overrides most lock detection, |
| 571 | ** but it does *not* override schema lock detection, so this all still |
| 572 | ** works even if READ_UNCOMMITTED is set. |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 573 | */ |
| 574 | for(i=0; i<db->nDb; i++) { |
| 575 | Btree *pBt = db->aDb[i].pBt; |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 576 | if( pBt ){ |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 577 | assert( sqlite3BtreeHoldsMutex(pBt) ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 578 | rc = sqlite3BtreeSchemaLocked(pBt); |
| 579 | if( rc ){ |
| 580 | const char *zDb = db->aDb[i].zName; |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 581 | sqlite3Error(db, rc, "database schema is locked: %s", zDb); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 582 | (void)sqlite3SafetyOff(db); |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 583 | testcase( db->flags & SQLITE_ReadUncommitted ); |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 584 | goto end_prepare; |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 585 | } |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 588 | |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 589 | sqlite3VtabUnlockList(db); |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 590 | |
| 591 | pParse->db = db; |
drh | d2d88bb | 2008-05-23 14:32:18 +0000 | [diff] [blame] | 592 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 593 | char *zSqlCopy; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 594 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
drh | 58fbb31 | 2009-06-17 00:35:30 +0000 | [diff] [blame] | 595 | testcase( nBytes==mxLen ); |
| 596 | testcase( nBytes==mxLen+1 ); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 597 | if( nBytes>mxLen ){ |
drh | be69bf6 | 2007-12-18 17:50:33 +0000 | [diff] [blame] | 598 | sqlite3Error(db, SQLITE_TOOBIG, "statement too long"); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 599 | (void)sqlite3SafetyOff(db); |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 600 | rc = sqlite3ApiExit(db, SQLITE_TOOBIG); |
| 601 | goto end_prepare; |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 602 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 603 | zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 604 | if( zSqlCopy ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 605 | sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 606 | sqlite3DbFree(db, zSqlCopy); |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 607 | pParse->zTail = &zSql[pParse->zTail-zSqlCopy]; |
danielk1977 | 3a2c8c8 | 2008-04-03 14:36:25 +0000 | [diff] [blame] | 608 | }else{ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 609 | pParse->zTail = &zSql[nBytes]; |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 610 | } |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 611 | }else{ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 612 | sqlite3RunParser(pParse, zSql, &zErrMsg); |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 613 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 614 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 615 | if( db->mallocFailed ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 616 | pParse->rc = SQLITE_NOMEM; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 617 | } |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 618 | if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 619 | if( pParse->checkSchema ){ |
| 620 | schemaIsValid(pParse); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 621 | } |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 622 | if( pParse->rc==SQLITE_SCHEMA ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 623 | sqlite3ResetInternalSchema(db, 0); |
| 624 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 625 | if( db->mallocFailed ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 626 | pParse->rc = SQLITE_NOMEM; |
drh | 344a627 | 2006-06-26 12:50:09 +0000 | [diff] [blame] | 627 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 628 | if( pzTail ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 629 | *pzTail = pParse->zTail; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 630 | } |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 631 | rc = pParse->rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 632 | |
| 633 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 634 | if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){ |
| 635 | static const char * const azColName[] = { |
| 636 | "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", |
| 637 | "order", "from", "detail" |
| 638 | }; |
| 639 | int iFirst, mx; |
| 640 | if( pParse->explain==2 ){ |
| 641 | sqlite3VdbeSetNumCols(pParse->pVdbe, 3); |
| 642 | iFirst = 8; |
| 643 | mx = 11; |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 644 | }else{ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 645 | sqlite3VdbeSetNumCols(pParse->pVdbe, 8); |
| 646 | iFirst = 0; |
| 647 | mx = 8; |
| 648 | } |
| 649 | for(i=iFirst; i<mx; i++){ |
| 650 | sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME, |
| 651 | azColName[i], SQLITE_STATIC); |
drh | ecc9242 | 2005-09-10 16:46:12 +0000 | [diff] [blame] | 652 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 653 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 654 | #endif |
| 655 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 656 | if( sqlite3SafetyOff(db) ){ |
| 657 | rc = SQLITE_MISUSE; |
| 658 | } |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 659 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 660 | assert( db->init.busy==0 || saveSqlFlag==0 ); |
| 661 | if( db->init.busy==0 ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 662 | Vdbe *pVdbe = pParse->pVdbe; |
| 663 | sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 664 | } |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 665 | if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ |
| 666 | sqlite3VdbeFinalize(pParse->pVdbe); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 667 | assert(!(*ppStmt)); |
| 668 | }else{ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 669 | *ppStmt = (sqlite3_stmt*)pParse->pVdbe; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if( zErrMsg ){ |
| 673 | sqlite3Error(db, rc, "%s", zErrMsg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 674 | sqlite3DbFree(db, zErrMsg); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 675 | }else{ |
| 676 | sqlite3Error(db, rc, 0); |
| 677 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 678 | |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 679 | while( pParse->pCodedTrigger ){ |
| 680 | CodedTrigger *pT = pParse->pCodedTrigger; |
| 681 | pParse->pCodedTrigger = pT->pNext; |
| 682 | sqlite3VdbeProgramDelete(db, pT->pProgram, 0); |
| 683 | sqlite3DbFree(db, pT); |
| 684 | } |
| 685 | |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 686 | end_prepare: |
| 687 | |
| 688 | sqlite3StackFree(db, pParse); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 689 | rc = sqlite3ApiExit(db, rc); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 690 | assert( (rc&db->errMask)==rc ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 691 | return rc; |
| 692 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 693 | static int sqlite3LockAndPrepare( |
| 694 | sqlite3 *db, /* Database handle. */ |
| 695 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 696 | int nBytes, /* Length of zSql in bytes. */ |
| 697 | int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ |
| 698 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 699 | const char **pzTail /* OUT: End of parsed string */ |
| 700 | ){ |
| 701 | int rc; |
drh | 860e077 | 2009-04-02 18:32:26 +0000 | [diff] [blame] | 702 | assert( ppStmt!=0 ); |
| 703 | *ppStmt = 0; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 704 | if( !sqlite3SafetyCheckOk(db) ){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 705 | return SQLITE_MISUSE; |
| 706 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 707 | sqlite3_mutex_enter(db->mutex); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 708 | sqlite3BtreeEnterAll(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 709 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 710 | if( rc==SQLITE_SCHEMA ){ |
| 711 | sqlite3_finalize(*ppStmt); |
| 712 | rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail); |
| 713 | } |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 714 | sqlite3BtreeLeaveAll(db); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 715 | sqlite3_mutex_leave(db->mutex); |
| 716 | return rc; |
| 717 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 718 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 719 | /* |
| 720 | ** Rerun the compilation of a statement after a schema change. |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 721 | ** |
| 722 | ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, |
| 723 | ** if the statement cannot be recompiled because another connection has |
| 724 | ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error |
| 725 | ** occurs, return SQLITE_SCHEMA. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 726 | */ |
| 727 | int sqlite3Reprepare(Vdbe *p){ |
| 728 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 729 | sqlite3_stmt *pNew; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 730 | const char *zSql; |
| 731 | sqlite3 *db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 732 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 733 | assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 734 | zSql = sqlite3_sql((sqlite3_stmt *)p); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 735 | assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 736 | db = sqlite3VdbeDb(p); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 737 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 738 | rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 739 | if( rc ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 740 | if( rc==SQLITE_NOMEM ){ |
| 741 | db->mallocFailed = 1; |
| 742 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 743 | assert( pNew==0 ); |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 744 | return (rc==SQLITE_LOCKED) ? SQLITE_LOCKED : SQLITE_SCHEMA; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 745 | }else{ |
| 746 | assert( pNew!=0 ); |
| 747 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 748 | sqlite3VdbeSwap((Vdbe*)pNew, p); |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 749 | sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 750 | sqlite3VdbeResetStepResult((Vdbe*)pNew); |
| 751 | sqlite3VdbeFinalize((Vdbe*)pNew); |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 752 | return SQLITE_OK; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | |
| 756 | /* |
| 757 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 758 | ** version, the original SQL text is not saved in the prepared statement |
| 759 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 760 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 761 | ** and the statement is automatically recompiled if an schema change |
| 762 | ** occurs. |
| 763 | */ |
| 764 | int sqlite3_prepare( |
| 765 | sqlite3 *db, /* Database handle. */ |
| 766 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 767 | int nBytes, /* Length of zSql in bytes. */ |
| 768 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 769 | const char **pzTail /* OUT: End of parsed string */ |
| 770 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 771 | int rc; |
| 772 | rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 773 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 774 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 775 | } |
| 776 | int sqlite3_prepare_v2( |
| 777 | sqlite3 *db, /* Database handle. */ |
| 778 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 779 | int nBytes, /* Length of zSql in bytes. */ |
| 780 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 781 | const char **pzTail /* OUT: End of parsed string */ |
| 782 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 783 | int rc; |
| 784 | rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 785 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 786 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 790 | #ifndef SQLITE_OMIT_UTF16 |
| 791 | /* |
| 792 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 793 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 794 | static int sqlite3Prepare16( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 795 | sqlite3 *db, /* Database handle. */ |
| 796 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 797 | int nBytes, /* Length of zSql in bytes. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 798 | int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 799 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 800 | const void **pzTail /* OUT: End of parsed string */ |
| 801 | ){ |
| 802 | /* This function currently works by first transforming the UTF-16 |
| 803 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 804 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 805 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 806 | char *zSql8; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 807 | const char *zTail8 = 0; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 808 | int rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 809 | |
drh | 860e077 | 2009-04-02 18:32:26 +0000 | [diff] [blame] | 810 | assert( ppStmt ); |
drh | 769e97e | 2009-04-01 16:33:37 +0000 | [diff] [blame] | 811 | *ppStmt = 0; |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 812 | if( !sqlite3SafetyCheckOk(db) ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 813 | return SQLITE_MISUSE; |
| 814 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 815 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 816 | zSql8 = sqlite3Utf16to8(db, zSql, nBytes); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 817 | if( zSql8 ){ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 818 | rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 819 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 820 | |
| 821 | if( zTail8 && pzTail ){ |
| 822 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 823 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 824 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 825 | ** the same number of characters into the UTF-16 string. |
| 826 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 827 | int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8)); |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 828 | *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 829 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 830 | sqlite3DbFree(db, zSql8); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 831 | rc = sqlite3ApiExit(db, rc); |
| 832 | sqlite3_mutex_leave(db->mutex); |
| 833 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 834 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 835 | |
| 836 | /* |
| 837 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 838 | ** version, the original SQL text is not saved in the prepared statement |
| 839 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 840 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 841 | ** and the statement is automatically recompiled if an schema change |
| 842 | ** occurs. |
| 843 | */ |
| 844 | int sqlite3_prepare16( |
| 845 | sqlite3 *db, /* Database handle. */ |
| 846 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 847 | int nBytes, /* Length of zSql in bytes. */ |
| 848 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 849 | const void **pzTail /* OUT: End of parsed string */ |
| 850 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 851 | int rc; |
| 852 | rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 853 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 854 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 855 | } |
| 856 | int sqlite3_prepare16_v2( |
| 857 | sqlite3 *db, /* Database handle. */ |
| 858 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 859 | int nBytes, /* Length of zSql in bytes. */ |
| 860 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 861 | const void **pzTail /* OUT: End of parsed string */ |
| 862 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 863 | int rc; |
| 864 | rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 865 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 866 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 867 | } |
| 868 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 869 | #endif /* SQLITE_OMIT_UTF16 */ |