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