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