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