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 | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame^] | 17 | ** $Id: main.c,v 1.298 2005/08/14 01:20:39 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 | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 24 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 25 | ** SQLITE_LITTLEENDIAN macros. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 26 | */ |
| 27 | const int sqlite3one = 1; |
| 28 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 29 | #ifndef SQLITE_OMIT_GLOBALRECOVER |
| 30 | /* |
| 31 | ** Linked list of all open database handles. This is used by the |
| 32 | ** sqlite3_global_recover() function. Entries are added to the list |
| 33 | ** by openDatabase() and removed by sqlite3_close(). |
| 34 | */ |
| 35 | static sqlite3 *pDbList = 0; |
| 36 | #endif |
| 37 | |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 38 | #ifndef SQLITE_OMIT_UTF16 |
| 39 | /* |
| 40 | ** Return the transient sqlite3_value object used for encoding conversions |
| 41 | ** during SQL compilation. |
| 42 | */ |
| 43 | sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){ |
| 44 | if( !db->pValue ){ |
| 45 | db->pValue = sqlite3ValueNew(); |
| 46 | } |
| 47 | return db->pValue; |
| 48 | } |
| 49 | #endif |
| 50 | |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 51 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 52 | ** The version of the library |
| 53 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 54 | const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $"; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 55 | const char sqlite3_version[] = SQLITE_VERSION; |
drh | 4aec8b6 | 2004-08-28 16:19:00 +0000 | [diff] [blame] | 56 | const char *sqlite3_libversion(void){ return sqlite3_version; } |
danielk1977 | 99ba19e | 2005-02-05 07:33:34 +0000 | [diff] [blame] | 57 | int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 60 | ** This is the default collating function named "BINARY" which is always |
| 61 | ** available. |
| 62 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 63 | static int binCollFunc( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 64 | void *NotUsed, |
| 65 | int nKey1, const void *pKey1, |
| 66 | int nKey2, const void *pKey2 |
| 67 | ){ |
| 68 | int rc, n; |
| 69 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 70 | rc = memcmp(pKey1, pKey2, n); |
| 71 | if( rc==0 ){ |
| 72 | rc = nKey1 - nKey2; |
| 73 | } |
| 74 | return rc; |
| 75 | } |
| 76 | |
| 77 | /* |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 78 | ** Another built-in collating sequence: NOCASE. |
| 79 | ** |
| 80 | ** This collating sequence is intended to be used for "case independant |
| 81 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 82 | ** extends only to the 26 characters used in the English language. |
| 83 | ** |
| 84 | ** At the moment there is only a UTF-8 implementation. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 85 | */ |
| 86 | static int nocaseCollatingFunc( |
| 87 | void *NotUsed, |
| 88 | int nKey1, const void *pKey1, |
| 89 | int nKey2, const void *pKey2 |
| 90 | ){ |
| 91 | int r = sqlite3StrNICmp( |
drh | 208f80a | 2004-08-29 20:08:58 +0000 | [diff] [blame] | 92 | (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 93 | if( 0==r ){ |
| 94 | r = nKey1-nKey2; |
| 95 | } |
| 96 | return r; |
| 97 | } |
| 98 | |
| 99 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 100 | ** Return the ROWID of the most recent insert |
| 101 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 102 | sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 103 | return db->lastRowid; |
| 104 | } |
| 105 | |
| 106 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 107 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 108 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 109 | int sqlite3_changes(sqlite3 *db){ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 110 | return db->nChange; |
| 111 | } |
| 112 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 113 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 114 | ** Return the number of changes since the database handle was opened. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 115 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 116 | int sqlite3_total_changes(sqlite3 *db){ |
| 117 | return db->nTotalChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 118 | } |
| 119 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 120 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 121 | ** Close an existing SQLite database |
| 122 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 123 | int sqlite3_close(sqlite3 *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 124 | HashElem *i; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 125 | int j; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 126 | |
| 127 | if( !db ){ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 128 | return SQLITE_OK; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 129 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 130 | if( sqlite3SafetyCheck(db) ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 131 | return SQLITE_MISUSE; |
| 132 | } |
| 133 | |
danielk1977 | 1f723bd | 2005-05-26 12:37:29 +0000 | [diff] [blame] | 134 | #ifdef SQLITE_SSE |
| 135 | sqlite3_finalize(db->pFetch); |
| 136 | #endif |
| 137 | |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 138 | /* If there are any outstanding VMs, return SQLITE_BUSY. */ |
| 139 | if( db->pVdbe ){ |
| 140 | sqlite3Error(db, SQLITE_BUSY, |
| 141 | "Unable to close due to unfinalised statements"); |
| 142 | return SQLITE_BUSY; |
| 143 | } |
| 144 | assert( !sqlite3SafetyCheck(db) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 145 | |
| 146 | /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database |
| 147 | ** cannot be opened for some reason. So this routine needs to run in |
| 148 | ** that case. But maybe there should be an extra magic value for the |
| 149 | ** "failed to open" state. |
| 150 | */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 151 | if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 152 | /* printf("DID NOT CLOSE\n"); fflush(stdout); */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 153 | return SQLITE_ERROR; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 154 | } |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 155 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 156 | for(j=0; j<db->nDb; j++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 157 | struct Db *pDb = &db->aDb[j]; |
| 158 | if( pDb->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 159 | sqlite3BtreeClose(pDb->pBt); |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 160 | pDb->pBt = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 161 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 162 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 163 | sqlite3ResetInternalSchema(db, 0); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 164 | assert( db->nDb<=2 ); |
| 165 | assert( db->aDb==db->aDbStatic ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 166 | for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 167 | FuncDef *pFunc, *pNext; |
| 168 | for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 169 | pNext = pFunc->pNext; |
| 170 | sqliteFree(pFunc); |
| 171 | } |
| 172 | } |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 173 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 174 | for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 175 | CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 176 | sqliteFree(pColl); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 177 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 178 | sqlite3HashClear(&db->aCollSeq); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 179 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 180 | sqlite3HashClear(&db->aFunc); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 181 | sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 182 | if( db->pValue ){ |
| 183 | sqlite3ValueFree(db->pValue); |
| 184 | } |
| 185 | if( db->pErr ){ |
| 186 | sqlite3ValueFree(db->pErr); |
| 187 | } |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 188 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 189 | #ifndef SQLITE_OMIT_GLOBALRECOVER |
| 190 | { |
danielk1977 | 3eb8db9 | 2005-03-29 23:34:58 +0000 | [diff] [blame] | 191 | sqlite3 *pPrev; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 192 | sqlite3OsEnterMutex(); |
danielk1977 | 3eb8db9 | 2005-03-29 23:34:58 +0000 | [diff] [blame] | 193 | pPrev = pDbList; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 194 | while( pPrev && pPrev->pNext!=db ){ |
| 195 | pPrev = pPrev->pNext; |
| 196 | } |
| 197 | if( pPrev ){ |
| 198 | pPrev->pNext = db->pNext; |
| 199 | }else{ |
| 200 | assert( pDbList==db ); |
| 201 | pDbList = db->pNext; |
| 202 | } |
| 203 | sqlite3OsLeaveMutex(); |
| 204 | } |
| 205 | #endif |
| 206 | |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 207 | db->magic = SQLITE_MAGIC_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 208 | sqliteFree(db); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 209 | return SQLITE_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 213 | ** Rollback all database files. |
| 214 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 215 | void sqlite3RollbackAll(sqlite3 *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 216 | int i; |
| 217 | for(i=0; i<db->nDb; i++){ |
| 218 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 219 | sqlite3BtreeRollback(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 220 | db->aDb[i].inTrans = 0; |
| 221 | } |
| 222 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 223 | sqlite3ResetInternalSchema(db, 0); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 227 | ** Return a static string that describes the kind of error specified in the |
| 228 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 229 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 230 | const char *sqlite3ErrStr(int rc){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 231 | const char *z; |
| 232 | switch( rc ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 233 | case SQLITE_ROW: |
| 234 | case SQLITE_DONE: |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 235 | case SQLITE_OK: z = "not an error"; break; |
| 236 | case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 237 | case SQLITE_PERM: z = "access permission denied"; break; |
| 238 | case SQLITE_ABORT: z = "callback requested query abort"; break; |
| 239 | case SQLITE_BUSY: z = "database is locked"; break; |
| 240 | case SQLITE_LOCKED: z = "database table is locked"; break; |
| 241 | case SQLITE_NOMEM: z = "out of memory"; break; |
| 242 | case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
| 243 | case SQLITE_INTERRUPT: z = "interrupted"; break; |
| 244 | case SQLITE_IOERR: z = "disk I/O error"; break; |
| 245 | case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
drh | 2db0bbc | 2005-08-11 02:10:18 +0000 | [diff] [blame] | 246 | case SQLITE_FULL: z = "database or disk is full"; break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 247 | case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
| 248 | case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
| 249 | case SQLITE_EMPTY: z = "table contains no data"; break; |
| 250 | case SQLITE_SCHEMA: z = "database schema has changed"; break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 251 | case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
| 252 | case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
| 253 | case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 254 | case SQLITE_NOLFS: z = "kernel lacks large file support"; break; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 255 | case SQLITE_AUTH: z = "authorization denied"; break; |
jplyon | 892f671 | 2003-06-12 08:59:00 +0000 | [diff] [blame] | 256 | case SQLITE_FORMAT: z = "auxiliary database format error"; break; |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 257 | case SQLITE_RANGE: z = "bind or column index out of range"; break; |
drh | c602f9a | 2004-02-12 19:01:04 +0000 | [diff] [blame] | 258 | case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 259 | default: z = "unknown error"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 260 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 261 | return z; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 265 | ** This routine implements a busy callback that sleeps and tries |
| 266 | ** again until a timeout value is reached. The timeout value is |
| 267 | ** an integer number of milliseconds passed in as the first |
| 268 | ** argument. |
| 269 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 270 | static int sqliteDefaultBusyCallback( |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 271 | void *ptr, /* Database connection */ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 272 | int count /* Number of times table has been busy */ |
| 273 | ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 274 | #if SQLITE_MIN_SLEEP_MS==1 |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 275 | static const u8 delays[] = |
| 276 | { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; |
| 277 | static const u8 totals[] = |
| 278 | { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 279 | # define NDELAY (sizeof(delays)/sizeof(delays[0])) |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 280 | int timeout = ((sqlite3 *)ptr)->busyTimeout; |
| 281 | int delay, prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 282 | |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 283 | assert( count>=0 ); |
| 284 | if( count < NDELAY ){ |
| 285 | delay = delays[count]; |
| 286 | prior = totals[count]; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 287 | }else{ |
| 288 | delay = delays[NDELAY-1]; |
drh | 68cb619 | 2005-05-06 22:05:56 +0000 | [diff] [blame] | 289 | prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 290 | } |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 291 | if( prior + delay > timeout ){ |
| 292 | delay = timeout - prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 293 | if( delay<=0 ) return 0; |
| 294 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 295 | sqlite3OsSleep(delay); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 296 | return 1; |
| 297 | #else |
drh | 3f73708 | 2005-06-14 02:24:31 +0000 | [diff] [blame] | 298 | int timeout = ((sqlite3 *)ptr)->busyTimeout; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 299 | if( (count+1)*1000 > timeout ){ |
| 300 | return 0; |
| 301 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 302 | sqlite3OsSleep(1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 303 | return 1; |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | /* |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 308 | ** Invoke the given busy handler. |
| 309 | ** |
| 310 | ** This routine is called when an operation failed with a lock. |
| 311 | ** If this routine returns non-zero, the lock is retried. If it |
| 312 | ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 313 | */ |
| 314 | int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 315 | int rc; |
| 316 | if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0; |
| 317 | rc = p->xFunc(p->pArg, p->nBusy); |
| 318 | if( rc==0 ){ |
| 319 | p->nBusy = -1; |
| 320 | }else{ |
| 321 | p->nBusy++; |
| 322 | } |
| 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 327 | ** This routine sets the busy callback for an Sqlite database to the |
| 328 | ** given callback function with the given argument. |
| 329 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 330 | int sqlite3_busy_handler( |
| 331 | sqlite3 *db, |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 332 | int (*xBusy)(void*,int), |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 333 | void *pArg |
| 334 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 335 | if( sqlite3SafetyCheck(db) ){ |
| 336 | return SQLITE_MISUSE; |
| 337 | } |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 338 | db->busyHandler.xFunc = xBusy; |
| 339 | db->busyHandler.pArg = pArg; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 340 | db->busyHandler.nBusy = 0; |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 341 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 342 | } |
| 343 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 344 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 345 | /* |
| 346 | ** This routine sets the progress callback for an Sqlite database to the |
| 347 | ** given callback function with the given argument. The progress callback will |
| 348 | ** be invoked every nOps opcodes. |
| 349 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 350 | void sqlite3_progress_handler( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 351 | sqlite3 *db, |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 352 | int nOps, |
| 353 | int (*xProgress)(void*), |
| 354 | void *pArg |
| 355 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 356 | if( !sqlite3SafetyCheck(db) ){ |
| 357 | if( nOps>0 ){ |
| 358 | db->xProgress = xProgress; |
| 359 | db->nProgressOps = nOps; |
| 360 | db->pProgressArg = pArg; |
| 361 | }else{ |
| 362 | db->xProgress = 0; |
| 363 | db->nProgressOps = 0; |
| 364 | db->pProgressArg = 0; |
| 365 | } |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 371 | /* |
| 372 | ** This routine installs a default busy handler that waits for the |
| 373 | ** specified number of milliseconds before returning 0. |
| 374 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 375 | int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 376 | if( ms>0 ){ |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 377 | db->busyTimeout = ms; |
| 378 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 379 | }else{ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 380 | sqlite3_busy_handler(db, 0, 0); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 381 | } |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 382 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 383 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | ** Cause any pending operation to stop at its earliest opportunity. |
| 387 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 388 | void sqlite3_interrupt(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 389 | if( !sqlite3SafetyCheck(db) ){ |
| 390 | db->flags |= SQLITE_Interrupt; |
| 391 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 392 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | ** Windows systems should call this routine to free memory that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 396 | ** is returned in the in the errmsg parameter of sqlite3_open() when |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 397 | ** SQLite is a DLL. For some reason, it does not work to call free() |
| 398 | ** directly. |
| 399 | ** |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 400 | ** Note that we need to call free() not sqliteFree() here. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 401 | */ |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 402 | void sqlite3_free(char *p){ free(p); } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 403 | |
| 404 | /* |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 405 | ** Create new user functions. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 406 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 407 | int sqlite3_create_function( |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 408 | sqlite3 *db, |
| 409 | const char *zFunctionName, |
| 410 | int nArg, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 411 | int enc, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 412 | void *pUserData, |
| 413 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 414 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 415 | void (*xFinal)(sqlite3_context*) |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 416 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 417 | FuncDef *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 418 | int nName; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 419 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 420 | if( sqlite3SafetyCheck(db) ){ |
| 421 | return SQLITE_MISUSE; |
| 422 | } |
| 423 | if( zFunctionName==0 || |
danielk1977 | 398eae7 | 2004-05-26 06:58:43 +0000 | [diff] [blame] | 424 | (xFunc && (xFinal || xStep)) || |
| 425 | (!xFunc && (xFinal && !xStep)) || |
| 426 | (!xFunc && (!xFinal && xStep)) || |
| 427 | (nArg<-1 || nArg>127) || |
| 428 | (255<(nName = strlen(zFunctionName))) ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 429 | return SQLITE_ERROR; |
| 430 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 431 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 432 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 433 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 434 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 435 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 436 | ** |
| 437 | ** If SQLITE_ANY is specified, add three versions of the function |
| 438 | ** to the hash table. |
| 439 | */ |
| 440 | if( enc==SQLITE_UTF16 ){ |
| 441 | enc = SQLITE_UTF16NATIVE; |
| 442 | }else if( enc==SQLITE_ANY ){ |
| 443 | int rc; |
| 444 | rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 445 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 446 | if( rc!=SQLITE_OK ) return rc; |
| 447 | rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 448 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 449 | if( rc!=SQLITE_OK ) return rc; |
| 450 | enc = SQLITE_UTF16BE; |
| 451 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 452 | #else |
| 453 | enc = SQLITE_UTF8; |
| 454 | #endif |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 455 | |
| 456 | /* Check if an existing function is being overridden or deleted. If so, |
| 457 | ** and there are active VMs, then return SQLITE_BUSY. If a function |
| 458 | ** is being overridden/deleted but there are no active VMs, allow the |
| 459 | ** operation to continue but invalidate all precompiled statements. |
| 460 | */ |
| 461 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0); |
| 462 | if( p && p->iPrefEnc==enc && p->nArg==nArg ){ |
| 463 | if( db->activeVdbeCnt ){ |
| 464 | sqlite3Error(db, SQLITE_BUSY, |
| 465 | "Unable to delete/modify user-function due to active statements"); |
| 466 | return SQLITE_BUSY; |
| 467 | }else{ |
| 468 | sqlite3ExpirePreparedStatements(db); |
| 469 | } |
| 470 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 471 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 472 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1); |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 473 | if( p==0 ) return SQLITE_NOMEM; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame^] | 474 | p->flags = 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 475 | p->xFunc = xFunc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 476 | p->xStep = xStep; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 477 | p->xFinalize = xFinal; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 478 | p->pUserData = pUserData; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 479 | return SQLITE_OK; |
| 480 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 481 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 482 | int sqlite3_create_function16( |
| 483 | sqlite3 *db, |
| 484 | const void *zFunctionName, |
| 485 | int nArg, |
| 486 | int eTextRep, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 487 | void *pUserData, |
| 488 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 489 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 490 | void (*xFinal)(sqlite3_context*) |
| 491 | ){ |
| 492 | int rc; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 493 | char const *zFunc8; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 494 | sqlite3_value *pTmp; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 495 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 496 | if( sqlite3SafetyCheck(db) ){ |
| 497 | return SQLITE_MISUSE; |
| 498 | } |
| 499 | pTmp = sqlite3GetTransientValue(db); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 500 | sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC); |
| 501 | zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
| 502 | |
| 503 | if( !zFunc8 ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 504 | return SQLITE_NOMEM; |
| 505 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 506 | rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 507 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 508 | return rc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 509 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 510 | #endif |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 511 | |
| 512 | /* |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 513 | ** Register a trace function. The pArg from the previously registered trace |
| 514 | ** is returned. |
| 515 | ** |
| 516 | ** A NULL trace function means that no tracing is executes. A non-NULL |
| 517 | ** trace is a pointer to a function that is invoked at the start of each |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 518 | ** sqlite3_exec(). |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 519 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 520 | void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 521 | void *pOld = db->pTraceArg; |
| 522 | db->xTrace = xTrace; |
| 523 | db->pTraceArg = pArg; |
| 524 | return pOld; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 525 | } |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 526 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 527 | /*** EXPERIMENTAL *** |
| 528 | ** |
| 529 | ** Register a function to be invoked when a transaction comments. |
| 530 | ** If either function returns non-zero, then the commit becomes a |
| 531 | ** rollback. |
| 532 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 533 | void *sqlite3_commit_hook( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 534 | sqlite3 *db, /* Attach the hook to this database */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 535 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 536 | void *pArg /* Argument to the function */ |
| 537 | ){ |
| 538 | void *pOld = db->pCommitArg; |
| 539 | db->xCommitCallback = xCallback; |
| 540 | db->pCommitArg = pArg; |
| 541 | return pOld; |
| 542 | } |
| 543 | |
| 544 | |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 545 | /* |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 546 | ** This routine is called to create a connection to a database BTree |
| 547 | ** driver. If zFilename is the name of a file, then that file is |
| 548 | ** opened and used. If zFilename is the magic name ":memory:" then |
| 549 | ** the database is stored in memory (and is thus forgotten as soon as |
| 550 | ** the connection is closed.) If zFilename is NULL then the database |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 551 | ** is a "virtual" database for transient use only and is deleted as |
| 552 | ** soon as the connection is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 553 | ** |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 554 | ** A virtual database can be either a disk file (that is automatically |
| 555 | ** deleted when the file is closed) or it an be held entirely in memory, |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 556 | ** depending on the values of the TEMP_STORE compile-time macro and the |
| 557 | ** db->temp_store variable, according to the following chart: |
| 558 | ** |
| 559 | ** TEMP_STORE db->temp_store Location of temporary database |
| 560 | ** ---------- -------------- ------------------------------ |
| 561 | ** 0 any file |
| 562 | ** 1 1 file |
| 563 | ** 1 2 memory |
| 564 | ** 1 0 file |
| 565 | ** 2 1 file |
| 566 | ** 2 2 memory |
| 567 | ** 2 0 memory |
| 568 | ** 3 any memory |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 569 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 570 | int sqlite3BtreeFactory( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 571 | const sqlite3 *db, /* Main database when opening aux otherwise 0 */ |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 572 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 573 | int omitJournal, /* if TRUE then do not journal this file */ |
| 574 | int nCache, /* How many pages in the page cache */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 575 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 576 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 577 | int btree_flags = 0; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 578 | int rc; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 579 | |
drh | eec983e | 2004-05-08 10:11:36 +0000 | [diff] [blame] | 580 | assert( ppBtree != 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 581 | if( omitJournal ){ |
| 582 | btree_flags |= BTREE_OMIT_JOURNAL; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 583 | } |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 584 | if( db->flags & SQLITE_NoReadlock ){ |
| 585 | btree_flags |= BTREE_NO_READLOCK; |
| 586 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 587 | if( zFilename==0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 588 | #if TEMP_STORE==0 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 589 | /* Do nothing */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 590 | #endif |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 591 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 592 | #if TEMP_STORE==1 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 593 | if( db->temp_store==2 ) zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 594 | #endif |
| 595 | #if TEMP_STORE==2 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 596 | if( db->temp_store!=1 ) zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 597 | #endif |
| 598 | #if TEMP_STORE==3 |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 599 | zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 600 | #endif |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 601 | #endif /* SQLITE_OMIT_MEMORYDB */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 602 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 603 | |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 604 | rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags); |
| 605 | if( rc==SQLITE_OK ){ |
| 606 | sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler); |
| 607 | sqlite3BtreeSetCacheSize(*ppBtree, nCache); |
| 608 | } |
| 609 | return rc; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 610 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 611 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 612 | /* |
| 613 | ** Return UTF-8 encoded English language explanation of the most recent |
| 614 | ** error. |
| 615 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 616 | const char *sqlite3_errmsg(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 617 | const char *z; |
| 618 | if( sqlite3_malloc_failed ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 619 | return sqlite3ErrStr(SQLITE_NOMEM); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 620 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 621 | if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 622 | return sqlite3ErrStr(SQLITE_MISUSE); |
| 623 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 624 | z = sqlite3_value_text(db->pErr); |
| 625 | if( z==0 ){ |
| 626 | z = sqlite3ErrStr(db->errCode); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 627 | } |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 628 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 629 | } |
| 630 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 631 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 632 | /* |
| 633 | ** Return UTF-16 encoded English language explanation of the most recent |
| 634 | ** error. |
| 635 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 636 | const void *sqlite3_errmsg16(sqlite3 *db){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 637 | /* Because all the characters in the string are in the unicode |
| 638 | ** range 0x00-0xFF, if we pad the big-endian string with a |
| 639 | ** zero byte, we can obtain the little-endian string with |
| 640 | ** &big_endian[1]. |
| 641 | */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 642 | static const char outOfMemBe[] = { |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 643 | 0, 'o', 0, 'u', 0, 't', 0, ' ', |
| 644 | 0, 'o', 0, 'f', 0, ' ', |
| 645 | 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0 |
| 646 | }; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 647 | static const char misuseBe [] = { |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 648 | 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ', |
| 649 | 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ', |
| 650 | 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', |
| 651 | 0, 'o', 0, 'u', 0, 't', 0, ' ', |
| 652 | 0, 'o', 0, 'f', 0, ' ', |
| 653 | 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0 |
| 654 | }; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 655 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 656 | const void *z; |
| 657 | if( sqlite3_malloc_failed ){ |
| 658 | return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
| 659 | } |
| 660 | if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ |
| 661 | return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
| 662 | } |
| 663 | z = sqlite3_value_text16(db->pErr); |
| 664 | if( z==0 ){ |
| 665 | sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), |
| 666 | SQLITE_UTF8, SQLITE_STATIC); |
| 667 | z = sqlite3_value_text16(db->pErr); |
| 668 | } |
| 669 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 670 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 671 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 672 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 673 | /* |
| 674 | ** Return the most recent error code generated by an SQLite routine. |
| 675 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 676 | int sqlite3_errcode(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 677 | if( sqlite3_malloc_failed ){ |
| 678 | return SQLITE_NOMEM; |
| 679 | } |
| 680 | if( sqlite3SafetyCheck(db) ){ |
| 681 | return SQLITE_MISUSE; |
| 682 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 683 | return db->errCode; |
| 684 | } |
| 685 | |
| 686 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 687 | ** This routine does the work of opening a database on behalf of |
| 688 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 689 | ** is UTF-8 encoded. |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 690 | */ |
| 691 | static int openDatabase( |
| 692 | const char *zFilename, /* Database filename UTF-8 encoded */ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 693 | sqlite3 **ppDb /* OUT: Returned database handle */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 694 | ){ |
| 695 | sqlite3 *db; |
| 696 | int rc, i; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 697 | |
| 698 | /* Allocate the sqlite data structure */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 699 | db = sqliteMalloc( sizeof(sqlite3) ); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 700 | if( db==0 ) goto opendb_out; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 701 | db->priorNewRowid = 0; |
| 702 | db->magic = SQLITE_MAGIC_BUSY; |
| 703 | db->nDb = 2; |
| 704 | db->aDb = db->aDbStatic; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 705 | db->enc = SQLITE_UTF8; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 706 | db->autoCommit = 1; |
drh | 47a6db2 | 2005-01-18 16:02:40 +0000 | [diff] [blame] | 707 | db->flags |= SQLITE_ShortColNames; |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 708 | sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 709 | sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0); |
| 710 | for(i=0; i<db->nDb; i++){ |
| 711 | sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0); |
| 712 | sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0); |
| 713 | sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0); |
| 714 | sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1); |
| 715 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 716 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 717 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 718 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 719 | ** conversions. The only error that can occur here is a malloc() failure. |
| 720 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 721 | if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) || |
| 722 | sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) || |
| 723 | !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 724 | rc = db->errCode; |
| 725 | assert( rc!=SQLITE_OK ); |
| 726 | db->magic = SQLITE_MAGIC_CLOSED; |
| 727 | goto opendb_out; |
| 728 | } |
| 729 | |
| 730 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 731 | sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 732 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 733 | /* Open the backend database driver */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 734 | rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); |
| 735 | if( rc!=SQLITE_OK ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 736 | sqlite3Error(db, rc, 0); |
| 737 | db->magic = SQLITE_MAGIC_CLOSED; |
| 738 | goto opendb_out; |
| 739 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 740 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 741 | /* The default safety_level for the main database is 'full'; for the temp |
| 742 | ** database it is 'NONE'. This matches the pager layer defaults. |
| 743 | */ |
| 744 | db->aDb[0].zName = "main"; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 745 | db->aDb[0].safety_level = 3; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 746 | #ifndef SQLITE_OMIT_TEMPDB |
| 747 | db->aDb[1].zName = "temp"; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 748 | db->aDb[1].safety_level = 1; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 749 | #endif |
| 750 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 751 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 752 | /* Register all built-in functions, but do not attempt to read the |
| 753 | ** database schema yet. This is delayed until the first time the database |
| 754 | ** is accessed. |
| 755 | */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 756 | sqlite3RegisterBuiltinFunctions(db); |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 757 | sqlite3Error(db, SQLITE_OK, 0); |
| 758 | db->magic = SQLITE_MAGIC_OPEN; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 759 | |
| 760 | opendb_out: |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 761 | if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){ |
| 762 | sqlite3Error(db, SQLITE_NOMEM, 0); |
| 763 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 764 | *ppDb = db; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 765 | #ifndef SQLITE_OMIT_GLOBALRECOVER |
| 766 | if( db ){ |
| 767 | sqlite3OsEnterMutex(); |
| 768 | db->pNext = pDbList; |
| 769 | pDbList = db; |
| 770 | sqlite3OsLeaveMutex(); |
| 771 | } |
| 772 | #endif |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 773 | return sqlite3_errcode(db); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | ** Open a new database handle. |
| 778 | */ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 779 | int sqlite3_open( |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 780 | const char *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 781 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 782 | ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 783 | return openDatabase(zFilename, ppDb); |
danielk1977 | 83ab5a8 | 2004-05-21 11:39:05 +0000 | [diff] [blame] | 784 | } |
| 785 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 786 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 787 | /* |
| 788 | ** Open a new database handle. |
| 789 | */ |
| 790 | int sqlite3_open16( |
| 791 | const void *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 792 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 793 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 794 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 795 | int rc = SQLITE_NOMEM; |
| 796 | sqlite3_value *pVal; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 797 | |
| 798 | assert( ppDb ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 799 | *ppDb = 0; |
| 800 | pVal = sqlite3ValueNew(); |
| 801 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 802 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 803 | if( zFilename8 ){ |
| 804 | rc = openDatabase(zFilename8, ppDb); |
| 805 | if( rc==SQLITE_OK && *ppDb ){ |
| 806 | sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); |
| 807 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 808 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 809 | if( pVal ){ |
| 810 | sqlite3ValueFree(pVal); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 811 | } |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 812 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 813 | return rc; |
| 814 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 815 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 816 | |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 817 | /* |
| 818 | ** The following routine destroys a virtual machine that is created by |
| 819 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 820 | ** success/failure code that describes the result of executing the virtual |
| 821 | ** machine. |
| 822 | ** |
| 823 | ** This routine sets the error code and string returned by |
| 824 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 825 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 826 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 827 | int rc; |
| 828 | if( pStmt==0 ){ |
| 829 | rc = SQLITE_OK; |
| 830 | }else{ |
| 831 | rc = sqlite3VdbeFinalize((Vdbe*)pStmt); |
| 832 | } |
| 833 | return rc; |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* |
| 837 | ** Terminate the current execution of an SQL statement and reset it |
| 838 | ** back to its starting state so that it can be reused. A success code from |
| 839 | ** the prior execution is returned. |
| 840 | ** |
| 841 | ** This routine sets the error code and string returned by |
| 842 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 843 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 844 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 845 | int rc; |
| 846 | if( pStmt==0 ){ |
| 847 | rc = SQLITE_OK; |
| 848 | }else{ |
| 849 | rc = sqlite3VdbeReset((Vdbe*)pStmt); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 850 | sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0); |
drh | 1bcdb0c | 2004-08-28 14:49:46 +0000 | [diff] [blame] | 851 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 852 | return rc; |
| 853 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 854 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 855 | /* |
| 856 | ** Register a new collation sequence with the database handle db. |
| 857 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 858 | int sqlite3_create_collation( |
| 859 | sqlite3* db, |
| 860 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 861 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 862 | void* pCtx, |
| 863 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 864 | ){ |
| 865 | CollSeq *pColl; |
| 866 | int rc = SQLITE_OK; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 867 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 868 | if( sqlite3SafetyCheck(db) ){ |
| 869 | return SQLITE_MISUSE; |
| 870 | } |
| 871 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 872 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 873 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 874 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 875 | */ |
| 876 | if( enc==SQLITE_UTF16 ){ |
| 877 | enc = SQLITE_UTF16NATIVE; |
| 878 | } |
| 879 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 880 | if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){ |
| 881 | sqlite3Error(db, SQLITE_ERROR, |
| 882 | "Param 3 to sqlite3_create_collation() must be one of " |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 883 | "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE" |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 884 | ); |
| 885 | return SQLITE_ERROR; |
| 886 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 887 | |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 888 | /* Check if this call is removing or replacing an existing collation |
| 889 | ** sequence. If so, and there are active VMs, return busy. If there |
| 890 | ** are no active VMs, invalidate any pre-compiled statements. |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 891 | */ |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 892 | pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 0); |
| 893 | if( pColl && pColl->xCmp ){ |
| 894 | if( db->activeVdbeCnt ){ |
| 895 | sqlite3Error(db, SQLITE_BUSY, |
| 896 | "Unable to delete/modify collation sequence due to active statements"); |
| 897 | return SQLITE_BUSY; |
| 898 | } |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame] | 899 | sqlite3ExpirePreparedStatements(db); |
| 900 | } |
| 901 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 902 | pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 903 | if( 0==pColl ){ |
| 904 | rc = SQLITE_NOMEM; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 905 | }else{ |
| 906 | pColl->xCmp = xCompare; |
| 907 | pColl->pUser = pCtx; |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 908 | pColl->enc = enc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 909 | } |
| 910 | sqlite3Error(db, rc, 0); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 911 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 912 | } |
| 913 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 914 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 915 | /* |
| 916 | ** Register a new collation sequence with the database handle db. |
| 917 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 918 | int sqlite3_create_collation16( |
| 919 | sqlite3* db, |
| 920 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 921 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 922 | void* pCtx, |
| 923 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 924 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 925 | char const *zName8; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 926 | sqlite3_value *pTmp; |
| 927 | if( sqlite3SafetyCheck(db) ){ |
| 928 | return SQLITE_MISUSE; |
| 929 | } |
| 930 | pTmp = sqlite3GetTransientValue(db); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 931 | sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 932 | zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
| 933 | return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 934 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 935 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 936 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 937 | /* |
| 938 | ** Register a collation sequence factory callback with the database handle |
| 939 | ** db. Replace any previously installed collation sequence factory. |
| 940 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 941 | int sqlite3_collation_needed( |
| 942 | sqlite3 *db, |
| 943 | void *pCollNeededArg, |
| 944 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 945 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 946 | if( sqlite3SafetyCheck(db) ){ |
| 947 | return SQLITE_MISUSE; |
| 948 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 949 | db->xCollNeeded = xCollNeeded; |
| 950 | db->xCollNeeded16 = 0; |
| 951 | db->pCollNeededArg = pCollNeededArg; |
| 952 | return SQLITE_OK; |
| 953 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 954 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 955 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 956 | /* |
| 957 | ** Register a collation sequence factory callback with the database handle |
| 958 | ** db. Replace any previously installed collation sequence factory. |
| 959 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 960 | int sqlite3_collation_needed16( |
| 961 | sqlite3 *db, |
| 962 | void *pCollNeededArg, |
| 963 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 964 | ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 965 | if( sqlite3SafetyCheck(db) ){ |
| 966 | return SQLITE_MISUSE; |
| 967 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 968 | db->xCollNeeded = 0; |
| 969 | db->xCollNeeded16 = xCollNeeded16; |
| 970 | db->pCollNeededArg = pCollNeededArg; |
| 971 | return SQLITE_OK; |
| 972 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 973 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 974 | |
| 975 | #ifndef SQLITE_OMIT_GLOBALRECOVER |
| 976 | /* |
| 977 | ** This function is called to recover from a malloc failure that occured |
| 978 | ** within SQLite. |
| 979 | ** |
| 980 | ** This function is *not* threadsafe. Calling this from within a threaded |
| 981 | ** application when threads other than the caller have used SQLite is |
| 982 | ** dangerous and will almost certainly result in malfunctions. |
| 983 | */ |
| 984 | int sqlite3_global_recover(){ |
| 985 | int rc = SQLITE_OK; |
| 986 | |
| 987 | if( sqlite3_malloc_failed ){ |
| 988 | sqlite3 *db; |
| 989 | int i; |
| 990 | sqlite3_malloc_failed = 0; |
| 991 | for(db=pDbList; db; db=db->pNext ){ |
| 992 | sqlite3ExpirePreparedStatements(db); |
| 993 | for(i=0; i<db->nDb; i++){ |
| 994 | Btree *pBt = db->aDb[i].pBt; |
| 995 | if( pBt && (rc=sqlite3BtreeReset(pBt)) ){ |
| 996 | goto recover_out; |
| 997 | } |
| 998 | } |
| 999 | db->autoCommit = 1; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | recover_out: |
| 1004 | if( rc!=SQLITE_OK ){ |
| 1005 | sqlite3_malloc_failed = 1; |
| 1006 | } |
| 1007 | return rc; |
| 1008 | } |
| 1009 | #endif |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 1010 | |
| 1011 | /* |
| 1012 | ** Test to see whether or not the database connection is in autocommit |
| 1013 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 1014 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 1015 | ** by the next COMMIT or ROLLBACK. |
| 1016 | ** |
| 1017 | ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** |
| 1018 | */ |
| 1019 | int sqlite3_get_autocommit(sqlite3 *db){ |
| 1020 | return db->autoCommit; |
| 1021 | } |