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