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