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