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 | bb3a6db | 2002-06-21 11:55:48 +0000 | [diff] [blame] | 17 | ** $Id: main.c,v 1.82 2002/06/21 11:55:49 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 | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | ** This is the callback routine for the code that initializes the |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 25 | ** database. See sqliteInit() below for additional information. |
| 26 | ** |
| 27 | ** Each callback contains the following information: |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 28 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 29 | ** argv[0] = "file-format" or "schema-cookie" or "table" or "index" |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 30 | ** argv[1] = table or index name or meta statement type. |
| 31 | ** argv[2] = root page number for table or index. NULL for meta. |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 32 | ** argv[3] = SQL create statement for the table or index |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 33 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 34 | */ |
drh | e0140fc | 2002-06-16 18:21:44 +0000 | [diff] [blame] | 35 | int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 36 | sqlite *db = (sqlite*)pDb; |
| 37 | Parse sParse; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 38 | int nErr = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 39 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 40 | /* TODO: Do some validity checks on all fields. In particular, |
| 41 | ** make sure fields do not contain NULLs. Otherwise we might core |
| 42 | ** when attempting to initialize from a corrupt database file. */ |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 43 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 44 | assert( argc==4 ); |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 45 | switch( argv[0][0] ){ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 46 | case 'c': { /* Recommended pager cache size */ |
| 47 | int size = atoi(argv[3]); |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 48 | if( size==0 ){ size = MAX_PAGES; } |
| 49 | db->cache_size = size; |
| 50 | sqliteBtreeSetCacheSize(db->pBe, size); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 51 | break; |
| 52 | } |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 53 | case 'f': { /* File format */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 54 | /* |
| 55 | ** file_format==1 Version 2.1.0. |
| 56 | ** file_format==2 Version 2.2.0. Integer primary key. |
| 57 | ** file_format==3 Version 2.6.0. Separate text and numeric datatypes. |
| 58 | */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 59 | db->file_format = atoi(argv[3]); |
| 60 | break; |
| 61 | } |
| 62 | case 's': { /* Schema cookie */ |
| 63 | db->schema_cookie = atoi(argv[3]); |
| 64 | db->next_cookie = db->schema_cookie; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 65 | break; |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 66 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 67 | case 'v': |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 68 | case 'i': |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 69 | case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 70 | if( argv[3] && argv[3][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 71 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 72 | ** But because sParse.initFlag is set to 1, no VDBE code is generated |
| 73 | ** or executed. All the parser does is build the internal data |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 74 | ** structures that describe the table, index, or view. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 75 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 76 | memset(&sParse, 0, sizeof(sParse)); |
| 77 | sParse.db = db; |
| 78 | sParse.initFlag = 1; |
| 79 | sParse.newTnum = atoi(argv[2]); |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame] | 80 | sqliteRunParser(&sParse, argv[3], 0); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 81 | }else{ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 82 | /* If the SQL column is blank it means this is an index that |
| 83 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 84 | ** constraint for a CREATE TABLE. The index should have already |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 85 | ** been created when we processed the CREATE TABLE. All we have |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 86 | ** to do here is record the root page number for that index. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 87 | */ |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 88 | Index *pIndex = sqliteFindIndex(db, argv[1]); |
| 89 | if( pIndex==0 || pIndex->tnum!=0 ){ |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 90 | /* This can occur if there exists an index on a TEMP table which |
| 91 | ** has the same name as another index on a permanent index. Since |
| 92 | ** the permanent table is hidden by the TEMP table, we can also |
| 93 | ** safely ignore the index on the permanent table. |
| 94 | */ |
| 95 | /* Do Nothing */; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 96 | }else{ |
| 97 | pIndex->tnum = atoi(argv[2]); |
| 98 | } |
| 99 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 100 | break; |
| 101 | } |
| 102 | default: { |
| 103 | /* This can not happen! */ |
| 104 | nErr = 1; |
| 105 | assert( nErr==0 ); |
| 106 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 107 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 108 | return nErr; |
| 109 | } |
| 110 | |
| 111 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 112 | ** Attempt to read the database schema and initialize internal |
| 113 | ** data structures. Return one of the SQLITE_ error codes to |
| 114 | ** indicate success or failure. |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 115 | ** |
| 116 | ** After the database is initialized, the SQLITE_Initialized |
| 117 | ** bit is set in the flags field of the sqlite structure. An |
| 118 | ** attempt is made to initialize the database as soon as it |
| 119 | ** is opened. If that fails (perhaps because another process |
| 120 | ** has the sqlite_master table locked) than another attempt |
| 121 | ** is made the first time the database is accessed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 122 | */ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 123 | static int sqliteInit(sqlite *db, char **pzErrMsg){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 124 | Vdbe *vdbe; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 125 | int rc; |
| 126 | |
| 127 | /* |
| 128 | ** The master database table has a structure like this |
| 129 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 130 | static char master_schema[] = |
| 131 | "CREATE TABLE " MASTER_NAME " (\n" |
| 132 | " type text,\n" |
| 133 | " name text,\n" |
| 134 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 135 | " rootpage integer,\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 136 | " sql text\n" |
| 137 | ")" |
| 138 | ; |
| 139 | |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 140 | /* The following VDBE program is used to initialize the internal |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 141 | ** structure holding the tables and indexes of the database. |
| 142 | ** The database contains a special table named "sqlite_master" |
| 143 | ** defined as follows: |
| 144 | ** |
| 145 | ** CREATE TABLE sqlite_master ( |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 146 | ** type text, -- Either "table" or "index" or "meta" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 147 | ** name text, -- Name of table or index |
| 148 | ** tbl_name text, -- Associated table |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 149 | ** rootpage integer, -- The integer page number of root page |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 150 | ** sql text -- The CREATE statement for this object |
| 151 | ** ); |
| 152 | ** |
| 153 | ** The sqlite_master table contains a single entry for each table |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 154 | ** and each index. The "type" column tells whether the entry is |
| 155 | ** a table or index. The "name" column is the name of the object. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 156 | ** The "tbl_name" is the name of the associated table. For tables, |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 157 | ** the tbl_name column is always the same as name. For indices, the |
| 158 | ** tbl_name column contains the name of the table that the index |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 159 | ** indexes. The "rootpage" column holds the number of the root page |
| 160 | ** for the b-tree for the table or index. Finally, the "sql" column |
| 161 | ** contains the complete text of the CREATE TABLE or CREATE INDEX |
| 162 | ** statement that originally created the table or index. If an index |
| 163 | ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table, |
| 164 | ** then the "sql" column is NULL. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 165 | ** |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 166 | ** In format 1, entries in the sqlite_master table are in a random |
| 167 | ** order. Two passes must be made through the table to initialize |
| 168 | ** internal data structures. The first pass reads table definitions |
| 169 | ** and the second pass read index definitions. Having two passes |
| 170 | ** insures that indices appear after their tables. |
| 171 | ** |
| 172 | ** In format 2, entries appear in chronological order. Only a single |
| 173 | ** pass needs to be made through the table since everything will be |
| 174 | ** in the write order. VIEWs may only occur in format 2. |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 175 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 176 | ** The following program invokes its callback on the SQL for each |
| 177 | ** table then goes back and invokes the callback on the |
| 178 | ** SQL for each index. The callback will invoke the |
| 179 | ** parser to build the internal representation of the |
| 180 | ** database scheme. |
| 181 | */ |
| 182 | static VdbeOp initProg[] = { |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 183 | /* Send the file format to the callback routine |
| 184 | */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 185 | { OP_Open, 0, 2, 0}, |
| 186 | { OP_String, 0, 0, "file-format"}, |
| 187 | { OP_String, 0, 0, 0}, |
| 188 | { OP_String, 0, 0, 0}, |
| 189 | { OP_ReadCookie, 0, 1, 0}, |
| 190 | { OP_Callback, 4, 0, 0}, |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 191 | |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 192 | /* Send the recommended pager cache size to the callback routine |
| 193 | */ |
| 194 | { OP_String, 0, 0, "cache-size"}, |
| 195 | { OP_String, 0, 0, 0}, |
| 196 | { OP_String, 0, 0, 0}, |
| 197 | { OP_ReadCookie, 0, 2, 0}, |
| 198 | { OP_Callback, 4, 0, 0}, |
| 199 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 200 | /* Send the initial schema cookie to the callback |
| 201 | */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 202 | { OP_String, 0, 0, "schema_cookie"}, |
| 203 | { OP_String, 0, 0, 0}, |
| 204 | { OP_String, 0, 0, 0}, |
| 205 | { OP_ReadCookie, 0, 0, 0}, |
| 206 | { OP_Callback, 4, 0, 0}, |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 207 | |
| 208 | /* Check the file format. If the format number is 2 or more, |
| 209 | ** then do a single pass through the SQLITE_MASTER table. For |
| 210 | ** a format number of less than 2, jump forward to a different |
| 211 | ** algorithm that makes two passes through the SQLITE_MASTER table, |
| 212 | ** once for tables and a second time for indices. |
| 213 | */ |
| 214 | { OP_ReadCookie, 0, 1, 0}, |
| 215 | { OP_Integer, 2, 0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 216 | { OP_Lt, 0, 28, 0}, |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 217 | |
| 218 | /* This is the code for doing a single scan through the SQLITE_MASTER |
| 219 | ** table. This code runs for format 2 and greater. |
| 220 | */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 221 | { OP_Rewind, 0, 26, 0}, |
| 222 | { OP_Column, 0, 0, 0}, /* 20 */ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 223 | { OP_Column, 0, 1, 0}, |
| 224 | { OP_Column, 0, 3, 0}, |
| 225 | { OP_Column, 0, 4, 0}, |
| 226 | { OP_Callback, 4, 0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 227 | { OP_Next, 0, 20, 0}, |
| 228 | { OP_Close, 0, 0, 0}, /* 26 */ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 229 | { OP_Halt, 0, 0, 0}, |
| 230 | |
| 231 | /* This is the code for doing two passes through SQLITE_MASTER. This |
| 232 | ** code runs for file format 1. |
| 233 | */ |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 234 | { OP_Rewind, 0, 48, 0}, /* 28 */ |
| 235 | { OP_Column, 0, 0, 0}, /* 29 */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 236 | { OP_String, 0, 0, "table"}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 237 | { OP_Ne, 0, 37, 0}, |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 238 | { OP_Column, 0, 0, 0}, |
| 239 | { OP_Column, 0, 1, 0}, |
| 240 | { OP_Column, 0, 3, 0}, |
| 241 | { OP_Column, 0, 4, 0}, |
| 242 | { OP_Callback, 4, 0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 243 | { OP_Next, 0, 29, 0}, /* 37 */ |
| 244 | { OP_Rewind, 0, 48, 0}, /* 38 */ |
| 245 | { OP_Column, 0, 0, 0}, /* 39 */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 246 | { OP_String, 0, 0, "index"}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 247 | { OP_Ne, 0, 47, 0}, |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 248 | { OP_Column, 0, 0, 0}, |
| 249 | { OP_Column, 0, 1, 0}, |
| 250 | { OP_Column, 0, 3, 0}, |
| 251 | { OP_Column, 0, 4, 0}, |
| 252 | { OP_Callback, 4, 0, 0}, |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 253 | { OP_Next, 0, 39, 0}, /* 47 */ |
| 254 | { OP_Close, 0, 0, 0}, /* 48 */ |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 255 | { OP_Halt, 0, 0, 0}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 258 | /* Create a virtual machine to run the initialization program. Run |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 259 | ** the program. Then delete the virtual machine. |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 260 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 261 | vdbe = sqliteVdbeCreate(db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 262 | if( vdbe==0 ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 263 | sqliteSetString(pzErrMsg, "out of memory", 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 264 | return SQLITE_NOMEM; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 265 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 266 | sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg); |
drh | e0140fc | 2002-06-16 18:21:44 +0000 | [diff] [blame] | 267 | rc = sqliteVdbeExec(vdbe, sqliteInitCallback, db, pzErrMsg, |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 268 | db->pBusyArg, db->xBusyCallback); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 269 | sqliteVdbeDelete(vdbe); |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 270 | if( rc==SQLITE_OK && db->nTable==0 ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 271 | db->file_format = 2; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 272 | } |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 273 | if( rc==SQLITE_OK && db->file_format>2 ){ |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 274 | sqliteSetString(pzErrMsg, "unsupported file format", 0); |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 275 | rc = SQLITE_ERROR; |
| 276 | } |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 277 | |
| 278 | /* The schema for the SQLITE_MASTER table is not stored in the |
| 279 | ** database itself. We have to invoke the callback one extra |
| 280 | ** time to get it to process the SQLITE_MASTER table defintion. |
| 281 | */ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 282 | if( rc==SQLITE_OK ){ |
| 283 | Table *pTab; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 284 | char *azArg[6]; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 285 | azArg[0] = "table"; |
| 286 | azArg[1] = MASTER_NAME; |
| 287 | azArg[2] = "2"; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 288 | azArg[3] = master_schema; |
| 289 | azArg[4] = 0; |
drh | e0140fc | 2002-06-16 18:21:44 +0000 | [diff] [blame] | 290 | sqliteInitCallback(db, 4, azArg, 0); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 291 | pTab = sqliteFindTable(db, MASTER_NAME); |
| 292 | if( pTab ){ |
| 293 | pTab->readOnly = 1; |
| 294 | } |
| 295 | db->flags |= SQLITE_Initialized; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 296 | sqliteCommitInternalChanges(db); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 297 | } |
| 298 | return rc; |
| 299 | } |
| 300 | |
| 301 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 302 | ** The version of the library |
| 303 | */ |
drh | 3d0b559 | 2000-08-22 13:40:51 +0000 | [diff] [blame] | 304 | const char sqlite_version[] = SQLITE_VERSION; |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 305 | |
| 306 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 307 | ** Does the library expect data to be encoded as UTF-8 or iso8859? The |
| 308 | ** following global constant always lets us know. |
| 309 | */ |
| 310 | #ifdef SQLITE_UTF8 |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 311 | const char sqlite_encoding[] = "UTF-8"; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 312 | #else |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 313 | const char sqlite_encoding[] = "iso8859"; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 314 | #endif |
| 315 | |
| 316 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 317 | ** Open a new SQLite database. Construct an "sqlite" structure to define |
| 318 | ** the state of this database and return a pointer to that structure. |
| 319 | ** |
| 320 | ** An attempt is made to initialize the in-memory data structures that |
| 321 | ** hold the database schema. But if this fails (because the schema file |
| 322 | ** is locked) then that step is deferred until the first call to |
| 323 | ** sqlite_exec(). |
| 324 | */ |
| 325 | sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){ |
| 326 | sqlite *db; |
| 327 | int rc; |
| 328 | |
| 329 | /* Allocate the sqlite data structure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 330 | db = sqliteMalloc( sizeof(sqlite) ); |
| 331 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 332 | if( db==0 ) goto no_mem_on_open; |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 333 | sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0); |
| 334 | sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 335 | sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); |
| 336 | sqliteHashInit(&db->trigDrop, SQLITE_HASH_STRING, 0); |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame] | 337 | sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0); |
| 338 | sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 339 | sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1); |
drh | 28f4b68 | 2002-06-09 10:14:18 +0000 | [diff] [blame] | 340 | sqliteRegisterBuiltinFunctions(db); |
drh | 1c92853 | 2002-01-31 15:54:21 +0000 | [diff] [blame] | 341 | db->onError = OE_Default; |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 342 | db->priorNewRowid = 0; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 343 | db->magic = SQLITE_MAGIC_BUSY; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 344 | |
| 345 | /* Open the backend database driver */ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 346 | rc = sqliteBtreeOpen(zFilename, mode, MAX_PAGES, &db->pBe); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 347 | if( rc!=SQLITE_OK ){ |
| 348 | switch( rc ){ |
| 349 | default: { |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 350 | sqliteSetString(pzErrMsg, "unable to open database: ", zFilename, 0); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 353 | sqliteFree(db); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 354 | sqliteStrRealloc(pzErrMsg); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 355 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 358 | /* Attempt to read the schema */ |
| 359 | rc = sqliteInit(db, pzErrMsg); |
drh | c67980b | 2002-06-14 20:54:14 +0000 | [diff] [blame] | 360 | db->magic = SQLITE_MAGIC_OPEN; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 361 | if( sqlite_malloc_failed ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 362 | sqlite_close(db); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 363 | goto no_mem_on_open; |
| 364 | }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 365 | sqlite_close(db); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 366 | sqliteStrRealloc(pzErrMsg); |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 367 | return 0; |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 368 | }else if( pzErrMsg ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 369 | sqliteFree(*pzErrMsg); |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 370 | *pzErrMsg = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 372 | return db; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 373 | |
| 374 | no_mem_on_open: |
| 375 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 376 | sqliteStrRealloc(pzErrMsg); |
| 377 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 381 | ** Erase all schema information from the schema hash table. Except |
| 382 | ** tables that are created using CREATE TEMPORARY TABLE are preserved |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 383 | ** if the preserveTemps flag is true. |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 384 | ** |
| 385 | ** The database schema is normally read in once when the database |
| 386 | ** is first opened and stored in a hash table in the sqlite structure. |
| 387 | ** This routine erases the stored schema. This erasure occurs because |
| 388 | ** either the database is being closed or because some other process |
| 389 | ** changed the schema and this process needs to reread it. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 390 | */ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 391 | static void clearHashTable(sqlite *db, int preserveTemps){ |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 392 | HashElem *pElem; |
| 393 | Hash temp1; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 394 | Hash temp2; |
drh | e4697f5 | 2002-05-23 02:09:03 +0000 | [diff] [blame] | 395 | |
| 396 | /* Make sure there are no uncommited DROPs */ |
| 397 | assert( sqliteHashFirst(&db->tblDrop)==0 || sqlite_malloc_failed ); |
| 398 | assert( sqliteHashFirst(&db->idxDrop)==0 || sqlite_malloc_failed ); |
| 399 | assert( sqliteHashFirst(&db->trigDrop)==0 || sqlite_malloc_failed ); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 400 | temp1 = db->tblHash; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 401 | temp2 = db->trigHash; |
| 402 | sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0); |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 403 | sqliteHashClear(&db->idxHash); |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 404 | |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 405 | for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 406 | Trigger * pTrigger = sqliteHashData(pElem); |
| 407 | Table *pTab = sqliteFindTable(db, pTrigger->table); |
| 408 | assert(pTab); |
drh | 0b4efed | 2002-05-23 13:15:37 +0000 | [diff] [blame] | 409 | if( pTab->isTemp && preserveTemps ){ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 410 | sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name), |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 411 | pTrigger); |
danielk1977 | f29ce55 | 2002-05-19 23:43:12 +0000 | [diff] [blame] | 412 | }else{ |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 413 | sqliteDeleteTrigger(pTrigger); |
| 414 | } |
| 415 | } |
| 416 | sqliteHashClear(&temp2); |
| 417 | |
| 418 | sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0); |
| 419 | |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 420 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 421 | Table *pTab = sqliteHashData(pElem); |
| 422 | if( preserveTemps && pTab->isTemp ){ |
| 423 | Index *pIdx; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 424 | int nName = strlen(pTab->zName); |
| 425 | Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab); |
| 426 | if( pOld!=0 ){ |
| 427 | assert( pOld==pTab ); /* Malloc failed on the HashInsert */ |
| 428 | sqliteDeleteTable(db, pOld); |
| 429 | continue; |
| 430 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 431 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 432 | int n = strlen(pIdx->zName)+1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 433 | Index *pOldIdx; |
| 434 | pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx); |
| 435 | if( pOld ){ |
| 436 | assert( pOldIdx==pIdx ); |
| 437 | sqliteUnlinkAndDeleteIndex(db, pOldIdx); |
| 438 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 439 | } |
| 440 | }else{ |
| 441 | sqliteDeleteTable(db, pTab); |
| 442 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 443 | } |
drh | beae319 | 2001-09-22 18:12:08 +0000 | [diff] [blame] | 444 | sqliteHashClear(&temp1); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 445 | db->flags &= ~SQLITE_Initialized; |
| 446 | } |
| 447 | |
| 448 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 449 | ** Return the ROWID of the most recent insert |
| 450 | */ |
| 451 | int sqlite_last_insert_rowid(sqlite *db){ |
| 452 | return db->lastRowid; |
| 453 | } |
| 454 | |
| 455 | /* |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 456 | ** Return the number of changes in the most recent call to sqlite_exec(). |
| 457 | */ |
| 458 | int sqlite_changes(sqlite *db){ |
| 459 | return db->nChange; |
| 460 | } |
| 461 | |
| 462 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 463 | ** Close an existing SQLite database |
| 464 | */ |
| 465 | void sqlite_close(sqlite *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 466 | HashElem *i; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 467 | if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; } |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 468 | db->magic = SQLITE_MAGIC_CLOSED; |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 469 | sqliteBtreeClose(db->pBe); |
drh | bb3a6db | 2002-06-21 11:55:48 +0000 | [diff] [blame] | 470 | sqliteRollbackInternalChanges(db); |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 471 | clearHashTable(db, 0); |
| 472 | if( db->pBeTemp ){ |
| 473 | sqliteBtreeClose(db->pBeTemp); |
| 474 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 475 | for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 476 | FuncDef *pFunc, *pNext; |
| 477 | for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 478 | pNext = pFunc->pNext; |
| 479 | sqliteFree(pFunc); |
| 480 | } |
| 481 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 482 | sqliteHashClear(&db->aFunc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 483 | sqliteFree(db); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | ** Return TRUE if the given SQL string ends in a semicolon. |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 488 | ** |
| 489 | ** Special handling is require for CREATE TRIGGER statements. |
| 490 | ** Whenever the CREATE TRIGGER keywords are seen, the statement |
| 491 | ** must end with ";END;". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 492 | */ |
| 493 | int sqlite_complete(const char *zSql){ |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 494 | int isComplete = 1; |
| 495 | int requireEnd = 0; |
| 496 | int seenText = 0; |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 497 | int seenCreate = 0; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 498 | while( *zSql ){ |
| 499 | switch( *zSql ){ |
| 500 | case ';': { |
| 501 | isComplete = 1; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 502 | seenText = 1; |
| 503 | seenCreate = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 504 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 505 | } |
| 506 | case ' ': |
| 507 | case '\t': |
| 508 | case '\n': |
| 509 | case '\f': { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 510 | break; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 511 | } |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 512 | case '[': { |
| 513 | isComplete = 0; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 514 | seenText = 1; |
| 515 | seenCreate = 0; |
drh | 969fa7c | 2002-02-18 18:30:32 +0000 | [diff] [blame] | 516 | zSql++; |
| 517 | while( *zSql && *zSql!=']' ){ zSql++; } |
| 518 | if( *zSql==0 ) return 0; |
| 519 | break; |
| 520 | } |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 521 | case '"': |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 522 | case '\'': { |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 523 | int c = *zSql; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 524 | isComplete = 0; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 525 | seenText = 1; |
| 526 | seenCreate = 0; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 527 | zSql++; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 528 | while( *zSql && *zSql!=c ){ zSql++; } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 529 | if( *zSql==0 ) return 0; |
| 530 | break; |
| 531 | } |
| 532 | case '-': { |
| 533 | if( zSql[1]!='-' ){ |
| 534 | isComplete = 0; |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 535 | seenCreate = 0; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | while( *zSql && *zSql!='\n' ){ zSql++; } |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 539 | if( *zSql==0 ) return seenText && isComplete && requireEnd==0; |
| 540 | break; |
| 541 | } |
| 542 | case 'c': |
| 543 | case 'C': { |
| 544 | seenText = 1; |
| 545 | if( !isComplete ) break; |
| 546 | isComplete = 0; |
| 547 | if( sqliteStrNICmp(zSql, "create", 6)!=0 ) break; |
| 548 | if( !isspace(zSql[6]) ) break; |
| 549 | zSql += 5; |
| 550 | seenCreate = 1; |
| 551 | while( isspace(zSql[1]) ) zSql++; |
| 552 | if( sqliteStrNICmp(&zSql[1],"trigger", 7)!=0 ) break; |
| 553 | zSql += 7; |
| 554 | requireEnd++; |
| 555 | break; |
| 556 | } |
| 557 | case 't': |
| 558 | case 'T': { |
| 559 | seenText = 1; |
| 560 | if( !seenCreate ) break; |
| 561 | seenCreate = 0; |
| 562 | isComplete = 0; |
| 563 | if( sqliteStrNICmp(zSql, "trigger", 7)!=0 ) break; |
| 564 | if( !isspace(zSql[7]) ) break; |
| 565 | zSql += 6; |
| 566 | requireEnd++; |
| 567 | break; |
| 568 | } |
| 569 | case 'e': |
| 570 | case 'E': { |
| 571 | seenCreate = 0; |
| 572 | seenText = 1; |
| 573 | if( !isComplete ) break; |
| 574 | isComplete = 0; |
| 575 | if( requireEnd==0 ) break; |
| 576 | if( sqliteStrNICmp(zSql, "end", 3)!=0 ) break; |
| 577 | zSql += 2; |
| 578 | while( isspace(zSql[1]) ) zSql++; |
| 579 | if( zSql[1]==';' ){ |
| 580 | zSql++; |
| 581 | isComplete = 1; |
| 582 | requireEnd--; |
| 583 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 584 | break; |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 585 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 586 | default: { |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 587 | seenCreate = 0; |
| 588 | seenText = 1; |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 589 | isComplete = 0; |
| 590 | break; |
| 591 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 592 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 593 | zSql++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 594 | } |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 595 | return seenText && isComplete && requireEnd==0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 599 | ** Execute SQL code. Return one of the SQLITE_ success/failure |
| 600 | ** codes. Also write an error message into memory obtained from |
| 601 | ** malloc() and make *pzErrMsg point to that message. |
| 602 | ** |
| 603 | ** If the SQL is a query, then for each row in the query result |
| 604 | ** the xCallback() function is called. pArg becomes the first |
| 605 | ** argument to xCallback(). If xCallback=NULL then no callback |
| 606 | ** is invoked, even for queries. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 607 | */ |
| 608 | int sqlite_exec( |
| 609 | sqlite *db, /* The database on which the SQL executes */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 610 | const char *zSql, /* The SQL to be executed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 611 | sqlite_callback xCallback, /* Invoke this callback routine */ |
| 612 | void *pArg, /* First argument to xCallback() */ |
| 613 | char **pzErrMsg /* Write error messages here */ |
| 614 | ){ |
| 615 | Parse sParse; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 616 | |
| 617 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 618 | if( sqliteSafetyOn(db) ) goto exec_misuse; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 619 | if( (db->flags & SQLITE_Initialized)==0 ){ |
| 620 | int rc = sqliteInit(db, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 621 | if( rc!=SQLITE_OK ){ |
| 622 | sqliteStrRealloc(pzErrMsg); |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 623 | sqliteSafetyOff(db); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 624 | return rc; |
| 625 | } |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 626 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 627 | if( db->recursionDepth==0 ){ db->nChange = 0; } |
| 628 | db->recursionDepth++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 629 | memset(&sParse, 0, sizeof(sParse)); |
| 630 | sParse.db = db; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 631 | sParse.pBe = db->pBe; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 632 | sParse.xCallback = xCallback; |
| 633 | sParse.pArg = pArg; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 634 | sqliteRunParser(&sParse, zSql, pzErrMsg); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 635 | if( sqlite_malloc_failed ){ |
| 636 | sqliteSetString(pzErrMsg, "out of memory", 0); |
| 637 | sParse.rc = SQLITE_NOMEM; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 638 | sqliteBtreeRollback(db->pBe); |
| 639 | if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp); |
| 640 | db->flags &= ~SQLITE_InTrans; |
| 641 | clearHashTable(db, 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 642 | } |
| 643 | sqliteStrRealloc(pzErrMsg); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 644 | if( sParse.rc==SQLITE_SCHEMA ){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 645 | clearHashTable(db, 1); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 646 | } |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 647 | db->recursionDepth--; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 648 | if( sqliteSafetyOff(db) ) goto exec_misuse; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 649 | return sParse.rc; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 650 | |
| 651 | exec_misuse: |
| 652 | if( pzErrMsg ){ |
| 653 | *pzErrMsg = 0; |
| 654 | sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), 0); |
| 655 | sqliteStrRealloc(pzErrMsg); |
| 656 | } |
| 657 | return SQLITE_MISUSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 658 | } |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 659 | |
| 660 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 661 | ** Return a static string that describes the kind of error specified in the |
| 662 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 663 | */ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 664 | const char *sqlite_error_string(int rc){ |
| 665 | const char *z; |
| 666 | switch( rc ){ |
| 667 | case SQLITE_OK: z = "not an error"; break; |
| 668 | case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
| 669 | case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break; |
| 670 | case SQLITE_PERM: z = "access permission denied"; break; |
| 671 | case SQLITE_ABORT: z = "callback requested query abort"; break; |
| 672 | case SQLITE_BUSY: z = "database is locked"; break; |
| 673 | case SQLITE_LOCKED: z = "database table is locked"; break; |
| 674 | case SQLITE_NOMEM: z = "out of memory"; break; |
| 675 | case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
| 676 | case SQLITE_INTERRUPT: z = "interrupted"; break; |
| 677 | case SQLITE_IOERR: z = "disk I/O error"; break; |
| 678 | case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
| 679 | case SQLITE_NOTFOUND: z = "table or record not found"; break; |
| 680 | case SQLITE_FULL: z = "database is full"; break; |
| 681 | case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
| 682 | case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
| 683 | case SQLITE_EMPTY: z = "table contains no data"; break; |
| 684 | case SQLITE_SCHEMA: z = "database schema has changed"; break; |
| 685 | case SQLITE_TOOBIG: z = "too much data for one table row"; break; |
| 686 | case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
| 687 | case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
| 688 | case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
| 689 | default: z = "unknown error"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 690 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 691 | return z; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 695 | ** This routine implements a busy callback that sleeps and tries |
| 696 | ** again until a timeout value is reached. The timeout value is |
| 697 | ** an integer number of milliseconds passed in as the first |
| 698 | ** argument. |
| 699 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 700 | static int sqliteDefaultBusyCallback( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 701 | void *Timeout, /* Maximum amount of time to wait */ |
| 702 | const char *NotUsed, /* The name of the table that is busy */ |
| 703 | int count /* Number of times table has been busy */ |
| 704 | ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 705 | #if SQLITE_MIN_SLEEP_MS==1 |
| 706 | int delay = 10; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 707 | int prior_delay = 0; |
| 708 | int timeout = (int)Timeout; |
| 709 | int i; |
| 710 | |
| 711 | for(i=1; i<count; i++){ |
| 712 | prior_delay += delay; |
| 713 | delay = delay*2; |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 714 | if( delay>=1000 ){ |
| 715 | delay = 1000; |
| 716 | prior_delay += 1000*(count - i - 1); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | } |
drh | 3109e02 | 2001-10-09 13:46:01 +0000 | [diff] [blame] | 720 | if( prior_delay + delay > timeout ){ |
| 721 | delay = timeout - prior_delay; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 722 | if( delay<=0 ) return 0; |
| 723 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 724 | sqliteOsSleep(delay); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 725 | return 1; |
| 726 | #else |
| 727 | int timeout = (int)Timeout; |
| 728 | if( (count+1)*1000 > timeout ){ |
| 729 | return 0; |
| 730 | } |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 731 | sqliteOsSleep(1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 732 | return 1; |
| 733 | #endif |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | ** This routine sets the busy callback for an Sqlite database to the |
| 738 | ** given callback function with the given argument. |
| 739 | */ |
| 740 | void sqlite_busy_handler( |
| 741 | sqlite *db, |
| 742 | int (*xBusy)(void*,const char*,int), |
| 743 | void *pArg |
| 744 | ){ |
| 745 | db->xBusyCallback = xBusy; |
| 746 | db->pBusyArg = pArg; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | ** This routine installs a default busy handler that waits for the |
| 751 | ** specified number of milliseconds before returning 0. |
| 752 | */ |
| 753 | void sqlite_busy_timeout(sqlite *db, int ms){ |
| 754 | if( ms>0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 755 | sqlite_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 756 | }else{ |
| 757 | sqlite_busy_handler(db, 0, 0); |
| 758 | } |
| 759 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | ** Cause any pending operation to stop at its earliest opportunity. |
| 763 | */ |
| 764 | void sqlite_interrupt(sqlite *db){ |
| 765 | db->flags |= SQLITE_Interrupt; |
| 766 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | ** Windows systems should call this routine to free memory that |
| 770 | ** is returned in the in the errmsg parameter of sqlite_open() when |
| 771 | ** SQLite is a DLL. For some reason, it does not work to call free() |
| 772 | ** directly. |
| 773 | ** |
| 774 | ** Note that we need to call free() not sqliteFree() here, since every |
| 775 | ** string that is exported from SQLite should have already passed through |
| 776 | ** sqliteStrRealloc(). |
| 777 | */ |
| 778 | void sqlite_freemem(void *p){ free(p); } |
| 779 | |
| 780 | /* |
| 781 | ** Windows systems need functions to call to return the sqlite_version |
| 782 | ** and sqlite_encoding strings. |
| 783 | */ |
| 784 | const char *sqlite_libversion(void){ return sqlite_version; } |
| 785 | const char *sqlite_libencoding(void){ return sqlite_encoding; } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | ** Create new user-defined functions. The sqlite_create_function() |
| 789 | ** routine creates a regular function and sqlite_create_aggregate() |
| 790 | ** creates an aggregate function. |
| 791 | ** |
| 792 | ** Passing a NULL xFunc argument or NULL xStep and xFinalize arguments |
| 793 | ** disables the function. Calling sqlite_create_function() with the |
| 794 | ** same name and number of arguments as a prior call to |
| 795 | ** sqlite_create_aggregate() disables the prior call to |
| 796 | ** sqlite_create_aggregate(), and vice versa. |
| 797 | ** |
| 798 | ** If nArg is -1 it means that this function will accept any number |
| 799 | ** of arguments, including 0. |
| 800 | */ |
| 801 | int sqlite_create_function( |
| 802 | sqlite *db, /* Add the function to this database connection */ |
| 803 | const char *zName, /* Name of the function to add */ |
| 804 | int nArg, /* Number of arguments */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 805 | void (*xFunc)(sqlite_func*,int,const char**), /* The implementation */ |
| 806 | void *pUserData /* User data */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 807 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 808 | FuncDef *p; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 809 | if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 810 | p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1); |
drh | 4e0f995 | 2002-02-27 01:53:13 +0000 | [diff] [blame] | 811 | if( p==0 ) return 1; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 812 | p->xFunc = xFunc; |
| 813 | p->xStep = 0; |
| 814 | p->xFinalize = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 815 | p->pUserData = pUserData; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | int sqlite_create_aggregate( |
| 819 | sqlite *db, /* Add the function to this database connection */ |
| 820 | const char *zName, /* Name of the function to add */ |
| 821 | int nArg, /* Number of arguments */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 822 | void (*xStep)(sqlite_func*,int,const char**), /* The step function */ |
| 823 | void (*xFinalize)(sqlite_func*), /* The finalizer */ |
| 824 | void *pUserData /* User data */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 825 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 826 | FuncDef *p; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 827 | if( db==0 || zName==0 || sqliteSafetyCheck(db) ) return 1; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 828 | p = sqliteFindFunction(db, zName, strlen(zName), nArg, 1); |
drh | 4e0f995 | 2002-02-27 01:53:13 +0000 | [diff] [blame] | 829 | if( p==0 ) return 1; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 830 | p->xFunc = 0; |
| 831 | p->xStep = xStep; |
| 832 | p->xFinalize = xFinalize; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 833 | p->pUserData = pUserData; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 834 | return 0; |
| 835 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | ** Change the datatype for all functions with a given name. |
| 839 | */ |
| 840 | int sqlite_function_type(sqlite *db, const char *zName, int dataType){ |
| 841 | FuncDef *p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, strlen(zName)); |
| 842 | while( p ){ |
| 843 | p->dataType = dataType; |
| 844 | p = p->pNext; |
| 845 | } |
| 846 | } |