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