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