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