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