drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Main file for the SQLite library. The routines in this file |
| 13 | ** implement the programmer interface to the library. Routines in |
| 14 | ** other files are for internal use by SQLite and should not be |
| 15 | ** accessed by users of the library. |
| 16 | ** |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame^] | 17 | ** $Id: main.c,v 1.87 2002/07/18 00:34:12 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 20 | #include "os.h" |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | ** This is the callback routine for the code that initializes the |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 25 | ** database. See sqliteInit() below for additional information. |
| 26 | ** |
| 27 | ** Each callback contains the following information: |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 28 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 29 | ** argv[0] = "file-format" or "schema-cookie" or "table" or "index" |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 30 | ** argv[1] = table or index name or meta statement type. |
| 31 | ** argv[2] = root page number for table or index. NULL for meta. |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 32 | ** argv[3] = SQL create statement for the table or index |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 33 | ** argv[4] = "1" for temporary files, "0" for main database |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 34 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 35 | */ |
drh | e0140fc | 2002-06-16 18:21:44 +0000 | [diff] [blame] | 36 | int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 37 | sqlite *db = (sqlite*)pDb; |
| 38 | Parse sParse; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 39 | int nErr = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 40 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 41 | /* TODO: Do some validity checks on all fields. In particular, |
| 42 | ** make sure fields do not contain NULLs. Otherwise we might core |
| 43 | ** when attempting to initialize from a corrupt database file. */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 44 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 45 | assert( argc==5 ); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 46 | switch( argv[0][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 47 | case 'v': |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 48 | case 'i': |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 49 | case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 50 | if( argv[3] && argv[3][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 51 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 52 | ** But because sParse.initFlag is set to 1, no VDBE code is generated |
| 53 | ** or executed. All the parser does is build the internal data |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 54 | ** structures that describe the table, index, or view. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 55 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 56 | memset(&sParse, 0, sizeof(sParse)); |
| 57 | sParse.db = db; |
| 58 | sParse.initFlag = 1; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 59 | sParse.isTemp = argv[4][0] - '0'; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 60 | sParse.newTnum = atoi(argv[2]); |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame] | 61 | sqliteRunParser(&sParse, argv[3], 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 62 | }else{ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 63 | /* If the SQL column is blank it means this is an index that |
| 64 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 65 | ** constraint for a CREATE TABLE. The index should have already |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 66 | ** been created when we processed the CREATE TABLE. All we have |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 67 | ** to do here is record the root page number for that index. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 68 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 69 | Index *pIndex = sqliteFindIndex(db, argv[1]); |
| 70 | if( pIndex==0 || pIndex->tnum!=0 ){ |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 71 | /* This can occur if there exists an index on a TEMP table which |
| 72 | ** has the same name as another index on a permanent index. Since |
| 73 | ** the permanent table is hidden by the TEMP table, we can also |
| 74 | ** safely ignore the index on the permanent table. |
| 75 | */ |
| 76 | /* Do Nothing */; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 77 | }else{ |
| 78 | pIndex->tnum = atoi(argv[2]); |
| 79 | } |
| 80 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | default: { |
| 84 | /* This can not happen! */ |
| 85 | nErr = 1; |
| 86 | assert( nErr==0 ); |
| 87 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 88 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 89 | return nErr; |
| 90 | } |
| 91 | |
| 92 | /* |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame^] | 93 | ** This is a callback procedure used to reconstruct a table. The |
| 94 | ** name of the table to be reconstructed is passed in as argv[0]. |
| 95 | ** |
| 96 | ** This routine is used to automatically upgrade a database from |
| 97 | ** format version 1 or 2 to version 3. The correct operation of |
| 98 | ** this routine relys on the fact that no indices are used when |
| 99 | ** copying a table out to a temporary file. |
| 100 | */ |
| 101 | static int |
| 102 | upgrade_3_callback(void *pDb, int argc, char **argv, char **NotUsed){ |
| 103 | sqlite *db = (sqlite*)pDb; |
| 104 | int rc; |
| 105 | |
| 106 | rc = sqlite_exec_printf(db, |
| 107 | "CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; " |
| 108 | "DELETE FROM '%q'; " |
| 109 | "INSERT INTO '%q' SELECT * FROM sqlite_x; " |
| 110 | "DROP TABLE sqlite_x;", |
| 111 | 0, 0, 0, argv[0], argv[0], argv[0]); |
| 112 | return rc!=SQLITE_OK; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 118 | ** Attempt to read the database schema and initialize internal |
| 119 | ** data structures. Return one of the SQLITE_ error codes to |
| 120 | ** indicate success or failure. |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 121 | ** |
| 122 | ** After the database is initialized, the SQLITE_Initialized |
| 123 | ** bit is set in the flags field of the sqlite structure. An |
| 124 | ** attempt is made to initialize the database as soon as it |
| 125 | ** is opened. If that fails (perhaps because another process |
| 126 | ** has the sqlite_master table locked) than another attempt |
| 127 | ** is made the first time the database is accessed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 128 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 129 | int sqliteInit(sqlite *db, char **pzErrMsg){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 130 | int rc; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 131 | BtCursor *curMain; |
| 132 | int size; |
| 133 | Table *pTab; |
| 134 | char *azArg[6]; |
| 135 | int meta[SQLITE_N_BTREE_META]; |
| 136 | Parse sParse; |
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 | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 141 | static 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 | ; |
| 150 | static char temp_master_schema[] = |
| 151 | "CREATE TEMP TABLE sqlite_temp_master(\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 152 | " type text,\n" |
| 153 | " name text,\n" |
| 154 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 155 | " rootpage integer,\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 156 | " sql text\n" |
| 157 | ")" |
| 158 | ; |
| 159 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 160 | /* The following SQL will read the schema from the master tables. |
| 161 | ** The first version works with SQLite file formats 2 or greater. |
| 162 | ** The second version is for format 1 files. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 163 | ** |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 164 | ** Beginning with file format 2, the rowid for new table entries |
| 165 | ** (including entries in sqlite_master) is an increasing integer. |
| 166 | ** So for file format 2 and later, we can play back sqlite_master |
| 167 | ** and all the CREATE statements will appear in the right order. |
| 168 | ** But with file format 1, table entries were random and so we |
| 169 | ** have to make sure the CREATE TABLEs occur before their corresponding |
| 170 | ** CREATE INDEXs. (We don't have to deal with CREATE VIEW or |
| 171 | ** CREATE TRIGGER in file format 1 because those constructs did |
| 172 | ** not exist then.) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 173 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 174 | static char init_script[] = |
| 175 | "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master " |
| 176 | "UNION ALL " |
| 177 | "SELECT type, name, rootpage, sql, 0 FROM sqlite_master"; |
| 178 | static char older_init_script[] = |
| 179 | "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master " |
| 180 | "UNION ALL " |
| 181 | "SELECT type, name, rootpage, sql, 0 FROM sqlite_master " |
| 182 | "WHERE type='table' " |
| 183 | "UNION ALL " |
| 184 | "SELECT type, name, rootpage, sql, 0 FROM sqlite_master " |
| 185 | "WHERE type='index'"; |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 186 | |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 187 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 188 | /* Construct the schema tables: sqlite_master and sqlite_temp_master |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 189 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 190 | azArg[0] = "table"; |
| 191 | azArg[1] = MASTER_NAME; |
| 192 | azArg[2] = "2"; |
| 193 | azArg[3] = master_schema; |
| 194 | azArg[4] = "0"; |
| 195 | azArg[5] = 0; |
| 196 | sqliteInitCallback(db, 5, azArg, 0); |
| 197 | pTab = sqliteFindTable(db, MASTER_NAME); |
| 198 | if( pTab ){ |
| 199 | pTab->readOnly = 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 200 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 201 | azArg[1] = TEMP_MASTER_NAME; |
| 202 | azArg[3] = temp_master_schema; |
| 203 | azArg[4] = "1"; |
| 204 | sqliteInitCallback(db, 5, azArg, 0); |
| 205 | pTab = sqliteFindTable(db, TEMP_MASTER_NAME); |
| 206 | if( pTab ){ |
| 207 | pTab->readOnly = 1; |
| 208 | } |
| 209 | |
| 210 | /* Create a cursor to hold the database open |
| 211 | */ |
| 212 | if( db->pBe==0 ) return SQLITE_OK; |
| 213 | rc = sqliteBtreeCursor(db->pBe, 2, 0, &curMain); |
| 214 | if( rc ) return rc; |
| 215 | |
| 216 | /* Get the database meta information |
| 217 | */ |
| 218 | rc = sqliteBtreeGetMeta(db->pBe, meta); |
| 219 | if( rc ){ |
| 220 | sqliteBtreeCloseCursor(curMain); |
| 221 | return rc; |
| 222 | } |
| 223 | db->schema_cookie = meta[1]; |
| 224 | db->next_cookie = db->schema_cookie; |
| 225 | db->file_format = meta[2]; |
| 226 | size = meta[3]; |
| 227 | if( size==0 ){ size = MAX_PAGES; } |
| 228 | db->cache_size = size; |
| 229 | sqliteBtreeSetCacheSize(db->pBe, size); |
| 230 | |
| 231 | /* |
| 232 | ** file_format==1 Version 2.1.0. |
| 233 | ** file_format==2 Version 2.2.0. Add support for INTEGER PRIMARY KEY. |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame^] | 234 | ** file_format==3 Version 2.6.0. Fix empty-string index bug. |
| 235 | ** file_format==4 Version 2.7.0. Add support for separate numeric and |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 236 | ** text datatypes. |
| 237 | */ |
| 238 | if( db->file_format==0 ){ |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame^] | 239 | /* This happens if the database was initially empty */ |
| 240 | db->file_format = 3; |
| 241 | }else if( db->file_format>3 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 242 | sqliteBtreeCloseCursor(curMain); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 243 | sqliteSetString(pzErrMsg, "unsupported file format", 0); |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 244 | rc = SQLITE_ERROR; |
| 245 | } |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 246 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 247 | /* Read the schema information out of the schema tables |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 248 | */ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 249 | memset(&sParse, 0, sizeof(sParse)); |
| 250 | sParse.db = db; |
| 251 | sParse.pBe = db->pBe; |
| 252 | sParse.xCallback = sqliteInitCallback; |
| 253 | sParse.pArg = (void*)db; |
| 254 | sParse.initFlag = 1; |
| 255 | sqliteRunParser(&sParse, |
| 256 | db->file_format>=2 ? init_script : older_init_script, |
| 257 | pzErrMsg); |
| 258 | if( sqlite_malloc_failed ){ |
| 259 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 260 | sParse.rc = SQLITE_NOMEM; |
| 261 | sqliteBtreeRollback(db->pBe); |
| 262 | sqliteResetInternalSchema(db); |
| 263 | } |
| 264 | if( sParse.rc==SQLITE_OK ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 265 | db->flags |= SQLITE_Initialized; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 266 | sqliteCommitInternalChanges(db); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 267 | }else{ |
| 268 | db->flags &= ~SQLITE_Initialized; |
| 269 | sqliteResetInternalSchema(db); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 270 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 271 | sqliteBtreeCloseCursor(curMain); |
| 272 | return sParse.rc; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 276 | ** The version of the library |
| 277 | */ |
drh | 3d0b559 | 2000-08-22 13:40:51 +0000 | [diff] [blame] | 278 | const char sqlite_version[] = SQLITE_VERSION; |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 279 | |
| 280 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 281 | ** Does the library expect data to be encoded as UTF-8 or iso8859? The |
| 282 | ** following global constant always lets us know. |
| 283 | */ |
| 284 | #ifdef SQLITE_UTF8 |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 285 | const char sqlite_encoding[] = "UTF-8"; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 286 | #else |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 287 | const char sqlite_encoding[] = "iso8859"; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 288 | #endif |
| 289 | |
| 290 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 291 | ** Open a new SQLite database. Construct an "sqlite" structure to define |
| 292 | ** the state of this database and return a pointer to that structure. |
| 293 | ** |
| 294 | ** An attempt is made to initialize the in-memory data structures that |
| 295 | ** hold the database schema. But if this fails (because the schema file |
| 296 | ** is locked) then that step is deferred until the first call to |
| 297 | ** sqlite_exec(). |
| 298 | */ |
| 299 | sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){ |
| 300 | sqlite *db; |
| 301 | int rc; |
| 302 | |
| 303 | /* Allocate the sqlite data structure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | db = sqliteMalloc( sizeof(sqlite) ); |
| 305 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 306 | if( db==0 ) goto no_mem_on_open; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 307 | sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0); |
| 308 | sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 309 | sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 310 | sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1); |
drh | 28f4b68 | 2002-06-09 10:14:18 +0000 | [diff] [blame] | 311 | sqliteRegisterBuiltinFunctions(db); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 312 | db->onError = OE_Default; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 313 | db->priorNewRowid = 0; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 314 | db->magic = SQLITE_MAGIC_BUSY; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | |
| 316 | /* Open the backend database driver */ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 317 | rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 318 | if( rc!=SQLITE_OK ){ |
| 319 | switch( rc ){ |
| 320 | default: { |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 321 | sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 324 | sqliteFree(db); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 325 | sqliteStrRealloc(pzErrMsg); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 326 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 327 | } |
| 328 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 329 | /* Attempt to read the schema */ |
| 330 | rc = sqliteInit(db, pzErrMsg); |
drh | c67980b | 2002-06-14 20:54:14 +0000 | [diff] [blame] | 331 | db->magic = SQLITE_MAGIC_OPEN; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 332 | if( sqlite_malloc_failed ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 333 | sqlite_close(db); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 334 | goto no_mem_on_open; |
| 335 | }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 336 | sqlite_close(db); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 337 | sqliteStrRealloc(pzErrMsg); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 338 | return 0; |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 339 | }else if( pzErrMsg ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 340 | sqliteFree(*pzErrMsg); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 341 | *pzErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 342 | } |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame^] | 343 | |
| 344 | /* If the database is in formats 1 or 2, then upgrade it to |
| 345 | ** version 3. This will reconstruct all indices. If the |
| 346 | ** upgrade fails for any reason (ex: out of disk space, database |
| 347 | ** is read only, interrupt receive, etc.) then refuse to open. |
| 348 | */ |
| 349 | if( db->file_format<3 ){ |
| 350 | char *zErr; |
| 351 | int meta[SQLITE_N_BTREE_META]; |
| 352 | |
| 353 | db->file_format = 3; |
| 354 | rc = sqlite_exec(db, |
| 355 | "BEGIN; SELECT name FROM sqlite_master WHERE type='table';", |
| 356 | upgrade_3_callback, |
| 357 | db, |
| 358 | &zErr); |
| 359 | if( rc==SQLITE_OK ){ |
| 360 | sqliteBtreeGetMeta(db->pBe, meta); |
| 361 | meta[2] = 3; |
| 362 | sqliteBtreeUpdateMeta(db->pBe, meta); |
| 363 | sqlite_exec(db, "COMMIT", 0, 0, 0); |
| 364 | } |
| 365 | if( rc!=SQLITE_OK ){ |
| 366 | sqliteSetString(pzErrMsg, |
| 367 | "unable to upgrade database to the version 2.6 format", |
| 368 | zErr ? ": " : 0, zErr, 0); |
| 369 | sqliteFree(zErr); |
| 370 | sqliteStrRealloc(pzErrMsg); |
| 371 | sqlite_close(db); |
| 372 | return 0; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* Return a pointer to the newly opened database structure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 377 | return db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 378 | |
| 379 | no_mem_on_open: |
| 380 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 381 | sqliteStrRealloc(pzErrMsg); |
| 382 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 386 | ** Return the ROWID of the most recent insert |
| 387 | */ |
| 388 | int sqlite_last_insert_rowid(sqlite *db){ |
| 389 | return db->lastRowid; |
| 390 | } |
| 391 | |
| 392 | /* |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 393 | ** Return the number of changes in the most recent call to sqlite_exec(). |
| 394 | */ |
| 395 | int sqlite_changes(sqlite *db){ |
| 396 | return db->nChange; |
| 397 | } |
| 398 | |
| 399 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 400 | ** Close an existing SQLite database |
| 401 | */ |
| 402 | void sqlite_close(sqlite *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 403 | HashElem *i; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 404 | if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; } |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 405 | db->magic = SQLITE_MAGIC_CLOSED; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 406 | sqliteBtreeClose(db->pBe); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 407 | sqliteResetInternalSchema(db); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 408 | if( db->pBeTemp ){ |
| 409 | sqliteBtreeClose(db->pBeTemp); |
| 410 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 411 | for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 412 | FuncDef *pFunc, *pNext; |
| 413 | for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 414 | pNext = pFunc->pNext; |
| 415 | sqliteFree(pFunc); |
| 416 | } |
| 417 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 418 | sqliteHashClear(&db->aFunc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 419 | sqliteFree(db); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | ** Return TRUE if the given SQL string ends in a semicolon. |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 424 | ** |
| 425 | ** Special handling is require for CREATE TRIGGER statements. |
| 426 | ** Whenever the CREATE TRIGGER keywords are seen, the statement |
| 427 | ** must end with ";END;". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 428 | */ |
| 429 | int sqlite_complete(const char *zSql){ |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 430 | int isComplete = 1; |
| 431 | int requireEnd = 0; |
| 432 | int seenText = 0; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 433 | int seenCreate = 0; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 434 | while( *zSql ){ |
| 435 | switch( *zSql ){ |
| 436 | case ';': { |
| 437 | isComplete = 1; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 438 | seenText = 1; |
| 439 | seenCreate = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 440 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 441 | } |
| 442 | case ' ': |
| 443 | case '\t': |
| 444 | case '\n': |
| 445 | case '\f': { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 446 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 447 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 448 | case '[': { |
| 449 | isComplete = 0; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 450 | seenText = 1; |
| 451 | seenCreate = 0; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 452 | zSql++; |
| 453 | while( *zSql && *zSql!=']' ){ zSql++; } |
| 454 | if( *zSql==0 ) return 0; |
| 455 | break; |
| 456 | } |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 457 | case '"': |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 458 | case '\'': { |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 459 | int c = *zSql; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 460 | isComplete = 0; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 461 | seenText = 1; |
| 462 | seenCreate = 0; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 463 | zSql++; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 464 | while( *zSql && *zSql!=c ){ zSql++; } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 465 | if( *zSql==0 ) return 0; |
| 466 | break; |
| 467 | } |
| 468 | case '-': { |
| 469 | if( zSql[1]!='-' ){ |
| 470 | isComplete = 0; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 471 | seenCreate = 0; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | while( *zSql && *zSql!='\n' ){ zSql++; } |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 475 | if( *zSql==0 ) return seenText && isComplete && requireEnd==0; |
| 476 | break; |
| 477 | } |
| 478 | case 'c': |
| 479 | case 'C': { |
| 480 | seenText = 1; |
| 481 | if( !isComplete ) break; |
| 482 | isComplete = 0; |
| 483 | if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break; |
| 484 | if( !isspace(zSql[6]) ) break; |
| 485 | zSql += 5; |
| 486 | seenCreate = 1; |
| 487 | while( isspace(zSql[1]) ) zSql++; |
| 488 | if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break; |
| 489 | zSql += 7; |
| 490 | requireEnd++; |
| 491 | break; |
| 492 | } |
| 493 | case 't': |
| 494 | case 'T': { |
| 495 | seenText = 1; |
| 496 | if( !seenCreate ) break; |
| 497 | seenCreate = 0; |
| 498 | isComplete = 0; |
| 499 | if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break; |
| 500 | if( !isspace(zSql[7]) ) break; |
| 501 | zSql += 6; |
| 502 | requireEnd++; |
| 503 | break; |
| 504 | } |
| 505 | case 'e': |
| 506 | case 'E': { |
| 507 | seenCreate = 0; |
| 508 | seenText = 1; |
| 509 | if( !isComplete ) break; |
| 510 | isComplete = 0; |
| 511 | if( requireEnd==0 ) break; |
| 512 | if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break; |
| 513 | zSql += 2; |
| 514 | while( isspace(zSql[1]) ) zSql++; |
| 515 | if( zSql[1]==';' ){ |
| 516 | zSql++; |
| 517 | isComplete = 1; |
| 518 | requireEnd--; |
| 519 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 520 | break; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 521 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 522 | default: { |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 523 | seenCreate = 0; |
| 524 | seenText = 1; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 525 | isComplete = 0; |
| 526 | break; |
| 527 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 528 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 529 | zSql++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 530 | } |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 531 | return seenText && isComplete && requireEnd==0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 535 | ** Execute SQL code. Return one of the SQLITE_ success/failure |
| 536 | ** codes. Also write an error message into memory obtained from |
| 537 | ** malloc() and make *pzErrMsg point to that message. |
| 538 | ** |
| 539 | ** If the SQL is a query, then for each row in the query result |
| 540 | ** the xCallback() function is called. pArg becomes the first |
| 541 | ** argument to xCallback(). If xCallback=NULL then no callback |
| 542 | ** is invoked, even for queries. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 543 | */ |
| 544 | int sqlite_exec( |
| 545 | sqlite *db, /* The database on which the SQL executes */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 546 | const char *zSql, /* The SQL to be executed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 547 | sqlite_callback xCallback, /* Invoke this callback routine */ |
| 548 | void *pArg, /* First argument to xCallback() */ |
| 549 | char **pzErrMsg /* Write error messages here */ |
| 550 | ){ |
| 551 | Parse sParse; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | |
| 553 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 554 | if( sqliteSafetyOn(db) ) goto exec_misuse; |
drh | 70562cd | 2002-07-13 17:23:21 +0000 | [diff] [blame] | 555 | if( (db->flags & SQLITE_InTrans)!=0 && db->pid!=sqliteOsProcessId() ){ |
| 556 | goto exec_misuse; |
| 557 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 558 | if( (db->flags & SQLITE_Initialized)==0 ){ |
| 559 | int rc = sqliteInit(db, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 560 | if( rc!=SQLITE_OK ){ |
| 561 | sqliteStrRealloc(pzErrMsg); |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 562 | sqliteSafetyOff(db); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 563 | return rc; |
| 564 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 565 | } |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame^] | 566 | if( db->file_format<3 ){ |
| 567 | sqliteSafetyOff(db); |
| 568 | sqliteSetString(pzErrMsg, "obsolete database file format", 0); |
| 569 | return SQLITE_ERROR; |
| 570 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 571 | if( db->recursionDepth==0 ){ db->nChange = 0; } |
| 572 | db->recursionDepth++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 573 | memset(&sParse, 0, sizeof(sParse)); |
| 574 | sParse.db = db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 575 | sParse.pBe = db->pBe; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 576 | sParse.xCallback = xCallback; |
| 577 | sParse.pArg = pArg; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 578 | sqliteRunParser(&sParse, zSql, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 579 | if( sqlite_malloc_failed ){ |
| 580 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 581 | sParse.rc = SQLITE_NOMEM; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 582 | sqliteBtreeRollback(db->pBe); |
| 583 | if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp); |
| 584 | db->flags &= ~SQLITE_InTrans; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 585 | sqliteResetInternalSchema(db); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 586 | } |
| 587 | sqliteStrRealloc(pzErrMsg); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 588 | if( sParse.rc==SQLITE_SCHEMA ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 589 | sqliteResetInternalSchema(db); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 590 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 591 | db->recursionDepth--; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 592 | if( sqliteSafetyOff(db) ) goto exec_misuse; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 593 | return sParse.rc; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 594 | |
| 595 | exec_misuse: |
| 596 | if( pzErrMsg ){ |
| 597 | *pzErrMsg = 0; |
| 598 | sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); |
| 599 | sqliteStrRealloc(pzErrMsg); |
| 600 | } |
| 601 | return SQLITE_MISUSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 602 | } |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 603 | |
| 604 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 605 | ** Return a static string that describes the kind of error specified in the |
| 606 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 607 | */ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 608 | const char *sqlite_error_string(int rc){ |
| 609 | const char *z; |
| 610 | switch( rc ){ |
| 611 | case SQLITE_OK: z = "not an error"; break; |
| 612 | case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
| 613 | case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break; |
| 614 | case SQLITE_PERM: z = "access permission denied"; break; |
| 615 | case SQLITE_ABORT: z = "callback requested query abort"; break; |
| 616 | case SQLITE_BUSY: z = "database is locked"; break; |
| 617 | case SQLITE_LOCKED: z = "database table is locked"; break; |
| 618 | case SQLITE_NOMEM: z = "out of memory"; break; |
| 619 | case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
| 620 | case SQLITE_INTERRUPT: z = "interrupted"; break; |
| 621 | case SQLITE_IOERR: z = "disk I/O error"; break; |
| 622 | case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
| 623 | case SQLITE_NOTFOUND: z = "table or record not found"; break; |
| 624 | case SQLITE_FULL: z = "database is full"; break; |
| 625 | case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
| 626 | case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
| 627 | case SQLITE_EMPTY: z = "table contains no data"; break; |
| 628 | case SQLITE_SCHEMA: z = "database schema has changed"; break; |
| 629 | case SQLITE_TOOBIG: z = "too much data for one table row"; break; |
| 630 | case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
| 631 | case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
| 632 | case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
| 633 | default: z = "unknown error"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 634 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 635 | return z; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 639 | ** This routine implements a busy callback that sleeps and tries |
| 640 | ** again until a timeout value is reached. The timeout value is |
| 641 | ** an integer number of milliseconds passed in as the first |
| 642 | ** argument. |
| 643 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 644 | static int sqliteDefaultBusyCallback( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 645 | void *Timeout, /* Maximum amount of time to wait */ |
| 646 | const char *NotUsed, /* The name of the table that is busy */ |
| 647 | int count /* Number of times table has been busy */ |
| 648 | ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 649 | #if SQLITE_MIN_SLEEP_MS==1 |
| 650 | int delay = 10; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 651 | int prior_delay = 0; |
| 652 | int timeout = (int)Timeout; |
| 653 | int i; |
| 654 | |
| 655 | for(i=1; i<count; i++){ |
| 656 | prior_delay += delay; |
| 657 | delay = delay*2; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 658 | if( delay>=1000 ){ |
| 659 | delay = 1000; |
| 660 | prior_delay += 1000*(count - i - 1); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | } |
drh | 3109e02 | 2001-10-09 13:46:01 +0000 | [diff] [blame] | 664 | if( prior_delay + delay > timeout ){ |
| 665 | delay = timeout - prior_delay; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 666 | if( delay<=0 ) return 0; |
| 667 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 668 | sqliteOsSleep(delay); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 669 | return 1; |
| 670 | #else |
| 671 | int timeout = (int)Timeout; |
| 672 | if( (count+1)*1000 > timeout ){ |
| 673 | return 0; |
| 674 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 675 | sqliteOsSleep(1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 676 | return 1; |
| 677 | #endif |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | ** This routine sets the busy callback for an Sqlite database to the |
| 682 | ** given callback function with the given argument. |
| 683 | */ |
| 684 | void sqlite_busy_handler( |
| 685 | sqlite *db, |
| 686 | int (*xBusy)(void*,const char*,int), |
| 687 | void *pArg |
| 688 | ){ |
| 689 | db->xBusyCallback = xBusy; |
| 690 | db->pBusyArg = pArg; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | ** This routine installs a default busy handler that waits for the |
| 695 | ** specified number of milliseconds before returning 0. |
| 696 | */ |
| 697 | void sqlite_busy_timeout(sqlite *db, int ms){ |
| 698 | if( ms>0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 699 | sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 700 | }else{ |
| 701 | sqlite_busy_handler(db, 0, 0); |
| 702 | } |
| 703 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | ** Cause any pending operation to stop at its earliest opportunity. |
| 707 | */ |
| 708 | void sqlite_interrupt(sqlite *db){ |
| 709 | db->flags |= SQLITE_Interrupt; |
| 710 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 711 | |
| 712 | /* |
| 713 | ** Windows systems should call this routine to free memory that |
| 714 | ** is returned in the in the errmsg parameter of sqlite_open() when |
| 715 | ** SQLite is a DLL. For some reason, it does not work to call free() |
| 716 | ** directly. |
| 717 | ** |
| 718 | ** Note that we need to call free() not sqliteFree() here, since every |
| 719 | ** string that is exported from SQLite should have already passed through |
| 720 | ** sqliteStrRealloc(). |
| 721 | */ |
| 722 | void sqlite_freemem(void *p){ free(p); } |
| 723 | |
| 724 | /* |
| 725 | ** Windows systems need functions to call to return the sqlite_version |
| 726 | ** and sqlite_encoding strings. |
| 727 | */ |
| 728 | const char *sqlite_libversion(void){ return sqlite_version; } |
| 729 | const char *sqlite_libencoding(void){ return sqlite_encoding; } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 730 | |
| 731 | /* |
| 732 | ** Create new user-defined functions. The sqlite_create_function() |
| 733 | ** routine creates a regular function and sqlite_create_aggregate() |
| 734 | ** creates an aggregate function. |
| 735 | ** |
| 736 | ** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments |
| 737 | ** disables the function. Calling sqlite_create_function() with the |
| 738 | ** same name and number of arguments as a prior call to |
| 739 | ** sqlite_create_aggregate() disables the prior call to |
| 740 | ** sqlite_create_aggregate(), and vice versa. |
| 741 | ** |
| 742 | ** If nArg is -1 it means that this function will accept any number |
| 743 | ** of arguments, including 0. |
| 744 | */ |
| 745 | int sqlite_create_function( |
| 746 | sqlite *db, /* Add the function to this database connection */ |
| 747 | const char *zName, /* Name of the function to add */ |
| 748 | int nArg, /* Number of arguments */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 749 | void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */ |
| 750 | void *pUserData /* User data */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 751 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 752 | FuncDef *p; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 753 | if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 754 | p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1); |
drh | 4e0f995 | 2002-02-27 01:53:13 +0000 | [diff] [blame] | 755 | if( p==0 ) return 1; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 756 | p->xFunc = xFunc; |
| 757 | p->xStep = 0; |
| 758 | p->xFinalize = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 759 | p->pUserData = pUserData; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | int sqlite_create_aggregate( |
| 763 | sqlite *db, /* Add the function to this database connection */ |
| 764 | const char *zName, /* Name of the function to add */ |
| 765 | int nArg, /* Number of arguments */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 766 | void (*xStep)(sqlite_func*,int,const char**), /* The step function */ |
| 767 | void (*xFinalize)(sqlite_func*), /* The finalizer */ |
| 768 | void *pUserData /* User data */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 769 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 770 | FuncDef *p; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 771 | if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 772 | p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1); |
drh | 4e0f995 | 2002-02-27 01:53:13 +0000 | [diff] [blame] | 773 | if( p==0 ) return 1; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 774 | p->xFunc = 0; |
| 775 | p->xStep = xStep; |
| 776 | p->xFinalize = xFinalize; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 777 | p->pUserData = pUserData; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 778 | return 0; |
| 779 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 780 | |
| 781 | /* |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 782 | ** Change the datatype for all functions with a given name. See the |
| 783 | ** header comment for the prototype of this function in sqlite.h for |
| 784 | ** additional information. |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 785 | */ |
| 786 | int sqlite_function_type(sqlite *db, const char *zName, int dataType){ |
| 787 | FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName)); |
| 788 | while( p ){ |
| 789 | p->dataType = dataType; |
| 790 | p = p->pNext; |
| 791 | } |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 792 | return SQLITE_OK; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 793 | } |
drh | 411995d | 2002-06-25 19:31:18 +0000 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | ** Attempt to open the file named in the argument as the auxiliary database |
| 797 | ** file. The auxiliary database file is used to store TEMP tables. But |
| 798 | ** by using this API, it is possible to trick SQLite into opening two |
| 799 | ** separate databases and acting on them as if they were one. |
| 800 | ** |
| 801 | ** This routine closes the existing auxiliary database file, which will |
| 802 | ** cause any previously created TEMP tables to be created. |
| 803 | ** |
| 804 | ** The zName parameter can be a NULL pointer or an empty string to cause |
| 805 | ** a temporary file to be opened and automatically deleted when closed. |
| 806 | */ |
| 807 | int sqlite_open_aux_file(sqlite *db, const char *zName, char **pzErrMsg){ |
| 808 | int rc; |
| 809 | if( zName && zName[0]==0 ) zName = 0; |
| 810 | if( sqliteSafetyOn(db) ) goto openaux_misuse; |
| 811 | sqliteResetInternalSchema(db); |
| 812 | if( db->pBeTemp!=0 ){ |
| 813 | sqliteBtreeClose(db->pBeTemp); |
| 814 | } |
| 815 | rc = sqliteBtreeOpen(zName, 0, MAX_PAGES, &db->pBeTemp); |
| 816 | if( rc ){ |
| 817 | if( zName==0 ) zName = "a temporary file"; |
| 818 | sqliteSetString(pzErrMsg, "unable to open ", zName, |
| 819 | ": ", sqlite_error_string(rc), 0); |
| 820 | sqliteStrRealloc(pzErrMsg); |
| 821 | sqliteSafetyOff(db); |
| 822 | return rc; |
| 823 | } |
| 824 | rc = sqliteInit(db, pzErrMsg); |
| 825 | if( sqliteSafetyOff(db) ) goto openaux_misuse; |
| 826 | sqliteStrRealloc(pzErrMsg); |
| 827 | return rc; |
| 828 | |
| 829 | openaux_misuse: |
| 830 | sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); |
| 831 | sqliteStrRealloc(pzErrMsg); |
| 832 | return SQLITE_MISUSE; |
| 833 | } |