|
1 /* |
|
2 ** 2001 September 15 |
|
3 ** |
|
4 ** The author disclaims copyright to this source code. In place of |
|
5 ** a legal notice, here is a blessing: |
|
6 ** |
|
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. |
|
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 ** |
|
17 ** $Id: main.c,v 1.508 2008/10/12 00:27:53 shane Exp $ |
|
18 */ |
|
19 #include "sqliteInt.h" |
|
20 #include <ctype.h> |
|
21 |
|
22 #ifdef SQLITE_ENABLE_FTS3 |
|
23 # include "fts3.h" |
|
24 #endif |
|
25 #ifdef SQLITE_ENABLE_RTREE |
|
26 # include "rtree.h" |
|
27 #endif |
|
28 #ifdef SQLITE_ENABLE_ICU |
|
29 # include "sqliteicu.h" |
|
30 #endif |
|
31 |
|
32 /* |
|
33 ** The version of the library |
|
34 */ |
|
35 const char sqlite3_version[] = SQLITE_VERSION; |
|
36 const char *sqlite3_libversion(void){ return sqlite3_version; } |
|
37 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
|
38 int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
|
39 |
|
40 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
|
41 /* |
|
42 ** If the following function pointer is not NULL and if |
|
43 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing |
|
44 ** I/O active are written using this function. These messages |
|
45 ** are intended for debugging activity only. |
|
46 */ |
|
47 void (*sqlite3IoTrace)(const char*, ...) = 0; |
|
48 #endif |
|
49 |
|
50 /* |
|
51 ** If the following global variable points to a string which is the |
|
52 ** name of a directory, then that directory will be used to store |
|
53 ** temporary files. |
|
54 ** |
|
55 ** See also the "PRAGMA temp_store_directory" SQL command. |
|
56 */ |
|
57 char *sqlite3_temp_directory = 0; |
|
58 |
|
59 /* |
|
60 ** Initialize SQLite. |
|
61 ** |
|
62 ** This routine must be called to initialize the memory allocation, |
|
63 ** VFS, and mutex subsystems prior to doing any serious work with |
|
64 ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT |
|
65 ** this routine will be called automatically by key routines such as |
|
66 ** sqlite3_open(). |
|
67 ** |
|
68 ** This routine is a no-op except on its very first call for the process, |
|
69 ** or for the first call after a call to sqlite3_shutdown. |
|
70 ** |
|
71 ** The first thread to call this routine runs the initialization to |
|
72 ** completion. If subsequent threads call this routine before the first |
|
73 ** thread has finished the initialization process, then the subsequent |
|
74 ** threads must block until the first thread finishes with the initialization. |
|
75 ** |
|
76 ** The first thread might call this routine recursively. Recursive |
|
77 ** calls to this routine should not block, of course. Otherwise the |
|
78 ** initialization process would never complete. |
|
79 ** |
|
80 ** Let X be the first thread to enter this routine. Let Y be some other |
|
81 ** thread. Then while the initial invocation of this routine by X is |
|
82 ** incomplete, it is required that: |
|
83 ** |
|
84 ** * Calls to this routine from Y must block until the outer-most |
|
85 ** call by X completes. |
|
86 ** |
|
87 ** * Recursive calls to this routine from thread X return immediately |
|
88 ** without blocking. |
|
89 */ |
|
90 int sqlite3_initialize(void){ |
|
91 sqlite3_mutex *pMaster; /* The main static mutex */ |
|
92 int rc; /* Result code */ |
|
93 |
|
94 #ifdef SQLITE_OMIT_WSD |
|
95 rc = sqlite3_wsd_init(4096, 24); |
|
96 if( rc!=SQLITE_OK ){ |
|
97 return rc; |
|
98 } |
|
99 #endif |
|
100 |
|
101 /* If SQLite is already completely initialized, then this call |
|
102 ** to sqlite3_initialize() should be a no-op. But the initialization |
|
103 ** must be complete. So isInit must not be set until the very end |
|
104 ** of this routine. |
|
105 */ |
|
106 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; |
|
107 |
|
108 /* Make sure the mutex subsystem is initialized. If unable to |
|
109 ** initialize the mutex subsystem, return early with the error. |
|
110 ** If the system is so sick that we are unable to allocate a mutex, |
|
111 ** there is not much SQLite is going to be able to do. |
|
112 ** |
|
113 ** The mutex subsystem must take care of serializing its own |
|
114 ** initialization. |
|
115 */ |
|
116 rc = sqlite3MutexInit(); |
|
117 if( rc ) return rc; |
|
118 |
|
119 /* Initialize the malloc() system and the recursive pInitMutex mutex. |
|
120 ** This operation is protected by the STATIC_MASTER mutex. Note that |
|
121 ** MutexAlloc() is called for a static mutex prior to initializing the |
|
122 ** malloc subsystem - this implies that the allocation of a static |
|
123 ** mutex must not require support from the malloc subsystem. |
|
124 */ |
|
125 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
|
126 sqlite3_mutex_enter(pMaster); |
|
127 if( !sqlite3GlobalConfig.isMallocInit ){ |
|
128 rc = sqlite3MallocInit(); |
|
129 } |
|
130 if( rc==SQLITE_OK ){ |
|
131 sqlite3GlobalConfig.isMallocInit = 1; |
|
132 if( !sqlite3GlobalConfig.pInitMutex ){ |
|
133 sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
|
134 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ |
|
135 rc = SQLITE_NOMEM; |
|
136 } |
|
137 } |
|
138 } |
|
139 if( rc==SQLITE_OK ){ |
|
140 sqlite3GlobalConfig.nRefInitMutex++; |
|
141 } |
|
142 sqlite3_mutex_leave(pMaster); |
|
143 |
|
144 /* If unable to initialize the malloc subsystem, then return early. |
|
145 ** There is little hope of getting SQLite to run if the malloc |
|
146 ** subsystem cannot be initialized. |
|
147 */ |
|
148 if( rc!=SQLITE_OK ){ |
|
149 return rc; |
|
150 } |
|
151 |
|
152 /* Do the rest of the initialization under the recursive mutex so |
|
153 ** that we will be able to handle recursive calls into |
|
154 ** sqlite3_initialize(). The recursive calls normally come through |
|
155 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
|
156 ** recursive calls might also be possible. |
|
157 */ |
|
158 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
|
159 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
|
160 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
|
161 sqlite3GlobalConfig.inProgress = 1; |
|
162 memset(pHash, 0, sizeof(sqlite3GlobalFunctions)); |
|
163 sqlite3RegisterGlobalFunctions(); |
|
164 rc = sqlite3_os_init(); |
|
165 if( rc==SQLITE_OK ){ |
|
166 rc = sqlite3PcacheInitialize(); |
|
167 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
|
168 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
|
169 } |
|
170 sqlite3GlobalConfig.inProgress = 0; |
|
171 sqlite3GlobalConfig.isInit = (rc==SQLITE_OK ? 1 : 0); |
|
172 } |
|
173 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); |
|
174 |
|
175 /* Go back under the static mutex and clean up the recursive |
|
176 ** mutex to prevent a resource leak. |
|
177 */ |
|
178 sqlite3_mutex_enter(pMaster); |
|
179 sqlite3GlobalConfig.nRefInitMutex--; |
|
180 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ |
|
181 assert( sqlite3GlobalConfig.nRefInitMutex==0 ); |
|
182 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); |
|
183 sqlite3GlobalConfig.pInitMutex = 0; |
|
184 } |
|
185 sqlite3_mutex_leave(pMaster); |
|
186 |
|
187 /* The following is just a sanity check to make sure SQLite has |
|
188 ** been compiled correctly. It is important to run this code, but |
|
189 ** we don't want to run it too often and soak up CPU cycles for no |
|
190 ** reason. So we run it once during initialization. |
|
191 */ |
|
192 #ifndef NDEBUG |
|
193 /* This section of code's only "output" is via assert() statements. */ |
|
194 if ( rc==SQLITE_OK ){ |
|
195 u64 x = (((u64)1)<<63)-1; |
|
196 double y; |
|
197 assert(sizeof(x)==8); |
|
198 assert(sizeof(x)==sizeof(y)); |
|
199 memcpy(&y, &x, 8); |
|
200 assert( sqlite3IsNaN(y) ); |
|
201 } |
|
202 #endif |
|
203 |
|
204 return rc; |
|
205 } |
|
206 |
|
207 /* |
|
208 ** Undo the effects of sqlite3_initialize(). Must not be called while |
|
209 ** there are outstanding database connections or memory allocations or |
|
210 ** while any part of SQLite is otherwise in use in any thread. This |
|
211 ** routine is not threadsafe. Not by a long shot. |
|
212 */ |
|
213 int sqlite3_shutdown(void){ |
|
214 sqlite3GlobalConfig.isMallocInit = 0; |
|
215 sqlite3PcacheShutdown(); |
|
216 if( sqlite3GlobalConfig.isInit ){ |
|
217 sqlite3_os_end(); |
|
218 } |
|
219 sqlite3MallocEnd(); |
|
220 sqlite3MutexEnd(); |
|
221 sqlite3GlobalConfig.isInit = 0; |
|
222 return SQLITE_OK; |
|
223 } |
|
224 |
|
225 /* |
|
226 ** This API allows applications to modify the global configuration of |
|
227 ** the SQLite library at run-time. |
|
228 ** |
|
229 ** This routine should only be called when there are no outstanding |
|
230 ** database connections or memory allocations. This routine is not |
|
231 ** threadsafe. Failure to heed these warnings can lead to unpredictable |
|
232 ** behavior. |
|
233 */ |
|
234 int sqlite3_config(int op, ...){ |
|
235 va_list ap; |
|
236 int rc = SQLITE_OK; |
|
237 |
|
238 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while |
|
239 ** the SQLite library is in use. */ |
|
240 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE; |
|
241 |
|
242 va_start(ap, op); |
|
243 switch( op ){ |
|
244 |
|
245 /* Mutex configuration options are only available in a threadsafe |
|
246 ** compile. |
|
247 */ |
|
248 #if SQLITE_THREADSAFE |
|
249 case SQLITE_CONFIG_SINGLETHREAD: { |
|
250 /* Disable all mutexing */ |
|
251 sqlite3GlobalConfig.bCoreMutex = 0; |
|
252 sqlite3GlobalConfig.bFullMutex = 0; |
|
253 break; |
|
254 } |
|
255 case SQLITE_CONFIG_MULTITHREAD: { |
|
256 /* Disable mutexing of database connections */ |
|
257 /* Enable mutexing of core data structures */ |
|
258 sqlite3GlobalConfig.bCoreMutex = 1; |
|
259 sqlite3GlobalConfig.bFullMutex = 0; |
|
260 break; |
|
261 } |
|
262 case SQLITE_CONFIG_SERIALIZED: { |
|
263 /* Enable all mutexing */ |
|
264 sqlite3GlobalConfig.bCoreMutex = 1; |
|
265 sqlite3GlobalConfig.bFullMutex = 1; |
|
266 break; |
|
267 } |
|
268 case SQLITE_CONFIG_MUTEX: { |
|
269 /* Specify an alternative mutex implementation */ |
|
270 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
|
271 break; |
|
272 } |
|
273 case SQLITE_CONFIG_GETMUTEX: { |
|
274 /* Retrieve the current mutex implementation */ |
|
275 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
|
276 break; |
|
277 } |
|
278 #endif |
|
279 |
|
280 |
|
281 case SQLITE_CONFIG_MALLOC: { |
|
282 /* Specify an alternative malloc implementation */ |
|
283 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
|
284 break; |
|
285 } |
|
286 case SQLITE_CONFIG_GETMALLOC: { |
|
287 /* Retrieve the current malloc() implementation */ |
|
288 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
|
289 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
|
290 break; |
|
291 } |
|
292 case SQLITE_CONFIG_MEMSTATUS: { |
|
293 /* Enable or disable the malloc status collection */ |
|
294 sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
|
295 break; |
|
296 } |
|
297 case SQLITE_CONFIG_SCRATCH: { |
|
298 /* Designate a buffer for scratch memory space */ |
|
299 sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
|
300 sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
|
301 sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
|
302 break; |
|
303 } |
|
304 case SQLITE_CONFIG_PAGECACHE: { |
|
305 /* Designate a buffer for scratch memory space */ |
|
306 sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
|
307 sqlite3GlobalConfig.szPage = va_arg(ap, int); |
|
308 sqlite3GlobalConfig.nPage = va_arg(ap, int); |
|
309 break; |
|
310 } |
|
311 |
|
312 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
|
313 case SQLITE_CONFIG_HEAP: { |
|
314 /* Designate a buffer for heap memory space */ |
|
315 sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
|
316 sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
|
317 sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
|
318 |
|
319 if( sqlite3GlobalConfig.pHeap==0 ){ |
|
320 /* If the heap pointer is NULL, then restore the malloc implementation |
|
321 ** back to NULL pointers too. This will cause the malloc to go |
|
322 ** back to its default implementation when sqlite3_initialize() is |
|
323 ** run. |
|
324 */ |
|
325 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
|
326 }else{ |
|
327 /* The heap pointer is not NULL, then install one of the |
|
328 ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor |
|
329 ** ENABLE_MEMSYS5 is defined, return an error. |
|
330 ** the default case and return an error. |
|
331 */ |
|
332 #ifdef SQLITE_ENABLE_MEMSYS3 |
|
333 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
|
334 #endif |
|
335 #ifdef SQLITE_ENABLE_MEMSYS5 |
|
336 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
|
337 #endif |
|
338 } |
|
339 break; |
|
340 } |
|
341 #endif |
|
342 |
|
343 #if defined(SQLITE_ENABLE_MEMSYS6) |
|
344 case SQLITE_CONFIG_CHUNKALLOC: { |
|
345 sqlite3GlobalConfig.nSmall = va_arg(ap, int); |
|
346 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys6(); |
|
347 break; |
|
348 } |
|
349 #endif |
|
350 |
|
351 case SQLITE_CONFIG_LOOKASIDE: { |
|
352 sqlite3GlobalConfig.szLookaside = va_arg(ap, int); |
|
353 sqlite3GlobalConfig.nLookaside = va_arg(ap, int); |
|
354 break; |
|
355 } |
|
356 |
|
357 default: { |
|
358 rc = SQLITE_ERROR; |
|
359 break; |
|
360 } |
|
361 } |
|
362 va_end(ap); |
|
363 return rc; |
|
364 } |
|
365 |
|
366 /* |
|
367 ** Set up the lookaside buffers for a database connection. |
|
368 ** Return SQLITE_OK on success. |
|
369 ** If lookaside is already active, return SQLITE_BUSY. |
|
370 ** |
|
371 ** The sz parameter is the number of bytes in each lookaside slot. |
|
372 ** The cnt parameter is the number of slots. If pStart is NULL the |
|
373 ** space for the lookaside memory is obtained from sqlite3_malloc(). |
|
374 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for |
|
375 ** the lookaside memory. |
|
376 */ |
|
377 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ |
|
378 void *pStart; |
|
379 if( db->lookaside.nOut ){ |
|
380 return SQLITE_BUSY; |
|
381 } |
|
382 if( sz<0 ) sz = 0; |
|
383 if( cnt<0 ) cnt = 0; |
|
384 if( pBuf==0 ){ |
|
385 sz = (sz + 7)&~7; |
|
386 sqlite3BeginBenignMalloc(); |
|
387 pStart = sqlite3Malloc( sz*cnt ); |
|
388 sqlite3EndBenignMalloc(); |
|
389 }else{ |
|
390 sz = sz&~7; |
|
391 pStart = pBuf; |
|
392 } |
|
393 if( db->lookaside.bMalloced ){ |
|
394 sqlite3_free(db->lookaside.pStart); |
|
395 } |
|
396 db->lookaside.pStart = pStart; |
|
397 db->lookaside.pFree = 0; |
|
398 db->lookaside.sz = sz; |
|
399 db->lookaside.bMalloced = pBuf==0; |
|
400 if( pStart ){ |
|
401 int i; |
|
402 LookasideSlot *p; |
|
403 p = (LookasideSlot*)pStart; |
|
404 for(i=cnt-1; i>=0; i--){ |
|
405 p->pNext = db->lookaside.pFree; |
|
406 db->lookaside.pFree = p; |
|
407 p = (LookasideSlot*)&((u8*)p)[sz]; |
|
408 } |
|
409 db->lookaside.pEnd = p; |
|
410 db->lookaside.bEnabled = 1; |
|
411 }else{ |
|
412 db->lookaside.pEnd = 0; |
|
413 db->lookaside.bEnabled = 0; |
|
414 } |
|
415 return SQLITE_OK; |
|
416 } |
|
417 |
|
418 /* |
|
419 ** Configuration settings for an individual database connection |
|
420 */ |
|
421 int sqlite3_db_config(sqlite3 *db, int op, ...){ |
|
422 va_list ap; |
|
423 int rc; |
|
424 va_start(ap, op); |
|
425 switch( op ){ |
|
426 case SQLITE_DBCONFIG_LOOKASIDE: { |
|
427 void *pBuf = va_arg(ap, void*); |
|
428 int sz = va_arg(ap, int); |
|
429 int cnt = va_arg(ap, int); |
|
430 rc = setupLookaside(db, pBuf, sz, cnt); |
|
431 break; |
|
432 } |
|
433 default: { |
|
434 rc = SQLITE_ERROR; |
|
435 break; |
|
436 } |
|
437 } |
|
438 va_end(ap); |
|
439 return rc; |
|
440 } |
|
441 |
|
442 /* |
|
443 ** Routine needed to support the testcase() macro. |
|
444 */ |
|
445 #ifdef SQLITE_COVERAGE_TEST |
|
446 void sqlite3Coverage(int x){ |
|
447 static int dummy = 0; |
|
448 dummy += x; |
|
449 } |
|
450 #endif |
|
451 |
|
452 |
|
453 /* |
|
454 ** Return true if the buffer z[0..n-1] contains all spaces. |
|
455 */ |
|
456 static int allSpaces(const char *z, int n){ |
|
457 while( n>0 && z[n-1]==' ' ){ n--; } |
|
458 return n==0; |
|
459 } |
|
460 |
|
461 /* |
|
462 ** This is the default collating function named "BINARY" which is always |
|
463 ** available. |
|
464 ** |
|
465 ** If the padFlag argument is not NULL then space padding at the end |
|
466 ** of strings is ignored. This implements the RTRIM collation. |
|
467 */ |
|
468 static int binCollFunc( |
|
469 void *padFlag, |
|
470 int nKey1, const void *pKey1, |
|
471 int nKey2, const void *pKey2 |
|
472 ){ |
|
473 int rc, n; |
|
474 n = nKey1<nKey2 ? nKey1 : nKey2; |
|
475 rc = memcmp(pKey1, pKey2, n); |
|
476 if( rc==0 ){ |
|
477 if( padFlag |
|
478 && allSpaces(((char*)pKey1)+n, nKey1-n) |
|
479 && allSpaces(((char*)pKey2)+n, nKey2-n) |
|
480 ){ |
|
481 /* Leave rc unchanged at 0 */ |
|
482 }else{ |
|
483 rc = nKey1 - nKey2; |
|
484 } |
|
485 } |
|
486 return rc; |
|
487 } |
|
488 |
|
489 /* |
|
490 ** Another built-in collating sequence: NOCASE. |
|
491 ** |
|
492 ** This collating sequence is intended to be used for "case independant |
|
493 ** comparison". SQLite's knowledge of upper and lower case equivalents |
|
494 ** extends only to the 26 characters used in the English language. |
|
495 ** |
|
496 ** At the moment there is only a UTF-8 implementation. |
|
497 */ |
|
498 static int nocaseCollatingFunc( |
|
499 void *NotUsed, |
|
500 int nKey1, const void *pKey1, |
|
501 int nKey2, const void *pKey2 |
|
502 ){ |
|
503 int r = sqlite3StrNICmp( |
|
504 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
|
505 if( 0==r ){ |
|
506 r = nKey1-nKey2; |
|
507 } |
|
508 return r; |
|
509 } |
|
510 |
|
511 /* |
|
512 ** Return the ROWID of the most recent insert |
|
513 */ |
|
514 sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
|
515 return db->lastRowid; |
|
516 } |
|
517 |
|
518 /* |
|
519 ** Return the number of changes in the most recent call to sqlite3_exec(). |
|
520 */ |
|
521 int sqlite3_changes(sqlite3 *db){ |
|
522 return db->nChange; |
|
523 } |
|
524 |
|
525 /* |
|
526 ** Return the number of changes since the database handle was opened. |
|
527 */ |
|
528 int sqlite3_total_changes(sqlite3 *db){ |
|
529 return db->nTotalChange; |
|
530 } |
|
531 |
|
532 /* |
|
533 ** Close an existing SQLite database |
|
534 */ |
|
535 int sqlite3_close(sqlite3 *db){ |
|
536 HashElem *i; |
|
537 int j; |
|
538 |
|
539 if( !db ){ |
|
540 return SQLITE_OK; |
|
541 } |
|
542 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
|
543 return SQLITE_MISUSE; |
|
544 } |
|
545 sqlite3_mutex_enter(db->mutex); |
|
546 |
|
547 #ifdef SQLITE_SSE |
|
548 { |
|
549 extern void sqlite3SseCleanup(sqlite3*); |
|
550 sqlite3SseCleanup(db); |
|
551 } |
|
552 #endif |
|
553 |
|
554 sqlite3ResetInternalSchema(db, 0); |
|
555 |
|
556 /* If a transaction is open, the ResetInternalSchema() call above |
|
557 ** will not have called the xDisconnect() method on any virtual |
|
558 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() |
|
559 ** call will do so. We need to do this before the check for active |
|
560 ** SQL statements below, as the v-table implementation may be storing |
|
561 ** some prepared statements internally. |
|
562 */ |
|
563 sqlite3VtabRollback(db); |
|
564 |
|
565 /* If there are any outstanding VMs, return SQLITE_BUSY. */ |
|
566 if( db->pVdbe ){ |
|
567 sqlite3Error(db, SQLITE_BUSY, |
|
568 "Unable to close due to unfinalised statements"); |
|
569 sqlite3_mutex_leave(db->mutex); |
|
570 return SQLITE_BUSY; |
|
571 } |
|
572 assert( sqlite3SafetyCheckSickOrOk(db) ); |
|
573 |
|
574 for(j=0; j<db->nDb; j++){ |
|
575 struct Db *pDb = &db->aDb[j]; |
|
576 if( pDb->pBt ){ |
|
577 sqlite3BtreeClose(pDb->pBt); |
|
578 pDb->pBt = 0; |
|
579 if( j!=1 ){ |
|
580 pDb->pSchema = 0; |
|
581 } |
|
582 } |
|
583 } |
|
584 sqlite3ResetInternalSchema(db, 0); |
|
585 assert( db->nDb<=2 ); |
|
586 assert( db->aDb==db->aDbStatic ); |
|
587 for(j=0; j<ArraySize(db->aFunc.a); j++){ |
|
588 FuncDef *pNext, *pHash, *p; |
|
589 for(p=db->aFunc.a[j]; p; p=pHash){ |
|
590 pHash = p->pHash; |
|
591 while( p ){ |
|
592 pNext = p->pNext; |
|
593 sqlite3DbFree(db, p); |
|
594 p = pNext; |
|
595 } |
|
596 } |
|
597 } |
|
598 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
|
599 CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
|
600 /* Invoke any destructors registered for collation sequence user data. */ |
|
601 for(j=0; j<3; j++){ |
|
602 if( pColl[j].xDel ){ |
|
603 pColl[j].xDel(pColl[j].pUser); |
|
604 } |
|
605 } |
|
606 sqlite3DbFree(db, pColl); |
|
607 } |
|
608 sqlite3HashClear(&db->aCollSeq); |
|
609 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
610 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ |
|
611 Module *pMod = (Module *)sqliteHashData(i); |
|
612 if( pMod->xDestroy ){ |
|
613 pMod->xDestroy(pMod->pAux); |
|
614 } |
|
615 sqlite3DbFree(db, pMod); |
|
616 } |
|
617 sqlite3HashClear(&db->aModule); |
|
618 #endif |
|
619 |
|
620 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
|
621 if( db->pErr ){ |
|
622 sqlite3ValueFree(db->pErr); |
|
623 } |
|
624 sqlite3CloseExtensions(db); |
|
625 |
|
626 db->magic = SQLITE_MAGIC_ERROR; |
|
627 |
|
628 /* The temp-database schema is allocated differently from the other schema |
|
629 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). |
|
630 ** So it needs to be freed here. Todo: Why not roll the temp schema into |
|
631 ** the same sqliteMalloc() as the one that allocates the database |
|
632 ** structure? |
|
633 */ |
|
634 sqlite3DbFree(db, db->aDb[1].pSchema); |
|
635 sqlite3_mutex_leave(db->mutex); |
|
636 db->magic = SQLITE_MAGIC_CLOSED; |
|
637 sqlite3_mutex_free(db->mutex); |
|
638 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ |
|
639 if( db->lookaside.bMalloced ){ |
|
640 sqlite3_free(db->lookaside.pStart); |
|
641 } |
|
642 sqlite3_free(db); |
|
643 return SQLITE_OK; |
|
644 } |
|
645 |
|
646 /* |
|
647 ** Rollback all database files. |
|
648 */ |
|
649 void sqlite3RollbackAll(sqlite3 *db){ |
|
650 int i; |
|
651 int inTrans = 0; |
|
652 assert( sqlite3_mutex_held(db->mutex) ); |
|
653 sqlite3BeginBenignMalloc(); |
|
654 for(i=0; i<db->nDb; i++){ |
|
655 if( db->aDb[i].pBt ){ |
|
656 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ |
|
657 inTrans = 1; |
|
658 } |
|
659 sqlite3BtreeRollback(db->aDb[i].pBt); |
|
660 db->aDb[i].inTrans = 0; |
|
661 } |
|
662 } |
|
663 sqlite3VtabRollback(db); |
|
664 sqlite3EndBenignMalloc(); |
|
665 |
|
666 if( db->flags&SQLITE_InternChanges ){ |
|
667 sqlite3ExpirePreparedStatements(db); |
|
668 sqlite3ResetInternalSchema(db, 0); |
|
669 } |
|
670 |
|
671 /* If one has been configured, invoke the rollback-hook callback */ |
|
672 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
|
673 db->xRollbackCallback(db->pRollbackArg); |
|
674 } |
|
675 } |
|
676 |
|
677 /* |
|
678 ** Return a static string that describes the kind of error specified in the |
|
679 ** argument. |
|
680 */ |
|
681 const char *sqlite3ErrStr(int rc){ |
|
682 const char *z; |
|
683 switch( rc & 0xff ){ |
|
684 case SQLITE_ROW: |
|
685 case SQLITE_DONE: |
|
686 case SQLITE_OK: z = "not an error"; break; |
|
687 case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
|
688 case SQLITE_PERM: z = "access permission denied"; break; |
|
689 case SQLITE_ABORT: z = "callback requested query abort"; break; |
|
690 case SQLITE_BUSY: z = "database is locked"; break; |
|
691 case SQLITE_LOCKED: z = "database table is locked"; break; |
|
692 case SQLITE_NOMEM: z = "out of memory"; break; |
|
693 case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
|
694 case SQLITE_INTERRUPT: z = "interrupted"; break; |
|
695 case SQLITE_IOERR: z = "disk I/O error"; break; |
|
696 case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
|
697 case SQLITE_FULL: z = "database or disk is full"; break; |
|
698 case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
|
699 case SQLITE_EMPTY: z = "table contains no data"; break; |
|
700 case SQLITE_SCHEMA: z = "database schema has changed"; break; |
|
701 case SQLITE_TOOBIG: z = "String or BLOB exceeded size limit"; break; |
|
702 case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
|
703 case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
|
704 case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
|
705 case SQLITE_NOLFS: z = "large file support is disabled"; break; |
|
706 case SQLITE_AUTH: z = "authorization denied"; break; |
|
707 case SQLITE_FORMAT: z = "auxiliary database format error"; break; |
|
708 case SQLITE_RANGE: z = "bind or column index out of range"; break; |
|
709 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; |
|
710 default: z = "unknown error"; break; |
|
711 } |
|
712 return z; |
|
713 } |
|
714 |
|
715 /* |
|
716 ** This routine implements a busy callback that sleeps and tries |
|
717 ** again until a timeout value is reached. The timeout value is |
|
718 ** an integer number of milliseconds passed in as the first |
|
719 ** argument. |
|
720 */ |
|
721 static int sqliteDefaultBusyCallback( |
|
722 void *ptr, /* Database connection */ |
|
723 int count /* Number of times table has been busy */ |
|
724 ){ |
|
725 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) |
|
726 static const u8 delays[] = |
|
727 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; |
|
728 static const u8 totals[] = |
|
729 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; |
|
730 # define NDELAY (sizeof(delays)/sizeof(delays[0])) |
|
731 sqlite3 *db = (sqlite3 *)ptr; |
|
732 int timeout = db->busyTimeout; |
|
733 int delay, prior; |
|
734 |
|
735 assert( count>=0 ); |
|
736 if( count < NDELAY ){ |
|
737 delay = delays[count]; |
|
738 prior = totals[count]; |
|
739 }else{ |
|
740 delay = delays[NDELAY-1]; |
|
741 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); |
|
742 } |
|
743 if( prior + delay > timeout ){ |
|
744 delay = timeout - prior; |
|
745 if( delay<=0 ) return 0; |
|
746 } |
|
747 sqlite3OsSleep(db->pVfs, delay*1000); |
|
748 return 1; |
|
749 #else |
|
750 sqlite3 *db = (sqlite3 *)ptr; |
|
751 int timeout = ((sqlite3 *)ptr)->busyTimeout; |
|
752 if( (count+1)*1000 > timeout ){ |
|
753 return 0; |
|
754 } |
|
755 sqlite3OsSleep(db->pVfs, 1000000); |
|
756 return 1; |
|
757 #endif |
|
758 } |
|
759 |
|
760 /* |
|
761 ** Invoke the given busy handler. |
|
762 ** |
|
763 ** This routine is called when an operation failed with a lock. |
|
764 ** If this routine returns non-zero, the lock is retried. If it |
|
765 ** returns 0, the operation aborts with an SQLITE_BUSY error. |
|
766 */ |
|
767 int sqlite3InvokeBusyHandler(BusyHandler *p){ |
|
768 int rc; |
|
769 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; |
|
770 rc = p->xFunc(p->pArg, p->nBusy); |
|
771 if( rc==0 ){ |
|
772 p->nBusy = -1; |
|
773 }else{ |
|
774 p->nBusy++; |
|
775 } |
|
776 return rc; |
|
777 } |
|
778 |
|
779 /* |
|
780 ** This routine sets the busy callback for an Sqlite database to the |
|
781 ** given callback function with the given argument. |
|
782 */ |
|
783 int sqlite3_busy_handler( |
|
784 sqlite3 *db, |
|
785 int (*xBusy)(void*,int), |
|
786 void *pArg |
|
787 ){ |
|
788 sqlite3_mutex_enter(db->mutex); |
|
789 db->busyHandler.xFunc = xBusy; |
|
790 db->busyHandler.pArg = pArg; |
|
791 db->busyHandler.nBusy = 0; |
|
792 sqlite3_mutex_leave(db->mutex); |
|
793 return SQLITE_OK; |
|
794 } |
|
795 |
|
796 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
|
797 /* |
|
798 ** This routine sets the progress callback for an Sqlite database to the |
|
799 ** given callback function with the given argument. The progress callback will |
|
800 ** be invoked every nOps opcodes. |
|
801 */ |
|
802 void sqlite3_progress_handler( |
|
803 sqlite3 *db, |
|
804 int nOps, |
|
805 int (*xProgress)(void*), |
|
806 void *pArg |
|
807 ){ |
|
808 sqlite3_mutex_enter(db->mutex); |
|
809 if( nOps>0 ){ |
|
810 db->xProgress = xProgress; |
|
811 db->nProgressOps = nOps; |
|
812 db->pProgressArg = pArg; |
|
813 }else{ |
|
814 db->xProgress = 0; |
|
815 db->nProgressOps = 0; |
|
816 db->pProgressArg = 0; |
|
817 } |
|
818 sqlite3_mutex_leave(db->mutex); |
|
819 } |
|
820 #endif |
|
821 |
|
822 |
|
823 /* |
|
824 ** This routine installs a default busy handler that waits for the |
|
825 ** specified number of milliseconds before returning 0. |
|
826 */ |
|
827 int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
|
828 if( ms>0 ){ |
|
829 db->busyTimeout = ms; |
|
830 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
|
831 }else{ |
|
832 sqlite3_busy_handler(db, 0, 0); |
|
833 } |
|
834 return SQLITE_OK; |
|
835 } |
|
836 |
|
837 /* |
|
838 ** Cause any pending operation to stop at its earliest opportunity. |
|
839 */ |
|
840 void sqlite3_interrupt(sqlite3 *db){ |
|
841 db->u1.isInterrupted = 1; |
|
842 } |
|
843 |
|
844 |
|
845 /* |
|
846 ** This function is exactly the same as sqlite3_create_function(), except |
|
847 ** that it is designed to be called by internal code. The difference is |
|
848 ** that if a malloc() fails in sqlite3_create_function(), an error code |
|
849 ** is returned and the mallocFailed flag cleared. |
|
850 */ |
|
851 int sqlite3CreateFunc( |
|
852 sqlite3 *db, |
|
853 const char *zFunctionName, |
|
854 int nArg, |
|
855 int enc, |
|
856 void *pUserData, |
|
857 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
|
858 void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
|
859 void (*xFinal)(sqlite3_context*) |
|
860 ){ |
|
861 FuncDef *p; |
|
862 int nName; |
|
863 |
|
864 assert( sqlite3_mutex_held(db->mutex) ); |
|
865 if( zFunctionName==0 || |
|
866 (xFunc && (xFinal || xStep)) || |
|
867 (!xFunc && (xFinal && !xStep)) || |
|
868 (!xFunc && (!xFinal && xStep)) || |
|
869 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || |
|
870 (255<(nName = sqlite3Strlen(db, zFunctionName))) ){ |
|
871 sqlite3Error(db, SQLITE_ERROR, "bad parameters"); |
|
872 return SQLITE_ERROR; |
|
873 } |
|
874 |
|
875 #ifndef SQLITE_OMIT_UTF16 |
|
876 /* If SQLITE_UTF16 is specified as the encoding type, transform this |
|
877 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
|
878 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
|
879 ** |
|
880 ** If SQLITE_ANY is specified, add three versions of the function |
|
881 ** to the hash table. |
|
882 */ |
|
883 if( enc==SQLITE_UTF16 ){ |
|
884 enc = SQLITE_UTF16NATIVE; |
|
885 }else if( enc==SQLITE_ANY ){ |
|
886 int rc; |
|
887 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, |
|
888 pUserData, xFunc, xStep, xFinal); |
|
889 if( rc==SQLITE_OK ){ |
|
890 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, |
|
891 pUserData, xFunc, xStep, xFinal); |
|
892 } |
|
893 if( rc!=SQLITE_OK ){ |
|
894 return rc; |
|
895 } |
|
896 enc = SQLITE_UTF16BE; |
|
897 } |
|
898 #else |
|
899 enc = SQLITE_UTF8; |
|
900 #endif |
|
901 |
|
902 /* Check if an existing function is being overridden or deleted. If so, |
|
903 ** and there are active VMs, then return SQLITE_BUSY. If a function |
|
904 ** is being overridden/deleted but there are no active VMs, allow the |
|
905 ** operation to continue but invalidate all precompiled statements. |
|
906 */ |
|
907 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0); |
|
908 if( p && p->iPrefEnc==enc && p->nArg==nArg ){ |
|
909 if( db->activeVdbeCnt ){ |
|
910 sqlite3Error(db, SQLITE_BUSY, |
|
911 "Unable to delete/modify user-function due to active statements"); |
|
912 assert( !db->mallocFailed ); |
|
913 return SQLITE_BUSY; |
|
914 }else{ |
|
915 sqlite3ExpirePreparedStatements(db); |
|
916 } |
|
917 } |
|
918 |
|
919 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1); |
|
920 assert(p || db->mallocFailed); |
|
921 if( !p ){ |
|
922 return SQLITE_NOMEM; |
|
923 } |
|
924 p->flags = 0; |
|
925 p->xFunc = xFunc; |
|
926 p->xStep = xStep; |
|
927 p->xFinalize = xFinal; |
|
928 p->pUserData = pUserData; |
|
929 p->nArg = nArg; |
|
930 return SQLITE_OK; |
|
931 } |
|
932 |
|
933 /* |
|
934 ** Create new user functions. |
|
935 */ |
|
936 int sqlite3_create_function( |
|
937 sqlite3 *db, |
|
938 const char *zFunctionName, |
|
939 int nArg, |
|
940 int enc, |
|
941 void *p, |
|
942 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
|
943 void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
|
944 void (*xFinal)(sqlite3_context*) |
|
945 ){ |
|
946 int rc; |
|
947 sqlite3_mutex_enter(db->mutex); |
|
948 rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); |
|
949 rc = sqlite3ApiExit(db, rc); |
|
950 sqlite3_mutex_leave(db->mutex); |
|
951 return rc; |
|
952 } |
|
953 |
|
954 #ifndef SQLITE_OMIT_UTF16 |
|
955 int sqlite3_create_function16( |
|
956 sqlite3 *db, |
|
957 const void *zFunctionName, |
|
958 int nArg, |
|
959 int eTextRep, |
|
960 void *p, |
|
961 void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
|
962 void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
|
963 void (*xFinal)(sqlite3_context*) |
|
964 ){ |
|
965 int rc; |
|
966 char *zFunc8; |
|
967 sqlite3_mutex_enter(db->mutex); |
|
968 assert( !db->mallocFailed ); |
|
969 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1); |
|
970 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); |
|
971 sqlite3DbFree(db, zFunc8); |
|
972 rc = sqlite3ApiExit(db, rc); |
|
973 sqlite3_mutex_leave(db->mutex); |
|
974 return rc; |
|
975 } |
|
976 #endif |
|
977 |
|
978 |
|
979 /* |
|
980 ** Declare that a function has been overloaded by a virtual table. |
|
981 ** |
|
982 ** If the function already exists as a regular global function, then |
|
983 ** this routine is a no-op. If the function does not exist, then create |
|
984 ** a new one that always throws a run-time error. |
|
985 ** |
|
986 ** When virtual tables intend to provide an overloaded function, they |
|
987 ** should call this routine to make sure the global function exists. |
|
988 ** A global function must exist in order for name resolution to work |
|
989 ** properly. |
|
990 */ |
|
991 int sqlite3_overload_function( |
|
992 sqlite3 *db, |
|
993 const char *zName, |
|
994 int nArg |
|
995 ){ |
|
996 int nName = sqlite3Strlen(db, zName); |
|
997 int rc; |
|
998 sqlite3_mutex_enter(db->mutex); |
|
999 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
|
1000 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
|
1001 0, sqlite3InvalidFunction, 0, 0); |
|
1002 } |
|
1003 rc = sqlite3ApiExit(db, SQLITE_OK); |
|
1004 sqlite3_mutex_leave(db->mutex); |
|
1005 return rc; |
|
1006 } |
|
1007 |
|
1008 #ifndef SQLITE_OMIT_TRACE |
|
1009 /* |
|
1010 ** Register a trace function. The pArg from the previously registered trace |
|
1011 ** is returned. |
|
1012 ** |
|
1013 ** A NULL trace function means that no tracing is executes. A non-NULL |
|
1014 ** trace is a pointer to a function that is invoked at the start of each |
|
1015 ** SQL statement. |
|
1016 */ |
|
1017 void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
|
1018 void *pOld; |
|
1019 sqlite3_mutex_enter(db->mutex); |
|
1020 pOld = db->pTraceArg; |
|
1021 db->xTrace = xTrace; |
|
1022 db->pTraceArg = pArg; |
|
1023 sqlite3_mutex_leave(db->mutex); |
|
1024 return pOld; |
|
1025 } |
|
1026 /* |
|
1027 ** Register a profile function. The pArg from the previously registered |
|
1028 ** profile function is returned. |
|
1029 ** |
|
1030 ** A NULL profile function means that no profiling is executes. A non-NULL |
|
1031 ** profile is a pointer to a function that is invoked at the conclusion of |
|
1032 ** each SQL statement that is run. |
|
1033 */ |
|
1034 void *sqlite3_profile( |
|
1035 sqlite3 *db, |
|
1036 void (*xProfile)(void*,const char*,sqlite_uint64), |
|
1037 void *pArg |
|
1038 ){ |
|
1039 void *pOld; |
|
1040 sqlite3_mutex_enter(db->mutex); |
|
1041 pOld = db->pProfileArg; |
|
1042 db->xProfile = xProfile; |
|
1043 db->pProfileArg = pArg; |
|
1044 sqlite3_mutex_leave(db->mutex); |
|
1045 return pOld; |
|
1046 } |
|
1047 #endif /* SQLITE_OMIT_TRACE */ |
|
1048 |
|
1049 /*** EXPERIMENTAL *** |
|
1050 ** |
|
1051 ** Register a function to be invoked when a transaction comments. |
|
1052 ** If the invoked function returns non-zero, then the commit becomes a |
|
1053 ** rollback. |
|
1054 */ |
|
1055 void *sqlite3_commit_hook( |
|
1056 sqlite3 *db, /* Attach the hook to this database */ |
|
1057 int (*xCallback)(void*), /* Function to invoke on each commit */ |
|
1058 void *pArg /* Argument to the function */ |
|
1059 ){ |
|
1060 void *pOld; |
|
1061 sqlite3_mutex_enter(db->mutex); |
|
1062 pOld = db->pCommitArg; |
|
1063 db->xCommitCallback = xCallback; |
|
1064 db->pCommitArg = pArg; |
|
1065 sqlite3_mutex_leave(db->mutex); |
|
1066 return pOld; |
|
1067 } |
|
1068 |
|
1069 /* |
|
1070 ** Register a callback to be invoked each time a row is updated, |
|
1071 ** inserted or deleted using this database connection. |
|
1072 */ |
|
1073 void *sqlite3_update_hook( |
|
1074 sqlite3 *db, /* Attach the hook to this database */ |
|
1075 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
|
1076 void *pArg /* Argument to the function */ |
|
1077 ){ |
|
1078 void *pRet; |
|
1079 sqlite3_mutex_enter(db->mutex); |
|
1080 pRet = db->pUpdateArg; |
|
1081 db->xUpdateCallback = xCallback; |
|
1082 db->pUpdateArg = pArg; |
|
1083 sqlite3_mutex_leave(db->mutex); |
|
1084 return pRet; |
|
1085 } |
|
1086 |
|
1087 /* |
|
1088 ** Register a callback to be invoked each time a transaction is rolled |
|
1089 ** back by this database connection. |
|
1090 */ |
|
1091 void *sqlite3_rollback_hook( |
|
1092 sqlite3 *db, /* Attach the hook to this database */ |
|
1093 void (*xCallback)(void*), /* Callback function */ |
|
1094 void *pArg /* Argument to the function */ |
|
1095 ){ |
|
1096 void *pRet; |
|
1097 sqlite3_mutex_enter(db->mutex); |
|
1098 pRet = db->pRollbackArg; |
|
1099 db->xRollbackCallback = xCallback; |
|
1100 db->pRollbackArg = pArg; |
|
1101 sqlite3_mutex_leave(db->mutex); |
|
1102 return pRet; |
|
1103 } |
|
1104 |
|
1105 /* |
|
1106 ** This routine is called to create a connection to a database BTree |
|
1107 ** driver. If zFilename is the name of a file, then that file is |
|
1108 ** opened and used. If zFilename is the magic name ":memory:" then |
|
1109 ** the database is stored in memory (and is thus forgotten as soon as |
|
1110 ** the connection is closed.) If zFilename is NULL then the database |
|
1111 ** is a "virtual" database for transient use only and is deleted as |
|
1112 ** soon as the connection is closed. |
|
1113 ** |
|
1114 ** A virtual database can be either a disk file (that is automatically |
|
1115 ** deleted when the file is closed) or it an be held entirely in memory, |
|
1116 ** depending on the values of the SQLITE_TEMP_STORE compile-time macro and the |
|
1117 ** db->temp_store variable, according to the following chart: |
|
1118 ** |
|
1119 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database |
|
1120 ** ----------------- -------------- ------------------------------ |
|
1121 ** 0 any file |
|
1122 ** 1 1 file |
|
1123 ** 1 2 memory |
|
1124 ** 1 0 file |
|
1125 ** 2 1 file |
|
1126 ** 2 2 memory |
|
1127 ** 2 0 memory |
|
1128 ** 3 any memory |
|
1129 */ |
|
1130 int sqlite3BtreeFactory( |
|
1131 const sqlite3 *db, /* Main database when opening aux otherwise 0 */ |
|
1132 const char *zFilename, /* Name of the file containing the BTree database */ |
|
1133 int omitJournal, /* if TRUE then do not journal this file */ |
|
1134 int nCache, /* How many pages in the page cache */ |
|
1135 int vfsFlags, /* Flags passed through to vfsOpen */ |
|
1136 Btree **ppBtree /* Pointer to new Btree object written here */ |
|
1137 ){ |
|
1138 int btFlags = 0; |
|
1139 int rc; |
|
1140 |
|
1141 assert( sqlite3_mutex_held(db->mutex) ); |
|
1142 assert( ppBtree != 0); |
|
1143 if( omitJournal ){ |
|
1144 btFlags |= BTREE_OMIT_JOURNAL; |
|
1145 } |
|
1146 if( db->flags & SQLITE_NoReadlock ){ |
|
1147 btFlags |= BTREE_NO_READLOCK; |
|
1148 } |
|
1149 if( zFilename==0 ){ |
|
1150 #if SQLITE_TEMP_STORE==0 |
|
1151 /* Do nothing */ |
|
1152 #endif |
|
1153 #ifndef SQLITE_OMIT_MEMORYDB |
|
1154 #if SQLITE_TEMP_STORE==1 |
|
1155 if( db->temp_store==2 ) zFilename = ":memory:"; |
|
1156 #endif |
|
1157 #if SQLITE_TEMP_STORE==2 |
|
1158 if( db->temp_store!=1 ) zFilename = ":memory:"; |
|
1159 #endif |
|
1160 #if SQLITE_TEMP_STORE==3 |
|
1161 zFilename = ":memory:"; |
|
1162 #endif |
|
1163 #endif /* SQLITE_OMIT_MEMORYDB */ |
|
1164 } |
|
1165 |
|
1166 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){ |
|
1167 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
|
1168 } |
|
1169 rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags); |
|
1170 |
|
1171 /* If the B-Tree was successfully opened, set the pager-cache size to the |
|
1172 ** default value. Except, if the call to BtreeOpen() returned a handle |
|
1173 ** open on an existing shared pager-cache, do not change the pager-cache |
|
1174 ** size. |
|
1175 */ |
|
1176 if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){ |
|
1177 sqlite3BtreeSetCacheSize(*ppBtree, nCache); |
|
1178 } |
|
1179 return rc; |
|
1180 } |
|
1181 |
|
1182 /* |
|
1183 ** Return UTF-8 encoded English language explanation of the most recent |
|
1184 ** error. |
|
1185 */ |
|
1186 const char *sqlite3_errmsg(sqlite3 *db){ |
|
1187 const char *z; |
|
1188 if( !db ){ |
|
1189 return sqlite3ErrStr(SQLITE_NOMEM); |
|
1190 } |
|
1191 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
|
1192 return sqlite3ErrStr(SQLITE_MISUSE); |
|
1193 } |
|
1194 sqlite3_mutex_enter(db->mutex); |
|
1195 assert( !db->mallocFailed ); |
|
1196 z = (char*)sqlite3_value_text(db->pErr); |
|
1197 assert( !db->mallocFailed ); |
|
1198 if( z==0 ){ |
|
1199 z = sqlite3ErrStr(db->errCode); |
|
1200 } |
|
1201 sqlite3_mutex_leave(db->mutex); |
|
1202 return z; |
|
1203 } |
|
1204 |
|
1205 #ifndef SQLITE_OMIT_UTF16 |
|
1206 /* |
|
1207 ** Return UTF-16 encoded English language explanation of the most recent |
|
1208 ** error. |
|
1209 */ |
|
1210 const void *sqlite3_errmsg16(sqlite3 *db){ |
|
1211 /* Because all the characters in the string are in the unicode |
|
1212 ** range 0x00-0xFF, if we pad the big-endian string with a |
|
1213 ** zero byte, we can obtain the little-endian string with |
|
1214 ** &big_endian[1]. |
|
1215 */ |
|
1216 static const char outOfMemBe[] = { |
|
1217 0, 'o', 0, 'u', 0, 't', 0, ' ', |
|
1218 0, 'o', 0, 'f', 0, ' ', |
|
1219 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0 |
|
1220 }; |
|
1221 static const char misuseBe [] = { |
|
1222 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ', |
|
1223 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ', |
|
1224 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', |
|
1225 0, 'o', 0, 'u', 0, 't', 0, ' ', |
|
1226 0, 'o', 0, 'f', 0, ' ', |
|
1227 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0 |
|
1228 }; |
|
1229 |
|
1230 const void *z; |
|
1231 if( !db ){ |
|
1232 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
|
1233 } |
|
1234 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
|
1235 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
|
1236 } |
|
1237 sqlite3_mutex_enter(db->mutex); |
|
1238 assert( !db->mallocFailed ); |
|
1239 z = sqlite3_value_text16(db->pErr); |
|
1240 if( z==0 ){ |
|
1241 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), |
|
1242 SQLITE_UTF8, SQLITE_STATIC); |
|
1243 z = sqlite3_value_text16(db->pErr); |
|
1244 } |
|
1245 /* A malloc() may have failed within the call to sqlite3_value_text16() |
|
1246 ** above. If this is the case, then the db->mallocFailed flag needs to |
|
1247 ** be cleared before returning. Do this directly, instead of via |
|
1248 ** sqlite3ApiExit(), to avoid setting the database handle error message. |
|
1249 */ |
|
1250 db->mallocFailed = 0; |
|
1251 sqlite3_mutex_leave(db->mutex); |
|
1252 return z; |
|
1253 } |
|
1254 #endif /* SQLITE_OMIT_UTF16 */ |
|
1255 |
|
1256 /* |
|
1257 ** Return the most recent error code generated by an SQLite routine. If NULL is |
|
1258 ** passed to this function, we assume a malloc() failed during sqlite3_open(). |
|
1259 */ |
|
1260 int sqlite3_errcode(sqlite3 *db){ |
|
1261 if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
|
1262 return SQLITE_MISUSE; |
|
1263 } |
|
1264 if( !db || db->mallocFailed ){ |
|
1265 return SQLITE_NOMEM; |
|
1266 } |
|
1267 return db->errCode & db->errMask; |
|
1268 } |
|
1269 |
|
1270 /* |
|
1271 ** Create a new collating function for database "db". The name is zName |
|
1272 ** and the encoding is enc. |
|
1273 */ |
|
1274 static int createCollation( |
|
1275 sqlite3* db, |
|
1276 const char *zName, |
|
1277 int enc, |
|
1278 void* pCtx, |
|
1279 int(*xCompare)(void*,int,const void*,int,const void*), |
|
1280 void(*xDel)(void*) |
|
1281 ){ |
|
1282 CollSeq *pColl; |
|
1283 int enc2; |
|
1284 int nName; |
|
1285 |
|
1286 assert( sqlite3_mutex_held(db->mutex) ); |
|
1287 |
|
1288 /* If SQLITE_UTF16 is specified as the encoding type, transform this |
|
1289 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
|
1290 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
|
1291 */ |
|
1292 enc2 = enc & ~SQLITE_UTF16_ALIGNED; |
|
1293 if( enc2==SQLITE_UTF16 ){ |
|
1294 enc2 = SQLITE_UTF16NATIVE; |
|
1295 } |
|
1296 if( (enc2&~3)!=0 ){ |
|
1297 return SQLITE_MISUSE; |
|
1298 } |
|
1299 |
|
1300 /* Check if this call is removing or replacing an existing collation |
|
1301 ** sequence. If so, and there are active VMs, return busy. If there |
|
1302 ** are no active VMs, invalidate any pre-compiled statements. |
|
1303 */ |
|
1304 nName = sqlite3Strlen(db, zName); |
|
1305 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, nName, 0); |
|
1306 if( pColl && pColl->xCmp ){ |
|
1307 if( db->activeVdbeCnt ){ |
|
1308 sqlite3Error(db, SQLITE_BUSY, |
|
1309 "Unable to delete/modify collation sequence due to active statements"); |
|
1310 return SQLITE_BUSY; |
|
1311 } |
|
1312 sqlite3ExpirePreparedStatements(db); |
|
1313 |
|
1314 /* If collation sequence pColl was created directly by a call to |
|
1315 ** sqlite3_create_collation, and not generated by synthCollSeq(), |
|
1316 ** then any copies made by synthCollSeq() need to be invalidated. |
|
1317 ** Also, collation destructor - CollSeq.xDel() - function may need |
|
1318 ** to be called. |
|
1319 */ |
|
1320 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ |
|
1321 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName); |
|
1322 int j; |
|
1323 for(j=0; j<3; j++){ |
|
1324 CollSeq *p = &aColl[j]; |
|
1325 if( p->enc==pColl->enc ){ |
|
1326 if( p->xDel ){ |
|
1327 p->xDel(p->pUser); |
|
1328 } |
|
1329 p->xCmp = 0; |
|
1330 } |
|
1331 } |
|
1332 } |
|
1333 } |
|
1334 |
|
1335 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, nName, 1); |
|
1336 if( pColl ){ |
|
1337 pColl->xCmp = xCompare; |
|
1338 pColl->pUser = pCtx; |
|
1339 pColl->xDel = xDel; |
|
1340 pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED); |
|
1341 } |
|
1342 sqlite3Error(db, SQLITE_OK, 0); |
|
1343 return SQLITE_OK; |
|
1344 } |
|
1345 |
|
1346 |
|
1347 /* |
|
1348 ** This array defines hard upper bounds on limit values. The |
|
1349 ** initializer must be kept in sync with the SQLITE_LIMIT_* |
|
1350 ** #defines in sqlite3.h. |
|
1351 */ |
|
1352 static const int aHardLimit[] = { |
|
1353 SQLITE_MAX_LENGTH, |
|
1354 SQLITE_MAX_SQL_LENGTH, |
|
1355 SQLITE_MAX_COLUMN, |
|
1356 SQLITE_MAX_EXPR_DEPTH, |
|
1357 SQLITE_MAX_COMPOUND_SELECT, |
|
1358 SQLITE_MAX_VDBE_OP, |
|
1359 SQLITE_MAX_FUNCTION_ARG, |
|
1360 SQLITE_MAX_ATTACHED, |
|
1361 SQLITE_MAX_LIKE_PATTERN_LENGTH, |
|
1362 SQLITE_MAX_VARIABLE_NUMBER, |
|
1363 }; |
|
1364 |
|
1365 /* |
|
1366 ** Make sure the hard limits are set to reasonable values |
|
1367 */ |
|
1368 #if SQLITE_MAX_LENGTH<100 |
|
1369 # error SQLITE_MAX_LENGTH must be at least 100 |
|
1370 #endif |
|
1371 #if SQLITE_MAX_SQL_LENGTH<100 |
|
1372 # error SQLITE_MAX_SQL_LENGTH must be at least 100 |
|
1373 #endif |
|
1374 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH |
|
1375 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH |
|
1376 #endif |
|
1377 #if SQLITE_MAX_COMPOUND_SELECT<2 |
|
1378 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 |
|
1379 #endif |
|
1380 #if SQLITE_MAX_VDBE_OP<40 |
|
1381 # error SQLITE_MAX_VDBE_OP must be at least 40 |
|
1382 #endif |
|
1383 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 |
|
1384 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 |
|
1385 #endif |
|
1386 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30 |
|
1387 # error SQLITE_MAX_ATTACHED must be between 0 and 30 |
|
1388 #endif |
|
1389 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 |
|
1390 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 |
|
1391 #endif |
|
1392 #if SQLITE_MAX_VARIABLE_NUMBER<1 |
|
1393 # error SQLITE_MAX_VARIABLE_NUMBER must be at least 1 |
|
1394 #endif |
|
1395 #if SQLITE_MAX_COLUMN>32767 |
|
1396 # error SQLITE_MAX_COLUMN must not exceed 32767 |
|
1397 #endif |
|
1398 |
|
1399 |
|
1400 /* |
|
1401 ** Change the value of a limit. Report the old value. |
|
1402 ** If an invalid limit index is supplied, report -1. |
|
1403 ** Make no changes but still report the old value if the |
|
1404 ** new limit is negative. |
|
1405 ** |
|
1406 ** A new lower limit does not shrink existing constructs. |
|
1407 ** It merely prevents new constructs that exceed the limit |
|
1408 ** from forming. |
|
1409 */ |
|
1410 int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
|
1411 int oldLimit; |
|
1412 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
|
1413 return -1; |
|
1414 } |
|
1415 oldLimit = db->aLimit[limitId]; |
|
1416 if( newLimit>=0 ){ |
|
1417 if( newLimit>aHardLimit[limitId] ){ |
|
1418 newLimit = aHardLimit[limitId]; |
|
1419 } |
|
1420 db->aLimit[limitId] = newLimit; |
|
1421 } |
|
1422 return oldLimit; |
|
1423 } |
|
1424 |
|
1425 /* |
|
1426 ** This routine does the work of opening a database on behalf of |
|
1427 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
|
1428 ** is UTF-8 encoded. |
|
1429 */ |
|
1430 static int openDatabase( |
|
1431 const char *zFilename, /* Database filename UTF-8 encoded */ |
|
1432 sqlite3 **ppDb, /* OUT: Returned database handle */ |
|
1433 unsigned flags, /* Operational flags */ |
|
1434 const char *zVfs /* Name of the VFS to use */ |
|
1435 ){ |
|
1436 sqlite3 *db; |
|
1437 int rc; |
|
1438 CollSeq *pColl; |
|
1439 int isThreadsafe; |
|
1440 |
|
1441 #ifndef SQLITE_OMIT_AUTOINIT |
|
1442 rc = sqlite3_initialize(); |
|
1443 if( rc ) return rc; |
|
1444 #endif |
|
1445 |
|
1446 if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
|
1447 isThreadsafe = 0; |
|
1448 }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
|
1449 isThreadsafe = 0; |
|
1450 }else if( flags & SQLITE_OPEN_FULLMUTEX ){ |
|
1451 isThreadsafe = 1; |
|
1452 }else{ |
|
1453 isThreadsafe = sqlite3GlobalConfig.bFullMutex; |
|
1454 } |
|
1455 |
|
1456 /* Remove harmful bits from the flags parameter */ |
|
1457 flags &= ~( SQLITE_OPEN_DELETEONCLOSE | |
|
1458 SQLITE_OPEN_MAIN_DB | |
|
1459 SQLITE_OPEN_TEMP_DB | |
|
1460 SQLITE_OPEN_TRANSIENT_DB | |
|
1461 SQLITE_OPEN_MAIN_JOURNAL | |
|
1462 SQLITE_OPEN_TEMP_JOURNAL | |
|
1463 SQLITE_OPEN_SUBJOURNAL | |
|
1464 SQLITE_OPEN_MASTER_JOURNAL | |
|
1465 SQLITE_OPEN_NOMUTEX | |
|
1466 SQLITE_OPEN_FULLMUTEX |
|
1467 ); |
|
1468 |
|
1469 /* Allocate the sqlite data structure */ |
|
1470 db = sqlite3MallocZero( sizeof(sqlite3) ); |
|
1471 if( db==0 ) goto opendb_out; |
|
1472 if( isThreadsafe ){ |
|
1473 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
|
1474 if( db->mutex==0 ){ |
|
1475 sqlite3_free(db); |
|
1476 db = 0; |
|
1477 goto opendb_out; |
|
1478 } |
|
1479 } |
|
1480 sqlite3_mutex_enter(db->mutex); |
|
1481 db->errMask = 0xff; |
|
1482 db->priorNewRowid = 0; |
|
1483 db->nDb = 2; |
|
1484 db->magic = SQLITE_MAGIC_BUSY; |
|
1485 db->aDb = db->aDbStatic; |
|
1486 |
|
1487 assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
|
1488 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
|
1489 db->autoCommit = 1; |
|
1490 db->nextAutovac = -1; |
|
1491 db->nextPagesize = 0; |
|
1492 db->flags |= SQLITE_ShortColNames |
|
1493 #if SQLITE_DEFAULT_FILE_FORMAT<4 |
|
1494 | SQLITE_LegacyFileFmt |
|
1495 #endif |
|
1496 #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
|
1497 | SQLITE_LoadExtension |
|
1498 #endif |
|
1499 ; |
|
1500 sqlite3HashInit(&db->aCollSeq, 0); |
|
1501 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
1502 sqlite3HashInit(&db->aModule, 0); |
|
1503 #endif |
|
1504 |
|
1505 db->pVfs = sqlite3_vfs_find(zVfs); |
|
1506 if( !db->pVfs ){ |
|
1507 rc = SQLITE_ERROR; |
|
1508 sqlite3Error(db, rc, "no such vfs: %s", zVfs); |
|
1509 goto opendb_out; |
|
1510 } |
|
1511 |
|
1512 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
|
1513 ** and UTF-16, so add a version for each to avoid any unnecessary |
|
1514 ** conversions. The only error that can occur here is a malloc() failure. |
|
1515 */ |
|
1516 createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
|
1517 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
|
1518 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
|
1519 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
|
1520 if( db->mallocFailed ){ |
|
1521 goto opendb_out; |
|
1522 } |
|
1523 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0); |
|
1524 assert( db->pDfltColl!=0 ); |
|
1525 |
|
1526 /* Also add a UTF-8 case-insensitive collation sequence. */ |
|
1527 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
|
1528 |
|
1529 /* Set flags on the built-in collating sequences */ |
|
1530 db->pDfltColl->type = SQLITE_COLL_BINARY; |
|
1531 pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0); |
|
1532 if( pColl ){ |
|
1533 pColl->type = SQLITE_COLL_NOCASE; |
|
1534 } |
|
1535 |
|
1536 /* Open the backend database driver */ |
|
1537 db->openFlags = flags; |
|
1538 rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, |
|
1539 flags | SQLITE_OPEN_MAIN_DB, |
|
1540 &db->aDb[0].pBt); |
|
1541 if( rc!=SQLITE_OK ){ |
|
1542 if( rc==SQLITE_IOERR_NOMEM ){ |
|
1543 rc = SQLITE_NOMEM; |
|
1544 } |
|
1545 sqlite3Error(db, rc, 0); |
|
1546 goto opendb_out; |
|
1547 } |
|
1548 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); |
|
1549 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); |
|
1550 |
|
1551 |
|
1552 /* The default safety_level for the main database is 'full'; for the temp |
|
1553 ** database it is 'NONE'. This matches the pager layer defaults. |
|
1554 */ |
|
1555 db->aDb[0].zName = "main"; |
|
1556 db->aDb[0].safety_level = 3; |
|
1557 #ifndef SQLITE_OMIT_TEMPDB |
|
1558 db->aDb[1].zName = "temp"; |
|
1559 db->aDb[1].safety_level = 1; |
|
1560 #endif |
|
1561 |
|
1562 db->magic = SQLITE_MAGIC_OPEN; |
|
1563 if( db->mallocFailed ){ |
|
1564 goto opendb_out; |
|
1565 } |
|
1566 |
|
1567 /* Register all built-in functions, but do not attempt to read the |
|
1568 ** database schema yet. This is delayed until the first time the database |
|
1569 ** is accessed. |
|
1570 */ |
|
1571 sqlite3Error(db, SQLITE_OK, 0); |
|
1572 sqlite3RegisterBuiltinFunctions(db); |
|
1573 |
|
1574 /* Load automatic extensions - extensions that have been registered |
|
1575 ** using the sqlite3_automatic_extension() API. |
|
1576 */ |
|
1577 (void)sqlite3AutoLoadExtensions(db); |
|
1578 if( sqlite3_errcode(db)!=SQLITE_OK ){ |
|
1579 goto opendb_out; |
|
1580 } |
|
1581 |
|
1582 #ifdef SQLITE_ENABLE_FTS1 |
|
1583 if( !db->mallocFailed ){ |
|
1584 extern int sqlite3Fts1Init(sqlite3*); |
|
1585 rc = sqlite3Fts1Init(db); |
|
1586 } |
|
1587 #endif |
|
1588 |
|
1589 #ifdef SQLITE_ENABLE_FTS2 |
|
1590 if( !db->mallocFailed && rc==SQLITE_OK ){ |
|
1591 extern int sqlite3Fts2Init(sqlite3*); |
|
1592 rc = sqlite3Fts2Init(db); |
|
1593 } |
|
1594 #endif |
|
1595 |
|
1596 #ifdef SQLITE_ENABLE_FTS3 |
|
1597 if( !db->mallocFailed && rc==SQLITE_OK ){ |
|
1598 rc = sqlite3Fts3Init(db); |
|
1599 } |
|
1600 #endif |
|
1601 |
|
1602 #ifdef SQLITE_ENABLE_ICU |
|
1603 if( !db->mallocFailed && rc==SQLITE_OK ){ |
|
1604 rc = sqlite3IcuInit(db); |
|
1605 } |
|
1606 #endif |
|
1607 |
|
1608 #ifdef SQLITE_ENABLE_RTREE |
|
1609 if( !db->mallocFailed && rc==SQLITE_OK){ |
|
1610 rc = sqlite3RtreeInit(db); |
|
1611 } |
|
1612 #endif |
|
1613 |
|
1614 sqlite3Error(db, rc, 0); |
|
1615 |
|
1616 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
|
1617 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
|
1618 ** mode. Doing nothing at all also makes NORMAL the default. |
|
1619 */ |
|
1620 #ifdef SQLITE_DEFAULT_LOCKING_MODE |
|
1621 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; |
|
1622 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), |
|
1623 SQLITE_DEFAULT_LOCKING_MODE); |
|
1624 #endif |
|
1625 |
|
1626 /* Enable the lookaside-malloc subsystem */ |
|
1627 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, |
|
1628 sqlite3GlobalConfig.nLookaside); |
|
1629 |
|
1630 opendb_out: |
|
1631 if( db ){ |
|
1632 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); |
|
1633 sqlite3_mutex_leave(db->mutex); |
|
1634 } |
|
1635 rc = sqlite3_errcode(db); |
|
1636 if( rc==SQLITE_NOMEM ){ |
|
1637 sqlite3_close(db); |
|
1638 db = 0; |
|
1639 }else if( rc!=SQLITE_OK ){ |
|
1640 db->magic = SQLITE_MAGIC_SICK; |
|
1641 } |
|
1642 *ppDb = db; |
|
1643 return sqlite3ApiExit(0, rc); |
|
1644 } |
|
1645 |
|
1646 /* |
|
1647 ** Open a new database handle. |
|
1648 */ |
|
1649 int sqlite3_open( |
|
1650 const char *zFilename, |
|
1651 sqlite3 **ppDb |
|
1652 ){ |
|
1653 return openDatabase(zFilename, ppDb, |
|
1654 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
|
1655 } |
|
1656 int sqlite3_open_v2( |
|
1657 const char *filename, /* Database filename (UTF-8) */ |
|
1658 sqlite3 **ppDb, /* OUT: SQLite db handle */ |
|
1659 int flags, /* Flags */ |
|
1660 const char *zVfs /* Name of VFS module to use */ |
|
1661 ){ |
|
1662 return openDatabase(filename, ppDb, flags, zVfs); |
|
1663 } |
|
1664 |
|
1665 #ifndef SQLITE_OMIT_UTF16 |
|
1666 /* |
|
1667 ** Open a new database handle. |
|
1668 */ |
|
1669 int sqlite3_open16( |
|
1670 const void *zFilename, |
|
1671 sqlite3 **ppDb |
|
1672 ){ |
|
1673 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
|
1674 sqlite3_value *pVal; |
|
1675 int rc; |
|
1676 |
|
1677 assert( zFilename ); |
|
1678 assert( ppDb ); |
|
1679 *ppDb = 0; |
|
1680 #ifndef SQLITE_OMIT_AUTOINIT |
|
1681 rc = sqlite3_initialize(); |
|
1682 if( rc ) return rc; |
|
1683 #endif |
|
1684 pVal = sqlite3ValueNew(0); |
|
1685 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
|
1686 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
|
1687 if( zFilename8 ){ |
|
1688 rc = openDatabase(zFilename8, ppDb, |
|
1689 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
|
1690 assert( *ppDb || rc==SQLITE_NOMEM ); |
|
1691 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ |
|
1692 ENC(*ppDb) = SQLITE_UTF16NATIVE; |
|
1693 } |
|
1694 }else{ |
|
1695 rc = SQLITE_NOMEM; |
|
1696 } |
|
1697 sqlite3ValueFree(pVal); |
|
1698 |
|
1699 return sqlite3ApiExit(0, rc); |
|
1700 } |
|
1701 #endif /* SQLITE_OMIT_UTF16 */ |
|
1702 |
|
1703 /* |
|
1704 ** Register a new collation sequence with the database handle db. |
|
1705 */ |
|
1706 int sqlite3_create_collation( |
|
1707 sqlite3* db, |
|
1708 const char *zName, |
|
1709 int enc, |
|
1710 void* pCtx, |
|
1711 int(*xCompare)(void*,int,const void*,int,const void*) |
|
1712 ){ |
|
1713 int rc; |
|
1714 sqlite3_mutex_enter(db->mutex); |
|
1715 assert( !db->mallocFailed ); |
|
1716 rc = createCollation(db, zName, enc, pCtx, xCompare, 0); |
|
1717 rc = sqlite3ApiExit(db, rc); |
|
1718 sqlite3_mutex_leave(db->mutex); |
|
1719 return rc; |
|
1720 } |
|
1721 |
|
1722 /* |
|
1723 ** Register a new collation sequence with the database handle db. |
|
1724 */ |
|
1725 int sqlite3_create_collation_v2( |
|
1726 sqlite3* db, |
|
1727 const char *zName, |
|
1728 int enc, |
|
1729 void* pCtx, |
|
1730 int(*xCompare)(void*,int,const void*,int,const void*), |
|
1731 void(*xDel)(void*) |
|
1732 ){ |
|
1733 int rc; |
|
1734 sqlite3_mutex_enter(db->mutex); |
|
1735 assert( !db->mallocFailed ); |
|
1736 rc = createCollation(db, zName, enc, pCtx, xCompare, xDel); |
|
1737 rc = sqlite3ApiExit(db, rc); |
|
1738 sqlite3_mutex_leave(db->mutex); |
|
1739 return rc; |
|
1740 } |
|
1741 |
|
1742 #ifndef SQLITE_OMIT_UTF16 |
|
1743 /* |
|
1744 ** Register a new collation sequence with the database handle db. |
|
1745 */ |
|
1746 int sqlite3_create_collation16( |
|
1747 sqlite3* db, |
|
1748 const void *zName, |
|
1749 int enc, |
|
1750 void* pCtx, |
|
1751 int(*xCompare)(void*,int,const void*,int,const void*) |
|
1752 ){ |
|
1753 int rc = SQLITE_OK; |
|
1754 char *zName8; |
|
1755 sqlite3_mutex_enter(db->mutex); |
|
1756 assert( !db->mallocFailed ); |
|
1757 zName8 = sqlite3Utf16to8(db, zName, -1); |
|
1758 if( zName8 ){ |
|
1759 rc = createCollation(db, zName8, enc, pCtx, xCompare, 0); |
|
1760 sqlite3DbFree(db, zName8); |
|
1761 } |
|
1762 rc = sqlite3ApiExit(db, rc); |
|
1763 sqlite3_mutex_leave(db->mutex); |
|
1764 return rc; |
|
1765 } |
|
1766 #endif /* SQLITE_OMIT_UTF16 */ |
|
1767 |
|
1768 /* |
|
1769 ** Register a collation sequence factory callback with the database handle |
|
1770 ** db. Replace any previously installed collation sequence factory. |
|
1771 */ |
|
1772 int sqlite3_collation_needed( |
|
1773 sqlite3 *db, |
|
1774 void *pCollNeededArg, |
|
1775 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
|
1776 ){ |
|
1777 sqlite3_mutex_enter(db->mutex); |
|
1778 db->xCollNeeded = xCollNeeded; |
|
1779 db->xCollNeeded16 = 0; |
|
1780 db->pCollNeededArg = pCollNeededArg; |
|
1781 sqlite3_mutex_leave(db->mutex); |
|
1782 return SQLITE_OK; |
|
1783 } |
|
1784 |
|
1785 #ifndef SQLITE_OMIT_UTF16 |
|
1786 /* |
|
1787 ** Register a collation sequence factory callback with the database handle |
|
1788 ** db. Replace any previously installed collation sequence factory. |
|
1789 */ |
|
1790 int sqlite3_collation_needed16( |
|
1791 sqlite3 *db, |
|
1792 void *pCollNeededArg, |
|
1793 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
|
1794 ){ |
|
1795 sqlite3_mutex_enter(db->mutex); |
|
1796 db->xCollNeeded = 0; |
|
1797 db->xCollNeeded16 = xCollNeeded16; |
|
1798 db->pCollNeededArg = pCollNeededArg; |
|
1799 sqlite3_mutex_leave(db->mutex); |
|
1800 return SQLITE_OK; |
|
1801 } |
|
1802 #endif /* SQLITE_OMIT_UTF16 */ |
|
1803 |
|
1804 #ifndef SQLITE_OMIT_GLOBALRECOVER |
|
1805 #ifndef SQLITE_OMIT_DEPRECATED |
|
1806 /* |
|
1807 ** This function is now an anachronism. It used to be used to recover from a |
|
1808 ** malloc() failure, but SQLite now does this automatically. |
|
1809 */ |
|
1810 int sqlite3_global_recover(void){ |
|
1811 return SQLITE_OK; |
|
1812 } |
|
1813 #endif |
|
1814 #endif |
|
1815 |
|
1816 /* |
|
1817 ** Test to see whether or not the database connection is in autocommit |
|
1818 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
|
1819 ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
|
1820 ** by the next COMMIT or ROLLBACK. |
|
1821 ** |
|
1822 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** |
|
1823 */ |
|
1824 int sqlite3_get_autocommit(sqlite3 *db){ |
|
1825 return db->autoCommit; |
|
1826 } |
|
1827 |
|
1828 #ifdef SQLITE_DEBUG |
|
1829 /* |
|
1830 ** The following routine is subtituted for constant SQLITE_CORRUPT in |
|
1831 ** debugging builds. This provides a way to set a breakpoint for when |
|
1832 ** corruption is first detected. |
|
1833 */ |
|
1834 int sqlite3Corrupt(void){ |
|
1835 return SQLITE_CORRUPT; |
|
1836 } |
|
1837 #endif |
|
1838 |
|
1839 #ifndef SQLITE_OMIT_DEPRECATED |
|
1840 /* |
|
1841 ** This is a convenience routine that makes sure that all thread-specific |
|
1842 ** data for this thread has been deallocated. |
|
1843 ** |
|
1844 ** SQLite no longer uses thread-specific data so this routine is now a |
|
1845 ** no-op. It is retained for historical compatibility. |
|
1846 */ |
|
1847 void sqlite3_thread_cleanup(void){ |
|
1848 } |
|
1849 #endif |
|
1850 |
|
1851 /* |
|
1852 ** Return meta information about a specific column of a database table. |
|
1853 ** See comment in sqlite3.h (sqlite.h.in) for details. |
|
1854 */ |
|
1855 #ifdef SQLITE_ENABLE_COLUMN_METADATA |
|
1856 int sqlite3_table_column_metadata( |
|
1857 sqlite3 *db, /* Connection handle */ |
|
1858 const char *zDbName, /* Database name or NULL */ |
|
1859 const char *zTableName, /* Table name */ |
|
1860 const char *zColumnName, /* Column name */ |
|
1861 char const **pzDataType, /* OUTPUT: Declared data type */ |
|
1862 char const **pzCollSeq, /* OUTPUT: Collation sequence name */ |
|
1863 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ |
|
1864 int *pPrimaryKey, /* OUTPUT: True if column part of PK */ |
|
1865 int *pAutoinc /* OUTPUT: True if column is auto-increment */ |
|
1866 ){ |
|
1867 int rc; |
|
1868 char *zErrMsg = 0; |
|
1869 Table *pTab = 0; |
|
1870 Column *pCol = 0; |
|
1871 int iCol; |
|
1872 |
|
1873 char const *zDataType = 0; |
|
1874 char const *zCollSeq = 0; |
|
1875 int notnull = 0; |
|
1876 int primarykey = 0; |
|
1877 int autoinc = 0; |
|
1878 |
|
1879 /* Ensure the database schema has been loaded */ |
|
1880 sqlite3_mutex_enter(db->mutex); |
|
1881 (void)sqlite3SafetyOn(db); |
|
1882 sqlite3BtreeEnterAll(db); |
|
1883 rc = sqlite3Init(db, &zErrMsg); |
|
1884 sqlite3BtreeLeaveAll(db); |
|
1885 if( SQLITE_OK!=rc ){ |
|
1886 goto error_out; |
|
1887 } |
|
1888 |
|
1889 /* Locate the table in question */ |
|
1890 pTab = sqlite3FindTable(db, zTableName, zDbName); |
|
1891 if( !pTab || pTab->pSelect ){ |
|
1892 pTab = 0; |
|
1893 goto error_out; |
|
1894 } |
|
1895 |
|
1896 /* Find the column for which info is requested */ |
|
1897 if( sqlite3IsRowid(zColumnName) ){ |
|
1898 iCol = pTab->iPKey; |
|
1899 if( iCol>=0 ){ |
|
1900 pCol = &pTab->aCol[iCol]; |
|
1901 } |
|
1902 }else{ |
|
1903 for(iCol=0; iCol<pTab->nCol; iCol++){ |
|
1904 pCol = &pTab->aCol[iCol]; |
|
1905 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ |
|
1906 break; |
|
1907 } |
|
1908 } |
|
1909 if( iCol==pTab->nCol ){ |
|
1910 pTab = 0; |
|
1911 goto error_out; |
|
1912 } |
|
1913 } |
|
1914 |
|
1915 /* The following block stores the meta information that will be returned |
|
1916 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey |
|
1917 ** and autoinc. At this point there are two possibilities: |
|
1918 ** |
|
1919 ** 1. The specified column name was rowid", "oid" or "_rowid_" |
|
1920 ** and there is no explicitly declared IPK column. |
|
1921 ** |
|
1922 ** 2. The table is not a view and the column name identified an |
|
1923 ** explicitly declared column. Copy meta information from *pCol. |
|
1924 */ |
|
1925 if( pCol ){ |
|
1926 zDataType = pCol->zType; |
|
1927 zCollSeq = pCol->zColl; |
|
1928 notnull = pCol->notNull!=0; |
|
1929 primarykey = pCol->isPrimKey!=0; |
|
1930 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; |
|
1931 }else{ |
|
1932 zDataType = "INTEGER"; |
|
1933 primarykey = 1; |
|
1934 } |
|
1935 if( !zCollSeq ){ |
|
1936 zCollSeq = "BINARY"; |
|
1937 } |
|
1938 |
|
1939 error_out: |
|
1940 (void)sqlite3SafetyOff(db); |
|
1941 |
|
1942 /* Whether the function call succeeded or failed, set the output parameters |
|
1943 ** to whatever their local counterparts contain. If an error did occur, |
|
1944 ** this has the effect of zeroing all output parameters. |
|
1945 */ |
|
1946 if( pzDataType ) *pzDataType = zDataType; |
|
1947 if( pzCollSeq ) *pzCollSeq = zCollSeq; |
|
1948 if( pNotNull ) *pNotNull = notnull; |
|
1949 if( pPrimaryKey ) *pPrimaryKey = primarykey; |
|
1950 if( pAutoinc ) *pAutoinc = autoinc; |
|
1951 |
|
1952 if( SQLITE_OK==rc && !pTab ){ |
|
1953 sqlite3DbFree(db, zErrMsg); |
|
1954 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, |
|
1955 zColumnName); |
|
1956 rc = SQLITE_ERROR; |
|
1957 } |
|
1958 sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); |
|
1959 sqlite3DbFree(db, zErrMsg); |
|
1960 rc = sqlite3ApiExit(db, rc); |
|
1961 sqlite3_mutex_leave(db->mutex); |
|
1962 return rc; |
|
1963 } |
|
1964 #endif |
|
1965 |
|
1966 /* |
|
1967 ** Sleep for a little while. Return the amount of time slept. |
|
1968 */ |
|
1969 int sqlite3_sleep(int ms){ |
|
1970 sqlite3_vfs *pVfs; |
|
1971 int rc; |
|
1972 pVfs = sqlite3_vfs_find(0); |
|
1973 if( pVfs==0 ) return 0; |
|
1974 |
|
1975 /* This function works in milliseconds, but the underlying OsSleep() |
|
1976 ** API uses microseconds. Hence the 1000's. |
|
1977 */ |
|
1978 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); |
|
1979 return rc; |
|
1980 } |
|
1981 |
|
1982 /* |
|
1983 ** Enable or disable the extended result codes. |
|
1984 */ |
|
1985 int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
|
1986 sqlite3_mutex_enter(db->mutex); |
|
1987 db->errMask = onoff ? 0xffffffff : 0xff; |
|
1988 sqlite3_mutex_leave(db->mutex); |
|
1989 return SQLITE_OK; |
|
1990 } |
|
1991 |
|
1992 /* |
|
1993 ** Invoke the xFileControl method on a particular database. |
|
1994 */ |
|
1995 int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
|
1996 int rc = SQLITE_ERROR; |
|
1997 int iDb; |
|
1998 sqlite3_mutex_enter(db->mutex); |
|
1999 if( zDbName==0 ){ |
|
2000 iDb = 0; |
|
2001 }else{ |
|
2002 for(iDb=0; iDb<db->nDb; iDb++){ |
|
2003 if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break; |
|
2004 } |
|
2005 } |
|
2006 if( iDb<db->nDb ){ |
|
2007 Btree *pBtree = db->aDb[iDb].pBt; |
|
2008 if( pBtree ){ |
|
2009 Pager *pPager; |
|
2010 sqlite3_file *fd; |
|
2011 sqlite3BtreeEnter(pBtree); |
|
2012 pPager = sqlite3BtreePager(pBtree); |
|
2013 assert( pPager!=0 ); |
|
2014 fd = sqlite3PagerFile(pPager); |
|
2015 assert( fd!=0 ); |
|
2016 if( fd->pMethods ){ |
|
2017 rc = sqlite3OsFileControl(fd, op, pArg); |
|
2018 } |
|
2019 sqlite3BtreeLeave(pBtree); |
|
2020 } |
|
2021 } |
|
2022 sqlite3_mutex_leave(db->mutex); |
|
2023 return rc; |
|
2024 } |
|
2025 |
|
2026 /* |
|
2027 ** Interface to the testing logic. |
|
2028 */ |
|
2029 int sqlite3_test_control(int op, ...){ |
|
2030 int rc = 0; |
|
2031 #ifndef SQLITE_OMIT_BUILTIN_TEST |
|
2032 va_list ap; |
|
2033 va_start(ap, op); |
|
2034 switch( op ){ |
|
2035 |
|
2036 /* |
|
2037 ** Save the current state of the PRNG. |
|
2038 */ |
|
2039 case SQLITE_TESTCTRL_PRNG_SAVE: { |
|
2040 sqlite3PrngSaveState(); |
|
2041 break; |
|
2042 } |
|
2043 |
|
2044 /* |
|
2045 ** Restore the state of the PRNG to the last state saved using |
|
2046 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then |
|
2047 ** this verb acts like PRNG_RESET. |
|
2048 */ |
|
2049 case SQLITE_TESTCTRL_PRNG_RESTORE: { |
|
2050 sqlite3PrngRestoreState(); |
|
2051 break; |
|
2052 } |
|
2053 |
|
2054 /* |
|
2055 ** Reset the PRNG back to its uninitialized state. The next call |
|
2056 ** to sqlite3_randomness() will reseed the PRNG using a single call |
|
2057 ** to the xRandomness method of the default VFS. |
|
2058 */ |
|
2059 case SQLITE_TESTCTRL_PRNG_RESET: { |
|
2060 sqlite3PrngResetState(); |
|
2061 break; |
|
2062 } |
|
2063 |
|
2064 /* |
|
2065 ** sqlite3_test_control(BITVEC_TEST, size, program) |
|
2066 ** |
|
2067 ** Run a test against a Bitvec object of size. The program argument |
|
2068 ** is an array of integers that defines the test. Return -1 on a |
|
2069 ** memory allocation error, 0 on success, or non-zero for an error. |
|
2070 ** See the sqlite3BitvecBuiltinTest() for additional information. |
|
2071 */ |
|
2072 case SQLITE_TESTCTRL_BITVEC_TEST: { |
|
2073 int sz = va_arg(ap, int); |
|
2074 int *aProg = va_arg(ap, int*); |
|
2075 rc = sqlite3BitvecBuiltinTest(sz, aProg); |
|
2076 break; |
|
2077 } |
|
2078 |
|
2079 /* |
|
2080 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) |
|
2081 ** |
|
2082 ** Register hooks to call to indicate which malloc() failures |
|
2083 ** are benign. |
|
2084 */ |
|
2085 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { |
|
2086 typedef void (*void_function)(void); |
|
2087 void_function xBenignBegin; |
|
2088 void_function xBenignEnd; |
|
2089 xBenignBegin = va_arg(ap, void_function); |
|
2090 xBenignEnd = va_arg(ap, void_function); |
|
2091 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); |
|
2092 break; |
|
2093 } |
|
2094 } |
|
2095 va_end(ap); |
|
2096 #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
|
2097 return rc; |
|
2098 } |