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