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