drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Main file for the SQLite library. The routines in this file |
| 13 | ** implement the programmer interface to the library. Routines in |
| 14 | ** other files are for internal use by SQLite and should not be |
| 15 | ** accessed by users of the library. |
| 16 | ** |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 17 | ** $Id: main.c,v 1.271 2005/01/13 02:14:25 danielk1977 Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 20 | #include "os.h" |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 24 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 25 | ** SQLITE_LITTLEENDIAN macros. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 26 | */ |
| 27 | const int sqlite3one = 1; |
| 28 | |
| 29 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 30 | ** Fill the InitData structure with an error message that indicates |
| 31 | ** that the database is corrupt. |
| 32 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 33 | static void corruptSchema(InitData *pData, const char *zExtra){ |
drh | ef0715c | 2004-06-29 10:53:54 +0000 | [diff] [blame] | 34 | if( !sqlite3_malloc_failed ){ |
| 35 | sqlite3SetString(pData->pzErrMsg, "malformed database schema", |
| 36 | zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
| 37 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 38 | } |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 41 | ** This is the callback routine for the code that initializes the |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 42 | ** database. See sqlite3Init() below for additional information. |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 43 | ** This routine is also called from the OP_ParseSchema opcode of the VDBE. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 44 | ** |
| 45 | ** Each callback contains the following information: |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 46 | ** |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 47 | ** argv[0] = name of thing being created |
| 48 | ** argv[1] = root page number for table or index. NULL for trigger or view. |
| 49 | ** argv[2] = SQL text for the CREATE statement. |
| 50 | ** argv[3] = "1" for temporary files, "0" for main database, "2" or more |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 51 | ** for auxiliary database files. |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 52 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 53 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 54 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 55 | InitData *pData = (InitData*)pInit; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 56 | sqlite3 *db = pData->db; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 57 | int iDb; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 58 | |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 59 | assert( argc==4 ); |
drh | 98e3e60 | 2003-07-27 17:26:22 +0000 | [diff] [blame] | 60 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 61 | if( argv[1]==0 || argv[3]==0 ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 62 | corruptSchema(pData, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 63 | return 1; |
| 64 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 65 | iDb = atoi(argv[3]); |
| 66 | assert( iDb>=0 && iDb<db->nDb ); |
| 67 | if( argv[2] && argv[2][0] ){ |
| 68 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 69 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 70 | ** or executed. All the parser does is build the internal data |
| 71 | ** structures that describe the table, index, or view. |
| 72 | */ |
| 73 | char *zErr; |
| 74 | int rc; |
| 75 | assert( db->init.busy ); |
| 76 | db->init.iDb = iDb; |
| 77 | db->init.newTnum = atoi(argv[1]); |
| 78 | rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); |
| 79 | db->init.iDb = 0; |
| 80 | if( SQLITE_OK!=rc ){ |
| 81 | corruptSchema(pData, zErr); |
| 82 | sqlite3_free(zErr); |
| 83 | return rc; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 84 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 85 | }else{ |
| 86 | /* If the SQL column is blank it means this is an index that |
| 87 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 88 | ** constraint for a CREATE TABLE. The index should have already |
| 89 | ** been created when we processed the CREATE TABLE. All we have |
| 90 | ** to do here is record the root page number for that index. |
| 91 | */ |
| 92 | Index *pIndex; |
| 93 | pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); |
| 94 | if( pIndex==0 || pIndex->tnum!=0 ){ |
| 95 | /* This can occur if there exists an index on a TEMP table which |
| 96 | ** has the same name as another index on a permanent index. Since |
| 97 | ** the permanent table is hidden by the TEMP table, we can also |
| 98 | ** safely ignore the index on the permanent table. |
| 99 | */ |
| 100 | /* Do Nothing */; |
| 101 | }else{ |
| 102 | pIndex->tnum = atoi(argv[1]); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 103 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 104 | } |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 105 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 109 | ** Attempt to read the database schema and initialize internal |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 110 | ** data structures for a single database file. The index of the |
| 111 | ** database file is given by iDb. iDb==0 is used for the main |
| 112 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 113 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 114 | ** indicate success or failure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 115 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 116 | static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 117 | int rc; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 118 | BtCursor *curMain; |
| 119 | int size; |
| 120 | Table *pTab; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 121 | char const *azArg[5]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 122 | char zDbNum[30]; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 123 | int meta[10]; |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 124 | InitData initData; |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 125 | char const *zMasterSchema; |
| 126 | char const *zMasterName; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | ** The master database table has a structure like this |
| 130 | */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 131 | static const char master_schema[] = |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 132 | "CREATE TABLE sqlite_master(\n" |
| 133 | " type text,\n" |
| 134 | " name text,\n" |
| 135 | " tbl_name text,\n" |
| 136 | " rootpage integer,\n" |
| 137 | " sql text\n" |
| 138 | ")" |
| 139 | ; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 140 | static const char temp_master_schema[] = |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 141 | "CREATE TEMP TABLE sqlite_temp_master(\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 142 | " type text,\n" |
| 143 | " name text,\n" |
| 144 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 145 | " rootpage integer,\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 146 | " sql text\n" |
| 147 | ")" |
| 148 | ; |
| 149 | |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 150 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 151 | |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 152 | /* zMasterSchema and zInitScript are set to point at the master schema |
| 153 | ** and initialisation script appropriate for the database being |
| 154 | ** initialised. zMasterName is the name of the master table. |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 155 | */ |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 156 | if( iDb==1 ){ |
| 157 | zMasterSchema = temp_master_schema; |
| 158 | zMasterName = TEMP_MASTER_NAME; |
| 159 | }else{ |
| 160 | zMasterSchema = master_schema; |
| 161 | zMasterName = MASTER_NAME; |
| 162 | } |
| 163 | |
| 164 | /* Construct the schema tables. */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 165 | sqlite3SafetyOff(db); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 166 | azArg[0] = zMasterName; |
| 167 | azArg[1] = "1"; |
| 168 | azArg[2] = zMasterSchema; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 169 | sprintf(zDbNum, "%d", iDb); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 170 | azArg[3] = zDbNum; |
| 171 | azArg[4] = 0; |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 172 | initData.db = db; |
| 173 | initData.pzErrMsg = pzErrMsg; |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 174 | rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0); |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 175 | if( rc!=SQLITE_OK ){ |
| 176 | sqlite3SafetyOn(db); |
| 177 | return rc; |
| 178 | } |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 179 | pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 180 | if( pTab ){ |
| 181 | pTab->readOnly = 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 182 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 183 | sqlite3SafetyOn(db); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 184 | |
| 185 | /* Create a cursor to hold the database open |
| 186 | */ |
drh | dc3ff9c | 2004-08-18 02:10:15 +0000 | [diff] [blame] | 187 | if( db->aDb[iDb].pBt==0 ){ |
| 188 | if( iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded); |
| 189 | return SQLITE_OK; |
| 190 | } |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 191 | rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 192 | if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 193 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | 92ed08a | 2002-07-30 18:43:40 +0000 | [diff] [blame] | 194 | return rc; |
| 195 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 196 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 197 | /* Get the database meta information. |
| 198 | ** |
| 199 | ** Meta values are as follows: |
| 200 | ** meta[0] Schema cookie. Changes with each schema change. |
| 201 | ** meta[1] File format of schema layer. |
| 202 | ** meta[2] Size of the page cache. |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 203 | ** meta[3] Use freelist if 0. Autovacuum if greater than zero. |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 204 | ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE |
danielk1977 | dae2495 | 2004-11-11 05:10:43 +0000 | [diff] [blame] | 205 | ** meta[5] The user cookie. Used by the application. |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 206 | ** meta[6] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 207 | ** meta[7] |
| 208 | ** meta[8] |
| 209 | ** meta[9] |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 210 | ** |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 211 | ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 212 | ** the possible values of meta[4]. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 213 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 214 | if( rc==SQLITE_OK ){ |
| 215 | int i; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 216 | for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 217 | rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 218 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 219 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 220 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 221 | sqlite3BtreeCloseCursor(curMain); |
| 222 | return rc; |
| 223 | } |
| 224 | }else{ |
| 225 | memset(meta, 0, sizeof(meta)); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 226 | } |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 227 | db->aDb[iDb].schema_cookie = meta[0]; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 228 | |
| 229 | /* If opening a non-empty database, check the text encoding. For the |
| 230 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 231 | ** For an attached db, it is an error if the encoding is not the same |
| 232 | ** as sqlite3.enc. |
| 233 | */ |
| 234 | if( meta[4] ){ /* text encoding */ |
| 235 | if( iDb==0 ){ |
| 236 | /* If opening the main database, set db->enc. */ |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 237 | db->enc = (u8)meta[4]; |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 238 | db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 239 | }else{ |
| 240 | /* If opening an attached database, the encoding much match db->enc */ |
| 241 | if( meta[4]!=db->enc ){ |
| 242 | sqlite3BtreeCloseCursor(curMain); |
| 243 | sqlite3SetString(pzErrMsg, "attached databases must use the same" |
| 244 | " text encoding as main database", (char*)0); |
| 245 | return SQLITE_ERROR; |
| 246 | } |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 247 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 248 | } |
| 249 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 250 | size = meta[2]; |
| 251 | if( size==0 ){ size = MAX_PAGES; } |
| 252 | db->aDb[iDb].cache_size = size; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 253 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 254 | if( iDb==0 ){ |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 255 | db->file_format = meta[1]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 256 | if( db->file_format==0 ){ |
| 257 | /* This happens if the database was initially empty */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 258 | db->file_format = 1; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 259 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 260 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | ** file_format==1 Version 3.0.0. |
| 264 | */ |
| 265 | if( meta[1]>1 ){ |
| 266 | sqlite3BtreeCloseCursor(curMain); |
| 267 | sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0); |
| 268 | return SQLITE_ERROR; |
| 269 | } |
| 270 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 271 | sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size); |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 272 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 273 | /* Read the schema information out of the schema tables |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 274 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 275 | assert( db->init.busy ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 276 | if( rc==SQLITE_EMPTY ){ |
| 277 | /* For an empty database, there is nothing to read */ |
| 278 | rc = SQLITE_OK; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 279 | }else{ |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 280 | char *zSql; |
| 281 | zSql = sqlite3MPrintf( |
| 282 | "SELECT name, rootpage, sql, %s FROM '%q'.%s", |
| 283 | zDbNum, db->aDb[iDb].zName, zMasterName); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 284 | sqlite3SafetyOff(db); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 285 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 286 | sqlite3SafetyOn(db); |
drh | 234c39d | 2004-07-24 03:30:47 +0000 | [diff] [blame] | 287 | sqliteFree(zSql); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 288 | sqlite3BtreeCloseCursor(curMain); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 289 | } |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 290 | if( sqlite3_malloc_failed ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 291 | sqlite3SetString(pzErrMsg, "out of memory", (char*)0); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 292 | rc = SQLITE_NOMEM; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 293 | sqlite3ResetInternalSchema(db, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 294 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 295 | if( rc==SQLITE_OK ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 296 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 297 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 298 | sqlite3ResetInternalSchema(db, iDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 299 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 300 | return rc; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* |
| 304 | ** Initialize all database files - the main database file, the file |
| 305 | ** used to store temporary tables, and any additional database files |
| 306 | ** created using ATTACH statements. Return a success code. If an |
| 307 | ** error occurs, write an error message into *pzErrMsg. |
| 308 | ** |
| 309 | ** After the database is initialized, the SQLITE_Initialized |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 310 | ** bit is set in the flags field of the sqlite structure. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 311 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 312 | int sqlite3Init(sqlite3 *db, char **pzErrMsg){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 313 | int i, rc; |
| 314 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 315 | if( db->init.busy ) return SQLITE_OK; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 316 | assert( (db->flags & SQLITE_Initialized)==0 ); |
| 317 | rc = SQLITE_OK; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 318 | db->init.busy = 1; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 319 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 320 | if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 321 | rc = sqlite3InitOne(db, i, pzErrMsg); |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 322 | if( rc ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 323 | sqlite3ResetInternalSchema(db, i); |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 324 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 325 | } |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 326 | |
| 327 | /* Once all the other databases have been initialised, load the schema |
| 328 | ** for the TEMP database. This is loaded last, as the TEMP database |
| 329 | ** schema may contain references to objects in other databases. |
| 330 | */ |
| 331 | if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ |
| 332 | rc = sqlite3InitOne(db, 1, pzErrMsg); |
| 333 | if( rc ){ |
| 334 | sqlite3ResetInternalSchema(db, 1); |
| 335 | } |
| 336 | } |
| 337 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 338 | db->init.busy = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 339 | if( rc==SQLITE_OK ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 340 | db->flags |= SQLITE_Initialized; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 341 | sqlite3CommitInternalChanges(db); |
drh | 2d71ca9 | 2004-02-10 02:27:04 +0000 | [diff] [blame] | 342 | } |
| 343 | |
drh | 2d71ca9 | 2004-02-10 02:27:04 +0000 | [diff] [blame] | 344 | if( rc!=SQLITE_OK ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 345 | db->flags &= ~SQLITE_Initialized; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 346 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 347 | return rc; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 351 | ** This routine is a no-op if the database schema is already initialised. |
| 352 | ** Otherwise, the schema is loaded. An error code is returned. |
| 353 | */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 354 | int sqlite3ReadSchema(Parse *pParse){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 355 | int rc = SQLITE_OK; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 356 | sqlite3 *db = pParse->db; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 357 | if( !db->init.busy ){ |
| 358 | if( (db->flags & SQLITE_Initialized)==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 359 | rc = sqlite3Init(db, &pParse->zErrMsg); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
danielk1977 | c039139 | 2004-06-09 12:30:04 +0000 | [diff] [blame] | 362 | assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy ); |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 363 | if( rc!=SQLITE_OK ){ |
| 364 | pParse->rc = rc; |
| 365 | pParse->nErr++; |
| 366 | } |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 367 | return rc; |
| 368 | } |
| 369 | |
| 370 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 371 | ** The version of the library |
| 372 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 373 | const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $"; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 374 | const char sqlite3_version[] = SQLITE_VERSION; |
drh | 4aec8b6 | 2004-08-28 16:19:00 +0000 | [diff] [blame] | 375 | const char *sqlite3_libversion(void){ return sqlite3_version; } |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 376 | |
| 377 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 378 | ** This is the default collating function named "BINARY" which is always |
| 379 | ** available. |
| 380 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 381 | static int binCollFunc( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 382 | void *NotUsed, |
| 383 | int nKey1, const void *pKey1, |
| 384 | int nKey2, const void *pKey2 |
| 385 | ){ |
| 386 | int rc, n; |
| 387 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 388 | rc = memcmp(pKey1, pKey2, n); |
| 389 | if( rc==0 ){ |
| 390 | rc = nKey1 - nKey2; |
| 391 | } |
| 392 | return rc; |
| 393 | } |
| 394 | |
| 395 | /* |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 396 | ** Another built-in collating sequence: NOCASE. |
| 397 | ** |
| 398 | ** This collating sequence is intended to be used for "case independant |
| 399 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 400 | ** extends only to the 26 characters used in the English language. |
| 401 | ** |
| 402 | ** At the moment there is only a UTF-8 implementation. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 403 | */ |
| 404 | static int nocaseCollatingFunc( |
| 405 | void *NotUsed, |
| 406 | int nKey1, const void *pKey1, |
| 407 | int nKey2, const void *pKey2 |
| 408 | ){ |
| 409 | int r = sqlite3StrNICmp( |
drh | 208f80a | 2004-08-29 20:08:58 +0000 | [diff] [blame] | 410 | (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 411 | if( 0==r ){ |
| 412 | r = nKey1-nKey2; |
| 413 | } |
| 414 | return r; |
| 415 | } |
| 416 | |
| 417 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 418 | ** Return the ROWID of the most recent insert |
| 419 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 420 | sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 421 | return db->lastRowid; |
| 422 | } |
| 423 | |
| 424 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 425 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 426 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 427 | int sqlite3_changes(sqlite3 *db){ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 428 | return db->nChange; |
| 429 | } |
| 430 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 431 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 432 | ** Return the number of changes since the database handle was opened. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 433 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 434 | int sqlite3_total_changes(sqlite3 *db){ |
| 435 | return db->nTotalChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 436 | } |
| 437 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 438 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 439 | ** Close an existing SQLite database |
| 440 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 441 | int sqlite3_close(sqlite3 *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 442 | HashElem *i; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 443 | int j; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 444 | |
| 445 | if( !db ){ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 446 | return SQLITE_OK; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 447 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 448 | if( sqlite3SafetyCheck(db) ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 449 | return SQLITE_MISUSE; |
| 450 | } |
| 451 | |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 452 | /* If there are any outstanding VMs, return SQLITE_BUSY. */ |
| 453 | if( db->pVdbe ){ |
| 454 | sqlite3Error(db, SQLITE_BUSY, |
| 455 | "Unable to close due to unfinalised statements"); |
| 456 | return SQLITE_BUSY; |
| 457 | } |
| 458 | assert( !sqlite3SafetyCheck(db) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 459 | |
| 460 | /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database |
| 461 | ** cannot be opened for some reason. So this routine needs to run in |
| 462 | ** that case. But maybe there should be an extra magic value for the |
| 463 | ** "failed to open" state. |
| 464 | */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 465 | if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 466 | /* printf("DID NOT CLOSE\n"); fflush(stdout); */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 467 | return SQLITE_ERROR; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 468 | } |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 469 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 470 | for(j=0; j<db->nDb; j++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 471 | struct Db *pDb = &db->aDb[j]; |
| 472 | if( pDb->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 473 | sqlite3BtreeClose(pDb->pBt); |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 474 | pDb->pBt = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 475 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 476 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 477 | sqlite3ResetInternalSchema(db, 0); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 478 | assert( db->nDb<=2 ); |
| 479 | assert( db->aDb==db->aDbStatic ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 480 | for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 481 | FuncDef *pFunc, *pNext; |
| 482 | for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 483 | pNext = pFunc->pNext; |
| 484 | sqliteFree(pFunc); |
| 485 | } |
| 486 | } |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 487 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 488 | for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 489 | CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 490 | sqliteFree(pColl); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 491 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 492 | sqlite3HashClear(&db->aCollSeq); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 493 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 494 | sqlite3HashClear(&db->aFunc); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 495 | sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 496 | if( db->pValue ){ |
| 497 | sqlite3ValueFree(db->pValue); |
| 498 | } |
| 499 | if( db->pErr ){ |
| 500 | sqlite3ValueFree(db->pErr); |
| 501 | } |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 502 | |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 503 | #ifndef SQLITE_OMIT_CURSOR |
| 504 | for(j=0; j<db->nSqlCursor; j++){ |
| 505 | sqlite3CursorDelete(db->apSqlCursor[j]); |
| 506 | } |
| 507 | sqliteFree(db->apSqlCursor); |
| 508 | #endif |
| 509 | |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 510 | db->magic = SQLITE_MAGIC_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 511 | sqliteFree(db); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 512 | return SQLITE_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 516 | ** Rollback all database files. |
| 517 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 518 | void sqlite3RollbackAll(sqlite3 *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 519 | int i; |
| 520 | for(i=0; i<db->nDb; i++){ |
| 521 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 522 | sqlite3BtreeRollback(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 523 | db->aDb[i].inTrans = 0; |
| 524 | } |
| 525 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 526 | sqlite3ResetInternalSchema(db, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 530 | ** Return a static string that describes the kind of error specified in the |
| 531 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 532 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 533 | const char *sqlite3ErrStr(int rc){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 534 | const char *z; |
| 535 | switch( rc ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 536 | case SQLITE_ROW: |
| 537 | case SQLITE_DONE: |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 538 | case SQLITE_OK: z = "not an error"; break; |
| 539 | case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
| 540 | case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break; |
| 541 | case SQLITE_PERM: z = "access permission denied"; break; |
| 542 | case SQLITE_ABORT: z = "callback requested query abort"; break; |
| 543 | case SQLITE_BUSY: z = "database is locked"; break; |
| 544 | case SQLITE_LOCKED: z = "database table is locked"; break; |
| 545 | case SQLITE_NOMEM: z = "out of memory"; break; |
| 546 | case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
| 547 | case SQLITE_INTERRUPT: z = "interrupted"; break; |
| 548 | case SQLITE_IOERR: z = "disk I/O error"; break; |
| 549 | case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
| 550 | case SQLITE_NOTFOUND: z = "table or record not found"; break; |
| 551 | case SQLITE_FULL: z = "database is full"; break; |
| 552 | case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
| 553 | case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
| 554 | case SQLITE_EMPTY: z = "table contains no data"; break; |
| 555 | case SQLITE_SCHEMA: z = "database schema has changed"; break; |
| 556 | case SQLITE_TOOBIG: z = "too much data for one table row"; break; |
| 557 | case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
| 558 | case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
| 559 | case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 560 | case SQLITE_NOLFS: z = "kernel lacks large file support"; break; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 561 | case SQLITE_AUTH: z = "authorization denied"; break; |
jplyon | 892f671 | 2003-06-12 08:59:00 +0000 | [diff] [blame] | 562 | case SQLITE_FORMAT: z = "auxiliary database format error"; break; |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 563 | case SQLITE_RANGE: z = "bind or column index out of range"; break; |
drh | c602f9a | 2004-02-12 19:01:04 +0000 | [diff] [blame] | 564 | case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 565 | default: z = "unknown error"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 566 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 567 | return z; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 571 | ** This routine implements a busy callback that sleeps and tries |
| 572 | ** again until a timeout value is reached. The timeout value is |
| 573 | ** an integer number of milliseconds passed in as the first |
| 574 | ** argument. |
| 575 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 576 | static int sqliteDefaultBusyCallback( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 577 | void *Timeout, /* Maximum amount of time to wait */ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 578 | int count /* Number of times table has been busy */ |
| 579 | ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 580 | #if SQLITE_MIN_SLEEP_MS==1 |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 581 | static const char delays[] = |
| 582 | { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100}; |
| 583 | static const short int totals[] = |
| 584 | { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287}; |
| 585 | # define NDELAY (sizeof(delays)/sizeof(delays[0])) |
drh | c44af71 | 2004-09-02 15:53:56 +0000 | [diff] [blame] | 586 | ptr timeout = (ptr)Timeout; |
| 587 | ptr delay, prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 588 | |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 589 | if( count <= NDELAY ){ |
| 590 | delay = delays[count-1]; |
| 591 | prior = totals[count-1]; |
| 592 | }else{ |
| 593 | delay = delays[NDELAY-1]; |
| 594 | prior = totals[NDELAY-1] + delay*(count-NDELAY-1); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 595 | } |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 596 | if( prior + delay > timeout ){ |
| 597 | delay = timeout - prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 598 | if( delay<=0 ) return 0; |
| 599 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 600 | sqlite3OsSleep(delay); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 601 | return 1; |
| 602 | #else |
| 603 | int timeout = (int)Timeout; |
| 604 | if( (count+1)*1000 > timeout ){ |
| 605 | return 0; |
| 606 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 607 | sqlite3OsSleep(1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 608 | return 1; |
| 609 | #endif |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | ** This routine sets the busy callback for an Sqlite database to the |
| 614 | ** given callback function with the given argument. |
| 615 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 616 | int sqlite3_busy_handler( |
| 617 | sqlite3 *db, |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 618 | int (*xBusy)(void*,int), |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 619 | void *pArg |
| 620 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 621 | if( sqlite3SafetyCheck(db) ){ |
| 622 | return SQLITE_MISUSE; |
| 623 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 624 | db->busyHandler.xFunc = xBusy; |
| 625 | db->busyHandler.pArg = pArg; |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 626 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 627 | } |
| 628 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 629 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 630 | /* |
| 631 | ** This routine sets the progress callback for an Sqlite database to the |
| 632 | ** given callback function with the given argument. The progress callback will |
| 633 | ** be invoked every nOps opcodes. |
| 634 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 635 | void sqlite3_progress_handler( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 636 | sqlite3 *db, |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 637 | int nOps, |
| 638 | int (*xProgress)(void*), |
| 639 | void *pArg |
| 640 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 641 | if( !sqlite3SafetyCheck(db) ){ |
| 642 | if( nOps>0 ){ |
| 643 | db->xProgress = xProgress; |
| 644 | db->nProgressOps = nOps; |
| 645 | db->pProgressArg = pArg; |
| 646 | }else{ |
| 647 | db->xProgress = 0; |
| 648 | db->nProgressOps = 0; |
| 649 | db->pProgressArg = 0; |
| 650 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | #endif |
| 654 | |
| 655 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 656 | /* |
| 657 | ** This routine installs a default busy handler that waits for the |
| 658 | ** specified number of milliseconds before returning 0. |
| 659 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 660 | int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 661 | if( ms>0 ){ |
drh | c44af71 | 2004-09-02 15:53:56 +0000 | [diff] [blame] | 662 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)(ptr)ms); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 663 | }else{ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 664 | sqlite3_busy_handler(db, 0, 0); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 665 | } |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 666 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 667 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | ** Cause any pending operation to stop at its earliest opportunity. |
| 671 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 672 | void sqlite3_interrupt(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 673 | if( !sqlite3SafetyCheck(db) ){ |
| 674 | db->flags |= SQLITE_Interrupt; |
| 675 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 676 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | ** Windows systems should call this routine to free memory that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 680 | ** is returned in the in the errmsg parameter of sqlite3_open() when |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 681 | ** SQLite is a DLL. For some reason, it does not work to call free() |
| 682 | ** directly. |
| 683 | ** |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 684 | ** Note that we need to call free() not sqliteFree() here. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 685 | */ |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 686 | void sqlite3_free(char *p){ free(p); } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 687 | |
| 688 | /* |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 689 | ** Create new user functions. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 690 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 691 | int sqlite3_create_function( |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 692 | sqlite3 *db, |
| 693 | const char *zFunctionName, |
| 694 | int nArg, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 695 | int enc, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 696 | void *pUserData, |
| 697 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 698 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 699 | void (*xFinal)(sqlite3_context*) |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 700 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 701 | FuncDef *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 702 | int nName; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 703 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 704 | if( sqlite3SafetyCheck(db) ){ |
| 705 | return SQLITE_MISUSE; |
| 706 | } |
| 707 | if( zFunctionName==0 || |
danielk1977 | 398eae7 | 2004-05-26 06:58:43 +0000 | [diff] [blame] | 708 | (xFunc && (xFinal || xStep)) || |
| 709 | (!xFunc && (xFinal && !xStep)) || |
| 710 | (!xFunc && (!xFinal && xStep)) || |
| 711 | (nArg<-1 || nArg>127) || |
| 712 | (255<(nName = strlen(zFunctionName))) ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 713 | return SQLITE_ERROR; |
| 714 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 715 | |
| 716 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 717 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 718 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 719 | ** |
| 720 | ** If SQLITE_ANY is specified, add three versions of the function |
| 721 | ** to the hash table. |
| 722 | */ |
| 723 | if( enc==SQLITE_UTF16 ){ |
| 724 | enc = SQLITE_UTF16NATIVE; |
| 725 | }else if( enc==SQLITE_ANY ){ |
| 726 | int rc; |
| 727 | rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 728 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 729 | if( rc!=SQLITE_OK ) return rc; |
| 730 | rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 731 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 732 | if( rc!=SQLITE_OK ) return rc; |
| 733 | enc = SQLITE_UTF16BE; |
| 734 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 735 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 736 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 737 | if( p==0 ) return SQLITE_NOMEM; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 738 | p->xFunc = xFunc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 739 | p->xStep = xStep; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 740 | p->xFinalize = xFinal; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 741 | p->pUserData = pUserData; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 742 | return SQLITE_OK; |
| 743 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 744 | int sqlite3_create_function16( |
| 745 | sqlite3 *db, |
| 746 | const void *zFunctionName, |
| 747 | int nArg, |
| 748 | int eTextRep, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 749 | void *pUserData, |
| 750 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 751 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 752 | void (*xFinal)(sqlite3_context*) |
| 753 | ){ |
| 754 | int rc; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 755 | char const *zFunc8; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 756 | sqlite3_value *pTmp; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 757 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 758 | if( sqlite3SafetyCheck(db) ){ |
| 759 | return SQLITE_MISUSE; |
| 760 | } |
| 761 | pTmp = sqlite3GetTransientValue(db); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 762 | sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC); |
| 763 | zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
| 764 | |
| 765 | if( !zFunc8 ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 766 | return SQLITE_NOMEM; |
| 767 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 768 | rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 769 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 770 | return rc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 771 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 772 | |
| 773 | /* |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 774 | ** Register a trace function. The pArg from the previously registered trace |
| 775 | ** is returned. |
| 776 | ** |
| 777 | ** A NULL trace function means that no tracing is executes. A non-NULL |
| 778 | ** trace is a pointer to a function that is invoked at the start of each |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 779 | ** sqlite3_exec(). |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 780 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 781 | void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 782 | void *pOld = db->pTraceArg; |
| 783 | db->xTrace = xTrace; |
| 784 | db->pTraceArg = pArg; |
| 785 | return pOld; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 786 | } |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 787 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 788 | /*** EXPERIMENTAL *** |
| 789 | ** |
| 790 | ** Register a function to be invoked when a transaction comments. |
| 791 | ** If either function returns non-zero, then the commit becomes a |
| 792 | ** rollback. |
| 793 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 794 | void *sqlite3_commit_hook( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 795 | sqlite3 *db, /* Attach the hook to this database */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 796 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 797 | void *pArg /* Argument to the function */ |
| 798 | ){ |
| 799 | void *pOld = db->pCommitArg; |
| 800 | db->xCommitCallback = xCallback; |
| 801 | db->pCommitArg = pArg; |
| 802 | return pOld; |
| 803 | } |
| 804 | |
| 805 | |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 806 | /* |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 807 | ** This routine is called to create a connection to a database BTree |
| 808 | ** driver. If zFilename is the name of a file, then that file is |
| 809 | ** opened and used. If zFilename is the magic name ":memory:" then |
| 810 | ** the database is stored in memory (and is thus forgotten as soon as |
| 811 | ** the connection is closed.) If zFilename is NULL then the database |
| 812 | ** is for temporary use only and is deleted as soon as the connection |
| 813 | ** is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 814 | ** |
| 815 | ** A temporary database can be either a disk file (that is automatically |
| 816 | ** deleted when the file is closed) or a set of red-black trees held in memory, |
| 817 | ** depending on the values of the TEMP_STORE compile-time macro and the |
| 818 | ** db->temp_store variable, according to the following chart: |
| 819 | ** |
| 820 | ** TEMP_STORE db->temp_store Location of temporary database |
| 821 | ** ---------- -------------- ------------------------------ |
| 822 | ** 0 any file |
| 823 | ** 1 1 file |
| 824 | ** 1 2 memory |
| 825 | ** 1 0 file |
| 826 | ** 2 1 file |
| 827 | ** 2 2 memory |
| 828 | ** 2 0 memory |
| 829 | ** 3 any memory |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 830 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 831 | int sqlite3BtreeFactory( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 832 | const sqlite3 *db, /* Main database when opening aux otherwise 0 */ |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 833 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 834 | int omitJournal, /* if TRUE then do not journal this file */ |
| 835 | int nCache, /* How many pages in the page cache */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 836 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 837 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 838 | int btree_flags = 0; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 839 | int rc; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 840 | |
drh | eec983e | 2004-05-08 10:11:36 +0000 | [diff] [blame] | 841 | assert( ppBtree != 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 842 | if( omitJournal ){ |
| 843 | btree_flags |= BTREE_OMIT_JOURNAL; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 844 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 845 | if( zFilename==0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 846 | #if TEMP_STORE==0 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 847 | /* Do nothing */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 848 | #endif |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 849 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 850 | #if TEMP_STORE==1 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 851 | if( db->temp_store==2 ) zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 852 | #endif |
| 853 | #if TEMP_STORE==2 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 854 | if( db->temp_store!=1 ) zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 855 | #endif |
| 856 | #if TEMP_STORE==3 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 857 | zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 858 | #endif |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 859 | #endif /* SQLITE_OMIT_MEMORYDB */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 860 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 861 | |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 862 | rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags); |
| 863 | if( rc==SQLITE_OK ){ |
| 864 | sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler); |
| 865 | sqlite3BtreeSetCacheSize(*ppBtree, nCache); |
| 866 | } |
| 867 | return rc; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 868 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 869 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 870 | /* |
| 871 | ** Return UTF-8 encoded English language explanation of the most recent |
| 872 | ** error. |
| 873 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 874 | const char *sqlite3_errmsg(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 875 | const char *z; |
| 876 | if( sqlite3_malloc_failed ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 877 | return sqlite3ErrStr(SQLITE_NOMEM); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 878 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 879 | if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 880 | return sqlite3ErrStr(SQLITE_MISUSE); |
| 881 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 882 | z = sqlite3_value_text(db->pErr); |
| 883 | if( z==0 ){ |
| 884 | z = sqlite3ErrStr(db->errCode); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 885 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 886 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 887 | } |
| 888 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 889 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 890 | /* |
| 891 | ** Return UTF-16 encoded English language explanation of the most recent |
| 892 | ** error. |
| 893 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 894 | const void *sqlite3_errmsg16(sqlite3 *db){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 895 | /* Because all the characters in the string are in the unicode |
| 896 | ** range 0x00-0xFF, if we pad the big-endian string with a |
| 897 | ** zero byte, we can obtain the little-endian string with |
| 898 | ** &big_endian[1]. |
| 899 | */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 900 | static const char outOfMemBe[] = { |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 901 | 0, 'o', 0, 'u', 0, 't', 0, ' ', |
| 902 | 0, 'o', 0, 'f', 0, ' ', |
| 903 | 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0 |
| 904 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 905 | static const char misuseBe [] = { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 906 | 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ', |
| 907 | 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ', |
| 908 | 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', |
| 909 | 0, 'o', 0, 'u', 0, 't', 0, ' ', |
| 910 | 0, 'o', 0, 'f', 0, ' ', |
| 911 | 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0 |
| 912 | }; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 913 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 914 | const void *z; |
| 915 | if( sqlite3_malloc_failed ){ |
| 916 | return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
| 917 | } |
| 918 | if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ |
| 919 | return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
| 920 | } |
| 921 | z = sqlite3_value_text16(db->pErr); |
| 922 | if( z==0 ){ |
| 923 | sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), |
| 924 | SQLITE_UTF8, SQLITE_STATIC); |
| 925 | z = sqlite3_value_text16(db->pErr); |
| 926 | } |
| 927 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 928 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 929 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 930 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 931 | /* |
| 932 | ** Return the most recent error code generated by an SQLite routine. |
| 933 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 934 | int sqlite3_errcode(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 935 | if( sqlite3_malloc_failed ){ |
| 936 | return SQLITE_NOMEM; |
| 937 | } |
| 938 | if( sqlite3SafetyCheck(db) ){ |
| 939 | return SQLITE_MISUSE; |
| 940 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 941 | return db->errCode; |
| 942 | } |
| 943 | |
| 944 | /* |
drh | c275b4e | 2004-07-19 17:25:24 +0000 | [diff] [blame] | 945 | ** Check schema cookies in all databases. If any cookie is out |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 946 | ** of date, return 0. If all schema cookies are current, return 1. |
| 947 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 948 | static int schemaIsValid(sqlite3 *db){ |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 949 | int iDb; |
| 950 | int rc; |
| 951 | BtCursor *curTemp; |
| 952 | int cookie; |
| 953 | int allOk = 1; |
| 954 | |
| 955 | for(iDb=0; allOk && iDb<db->nDb; iDb++){ |
| 956 | Btree *pBt; |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 957 | pBt = db->aDb[iDb].pBt; |
| 958 | if( pBt==0 ) continue; |
| 959 | rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp); |
| 960 | if( rc==SQLITE_OK ){ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 961 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie); |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 962 | if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){ |
| 963 | allOk = 0; |
| 964 | } |
| 965 | sqlite3BtreeCloseCursor(curTemp); |
| 966 | } |
| 967 | } |
| 968 | return allOk; |
| 969 | } |
| 970 | |
| 971 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 972 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 973 | */ |
| 974 | int sqlite3_prepare( |
| 975 | sqlite3 *db, /* Database handle. */ |
| 976 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 977 | int nBytes, /* Length of zSql in bytes. */ |
| 978 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 979 | const char** pzTail /* OUT: End of parsed string */ |
| 980 | ){ |
| 981 | Parse sParse; |
| 982 | char *zErrMsg = 0; |
| 983 | int rc = SQLITE_OK; |
| 984 | |
danielk1977 | 96fb0dd | 2004-06-30 09:49:22 +0000 | [diff] [blame] | 985 | if( sqlite3_malloc_failed ){ |
| 986 | return SQLITE_NOMEM; |
| 987 | } |
| 988 | |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 989 | assert( ppStmt ); |
| 990 | *ppStmt = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 991 | if( sqlite3SafetyOn(db) ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 992 | return SQLITE_MISUSE; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 993 | } |
| 994 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 995 | memset(&sParse, 0, sizeof(sParse)); |
| 996 | sParse.db = db; |
| 997 | sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 998 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 999 | if( sqlite3_malloc_failed ){ |
| 1000 | rc = SQLITE_NOMEM; |
| 1001 | sqlite3RollbackAll(db); |
| 1002 | sqlite3ResetInternalSchema(db, 0); |
| 1003 | db->flags &= ~SQLITE_InTrans; |
| 1004 | goto prepare_out; |
| 1005 | } |
| 1006 | if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
drh | 3f7d4e4 | 2004-07-24 14:35:58 +0000 | [diff] [blame] | 1007 | if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){ |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 1008 | sParse.rc = SQLITE_SCHEMA; |
| 1009 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1010 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 1011 | sqlite3ResetInternalSchema(db, 0); |
| 1012 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1013 | if( pzTail ) *pzTail = sParse.zTail; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1014 | rc = sParse.rc; |
| 1015 | |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1016 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
| 1017 | sqlite3VdbeSetNumCols(sParse.pVdbe, 5); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1018 | sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC); |
| 1019 | sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC); |
| 1020 | sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC); |
| 1021 | sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC); |
| 1022 | sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1025 | prepare_out: |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1026 | if( sqlite3SafetyOff(db) ){ |
| 1027 | rc = SQLITE_MISUSE; |
| 1028 | } |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 1029 | if( rc==SQLITE_OK ){ |
| 1030 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
| 1031 | }else if( sParse.pVdbe ){ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1032 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1035 | if( zErrMsg ){ |
| 1036 | sqlite3Error(db, rc, "%s", zErrMsg); |
danielk1977 | b20e56b | 2004-06-15 13:36:30 +0000 | [diff] [blame] | 1037 | sqliteFree(zErrMsg); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1038 | }else{ |
| 1039 | sqlite3Error(db, rc, 0); |
| 1040 | } |
| 1041 | return rc; |
| 1042 | } |
| 1043 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1044 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1045 | /* |
| 1046 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 1047 | */ |
| 1048 | int sqlite3_prepare16( |
| 1049 | sqlite3 *db, /* Database handle. */ |
| 1050 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 1051 | int nBytes, /* Length of zSql in bytes. */ |
| 1052 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1053 | const void **pzTail /* OUT: End of parsed string */ |
| 1054 | ){ |
| 1055 | /* This function currently works by first transforming the UTF-16 |
| 1056 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 1057 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 1058 | */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1059 | char const *zSql8 = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1060 | char const *zTail8 = 0; |
| 1061 | int rc; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1062 | sqlite3_value *pTmp; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1063 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1064 | if( sqlite3SafetyCheck(db) ){ |
| 1065 | return SQLITE_MISUSE; |
| 1066 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1067 | pTmp = sqlite3GetTransientValue(db); |
| 1068 | sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 1069 | zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1070 | if( !zSql8 ){ |
| 1071 | sqlite3Error(db, SQLITE_NOMEM, 0); |
| 1072 | return SQLITE_NOMEM; |
| 1073 | } |
| 1074 | rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8); |
| 1075 | |
| 1076 | if( zTail8 && pzTail ){ |
| 1077 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 1078 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 1079 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 1080 | ** the same number of characters into the UTF-16 string. |
| 1081 | */ |
| 1082 | int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8); |
| 1083 | *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed); |
| 1084 | } |
| 1085 | |
| 1086 | return rc; |
| 1087 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1088 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1089 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1090 | /* |
| 1091 | ** This routine does the work of opening a database on behalf of |
| 1092 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| 1093 | ** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_* |
| 1094 | ** macros from sqliteInt.h. If we end up creating a new database file |
| 1095 | ** (not opening an existing one), the text encoding of the database |
| 1096 | ** will be set to this value. |
| 1097 | */ |
| 1098 | static int openDatabase( |
| 1099 | const char *zFilename, /* Database filename UTF-8 encoded */ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1100 | sqlite3 **ppDb /* OUT: Returned database handle */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1101 | ){ |
| 1102 | sqlite3 *db; |
| 1103 | int rc, i; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1104 | |
| 1105 | /* Allocate the sqlite data structure */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1106 | db = sqliteMalloc( sizeof(sqlite3) ); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1107 | if( db==0 ) goto opendb_out; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1108 | db->priorNewRowid = 0; |
| 1109 | db->magic = SQLITE_MAGIC_BUSY; |
| 1110 | db->nDb = 2; |
| 1111 | db->aDb = db->aDbStatic; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 1112 | db->enc = SQLITE_UTF8; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1113 | db->autoCommit = 1; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1114 | /* db->flags |= SQLITE_ShortColNames; */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1115 | sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1116 | sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0); |
| 1117 | for(i=0; i<db->nDb; i++){ |
| 1118 | sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0); |
| 1119 | sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0); |
| 1120 | sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0); |
| 1121 | sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1); |
| 1122 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1123 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1124 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 1125 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 1126 | ** conversions. The only error that can occur here is a malloc() failure. |
| 1127 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 1128 | if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) || |
| 1129 | sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) || |
| 1130 | !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1131 | rc = db->errCode; |
| 1132 | assert( rc!=SQLITE_OK ); |
| 1133 | db->magic = SQLITE_MAGIC_CLOSED; |
| 1134 | goto opendb_out; |
| 1135 | } |
| 1136 | |
| 1137 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1138 | sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1139 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1140 | /* Open the backend database driver */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1141 | rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); |
| 1142 | if( rc!=SQLITE_OK ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1143 | sqlite3Error(db, rc, 0); |
| 1144 | db->magic = SQLITE_MAGIC_CLOSED; |
| 1145 | goto opendb_out; |
| 1146 | } |
| 1147 | db->aDb[0].zName = "main"; |
| 1148 | db->aDb[1].zName = "temp"; |
| 1149 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1150 | /* The default safety_level for the main database is 'full' for the temp |
| 1151 | ** database it is 'NONE'. This matches the pager layer defaults. */ |
| 1152 | db->aDb[0].safety_level = 3; |
| 1153 | db->aDb[1].safety_level = 1; |
| 1154 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1155 | /* Register all built-in functions, but do not attempt to read the |
| 1156 | ** database schema yet. This is delayed until the first time the database |
| 1157 | ** is accessed. |
| 1158 | */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1159 | sqlite3RegisterBuiltinFunctions(db); |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 1160 | sqlite3Error(db, SQLITE_OK, 0); |
| 1161 | db->magic = SQLITE_MAGIC_OPEN; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1162 | |
| 1163 | opendb_out: |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 1164 | if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){ |
| 1165 | sqlite3Error(db, SQLITE_NOMEM, 0); |
| 1166 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1167 | *ppDb = db; |
| 1168 | return sqlite3_errcode(db); |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | ** Open a new database handle. |
| 1173 | */ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 1174 | int sqlite3_open( |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1175 | const char *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1176 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1177 | ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1178 | return openDatabase(zFilename, ppDb); |
danielk1977 | 83ab5a8 | 2004-05-21 11:39:05 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1181 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1182 | /* |
| 1183 | ** Open a new database handle. |
| 1184 | */ |
| 1185 | int sqlite3_open16( |
| 1186 | const void *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1187 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1188 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1189 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 1190 | int rc = SQLITE_NOMEM; |
| 1191 | sqlite3_value *pVal; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1192 | |
| 1193 | assert( ppDb ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1194 | *ppDb = 0; |
| 1195 | pVal = sqlite3ValueNew(); |
| 1196 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 1197 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 1198 | if( zFilename8 ){ |
| 1199 | rc = openDatabase(zFilename8, ppDb); |
| 1200 | if( rc==SQLITE_OK && *ppDb ){ |
| 1201 | sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); |
| 1202 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1203 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1204 | if( pVal ){ |
| 1205 | sqlite3ValueFree(pVal); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1206 | } |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1207 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1208 | return rc; |
| 1209 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1210 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1211 | |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1212 | /* |
| 1213 | ** The following routine destroys a virtual machine that is created by |
| 1214 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 1215 | ** success/failure code that describes the result of executing the virtual |
| 1216 | ** machine. |
| 1217 | ** |
| 1218 | ** This routine sets the error code and string returned by |
| 1219 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 1220 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1221 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 1222 | int rc; |
| 1223 | if( pStmt==0 ){ |
| 1224 | rc = SQLITE_OK; |
| 1225 | }else{ |
| 1226 | rc = sqlite3VdbeFinalize((Vdbe*)pStmt); |
| 1227 | } |
| 1228 | return rc; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | ** Terminate the current execution of an SQL statement and reset it |
| 1233 | ** back to its starting state so that it can be reused. A success code from |
| 1234 | ** the prior execution is returned. |
| 1235 | ** |
| 1236 | ** This routine sets the error code and string returned by |
| 1237 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 1238 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1239 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 1240 | int rc; |
| 1241 | if( pStmt==0 ){ |
| 1242 | rc = SQLITE_OK; |
| 1243 | }else{ |
| 1244 | rc = sqlite3VdbeReset((Vdbe*)pStmt); |
| 1245 | sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0); |
| 1246 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1247 | return rc; |
| 1248 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1249 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1250 | /* |
| 1251 | ** Register a new collation sequence with the database handle db. |
| 1252 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1253 | int sqlite3_create_collation( |
| 1254 | sqlite3* db, |
| 1255 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1256 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1257 | void* pCtx, |
| 1258 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 1259 | ){ |
| 1260 | CollSeq *pColl; |
| 1261 | int rc = SQLITE_OK; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1262 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1263 | if( sqlite3SafetyCheck(db) ){ |
| 1264 | return SQLITE_MISUSE; |
| 1265 | } |
| 1266 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1267 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 1268 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 1269 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 1270 | */ |
| 1271 | if( enc==SQLITE_UTF16 ){ |
| 1272 | enc = SQLITE_UTF16NATIVE; |
| 1273 | } |
| 1274 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1275 | if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){ |
| 1276 | sqlite3Error(db, SQLITE_ERROR, |
| 1277 | "Param 3 to sqlite3_create_collation() must be one of " |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1278 | "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE" |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1279 | ); |
| 1280 | return SQLITE_ERROR; |
| 1281 | } |
| 1282 | pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1283 | if( 0==pColl ){ |
| 1284 | rc = SQLITE_NOMEM; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1285 | }else{ |
| 1286 | pColl->xCmp = xCompare; |
| 1287 | pColl->pUser = pCtx; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 1288 | pColl->enc = enc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1289 | } |
| 1290 | sqlite3Error(db, rc, 0); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1291 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1294 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1295 | /* |
| 1296 | ** Register a new collation sequence with the database handle db. |
| 1297 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1298 | int sqlite3_create_collation16( |
| 1299 | sqlite3* db, |
| 1300 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1301 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1302 | void* pCtx, |
| 1303 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 1304 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1305 | char const *zName8; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1306 | sqlite3_value *pTmp; |
| 1307 | if( sqlite3SafetyCheck(db) ){ |
| 1308 | return SQLITE_MISUSE; |
| 1309 | } |
| 1310 | pTmp = sqlite3GetTransientValue(db); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1311 | sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 1312 | zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
| 1313 | return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1314 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1315 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1316 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1317 | /* |
| 1318 | ** Register a collation sequence factory callback with the database handle |
| 1319 | ** db. Replace any previously installed collation sequence factory. |
| 1320 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1321 | int sqlite3_collation_needed( |
| 1322 | sqlite3 *db, |
| 1323 | void *pCollNeededArg, |
| 1324 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 1325 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1326 | if( sqlite3SafetyCheck(db) ){ |
| 1327 | return SQLITE_MISUSE; |
| 1328 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1329 | db->xCollNeeded = xCollNeeded; |
| 1330 | db->xCollNeeded16 = 0; |
| 1331 | db->pCollNeededArg = pCollNeededArg; |
| 1332 | return SQLITE_OK; |
| 1333 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1334 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1335 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1336 | /* |
| 1337 | ** Register a collation sequence factory callback with the database handle |
| 1338 | ** db. Replace any previously installed collation sequence factory. |
| 1339 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1340 | int sqlite3_collation_needed16( |
| 1341 | sqlite3 *db, |
| 1342 | void *pCollNeededArg, |
| 1343 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 1344 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1345 | if( sqlite3SafetyCheck(db) ){ |
| 1346 | return SQLITE_MISUSE; |
| 1347 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1348 | db->xCollNeeded = 0; |
| 1349 | db->xCollNeeded16 = xCollNeeded16; |
| 1350 | db->pCollNeededArg = pCollNeededArg; |
| 1351 | return SQLITE_OK; |
| 1352 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1353 | #endif /* SQLITE_OMIT_UTF16 */ |