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