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