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