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