drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (c) 1999, 2000 D. Richard Hipp |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or |
| 5 | ** modify it under the terms of the GNU General Public |
| 6 | ** License as published by the Free Software Foundation; either |
| 7 | ** version 2 of the License, or (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ** General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public |
| 15 | ** License along with this library; if not, write to the |
| 16 | ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | ** Boston, MA 02111-1307, USA. |
| 18 | ** |
| 19 | ** Author contact information: |
| 20 | ** drh@hwaci.com |
| 21 | ** http://www.hwaci.com/drh/ |
| 22 | ** |
| 23 | ************************************************************************* |
| 24 | ** Main file for the SQLite library. The routines in this file |
| 25 | ** implement the programmer interface to the library. Routines in |
| 26 | ** other files are for internal use by SQLite and should not be |
| 27 | ** accessed by users of the library. |
| 28 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 29 | ** $Id: main.c,v 1.36 2001/09/15 00:57:29 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 30 | */ |
| 31 | #include "sqliteInt.h" |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 32 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 34 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | ** This is the callback routine for the code that initializes the |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 38 | ** database. Each callback contains the following information: |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 39 | ** |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 40 | ** argv[0] = "meta" or "table" or "index" |
| 41 | ** argv[1] = table or index name |
| 42 | ** argv[2] = root page number for table or index |
| 43 | ** argv[3] = SQL create statement for the table or index |
| 44 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 45 | */ |
| 46 | static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){ |
| 47 | sqlite *db = (sqlite*)pDb; |
| 48 | Parse sParse; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 49 | int nErr = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 50 | |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 51 | assert( argc==4 ); |
| 52 | switch( argv[0][0] ){ |
| 53 | case 'm': { /* Meta information */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 54 | if( strcmp(argv[1],"file-format")==0 ){ |
| 55 | db->file_format = atoi(argv[3]); |
| 56 | }else if( strcmp(argv[1],"schema-cookie")==0 ){ |
| 57 | db->schema_cookie = atoi(argv[3]); |
| 58 | db->next_cookie = db->schema_cookie; |
| 59 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 60 | break; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 61 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 62 | case 'i': |
| 63 | case 't': { /* CREATE TABLE and CREATE INDEX statements */ |
| 64 | memset(&sParse, 0, sizeof(sParse)); |
| 65 | sParse.db = db; |
| 66 | sParse.initFlag = 1; |
| 67 | sParse.newTnum = atoi(argv[2]); |
| 68 | nErr = sqliteRunParser(&sParse, argv[3], 0); |
| 69 | break; |
| 70 | } |
| 71 | default: { |
| 72 | /* This can not happen! */ |
| 73 | nErr = 1; |
| 74 | assert( nErr==0 ); |
| 75 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 76 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 77 | return nErr; |
| 78 | } |
| 79 | |
| 80 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 81 | ** Attempt to read the database schema and initialize internal |
| 82 | ** data structures. Return one of the SQLITE_ error codes to |
| 83 | ** indicate success or failure. |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 84 | ** |
| 85 | ** After the database is initialized, the SQLITE_Initialized |
| 86 | ** bit is set in the flags field of the sqlite structure. An |
| 87 | ** attempt is made to initialize the database as soon as it |
| 88 | ** is opened. If that fails (perhaps because another process |
| 89 | ** has the sqlite_master table locked) than another attempt |
| 90 | ** is made the first time the database is accessed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 91 | */ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 92 | static int sqliteInit(sqlite *db, char **pzErrMsg){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 93 | Vdbe *vdbe; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 94 | int rc; |
| 95 | |
| 96 | /* |
| 97 | ** The master database table has a structure like this |
| 98 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 99 | static char master_schema[] = |
| 100 | "CREATE TABLE " MASTER_NAME " (\n" |
| 101 | " type text,\n" |
| 102 | " name text,\n" |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 103 | " tnum integer,\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 104 | " tbl_name text,\n" |
| 105 | " sql text\n" |
| 106 | ")" |
| 107 | ; |
| 108 | |
| 109 | /* The following program is used to initialize the internal |
| 110 | ** structure holding the tables and indexes of the database. |
| 111 | ** The database contains a special table named "sqlite_master" |
| 112 | ** defined as follows: |
| 113 | ** |
| 114 | ** CREATE TABLE sqlite_master ( |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 115 | ** type text, -- Either "table" or "index" or "meta" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 116 | ** name text, -- Name of table or index |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 117 | ** tnum integer, -- The integer page number of root page |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 118 | ** tbl_name text, -- Associated table |
| 119 | ** sql text -- The CREATE statement for this object |
| 120 | ** ); |
| 121 | ** |
| 122 | ** The sqlite_master table contains a single entry for each table |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 123 | ** and each index. The "type" column tells whether the entry is |
| 124 | ** a table or index. The "name" column is the name of the object. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 125 | ** The "tbl_name" is the name of the associated table. For tables, |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 126 | ** the tbl_name column is always the same as name. For indices, the |
| 127 | ** tbl_name column contains the name of the table that the index |
| 128 | ** indexes. Finally, the "sql" column contains the complete text of |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 129 | ** the CREATE TABLE or CREATE INDEX statement that originally created |
| 130 | ** the table or index. |
| 131 | ** |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 132 | ** If the "type" column has the value "meta", then the "sql" column |
| 133 | ** contains extra information about the database, such as the |
| 134 | ** file format version number. All meta information must be processed |
| 135 | ** before any tables or indices are constructed. |
| 136 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 137 | ** The following program invokes its callback on the SQL for each |
| 138 | ** table then goes back and invokes the callback on the |
| 139 | ** SQL for each index. The callback will invoke the |
| 140 | ** parser to build the internal representation of the |
| 141 | ** database scheme. |
| 142 | */ |
| 143 | static VdbeOp initProg[] = { |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 144 | { OP_Open, 0, 2, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 145 | { OP_Rewind, 0, 0, 0}, |
| 146 | { OP_Next, 0, 12, 0}, /* 2 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 147 | { OP_Column, 0, 0, 0}, |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 148 | { OP_String, 0, 0, "meta"}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 149 | { OP_Ne, 0, 2, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 150 | { OP_Column, 0, 0, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 151 | { OP_Column, 0, 1, 0}, |
| 152 | { OP_Column, 0, 2, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 153 | { OP_Column, 0, 4, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 154 | { OP_Callback, 4, 0, 0}, |
| 155 | { OP_Goto, 0, 2, 0}, |
| 156 | { OP_Rewind, 0, 0, 0}, /* 12 */ |
| 157 | { OP_Next, 0, 23, 0}, /* 13 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 158 | { OP_Column, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 159 | { OP_String, 0, 0, "table"}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 160 | { OP_Ne, 0, 13, 0}, |
| 161 | { OP_Column, 0, 0, 0}, |
| 162 | { OP_Column, 0, 1, 0}, |
| 163 | { OP_Column, 0, 2, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 164 | { OP_Column, 0, 4, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 165 | { OP_Callback, 4, 0, 0}, |
| 166 | { OP_Goto, 0, 13, 0}, |
| 167 | { OP_Rewind, 0, 0, 0}, /* 23 */ |
| 168 | { OP_Next, 0, 34, 0}, /* 24 */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 169 | { OP_Column, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 170 | { OP_String, 0, 0, "index"}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 171 | { OP_Ne, 0, 24, 0}, |
| 172 | { OP_Column, 0, 0, 0}, |
| 173 | { OP_Column, 0, 1, 0}, |
| 174 | { OP_Column, 0, 2, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 175 | { OP_Column, 0, 4, 0}, |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 176 | { OP_Callback, 4, 0, 0}, |
| 177 | { OP_Goto, 0, 24, 0}, |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 178 | { OP_String, 0, 0, "meta"}, /* 34 */ |
| 179 | { OP_String, 0, 0, "schema-cookie"}, |
| 180 | { OP_String, 0, 0, ""}, |
| 181 | { OP_ReadCookie,0,0, 0}, |
| 182 | { OP_Callback, 4, 0, 0}, |
| 183 | { OP_Close, 0, 0, 0}, |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 184 | { OP_Halt, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 187 | /* Create a virtual machine to run the initialization program. Run |
| 188 | ** the program. The delete the virtual machine. |
| 189 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 190 | vdbe = sqliteVdbeCreate(db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 191 | if( vdbe==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 192 | sqliteSetString(pzErrMsg, "out of memory"); |
| 193 | return SQLITE_NOMEM; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 194 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 195 | sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 196 | rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg, |
| 197 | db->pBusyArg, db->xBusyCallback); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 198 | sqliteVdbeDelete(vdbe); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 199 | if( rc==SQLITE_OK && db->file_format>1 && db->nTable>0 ){ |
| 200 | sqliteSetString(pzErrMsg, "unsupported file format", 0); |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 201 | rc = SQLITE_ERROR; |
| 202 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 203 | if( rc==SQLITE_OK ){ |
| 204 | Table *pTab; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 205 | char *azArg[5]; |
| 206 | azArg[0] = "table"; |
| 207 | azArg[1] = MASTER_NAME; |
| 208 | azArg[2] = "2"; |
| 209 | azArg[3] = master_schema; |
| 210 | azArg[4] = 0; |
| 211 | sqliteOpenCb(db, 4, azArg, 0); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 212 | pTab = sqliteFindTable(db, MASTER_NAME); |
| 213 | if( pTab ){ |
| 214 | pTab->readOnly = 1; |
| 215 | } |
| 216 | db->flags |= SQLITE_Initialized; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 217 | sqliteCommitInternalChanges(db); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 218 | } |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 223 | ** The version of the library |
| 224 | */ |
drh | 3d0b559 | 2000-08-22 13:40:51 +0000 | [diff] [blame] | 225 | const char sqlite_version[] = SQLITE_VERSION; |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 226 | |
| 227 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 228 | ** Does the library expect data to be encoded as UTF-8 or iso8859? The |
| 229 | ** following global constant always lets us know. |
| 230 | */ |
| 231 | #ifdef SQLITE_UTF8 |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 232 | const char sqlite_encoding[] = "UTF-8"; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 233 | #else |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 234 | const char sqlite_encoding[] = "iso8859"; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 238 | ** Open a new SQLite database. Construct an "sqlite" structure to define |
| 239 | ** the state of this database and return a pointer to that structure. |
| 240 | ** |
| 241 | ** An attempt is made to initialize the in-memory data structures that |
| 242 | ** hold the database schema. But if this fails (because the schema file |
| 243 | ** is locked) then that step is deferred until the first call to |
| 244 | ** sqlite_exec(). |
| 245 | */ |
| 246 | sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){ |
| 247 | sqlite *db; |
| 248 | int rc; |
| 249 | |
| 250 | /* Allocate the sqlite data structure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 251 | db = sqliteMalloc( sizeof(sqlite) ); |
| 252 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 253 | if( db==0 ) goto no_mem_on_open; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 254 | |
| 255 | /* Open the backend database driver */ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 256 | rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 257 | if( rc!=SQLITE_OK ){ |
| 258 | switch( rc ){ |
| 259 | default: { |
| 260 | if( pzErrMsg ){ |
| 261 | sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0); |
| 262 | } |
| 263 | } |
| 264 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 265 | sqliteFree(db); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 266 | sqliteStrRealloc(pzErrMsg); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 267 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 268 | } |
| 269 | |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 270 | /* Assume file format 1 unless the database says otherwise */ |
| 271 | db->file_format = 1; |
| 272 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 273 | /* Attempt to read the schema */ |
| 274 | rc = sqliteInit(db, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 275 | if( sqlite_malloc_failed ){ |
| 276 | goto no_mem_on_open; |
| 277 | }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 278 | sqlite_close(db); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 279 | sqliteStrRealloc(pzErrMsg); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 280 | return 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 281 | }else /* if( pzErrMsg ) */{ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 282 | sqliteFree(*pzErrMsg); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 283 | *pzErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 284 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 285 | return db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 286 | |
| 287 | no_mem_on_open: |
| 288 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 289 | sqliteStrRealloc(pzErrMsg); |
| 290 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 294 | ** Erase all schema information from the schema hash table. |
| 295 | ** |
| 296 | ** The database schema is normally read in once when the database |
| 297 | ** is first opened and stored in a hash table in the sqlite structure. |
| 298 | ** This routine erases the stored schema. This erasure occurs because |
| 299 | ** either the database is being closed or because some other process |
| 300 | ** changed the schema and this process needs to reread it. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 301 | */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 302 | static void clearHashTable(sqlite *db){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 303 | int i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | for(i=0; i<N_HASH; i++){ |
| 305 | Table *pNext, *pList = db->apTblHash[i]; |
| 306 | db->apTblHash[i] = 0; |
| 307 | while( pList ){ |
| 308 | pNext = pList->pHash; |
| 309 | pList->pHash = 0; |
| 310 | sqliteDeleteTable(db, pList); |
| 311 | pList = pNext; |
| 312 | } |
| 313 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 314 | db->flags &= ~SQLITE_Initialized; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | ** Close an existing SQLite database |
| 319 | */ |
| 320 | void sqlite_close(sqlite *db){ |
| 321 | sqliteBtreeClose(db->pBe); |
| 322 | clearHashTable(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | sqliteFree(db); |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | ** Return TRUE if the given SQL string ends in a semicolon. |
| 328 | */ |
| 329 | int sqlite_complete(const char *zSql){ |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 330 | int isComplete = 0; |
| 331 | while( *zSql ){ |
| 332 | switch( *zSql ){ |
| 333 | case ';': { |
| 334 | isComplete = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 335 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 336 | } |
| 337 | case ' ': |
| 338 | case '\t': |
| 339 | case '\n': |
| 340 | case '\f': { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 341 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 342 | } |
| 343 | case '\'': { |
| 344 | isComplete = 0; |
| 345 | zSql++; |
| 346 | while( *zSql && *zSql!='\'' ){ zSql++; } |
| 347 | if( *zSql==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 348 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 349 | } |
| 350 | case '"': { |
| 351 | isComplete = 0; |
| 352 | zSql++; |
| 353 | while( *zSql && *zSql!='"' ){ zSql++; } |
| 354 | if( *zSql==0 ) return 0; |
| 355 | break; |
| 356 | } |
| 357 | case '-': { |
| 358 | if( zSql[1]!='-' ){ |
| 359 | isComplete = 0; |
| 360 | break; |
| 361 | } |
| 362 | while( *zSql && *zSql!='\n' ){ zSql++; } |
| 363 | if( *zSql==0 ) return isComplete; |
| 364 | break; |
| 365 | } |
| 366 | default: { |
| 367 | isComplete = 0; |
| 368 | break; |
| 369 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 370 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 371 | zSql++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 372 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 373 | return isComplete; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 377 | ** Execute SQL code. Return one of the SQLITE_ success/failure |
| 378 | ** codes. Also write an error message into memory obtained from |
| 379 | ** malloc() and make *pzErrMsg point to that message. |
| 380 | ** |
| 381 | ** If the SQL is a query, then for each row in the query result |
| 382 | ** the xCallback() function is called. pArg becomes the first |
| 383 | ** argument to xCallback(). If xCallback=NULL then no callback |
| 384 | ** is invoked, even for queries. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 385 | */ |
| 386 | int sqlite_exec( |
| 387 | sqlite *db, /* The database on which the SQL executes */ |
| 388 | char *zSql, /* The SQL to be executed */ |
| 389 | sqlite_callback xCallback, /* Invoke this callback routine */ |
| 390 | void *pArg, /* First argument to xCallback() */ |
| 391 | char **pzErrMsg /* Write error messages here */ |
| 392 | ){ |
| 393 | Parse sParse; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 394 | |
| 395 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 396 | if( (db->flags & SQLITE_Initialized)==0 ){ |
| 397 | int rc = sqliteInit(db, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 398 | if( rc!=SQLITE_OK ){ |
| 399 | sqliteStrRealloc(pzErrMsg); |
| 400 | return rc; |
| 401 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 402 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 403 | memset(&sParse, 0, sizeof(sParse)); |
| 404 | sParse.db = db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 405 | sParse.pBe = db->pBe; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 406 | sParse.xCallback = xCallback; |
| 407 | sParse.pArg = pArg; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 408 | sqliteRunParser(&sParse, zSql, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 409 | if( sqlite_malloc_failed ){ |
| 410 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 411 | sParse.rc = SQLITE_NOMEM; |
| 412 | } |
| 413 | sqliteStrRealloc(pzErrMsg); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 414 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 415 | clearHashTable(db); |
| 416 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 417 | return sParse.rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 418 | } |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | ** This routine implements a busy callback that sleeps and tries |
| 422 | ** again until a timeout value is reached. The timeout value is |
| 423 | ** an integer number of milliseconds passed in as the first |
| 424 | ** argument. |
| 425 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 426 | static int sqliteDefaultBusyCallback( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 427 | void *Timeout, /* Maximum amount of time to wait */ |
| 428 | const char *NotUsed, /* The name of the table that is busy */ |
| 429 | int count /* Number of times table has been busy */ |
| 430 | ){ |
drh | 82ad383 | 2000-07-31 13:38:24 +0000 | [diff] [blame] | 431 | #if defined(HAVE_USLEEP) && HAVE_USLEEP |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 432 | int delay = 10000; |
| 433 | int prior_delay = 0; |
| 434 | int timeout = (int)Timeout; |
| 435 | int i; |
| 436 | |
| 437 | for(i=1; i<count; i++){ |
| 438 | prior_delay += delay; |
| 439 | delay = delay*2; |
| 440 | if( delay>=1000000 ){ |
| 441 | delay = 1000000; |
| 442 | prior_delay += 1000000*(count - i - 1); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | if( prior_delay + delay > timeout*1000 ){ |
| 447 | delay = timeout*1000 - prior_delay; |
| 448 | if( delay<=0 ) return 0; |
| 449 | } |
| 450 | usleep(delay); |
| 451 | return 1; |
| 452 | #else |
| 453 | int timeout = (int)Timeout; |
| 454 | if( (count+1)*1000 > timeout ){ |
| 455 | return 0; |
| 456 | } |
| 457 | sleep(1); |
| 458 | return 1; |
| 459 | #endif |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | ** This routine sets the busy callback for an Sqlite database to the |
| 464 | ** given callback function with the given argument. |
| 465 | */ |
| 466 | void sqlite_busy_handler( |
| 467 | sqlite *db, |
| 468 | int (*xBusy)(void*,const char*,int), |
| 469 | void *pArg |
| 470 | ){ |
| 471 | db->xBusyCallback = xBusy; |
| 472 | db->pBusyArg = pArg; |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | ** This routine installs a default busy handler that waits for the |
| 477 | ** specified number of milliseconds before returning 0. |
| 478 | */ |
| 479 | void sqlite_busy_timeout(sqlite *db, int ms){ |
| 480 | if( ms>0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 481 | sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 482 | }else{ |
| 483 | sqlite_busy_handler(db, 0, 0); |
| 484 | } |
| 485 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | ** Cause any pending operation to stop at its earliest opportunity. |
| 489 | */ |
| 490 | void sqlite_interrupt(sqlite *db){ |
| 491 | db->flags |= SQLITE_Interrupt; |
| 492 | } |