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. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | ** Fill the InitData structure with an error message that indicates |
| 20 | ** that the database is corrupt. |
| 21 | */ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 22 | static void corruptSchema( |
| 23 | InitData *pData, /* Initialization context */ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 24 | char **azObj, /* Type and name of object being parsed */ |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 25 | const char *zExtra /* Error information */ |
| 26 | ){ |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 27 | sqlite3 *db = pData->db; |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 28 | if( db->mallocFailed ){ |
| 29 | pData->rc = SQLITE_NOMEM_BKPT; |
| 30 | }else if( pData->pzErrMsg[0]!=0 ){ |
| 31 | /* A error message has already been generated. Do not overwrite it */ |
drh | ac894af | 2021-11-03 15:59:17 +0000 | [diff] [blame] | 32 | }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){ |
| 33 | static const char *azAlterType[] = { |
| 34 | "rename", |
| 35 | "drop column", |
| 36 | "add column" |
| 37 | }; |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 38 | *pData->pzErrMsg = sqlite3MPrintf(db, |
| 39 | "error in %s %s after %s: %s", azObj[0], azObj[1], |
drh | ac894af | 2021-11-03 15:59:17 +0000 | [diff] [blame] | 40 | azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1], |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 41 | zExtra |
| 42 | ); |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 43 | pData->rc = SQLITE_ERROR; |
| 44 | }else if( db->flags & SQLITE_WriteSchema ){ |
| 45 | pData->rc = SQLITE_CORRUPT_BKPT; |
| 46 | }else{ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 47 | char *z; |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 48 | const char *zObj = azObj[1] ? azObj[1] : "?"; |
drh | 17a936f | 2016-02-05 02:50:11 +0000 | [diff] [blame] | 49 | z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj); |
drh | 1e9c47b | 2018-03-16 20:15:58 +0000 | [diff] [blame] | 50 | if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra); |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 51 | *pData->pzErrMsg = z; |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 52 | pData->rc = SQLITE_CORRUPT_BKPT; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | /* |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 57 | ** Check to see if any sibling index (another index on the same table) |
| 58 | ** of pIndex has the same root page number, and if it does, return true. |
| 59 | ** This would indicate a corrupt schema. |
| 60 | */ |
| 61 | int sqlite3IndexHasDuplicateRootPage(Index *pIndex){ |
| 62 | Index *p; |
| 63 | for(p=pIndex->pTable->pIndex; p; p=p->pNext){ |
| 64 | if( p->tnum==pIndex->tnum && p!=pIndex ) return 1; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
drh | a22d2fc | 2019-10-05 18:33:25 +0000 | [diff] [blame] | 69 | /* forward declaration */ |
| 70 | static int sqlite3Prepare( |
| 71 | sqlite3 *db, /* Database handle. */ |
| 72 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 73 | int nBytes, /* Length of zSql in bytes. */ |
| 74 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 75 | Vdbe *pReprepare, /* VM being reprepared */ |
| 76 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 77 | const char **pzTail /* OUT: End of parsed string */ |
| 78 | ); |
| 79 | |
| 80 | |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 81 | /* |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 82 | ** This is the callback routine for the code that initializes the |
| 83 | ** database. See sqlite3Init() below for additional information. |
| 84 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
| 85 | ** |
| 86 | ** Each callback contains the following information: |
| 87 | ** |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 88 | ** argv[0] = type of object: "table", "index", "trigger", or "view". |
| 89 | ** argv[1] = name of thing being created |
| 90 | ** argv[2] = associated table if an index or trigger |
| 91 | ** argv[3] = root page number for table or index. 0 for trigger or view. |
| 92 | ** argv[4] = SQL text for the CREATE statement. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 93 | ** |
| 94 | */ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 95 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 96 | InitData *pData = (InitData*)pInit; |
| 97 | sqlite3 *db = pData->db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 98 | int iDb = pData->iDb; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 99 | |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 100 | assert( argc==5 ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 101 | UNUSED_PARAMETER2(NotUsed, argc); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 102 | assert( sqlite3_mutex_held(db->mutex) ); |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 103 | db->mDbFlags |= DBFLAG_EncodingFixed; |
drh | 6000e08 | 2021-04-13 13:01:07 +0000 | [diff] [blame] | 104 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 105 | pData->nInitRow++; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 106 | if( db->mallocFailed ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 107 | corruptSchema(pData, argv, 0); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 108 | return 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 109 | } |
| 110 | |
danielk1977 | ff9b2e7 | 2008-09-08 09:06:18 +0000 | [diff] [blame] | 111 | assert( iDb>=0 && iDb<db->nDb ); |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 112 | if( argv[3]==0 ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 113 | corruptSchema(pData, argv, 0); |
drh | 630fc34 | 2021-01-01 21:02:37 +0000 | [diff] [blame] | 114 | }else if( argv[4] |
| 115 | && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]] |
| 116 | && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 117 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 118 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 119 | ** or executed. All the parser does is build the internal data |
| 120 | ** structures that describe the table, index, or view. |
drh | 630fc34 | 2021-01-01 21:02:37 +0000 | [diff] [blame] | 121 | ** |
| 122 | ** No other valid SQL statement, other than the variable CREATE statements, |
| 123 | ** can begin with the letters "C" and "R". Thus, it is not possible run |
| 124 | ** any other kind of statement while parsing the schema, even a corrupt |
| 125 | ** schema. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 126 | */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 127 | int rc; |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 128 | u8 saved_iDb = db->init.iDb; |
drh | 6498f0b | 2010-04-09 09:14:05 +0000 | [diff] [blame] | 129 | sqlite3_stmt *pStmt; |
drh | 9e55d47 | 2010-07-06 09:29:01 +0000 | [diff] [blame] | 130 | TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ |
drh | 6498f0b | 2010-04-09 09:14:05 +0000 | [diff] [blame] | 131 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 132 | assert( db->init.busy ); |
| 133 | db->init.iDb = iDb; |
drh | 69306bf | 2020-07-22 20:12:10 +0000 | [diff] [blame] | 134 | if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0 |
| 135 | || (db->init.newTnum>pData->mxPage && pData->mxPage>0) |
| 136 | ){ |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 137 | if( sqlite3Config.bExtraSchemaChecks ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 138 | corruptSchema(pData, argv, "invalid rootpage"); |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 139 | } |
drh | 3b3ddba | 2020-07-22 18:03:56 +0000 | [diff] [blame] | 140 | } |
drh | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 141 | db->init.orphanTrigger = 0; |
drh | 2a6a72a | 2021-09-24 02:14:35 +0000 | [diff] [blame] | 142 | db->init.azInit = (const char**)argv; |
drh | a22d2fc | 2019-10-05 18:33:25 +0000 | [diff] [blame] | 143 | pStmt = 0; |
| 144 | TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0); |
dan | 9859c42 | 2010-07-06 07:36:18 +0000 | [diff] [blame] | 145 | rc = db->errCode; |
| 146 | assert( (rc&0xFF)==(rcp&0xFF) ); |
drh | 9ef5e77 | 2016-08-19 14:20:56 +0000 | [diff] [blame] | 147 | db->init.iDb = saved_iDb; |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 148 | /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 149 | if( SQLITE_OK!=rc ){ |
drh | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 150 | if( db->init.orphanTrigger ){ |
| 151 | assert( iDb==1 ); |
| 152 | }else{ |
drh | d4da493 | 2019-08-15 13:46:39 +0000 | [diff] [blame] | 153 | if( rc > pData->rc ) pData->rc = rc; |
drh | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 154 | if( rc==SQLITE_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 155 | sqlite3OomFault(db); |
dan | 9859c42 | 2010-07-06 07:36:18 +0000 | [diff] [blame] | 156 | }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 157 | corruptSchema(pData, argv, sqlite3_errmsg(db)); |
drh | 3d5f74b | 2009-08-06 17:43:31 +0000 | [diff] [blame] | 158 | } |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 159 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 160 | } |
drh | 2a6a72a | 2021-09-24 02:14:35 +0000 | [diff] [blame] | 161 | db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */ |
drh | 6498f0b | 2010-04-09 09:14:05 +0000 | [diff] [blame] | 162 | sqlite3_finalize(pStmt); |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 163 | }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 164 | corruptSchema(pData, argv, 0); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 165 | }else{ |
| 166 | /* If the SQL column is blank it means this is an index that |
| 167 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 168 | ** constraint for a CREATE TABLE. The index should have already |
| 169 | ** been created when we processed the CREATE TABLE. All we have |
| 170 | ** to do here is record the root page number for that index. |
| 171 | */ |
| 172 | Index *pIndex; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 173 | pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName); |
drh | 69306bf | 2020-07-22 20:12:10 +0000 | [diff] [blame] | 174 | if( pIndex==0 ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 175 | corruptSchema(pData, argv, "orphan index"); |
drh | 69306bf | 2020-07-22 20:12:10 +0000 | [diff] [blame] | 176 | }else |
| 177 | if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0 |
drh | 69ab18d | 2019-01-10 14:33:15 +0000 | [diff] [blame] | 178 | || pIndex->tnum<2 |
drh | 48bf2d7 | 2020-07-30 17:14:55 +0000 | [diff] [blame] | 179 | || pIndex->tnum>pData->mxPage |
drh | 8d40673 | 2019-01-30 18:33:33 +0000 | [diff] [blame] | 180 | || sqlite3IndexHasDuplicateRootPage(pIndex) |
drh | 69ab18d | 2019-01-10 14:33:15 +0000 | [diff] [blame] | 181 | ){ |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 182 | if( sqlite3Config.bExtraSchemaChecks ){ |
dan | 6a5a13d | 2021-02-17 20:08:22 +0000 | [diff] [blame] | 183 | corruptSchema(pData, argv, "invalid rootpage"); |
drh | ca439a4 | 2020-07-22 21:05:23 +0000 | [diff] [blame] | 184 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | ** Attempt to read the database schema and initialize internal |
| 192 | ** data structures for a single database file. The index of the |
| 193 | ** database file is given by iDb. iDb==0 is used for the main |
| 194 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 195 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
| 196 | ** indicate success or failure. |
| 197 | */ |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 198 | int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 199 | int rc; |
drh | 8a93919 | 2009-04-20 17:43:03 +0000 | [diff] [blame] | 200 | int i; |
mistachkin | ba2bba3 | 2012-09-18 23:21:32 +0000 | [diff] [blame] | 201 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 202 | int size; |
mistachkin | ba2bba3 | 2012-09-18 23:21:32 +0000 | [diff] [blame] | 203 | #endif |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 204 | Db *pDb; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 205 | char const *azArg[6]; |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 206 | int meta[5]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 207 | InitData initData; |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 208 | const char *zSchemaTabName; |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 209 | int openedTransaction = 0; |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 210 | int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 211 | |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 212 | assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 213 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 214 | assert( db->aDb[iDb].pSchema ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 215 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 4eab8b7 | 2007-12-27 15:12:16 +0000 | [diff] [blame] | 216 | assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 217 | |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 218 | db->init.busy = 1; |
| 219 | |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 220 | /* Construct the in-memory representation schema tables (sqlite_schema or |
| 221 | ** sqlite_temp_schema) by invoking the parser directly. The appropriate |
drh | 055f298 | 2016-01-15 15:06:41 +0000 | [diff] [blame] | 222 | ** table name will be inserted automatically by the parser so we can just |
| 223 | ** use the abbreviation "x" here. The parser will also automatically tag |
| 224 | ** the schema table as read-only. */ |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 225 | azArg[0] = "table"; |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 226 | azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb); |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 227 | azArg[2] = azArg[1]; |
| 228 | azArg[3] = "1"; |
| 229 | azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text," |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 230 | "rootpage int,sql text)"; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 231 | azArg[5] = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 232 | initData.db = db; |
drh | ece3c72 | 2006-09-23 20:36:01 +0000 | [diff] [blame] | 233 | initData.iDb = iDb; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 234 | initData.rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 235 | initData.pzErrMsg = pzErrMsg; |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 236 | initData.mInitFlags = mFlags; |
drh | 6b86e51 | 2019-01-05 21:09:37 +0000 | [diff] [blame] | 237 | initData.nInitRow = 0; |
drh | 3b3ddba | 2020-07-22 18:03:56 +0000 | [diff] [blame] | 238 | initData.mxPage = 0; |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 239 | sqlite3InitCallback(&initData, 5, (char **)azArg, 0); |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 240 | db->mDbFlags &= mask; |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 241 | if( initData.rc ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 242 | rc = initData.rc; |
| 243 | goto error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 244 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 245 | |
| 246 | /* Create a cursor to hold the database open |
| 247 | */ |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 248 | pDb = &db->aDb[iDb]; |
| 249 | if( pDb->pBt==0 ){ |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 250 | assert( iDb==1 ); |
| 251 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 252 | rc = SQLITE_OK; |
| 253 | goto error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 254 | } |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 255 | |
| 256 | /* If there is not already a read-only (or read-write) transaction opened |
| 257 | ** on the b-tree database, open one now. If a transaction is opened, it |
| 258 | ** will be closed before this function returns. */ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 259 | sqlite3BtreeEnter(pDb->pBt); |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 260 | if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 261 | rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 262 | if( rc!=SQLITE_OK ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 263 | sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc)); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 264 | goto initone_error_out; |
| 265 | } |
| 266 | openedTransaction = 1; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 267 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 268 | |
| 269 | /* Get the database meta information. |
| 270 | ** |
| 271 | ** Meta values are as follows: |
| 272 | ** meta[0] Schema cookie. Changes with each schema change. |
| 273 | ** meta[1] File format of schema layer. |
| 274 | ** meta[2] Size of the page cache. |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 275 | ** meta[3] Largest rootpage (auto/incr_vacuum mode) |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 276 | ** 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] | 277 | ** meta[5] User version |
| 278 | ** meta[6] Incremental vacuum mode |
| 279 | ** meta[7] unused |
| 280 | ** meta[8] unused |
| 281 | ** meta[9] unused |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 282 | ** |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 283 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 284 | ** the possible values of meta[4]. |
| 285 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 286 | for(i=0; i<ArraySize(meta); i++){ |
| 287 | sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 288 | } |
drh | 0314cf3 | 2018-04-28 01:27:09 +0000 | [diff] [blame] | 289 | if( (db->flags & SQLITE_ResetDatabase)!=0 ){ |
| 290 | memset(meta, 0, sizeof(meta)); |
| 291 | } |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 292 | pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 293 | |
| 294 | /* If opening a non-empty database, check the text encoding. For the |
| 295 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 296 | ** For an attached db, it is an error if the encoding is not the same |
| 297 | ** as sqlite3.enc. |
| 298 | */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 299 | if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 300 | if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){ |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 301 | u8 encoding; |
drh | 42a630b | 2020-03-05 16:13:24 +0000 | [diff] [blame] | 302 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 303 | /* If opening the main database, set ENC(db). */ |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 304 | encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; |
| 305 | if( encoding==0 ) encoding = SQLITE_UTF8; |
dan | dbd4d5f | 2013-03-08 07:10:37 +0000 | [diff] [blame] | 306 | #else |
drh | 42a630b | 2020-03-05 16:13:24 +0000 | [diff] [blame] | 307 | encoding = SQLITE_UTF8; |
dan | dbd4d5f | 2013-03-08 07:10:37 +0000 | [diff] [blame] | 308 | #endif |
drh | 42a630b | 2020-03-05 16:13:24 +0000 | [diff] [blame] | 309 | sqlite3SetTextEncoding(db, encoding); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 310 | }else{ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 311 | /* If opening an attached database, the encoding much match ENC(db) */ |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 312 | if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 313 | sqlite3SetString(pzErrMsg, db, "attached databases must use the same" |
| 314 | " text encoding as main database"); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 315 | rc = SQLITE_ERROR; |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 316 | goto initone_error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 320 | pDb->pSchema->enc = ENC(db); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 321 | |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 322 | if( pDb->pSchema->cache_size==0 ){ |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 323 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 324 | size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]); |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 325 | if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 326 | pDb->pSchema->cache_size = size; |
drh | e73c914 | 2011-11-09 16:12:24 +0000 | [diff] [blame] | 327 | #else |
| 328 | pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE; |
| 329 | #endif |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 330 | sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); |
| 331 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | ** file_format==1 Version 3.0.0. |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 335 | ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN |
| 336 | ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults |
drh | d946db0 | 2005-12-29 19:23:06 +0000 | [diff] [blame] | 337 | ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 338 | */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 339 | pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 340 | if( pDb->pSchema->file_format==0 ){ |
| 341 | pDb->pSchema->file_format = 1; |
drh | fdd6e85 | 2005-12-16 01:06:16 +0000 | [diff] [blame] | 342 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 343 | if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 344 | sqlite3SetString(pzErrMsg, db, "unsupported file format"); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 345 | rc = SQLITE_ERROR; |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 346 | goto initone_error_out; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 347 | } |
| 348 | |
drh | 4aa2bfe | 2007-11-28 13:43:16 +0000 | [diff] [blame] | 349 | /* Ticket #2804: When we open a database in the newer file format, |
| 350 | ** clear the legacy_file_format pragma flag so that a VACUUM will |
| 351 | ** not downgrade the database and thus invalidate any descending |
| 352 | ** indices that the user might have created. |
| 353 | */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 354 | if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ |
drh | d5b44d6 | 2018-12-06 17:06:02 +0000 | [diff] [blame] | 355 | db->flags &= ~(u64)SQLITE_LegacyFileFmt; |
drh | 4aa2bfe | 2007-11-28 13:43:16 +0000 | [diff] [blame] | 356 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 357 | |
| 358 | /* Read the schema information out of the schema tables |
| 359 | */ |
| 360 | assert( db->init.busy ); |
drh | 3b3ddba | 2020-07-22 18:03:56 +0000 | [diff] [blame] | 361 | initData.mxPage = sqlite3BtreeLastPage(pDb->pBt); |
drh | 9da742f | 2009-06-16 17:49:36 +0000 | [diff] [blame] | 362 | { |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 363 | char *zSql; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 364 | zSql = sqlite3MPrintf(db, |
drh | c5a93d4 | 2019-08-12 00:08:07 +0000 | [diff] [blame] | 365 | "SELECT*FROM\"%w\".%s ORDER BY rowid", |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 366 | db->aDb[iDb].zDbSName, zSchemaTabName); |
drh | a6d0ffc | 2007-10-12 20:42:28 +0000 | [diff] [blame] | 367 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 368 | { |
drh | 32c6a48 | 2014-09-11 13:44:52 +0000 | [diff] [blame] | 369 | sqlite3_xauth xAuth; |
drh | a6d0ffc | 2007-10-12 20:42:28 +0000 | [diff] [blame] | 370 | xAuth = db->xAuth; |
| 371 | db->xAuth = 0; |
| 372 | #endif |
| 373 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 374 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 375 | db->xAuth = xAuth; |
| 376 | } |
| 377 | #endif |
drh | c456e57 | 2008-08-11 18:44:58 +0000 | [diff] [blame] | 378 | if( rc==SQLITE_OK ) rc = initData.rc; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 379 | sqlite3DbFree(db, zSql); |
drh | 497e446 | 2005-07-23 03:18:40 +0000 | [diff] [blame] | 380 | #ifndef SQLITE_OMIT_ANALYZE |
| 381 | if( rc==SQLITE_OK ){ |
| 382 | sqlite3AnalysisLoad(db, iDb); |
| 383 | } |
| 384 | #endif |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 385 | } |
drh | 7a7cefa | 2021-06-13 17:55:58 +0000 | [diff] [blame] | 386 | assert( pDb == &(db->aDb[iDb]) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 387 | if( db->mallocFailed ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 388 | rc = SQLITE_NOMEM_BKPT; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 389 | sqlite3ResetAllSchemasOfConnection(db); |
drh | 7a7cefa | 2021-06-13 17:55:58 +0000 | [diff] [blame] | 390 | pDb = &db->aDb[iDb]; |
drh | 569143c | 2021-04-13 13:20:55 +0000 | [diff] [blame] | 391 | }else |
drh | 5705b41 | 2022-03-24 14:01:55 +0000 | [diff] [blame] | 392 | if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){ |
drh | 569143c | 2021-04-13 13:20:55 +0000 | [diff] [blame] | 393 | /* Hack: If the SQLITE_NoSchemaError flag is set, then consider |
| 394 | ** the schema loaded, even if errors (other than OOM) occurred. In |
| 395 | ** this situation the current sqlite3_prepare() operation will fail, |
| 396 | ** but the following one will attempt to compile the supplied statement |
| 397 | ** against whatever subset of the schema was loaded before the error |
| 398 | ** occurred. |
| 399 | ** |
| 400 | ** The primary purpose of this is to allow access to the sqlite_schema |
| 401 | ** table even when its contents have been corrupted. |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 402 | */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 403 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
danielk1977 | 34c68fb | 2007-03-14 15:37:04 +0000 | [diff] [blame] | 404 | rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 405 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 406 | |
| 407 | /* Jump here for an error that occurs after successfully allocating |
| 408 | ** curMain and calling sqlite3BtreeEnter(). For an error that occurs |
| 409 | ** before that point, jump to error_out. |
| 410 | */ |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 411 | initone_error_out: |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 412 | if( openedTransaction ){ |
| 413 | sqlite3BtreeCommit(pDb->pBt); |
| 414 | } |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 415 | sqlite3BtreeLeave(pDb->pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 416 | |
| 417 | error_out: |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 418 | if( rc ){ |
| 419 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
| 420 | sqlite3OomFault(db); |
| 421 | } |
| 422 | sqlite3ResetOneSchema(db, iDb); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 423 | } |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 424 | db->init.busy = 0; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | ** Initialize all database files - the main database file, the file |
| 430 | ** used to store temporary tables, and any additional database files |
| 431 | ** created using ATTACH statements. Return a success code. If an |
| 432 | ** error occurs, write an error message into *pzErrMsg. |
| 433 | ** |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 434 | ** After a database is initialized, the DB_SchemaLoaded bit is set |
dan | 0ea2d42 | 2020-03-05 18:04:09 +0000 | [diff] [blame] | 435 | ** bit is set in the flags field of the Db structure. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 436 | */ |
| 437 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
| 438 | int i, rc; |
drh | 8257aa8 | 2017-07-26 19:59:13 +0000 | [diff] [blame] | 439 | int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 440 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 441 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 9bd3cc4 | 2014-12-12 23:17:54 +0000 | [diff] [blame] | 442 | assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) ); |
drh | 09e6054 | 2014-09-10 22:46:46 +0000 | [diff] [blame] | 443 | assert( db->init.busy==0 ); |
drh | 9bd3cc4 | 2014-12-12 23:17:54 +0000 | [diff] [blame] | 444 | ENC(db) = SCHEMA_ENC(db); |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 445 | assert( db->nDb>0 ); |
| 446 | /* Do the main schema first */ |
| 447 | if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){ |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 448 | rc = sqlite3InitOne(db, 0, pzErrMsg, 0); |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 449 | if( rc ) return rc; |
| 450 | } |
| 451 | /* All other schemas after the main schema. The "temp" schema must be last */ |
| 452 | for(i=db->nDb-1; i>0; i--){ |
drh | 3215872 | 2018-04-25 10:30:46 +0000 | [diff] [blame] | 453 | assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) ); |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 454 | if( !DbHasProperty(db, i, DB_SchemaLoaded) ){ |
drh | 1595abc | 2018-08-14 19:27:51 +0000 | [diff] [blame] | 455 | rc = sqlite3InitOne(db, i, pzErrMsg, 0); |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 456 | if( rc ) return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 459 | if( commit_internal ){ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 460 | sqlite3CommitInternalChanges(db); |
| 461 | } |
drh | 36494b8 | 2017-08-25 15:43:34 +0000 | [diff] [blame] | 462 | return SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 466 | ** This routine is a no-op if the database schema is already initialized. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 467 | ** Otherwise, the schema is loaded. An error code is returned. |
| 468 | */ |
| 469 | int sqlite3ReadSchema(Parse *pParse){ |
| 470 | int rc = SQLITE_OK; |
| 471 | sqlite3 *db = pParse->db; |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 472 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 473 | if( !db->init.busy ){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 474 | rc = sqlite3Init(db, &pParse->zErrMsg); |
drh | b2c8559 | 2018-04-25 12:01:45 +0000 | [diff] [blame] | 475 | if( rc!=SQLITE_OK ){ |
| 476 | pParse->rc = rc; |
| 477 | pParse->nErr++; |
| 478 | }else if( db->noSharedCache ){ |
| 479 | db->mDbFlags |= DBFLAG_SchemaKnownOk; |
| 480 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 481 | } |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /* |
| 487 | ** Check schema cookies in all databases. If any cookie is out |
drh | 1adecdf | 2009-07-03 19:19:50 +0000 | [diff] [blame] | 488 | ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies |
| 489 | ** make no changes to pParse->rc. |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 490 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 491 | static void schemaIsValid(Parse *pParse){ |
| 492 | sqlite3 *db = pParse->db; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 493 | int iDb; |
| 494 | int rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 495 | int cookie; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 496 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 497 | assert( pParse->checkSchema ); |
| 498 | assert( sqlite3_mutex_held(db->mutex) ); |
| 499 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 500 | int openedTransaction = 0; /* True if a transaction is opened */ |
| 501 | Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */ |
| 502 | if( pBt==0 ) continue; |
| 503 | |
| 504 | /* If there is not already a read-only (or read-write) transaction opened |
| 505 | ** on the b-tree database, open one now. If a transaction is opened, it |
| 506 | ** will be closed immediately after reading the meta-value. */ |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 507 | if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE ){ |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 508 | rc = sqlite3BtreeBeginTrans(pBt, 0, 0); |
drh | 1adecdf | 2009-07-03 19:19:50 +0000 | [diff] [blame] | 509 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 510 | sqlite3OomFault(db); |
drh | 2389048 | 2021-04-05 11:39:55 +0000 | [diff] [blame] | 511 | pParse->rc = SQLITE_NOMEM; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 512 | } |
| 513 | if( rc!=SQLITE_OK ) return; |
| 514 | openedTransaction = 1; |
| 515 | } |
| 516 | |
| 517 | /* Read the schema cookie from the database. If it does not match the |
shaneh | d77f56e | 2009-12-17 21:05:42 +0000 | [diff] [blame] | 518 | ** value stored as part of the in-memory schema representation, |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 519 | ** set Parse.rc to SQLITE_SCHEMA. */ |
| 520 | sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 521 | assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 522 | if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ |
dan | 873a840 | 2022-11-10 19:19:52 +0000 | [diff] [blame] | 523 | if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA; |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 524 | sqlite3ResetOneSchema(db, iDb); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* Close the transaction, if one was opened. */ |
| 528 | if( openedTransaction ){ |
| 529 | sqlite3BtreeCommit(pBt); |
| 530 | } |
| 531 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
drh | f248e21 | 2006-01-25 22:50:38 +0000 | [diff] [blame] | 535 | ** Convert a schema pointer into the iDb index that indicates |
| 536 | ** which database file in db->aDb[] the schema refers to. |
| 537 | ** |
| 538 | ** If the same database is attached more than once, the first |
| 539 | ** attached database is returned. |
| 540 | */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 541 | int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ |
drh | bdd4f7d | 2020-05-26 10:54:46 +0000 | [diff] [blame] | 542 | int i = -32768; |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 543 | |
drh | bdd4f7d | 2020-05-26 10:54:46 +0000 | [diff] [blame] | 544 | /* If pSchema is NULL, then return -32768. This happens when code in |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 545 | ** expr.c is trying to resolve a reference to a transient table (i.e. one |
| 546 | ** created by a sub-select). In this case the return value of this |
| 547 | ** function should never be used. |
| 548 | ** |
drh | bdd4f7d | 2020-05-26 10:54:46 +0000 | [diff] [blame] | 549 | ** We return -32768 instead of the more usual -1 simply because using |
| 550 | ** -32768 as the incorrect index into db->aDb[] is much |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 551 | ** more likely to cause a segfault than -1 (of course there are assert() |
drh | bdd4f7d | 2020-05-26 10:54:46 +0000 | [diff] [blame] | 552 | ** statements too, but it never hurts to play the odds) and |
| 553 | ** -32768 will still fit into a 16-bit signed integer. |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 554 | */ |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 555 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 556 | if( pSchema ){ |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 557 | for(i=0; 1; i++){ |
| 558 | assert( i<db->nDb ); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 559 | if( db->aDb[i].pSchema==pSchema ){ |
| 560 | break; |
| 561 | } |
| 562 | } |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 563 | assert( i>=0 && i<db->nDb ); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 564 | } |
| 565 | return i; |
| 566 | } |
| 567 | |
| 568 | /* |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 569 | ** Free all memory allocations in the pParse object |
| 570 | */ |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 571 | void sqlite3ParseObjectReset(Parse *pParse){ |
drh | 6903bf6 | 2017-08-01 20:59:41 +0000 | [diff] [blame] | 572 | sqlite3 *db = pParse->db; |
drh | 1da88b5 | 2022-01-24 19:38:56 +0000 | [diff] [blame] | 573 | assert( db!=0 ); |
| 574 | assert( db->pParse==pParse ); |
drh | 1611826 | 2021-12-31 17:54:48 +0000 | [diff] [blame] | 575 | assert( pParse->nested==0 ); |
| 576 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 577 | if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock); |
drh | 1611826 | 2021-12-31 17:54:48 +0000 | [diff] [blame] | 578 | #endif |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 579 | while( pParse->pCleanup ){ |
drh | 5e5683a | 2021-01-13 21:05:07 +0000 | [diff] [blame] | 580 | ParseCleanup *pCleanup = pParse->pCleanup; |
| 581 | pParse->pCleanup = pCleanup->pNext; |
| 582 | pCleanup->xCleanup(db, pCleanup->pPtr); |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 583 | sqlite3DbNNFreeNN(db, pCleanup); |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 584 | } |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 585 | if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel); |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 586 | if( pParse->pConstExpr ){ |
| 587 | sqlite3ExprListDelete(db, pParse->pConstExpr); |
| 588 | } |
drh | 1da88b5 | 2022-01-24 19:38:56 +0000 | [diff] [blame] | 589 | assert( db->lookaside.bDisable >= pParse->disableLookaside ); |
| 590 | db->lookaside.bDisable -= pParse->disableLookaside; |
| 591 | db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue; |
| 592 | assert( pParse->db->pParse==pParse ); |
| 593 | db->pParse = pParse->pOuterParse; |
| 594 | pParse->db = 0; |
drh | 6903bf6 | 2017-08-01 20:59:41 +0000 | [diff] [blame] | 595 | pParse->disableLookaside = 0; |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 599 | ** Add a new cleanup operation to a Parser. The cleanup should happen when |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 600 | ** the parser object is destroyed. But, beware: the cleanup might happen |
| 601 | ** immediately. |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 602 | ** |
| 603 | ** Use this mechanism for uncommon cleanups. There is a higher setup |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 604 | ** cost for this mechansim (an extra malloc), so it should not be used |
| 605 | ** for common cleanups that happen on most calls. But for less |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 606 | ** common cleanups, we save a single NULL-pointer comparison in |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 607 | ** sqlite3ParseObjectReset(), which reduces the total CPU cycle count. |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 608 | ** |
| 609 | ** If a memory allocation error occurs, then the cleanup happens immediately. |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 610 | ** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 611 | ** pParse->earlyCleanup flag is set in that case. Calling code show verify |
| 612 | ** that test cases exist for which this happens, to guard against possible |
| 613 | ** use-after-free errors following an OOM. The preferred way to do this is |
| 614 | ** to immediately follow the call to this routine with: |
| 615 | ** |
| 616 | ** testcase( pParse->earlyCleanup ); |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 617 | ** |
| 618 | ** This routine returns a copy of its pPtr input (the third parameter) |
| 619 | ** except if an early cleanup occurs, in which case it returns NULL. So |
| 620 | ** another way to check for early cleanup is to check the return value. |
| 621 | ** Or, stop using the pPtr parameter with this call and use only its |
| 622 | ** return value thereafter. Something like this: |
| 623 | ** |
| 624 | ** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj); |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 625 | */ |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 626 | void *sqlite3ParserAddCleanup( |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 627 | Parse *pParse, /* Destroy when this Parser finishes */ |
| 628 | void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */ |
| 629 | void *pPtr /* Pointer to object to be cleaned up */ |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 630 | ){ |
| 631 | ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup)); |
| 632 | if( pCleanup ){ |
| 633 | pCleanup->pNext = pParse->pCleanup; |
| 634 | pParse->pCleanup = pCleanup; |
| 635 | pCleanup->pPtr = pPtr; |
| 636 | pCleanup->xCleanup = xCleanup; |
| 637 | }else{ |
| 638 | xCleanup(pParse->db, pPtr); |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 639 | pPtr = 0; |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 640 | #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) |
| 641 | pParse->earlyCleanup = 1; |
| 642 | #endif |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 643 | } |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 644 | return pPtr; |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 648 | ** Turn bulk memory into a valid Parse object and link that Parse object |
| 649 | ** into database connection db. |
| 650 | ** |
| 651 | ** Call sqlite3ParseObjectReset() to undo this operation. |
| 652 | ** |
| 653 | ** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which |
| 654 | ** is generated by Lemon. |
| 655 | */ |
| 656 | void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){ |
| 657 | memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ); |
| 658 | memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ); |
| 659 | assert( db->pParse!=pParse ); |
| 660 | pParse->pOuterParse = db->pParse; |
| 661 | db->pParse = pParse; |
| 662 | pParse->db = db; |
drh | 75863ec | 2022-01-28 21:39:29 +0000 | [diff] [blame] | 663 | if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory"); |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
drh | 87b7ac0 | 2022-05-06 00:43:06 +0000 | [diff] [blame] | 667 | ** Maximum number of times that we will try again to prepare a statement |
| 668 | ** that returns SQLITE_ERROR_RETRY. |
| 669 | */ |
| 670 | #ifndef SQLITE_MAX_PREPARE_RETRY |
| 671 | # define SQLITE_MAX_PREPARE_RETRY 25 |
| 672 | #endif |
| 673 | |
| 674 | /* |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 675 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 676 | */ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 677 | static int sqlite3Prepare( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 678 | sqlite3 *db, /* Database handle. */ |
| 679 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 680 | int nBytes, /* Length of zSql in bytes. */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 681 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 682 | Vdbe *pReprepare, /* VM being reprepared */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 683 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 684 | const char **pzTail /* OUT: End of parsed string */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 685 | ){ |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 686 | int rc = SQLITE_OK; /* Result code */ |
| 687 | int i; /* Loop counter */ |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 688 | Parse sParse; /* Parsing context */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 689 | |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 690 | /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */ |
| 691 | memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ); |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 692 | memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ); |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 693 | sParse.pOuterParse = db->pParse; |
| 694 | db->pParse = &sParse; |
| 695 | sParse.db = db; |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 696 | sParse.pReprepare = pReprepare; |
drh | 860e077 | 2009-04-02 18:32:26 +0000 | [diff] [blame] | 697 | assert( ppStmt && *ppStmt==0 ); |
drh | 75863ec | 2022-01-28 21:39:29 +0000 | [diff] [blame] | 698 | if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory"); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 699 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 700 | |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 701 | /* For a long-term use prepared statement avoid the use of |
| 702 | ** lookaside memory. |
| 703 | */ |
| 704 | if( prepFlags & SQLITE_PREPARE_PERSISTENT ){ |
| 705 | sParse.disableLookaside++; |
drh | 31f6962 | 2019-10-05 14:39:36 +0000 | [diff] [blame] | 706 | DisableLookaside; |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 707 | } |
drh | 7424aef | 2022-10-01 13:17:53 +0000 | [diff] [blame] | 708 | sParse.prepFlags = prepFlags & 0xff; |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 709 | |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 710 | /* Check to verify that it is possible to get a read lock on all |
| 711 | ** database schemas. The inability to get a read lock indicates that |
| 712 | ** some other database connection is holding a write-lock, which in |
| 713 | ** turn means that the other connection has made uncommitted changes |
| 714 | ** to the schema. |
| 715 | ** |
| 716 | ** Were we to proceed and prepare the statement against the uncommitted |
| 717 | ** schema changes and if those schema changes are subsequently rolled |
| 718 | ** back and different changes are made in their place, then when this |
| 719 | ** prepared statement goes to run the schema cookie would fail to detect |
| 720 | ** the schema change. Disaster would follow. |
| 721 | ** |
| 722 | ** This thread is currently holding mutexes on all Btrees (because |
| 723 | ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it |
| 724 | ** is not possible for another thread to start a new schema change |
| 725 | ** while this routine is running. Hence, we do not need to hold |
| 726 | ** locks on the schema, we just need to make sure nobody else is |
| 727 | ** holding them. |
| 728 | ** |
| 729 | ** Note that setting READ_UNCOMMITTED overrides most lock detection, |
| 730 | ** but it does *not* override schema lock detection, so this all still |
| 731 | ** works even if READ_UNCOMMITTED is set. |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 732 | */ |
drh | 705e733 | 2019-10-05 19:53:21 +0000 | [diff] [blame] | 733 | if( !db->noSharedCache ){ |
| 734 | for(i=0; i<db->nDb; i++) { |
| 735 | Btree *pBt = db->aDb[i].pBt; |
| 736 | if( pBt ){ |
| 737 | assert( sqlite3BtreeHoldsMutex(pBt) ); |
| 738 | rc = sqlite3BtreeSchemaLocked(pBt); |
| 739 | if( rc ){ |
| 740 | const char *zDb = db->aDb[i].zDbSName; |
| 741 | sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb); |
| 742 | testcase( db->flags & SQLITE_ReadUncommit ); |
| 743 | goto end_prepare; |
| 744 | } |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 745 | } |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 748 | |
drh | 7d34b8d | 2022-10-01 20:27:29 +0000 | [diff] [blame] | 749 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 750 | if( db->pDisconnect ) sqlite3VtabUnlockList(db); |
| 751 | #endif |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 752 | |
drh | d2d88bb | 2008-05-23 14:32:18 +0000 | [diff] [blame] | 753 | if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 754 | char *zSqlCopy; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 755 | int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
drh | 58fbb31 | 2009-06-17 00:35:30 +0000 | [diff] [blame] | 756 | testcase( nBytes==mxLen ); |
| 757 | testcase( nBytes==mxLen+1 ); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 758 | if( nBytes>mxLen ){ |
drh | 13f40da | 2014-08-22 18:00:11 +0000 | [diff] [blame] | 759 | sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long"); |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 760 | rc = sqlite3ApiExit(db, SQLITE_TOOBIG); |
| 761 | goto end_prepare; |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 762 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 763 | zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 764 | if( zSqlCopy ){ |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 765 | sqlite3RunParser(&sParse, zSqlCopy); |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 766 | sParse.zTail = &zSql[sParse.zTail-zSqlCopy]; |
dan | 98a4d5a | 2016-01-29 08:38:35 +0000 | [diff] [blame] | 767 | sqlite3DbFree(db, zSqlCopy); |
danielk1977 | 3a2c8c8 | 2008-04-03 14:36:25 +0000 | [diff] [blame] | 768 | }else{ |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 769 | sParse.zTail = &zSql[nBytes]; |
drh | 276fdbf | 2007-04-30 21:39:16 +0000 | [diff] [blame] | 770 | } |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 771 | }else{ |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 772 | sqlite3RunParser(&sParse, zSql); |
drh | 9051a42 | 2006-01-30 22:35:43 +0000 | [diff] [blame] | 773 | } |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 774 | assert( 0==sParse.nQueryLoop ); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 775 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 776 | if( pzTail ){ |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 777 | *pzTail = sParse.zTail; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 778 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 779 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 780 | if( db->init.busy==0 ){ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 781 | sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags); |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 782 | } |
drh | f3ce248 | 2019-10-09 01:19:07 +0000 | [diff] [blame] | 783 | if( db->mallocFailed ){ |
| 784 | sParse.rc = SQLITE_NOMEM_BKPT; |
drh | ad3930b | 2021-04-03 20:35:08 +0000 | [diff] [blame] | 785 | sParse.checkSchema = 0; |
drh | f3ce248 | 2019-10-09 01:19:07 +0000 | [diff] [blame] | 786 | } |
drh | 88efc79 | 2021-01-01 18:23:56 +0000 | [diff] [blame] | 787 | if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){ |
drh | 24a82ea | 2022-01-01 22:55:31 +0000 | [diff] [blame] | 788 | if( sParse.checkSchema && db->init.busy==0 ){ |
drh | 88efc79 | 2021-01-01 18:23:56 +0000 | [diff] [blame] | 789 | schemaIsValid(&sParse); |
| 790 | } |
| 791 | if( sParse.pVdbe ){ |
| 792 | sqlite3VdbeFinalize(sParse.pVdbe); |
| 793 | } |
| 794 | assert( 0==(*ppStmt) ); |
| 795 | rc = sParse.rc; |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 796 | if( sParse.zErrMsg ){ |
| 797 | sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg); |
| 798 | sqlite3DbFree(db, sParse.zErrMsg); |
drh | 88efc79 | 2021-01-01 18:23:56 +0000 | [diff] [blame] | 799 | }else{ |
| 800 | sqlite3Error(db, rc); |
| 801 | } |
danielk1977 | 7e29e95 | 2007-04-19 11:09:01 +0000 | [diff] [blame] | 802 | }else{ |
drh | 54bc638 | 2021-12-31 19:20:42 +0000 | [diff] [blame] | 803 | assert( sParse.zErrMsg==0 ); |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 804 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
drh | 88efc79 | 2021-01-01 18:23:56 +0000 | [diff] [blame] | 805 | rc = SQLITE_OK; |
| 806 | sqlite3ErrorClear(db); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 807 | } |
| 808 | |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 809 | |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 810 | /* Delete any TriggerPrg structures allocated while parsing this statement. */ |
drh | cb43a93 | 2016-10-03 01:21:51 +0000 | [diff] [blame] | 811 | while( sParse.pTriggerPrg ){ |
| 812 | TriggerPrg *pT = sParse.pTriggerPrg; |
| 813 | sParse.pTriggerPrg = pT->pNext; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 814 | sqlite3DbFree(db, pT); |
| 815 | } |
| 816 | |
drh | e7b34707 | 2009-06-01 18:18:20 +0000 | [diff] [blame] | 817 | end_prepare: |
| 818 | |
drh | c692df2 | 2022-01-24 15:34:55 +0000 | [diff] [blame] | 819 | sqlite3ParseObjectReset(&sParse); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 820 | return rc; |
| 821 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 822 | static int sqlite3LockAndPrepare( |
| 823 | sqlite3 *db, /* Database handle. */ |
| 824 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 825 | int nBytes, /* Length of zSql in bytes. */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 826 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 827 | Vdbe *pOld, /* VM being reprepared */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 828 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 829 | const char **pzTail /* OUT: End of parsed string */ |
| 830 | ){ |
| 831 | int rc; |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 832 | int cnt = 0; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 833 | |
| 834 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 835 | if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 836 | #endif |
drh | 860e077 | 2009-04-02 18:32:26 +0000 | [diff] [blame] | 837 | *ppStmt = 0; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 838 | if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 839 | return SQLITE_MISUSE_BKPT; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 840 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 841 | sqlite3_mutex_enter(db->mutex); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 842 | sqlite3BtreeEnterAll(db); |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 843 | do{ |
| 844 | /* Make multiple attempts to compile the SQL, until it either succeeds |
| 845 | ** or encounters a permanent error. A schema problem after one schema |
| 846 | ** reset is considered a permanent error. */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 847 | rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail); |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 848 | assert( rc==SQLITE_OK || *ppStmt==0 ); |
drh | 15561b9 | 2021-12-09 14:09:47 +0000 | [diff] [blame] | 849 | if( rc==SQLITE_OK || db->mallocFailed ) break; |
drh | 87b7ac0 | 2022-05-06 00:43:06 +0000 | [diff] [blame] | 850 | }while( (rc==SQLITE_ERROR_RETRY && (cnt++)<SQLITE_MAX_PREPARE_RETRY) |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 851 | || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) ); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 852 | sqlite3BtreeLeaveAll(db); |
drh | 7e8515d | 2017-12-08 19:37:04 +0000 | [diff] [blame] | 853 | rc = sqlite3ApiExit(db, rc); |
| 854 | assert( (rc&db->errMask)==rc ); |
dan | 2b06b07 | 2020-09-04 17:30:59 +0000 | [diff] [blame] | 855 | db->busyHandler.nBusy = 0; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 856 | sqlite3_mutex_leave(db->mutex); |
| 857 | return rc; |
| 858 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 859 | |
mistachkin | 8bee11a | 2018-10-29 17:53:23 +0000 | [diff] [blame] | 860 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 861 | /* |
| 862 | ** Rerun the compilation of a statement after a schema change. |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 863 | ** |
| 864 | ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, |
| 865 | ** if the statement cannot be recompiled because another connection has |
drh | 1e32bed | 2020-06-19 13:33:53 +0000 | [diff] [blame] | 866 | ** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 867 | ** occurs, return SQLITE_SCHEMA. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 868 | */ |
| 869 | int sqlite3Reprepare(Vdbe *p){ |
| 870 | int rc; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 871 | sqlite3_stmt *pNew; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 872 | const char *zSql; |
| 873 | sqlite3 *db; |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 874 | u8 prepFlags; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 875 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 876 | assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); |
danielk1977 | d0e2a85 | 2007-11-14 06:48:48 +0000 | [diff] [blame] | 877 | zSql = sqlite3_sql((sqlite3_stmt *)p); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 878 | assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 879 | db = sqlite3VdbeDb(p); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 880 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 881 | prepFlags = sqlite3VdbePrepareFlags(p); |
| 882 | rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 883 | if( rc ){ |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 884 | if( rc==SQLITE_NOMEM ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 885 | sqlite3OomFault(db); |
danielk1977 | 8e55652 | 2007-11-13 10:30:24 +0000 | [diff] [blame] | 886 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 887 | assert( pNew==0 ); |
drh | a6129fa | 2010-02-24 17:15:19 +0000 | [diff] [blame] | 888 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 889 | }else{ |
| 890 | assert( pNew!=0 ); |
| 891 | } |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 892 | sqlite3VdbeSwap((Vdbe*)pNew, p); |
shane | 145834a | 2008-09-04 12:03:42 +0000 | [diff] [blame] | 893 | sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 894 | sqlite3VdbeResetStepResult((Vdbe*)pNew); |
| 895 | sqlite3VdbeFinalize((Vdbe*)pNew); |
danielk1977 | 65a2ea1 | 2009-03-19 07:58:31 +0000 | [diff] [blame] | 896 | return SQLITE_OK; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
| 900 | /* |
| 901 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 902 | ** version, the original SQL text is not saved in the prepared statement |
| 903 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 904 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 905 | ** and the statement is automatically recompiled if an schema change |
| 906 | ** occurs. |
| 907 | */ |
| 908 | int sqlite3_prepare( |
| 909 | sqlite3 *db, /* Database handle. */ |
| 910 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 911 | int nBytes, /* Length of zSql in bytes. */ |
| 912 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 913 | const char **pzTail /* OUT: End of parsed string */ |
| 914 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 915 | int rc; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 916 | rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 917 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 918 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 919 | } |
| 920 | int sqlite3_prepare_v2( |
| 921 | sqlite3 *db, /* Database handle. */ |
| 922 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 923 | int nBytes, /* Length of zSql in bytes. */ |
| 924 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 925 | const char **pzTail /* OUT: End of parsed string */ |
| 926 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 927 | int rc; |
drh | 3cef364 | 2017-07-14 19:22:08 +0000 | [diff] [blame] | 928 | /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works |
| 929 | ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags |
| 930 | ** parameter. |
| 931 | ** |
| 932 | ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 933 | rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0, |
| 934 | ppStmt,pzTail); |
drh | 3cef364 | 2017-07-14 19:22:08 +0000 | [diff] [blame] | 935 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 936 | return rc; |
| 937 | } |
| 938 | int sqlite3_prepare_v3( |
| 939 | sqlite3 *db, /* Database handle. */ |
| 940 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 941 | int nBytes, /* Length of zSql in bytes. */ |
| 942 | unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 943 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 944 | const char **pzTail /* OUT: End of parsed string */ |
| 945 | ){ |
| 946 | int rc; |
drh | 3cef364 | 2017-07-14 19:22:08 +0000 | [diff] [blame] | 947 | /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from |
| 948 | ** sqlite3_prepare_v2() only in having the extra prepFlags parameter, |
| 949 | ** which is a bit array consisting of zero or more of the |
| 950 | ** SQLITE_PREPARE_* flags. |
| 951 | ** |
| 952 | ** Proof by comparison to the implementation of sqlite3_prepare_v2() |
| 953 | ** directly above. */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 954 | rc = sqlite3LockAndPrepare(db,zSql,nBytes, |
| 955 | SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK), |
| 956 | 0,ppStmt,pzTail); |
drh | 3cef364 | 2017-07-14 19:22:08 +0000 | [diff] [blame] | 957 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 958 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 962 | #ifndef SQLITE_OMIT_UTF16 |
| 963 | /* |
| 964 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 965 | */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 966 | static int sqlite3Prepare16( |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 967 | sqlite3 *db, /* Database handle. */ |
dan | 0ecdeb2 | 2011-01-25 13:43:35 +0000 | [diff] [blame] | 968 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 969 | int nBytes, /* Length of zSql in bytes. */ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 970 | u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 971 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 972 | const void **pzTail /* OUT: End of parsed string */ |
| 973 | ){ |
| 974 | /* This function currently works by first transforming the UTF-16 |
| 975 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 976 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 977 | */ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 978 | char *zSql8; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 979 | const char *zTail8 = 0; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 980 | int rc = SQLITE_OK; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 981 | |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 982 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 983 | if( ppStmt==0 ) return SQLITE_MISUSE_BKPT; |
| 984 | #endif |
drh | 769e97e | 2009-04-01 16:33:37 +0000 | [diff] [blame] | 985 | *ppStmt = 0; |
drh | 9ca9573 | 2014-10-24 00:35:58 +0000 | [diff] [blame] | 986 | if( !sqlite3SafetyCheckOk(db)||zSql==0 ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 987 | return SQLITE_MISUSE_BKPT; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 988 | } |
drh | 7232ad0 | 2013-07-16 23:26:43 +0000 | [diff] [blame] | 989 | if( nBytes>=0 ){ |
| 990 | int sz; |
| 991 | const char *z = (const char*)zSql; |
| 992 | for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){} |
| 993 | nBytes = sz; |
| 994 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 995 | sqlite3_mutex_enter(db->mutex); |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 996 | zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 997 | if( zSql8 ){ |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 998 | rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 999 | } |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 1000 | |
| 1001 | if( zTail8 && pzTail ){ |
| 1002 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 1003 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 1004 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 1005 | ** the same number of characters into the UTF-16 string. |
| 1006 | */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1007 | int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8)); |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 1008 | *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 1009 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1010 | sqlite3DbFree(db, zSql8); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 1011 | rc = sqlite3ApiExit(db, rc); |
| 1012 | sqlite3_mutex_leave(db->mutex); |
| 1013 | return rc; |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 1014 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1015 | |
| 1016 | /* |
| 1017 | ** Two versions of the official API. Legacy and new use. In the legacy |
| 1018 | ** version, the original SQL text is not saved in the prepared statement |
| 1019 | ** and so if a schema change occurs, SQLITE_SCHEMA is returned by |
| 1020 | ** sqlite3_step(). In the new version, the original SQL text is retained |
| 1021 | ** and the statement is automatically recompiled if an schema change |
| 1022 | ** occurs. |
| 1023 | */ |
| 1024 | int sqlite3_prepare16( |
| 1025 | sqlite3 *db, /* Database handle. */ |
dan | 0ecdeb2 | 2011-01-25 13:43:35 +0000 | [diff] [blame] | 1026 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1027 | int nBytes, /* Length of zSql in bytes. */ |
| 1028 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1029 | const void **pzTail /* OUT: End of parsed string */ |
| 1030 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 1031 | int rc; |
| 1032 | rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 1033 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 1034 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1035 | } |
| 1036 | int sqlite3_prepare16_v2( |
| 1037 | sqlite3 *db, /* Database handle. */ |
dan | 0ecdeb2 | 2011-01-25 13:43:35 +0000 | [diff] [blame] | 1038 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1039 | int nBytes, /* Length of zSql in bytes. */ |
| 1040 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1041 | const void **pzTail /* OUT: End of parsed string */ |
| 1042 | ){ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 1043 | int rc; |
drh | 2c2f392 | 2017-06-01 00:54:35 +0000 | [diff] [blame] | 1044 | rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail); |
| 1045 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 1046 | return rc; |
| 1047 | } |
| 1048 | int sqlite3_prepare16_v3( |
| 1049 | sqlite3 *db, /* Database handle. */ |
| 1050 | const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 1051 | int nBytes, /* Length of zSql in bytes. */ |
| 1052 | unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */ |
| 1053 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1054 | const void **pzTail /* OUT: End of parsed string */ |
| 1055 | ){ |
| 1056 | int rc; |
| 1057 | rc = sqlite3Prepare16(db,zSql,nBytes, |
| 1058 | SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK), |
| 1059 | ppStmt,pzTail); |
drh | 58edb65 | 2008-03-08 12:23:30 +0000 | [diff] [blame] | 1060 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
drh | 17eaae7 | 2008-03-03 18:47:28 +0000 | [diff] [blame] | 1061 | return rc; |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
danielk1977 | fa256a3 | 2005-05-25 04:11:56 +0000 | [diff] [blame] | 1064 | #endif /* SQLITE_OMIT_UTF16 */ |