wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Diagnostics;
3 using System.Text;
4  
5 using sqlite_int64 = System.Int64;
6 using unsigned = System.Int32;
7  
8 using i16 = System.Int16;
9 using u8 = System.Byte;
10 using u16 = System.UInt16;
11 using u32 = System.UInt32;
12 using u64 = System.UInt64;
13  
14 using Pgno = System.UInt32;
15 using sqlite3_int64 = System.Int64;
16  
17 namespace Community.CsharpSqlite
18 {
19 using sqlite3_value = Sqlite3.Mem;
20  
21 public partial class Sqlite3
22 {
23 /*
24 ** 2001 September 15
25 **
26 ** The author disclaims copyright to this source code. In place of
27 ** a legal notice, here is a blessing:
28 **
29 ** May you do good and not evil.
30 ** May you find forgiveness for yourself and forgive others.
31 ** May you share freely, never taking more than you give.
32 **
33 *************************************************************************
34 ** Main file for the SQLite library. The routines in this file
35 ** implement the programmer interface to the library. Routines in
36 ** other files are for internal use by SQLite and should not be
37 ** accessed by users of the library.
38 *************************************************************************
39 ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
40 ** C#-SQLite is an independent reimplementation of the SQLite software library
41 **
42 ** SQLITE_SOURCE_ID: 2011-06-23 19:49:22 4374b7e83ea0a3fbc3691f9c0c936272862f32f2
43 **
44 *************************************************************************
45 */
46 //#include "sqliteInt.h"
47 #if SQLITE_ENABLE_FTS3
48 //# include "fts3.h"
49 #endif
50 #if SQLITE_ENABLE_RTREE
51 //# include "rtree.h"
52 #endif
53 #if SQLITE_ENABLE_ICU
54 //# include "sqliteicu.h"
55 #endif
56  
57 #if !SQLITE_AMALGAMATION
58 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
59 ** contains the text of SQLITE_VERSION macro.
60 */
61 public static string sqlite3_version = SQLITE_VERSION;
62 #endif
63  
64 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
65 ** a pointer to the to the sqlite3_version[] string constant.
66 */
67 public static string sqlite3_libversion()
68 {
69 return sqlite3_version;
70 }
71  
72 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
73 ** pointer to a string constant whose value is the same as the
74 ** SQLITE_SOURCE_ID C preprocessor macro.
75 */
76 public static string sqlite3_sourceid()
77 {
78 return SQLITE_SOURCE_ID;
79 }
80  
81 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
82 ** returns an integer equal to SQLITE_VERSION_NUMBER.
83 */
84 public static int sqlite3_libversion_number()
85 {
86 return SQLITE_VERSION_NUMBER;
87 }
88  
89 /* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns
90 ** zero if and only if SQLite was compiled mutexing code omitted due to
91 ** the SQLITE_THREADSAFE compile-time option being set to 0.
92 */
93 public static int sqlite3_threadsafe()
94 {
95 return SQLITE_THREADSAFE;
96 }
97  
98 #if !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE
99 /*
100 ** If the following function pointer is not NULL and if
101 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
102 ** I/O active are written using this function. These messages
103 ** are intended for debugging activity only.
104 */
105 //void (*sqlite3IoTrace)(const char*, ...) = 0;
106 static void sqlite3IoTrace( string X, params object[] ap ) { }
107 #endif
108  
109 /*
110 ** If the following global variable points to a string which is the
111 ** name of a directory, then that directory will be used to store
112 ** temporary files.
113 **
114 ** See also the "PRAGMA temp_store_directory" SQL command.
115 */
116 static string sqlite3_temp_directory = string.Empty;//string sqlite3_temp_directory = 0;
117  
118 /*
119 ** Initialize SQLite.
120 **
121 ** This routine must be called to initialize the memory allocation,
122 ** VFS, and mutex subsystems prior to doing any serious work with
123 ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
124 ** this routine will be called automatically by key routines such as
125 ** sqlite3_open().
126 **
127 ** This routine is a no-op except on its very first call for the process,
128 ** or for the first call after a call to sqlite3_shutdown.
129 **
130 ** The first thread to call this routine runs the initialization to
131 ** completion. If subsequent threads call this routine before the first
132 ** thread has finished the initialization process, then the subsequent
133 ** threads must block until the first thread finishes with the initialization.
134 **
135 ** The first thread might call this routine recursively. Recursive
136 ** calls to this routine should not block, of course. Otherwise the
137 ** initialization process would never complete.
138 **
139 ** Let X be the first thread to enter this routine. Let Y be some other
140 ** thread. Then while the initial invocation of this routine by X is
141 ** incomplete, it is required that:
142 **
143 ** * Calls to this routine from Y must block until the outer-most
144 ** call by X completes.
145 **
146 ** * Recursive calls to this routine from thread X return immediately
147 ** without blocking.
148 */
149 static int sqlite3_initialize()
150 {
151 //--------------------------------------------------------------------
152 // Under C#, Need to initialize some static variables
153 //
154 if ( sqlite3_version == null )
155 sqlite3_version = SQLITE_VERSION;
156 if ( sqlite3OpcodeProperty == null )
157 sqlite3OpcodeProperty = OPFLG_INITIALIZER;
158 if ( sqlite3GlobalConfig == null )
159 sqlite3GlobalConfig = sqlite3Config;
160 //--------------------------------------------------------------------
161  
162  
163 sqlite3_mutex pMaster; /* The main static mutex */
164 int rc; /* Result code */
165  
166 #if SQLITE_OMIT_WSD
167 rc = sqlite3_wsd_init(4096, 24);
168 if( rc!=SQLITE_OK ){
169 return rc;
170 }
171 #endif
172 /* If SQLite is already completely initialized, then this call
173 ** to sqlite3_initialize() should be a no-op. But the initialization
174 ** must be complete. So isInit must not be set until the very end
175 ** of this routine.
176 */
177 if ( sqlite3GlobalConfig.isInit != 0 )
178 return SQLITE_OK;
179  
180 /* Make sure the mutex subsystem is initialized. If unable to
181 ** initialize the mutex subsystem, return early with the error.
182 ** If the system is so sick that we are unable to allocate a mutex,
183 ** there is not much SQLite is going to be able to do.
184 **
185 ** The mutex subsystem must take care of serializing its own
186 ** initialization.
187 */
188 rc = sqlite3MutexInit();
189 if ( rc != 0 )
190 return rc;
191  
192 /* Initialize the malloc() system and the recursive pInitMutex mutex.
193 ** This operation is protected by the STATIC_MASTER mutex. Note that
194 ** MutexAlloc() is called for a static mutex prior to initializing the
195 ** malloc subsystem - this implies that the allocation of a static
196 ** mutex must not require support from the malloc subsystem.
197 */
198 pMaster = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER );
199 //sqlite3_mutex_enter( pMaster );
200 lock ( pMaster )
201 {
202 sqlite3GlobalConfig.isMutexInit = 1;
203 if ( sqlite3GlobalConfig.isMallocInit == 0 )
204 {
205 rc = sqlite3MallocInit();
206 }
207 if ( rc == SQLITE_OK )
208 {
209 sqlite3GlobalConfig.isMallocInit = 1;
210 if ( sqlite3GlobalConfig.pInitMutex == null )
211 {
212 sqlite3GlobalConfig.pInitMutex =
213 sqlite3MutexAlloc( SQLITE_MUTEX_RECURSIVE );
214 if ( sqlite3GlobalConfig.bCoreMutex && sqlite3GlobalConfig.pInitMutex == null )
215 {
216 rc = SQLITE_NOMEM;
217 }
218 }
219 }
220 if ( rc == SQLITE_OK )
221 {
222 sqlite3GlobalConfig.nRefInitMutex++;
223 }
224 }
225 //sqlite3_mutex_leave( pMaster );
226  
227 /* If rc is not SQLITE_OK at this point, then either the malloc
228 ** subsystem could not be initialized or the system failed to allocate
229 ** the pInitMutex mutex. Return an error in either case. */
230 if ( rc != SQLITE_OK )
231 {
232 return rc;
233 }
234  
235 /* Do the rest of the initialization under the recursive mutex so
236 ** that we will be able to handle recursive calls into
237 ** sqlite3_initialize(). The recursive calls normally come through
238 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
239 ** recursive calls might also be possible.
240 **
241 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
242 ** to the xInit method, so the xInit method need not be threadsafe.
243 **
244 ** The following mutex is what serializes access to the appdef pcache xInit
245 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
246 ** call to sqlite3PcacheInitialize().
247 */
248 //sqlite3_mutex_enter( sqlite3GlobalConfig.pInitMutex );
249 lock ( sqlite3GlobalConfig.pInitMutex )
250 {
251 if ( sqlite3GlobalConfig.isInit == 0 && sqlite3GlobalConfig.inProgress == 0 )
252 {
253 sqlite3GlobalConfig.inProgress = 1;
254 #if SQLITE_OMIT_WSD
255 FuncDefHash *pHash = GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
256 memset( pHash, 0, sizeof( sqlite3GlobalFunctions ) );
257 #else
258 sqlite3GlobalFunctions = new FuncDefHash();
259 ////FuncDefHash pHash = sqlite3GlobalFunctions;
260 #endif
261 sqlite3RegisterGlobalFunctions();
262 if ( sqlite3GlobalConfig.isPCacheInit == 0 )
263 {
264 rc = sqlite3PcacheInitialize();
265 }
266 if ( rc == SQLITE_OK )
267 {
268 sqlite3GlobalConfig.isPCacheInit = 1;
269 rc = sqlite3_os_init();
270 }
271 if ( rc == SQLITE_OK )
272 {
273 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
274 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage );
275 sqlite3GlobalConfig.isInit = 1;
276 }
277 sqlite3GlobalConfig.inProgress = 0;
278 }
279 }
280 //sqlite3_mutex_leave( sqlite3GlobalConfig.pInitMutex );
281  
282 /* Go back under the static mutex and clean up the recursive
283 ** mutex to prevent a resource leak.
284 */
285 //sqlite3_mutex_enter( pMaster );
286 lock ( pMaster )
287 {
288 sqlite3GlobalConfig.nRefInitMutex--;
289 if ( sqlite3GlobalConfig.nRefInitMutex <= 0 )
290 {
291 Debug.Assert( sqlite3GlobalConfig.nRefInitMutex == 0 );
292 //sqlite3_mutex_free( ref sqlite3GlobalConfig.pInitMutex );
293 sqlite3GlobalConfig.pInitMutex = null;
294 }
295 }
296 //sqlite3_mutex_leave( pMaster );
297  
298 /* The following is just a sanity check to make sure SQLite has
299 ** been compiled correctly. It is important to run this code, but
300 ** we don't want to run it too often and soak up CPU cycles for no
301 ** reason. So we run it once during initialization.
302 */
303 #if !NDEBUG
304 #if !SQLITE_OMIT_FLOATING_POINT
305 /* This section of code's only "output" is via Debug.Assert() statements. */
306 if ( rc == SQLITE_OK )
307 {
308 //u64 x = ( ( (u64)1 ) << 63 ) - 1;
309 //double y;
310 //Debug.Assert( sizeof( u64 ) == 8 );
311 //Debug.Assert( sizeof( u64 ) == sizeof( double ) );
312 //memcpy( &y, x, 8 );
313 //Debug.Assert( sqlite3IsNaN( y ) );
314 }
315 #endif
316 #endif
317  
318 return rc;
319 }
320  
321 /*
322 ** Undo the effects of sqlite3_initialize(). Must not be called while
323 ** there are outstanding database connections or memory allocations or
324 ** while any part of SQLite is otherwise in use in any thread. This
325 ** routine is not threadsafe. But it is safe to invoke this routine
326 ** on when SQLite is already shut down. If SQLite is already shut down
327 ** when this routine is invoked, then this routine is a harmless no-op.
328 */
329 public static int sqlite3_shutdown()
330 {
331 if ( sqlite3GlobalConfig.isInit != 0 )
332 {
333 sqlite3_os_end();
334 sqlite3_reset_auto_extension();
335 sqlite3GlobalConfig.isInit = 0;
336 }
337 if ( sqlite3GlobalConfig.isPCacheInit != 0 )
338 {
339 sqlite3PcacheShutdown();
340 sqlite3GlobalConfig.isPCacheInit = 0;
341 }
342 if ( sqlite3GlobalConfig.isMallocInit != 0 )
343 {
344 sqlite3MallocEnd();
345 sqlite3GlobalConfig.isMallocInit = 0;
346 }
347 if ( sqlite3GlobalConfig.isMutexInit != 0 )
348 {
349 sqlite3MutexEnd();
350 sqlite3GlobalConfig.isMutexInit = 0;
351 }
352 return SQLITE_OK;
353 }
354  
355 /*
356 ** This API allows applications to modify the global configuration of
357 ** the SQLite library at run-time.
358 **
359 ** This routine should only be called when there are no outstanding
360 ** database connections or memory allocations. This routine is not
361 ** threadsafe. Failure to heed these warnings can lead to unpredictable
362 ** behavior.
363 */
364 // Overloads for ap assignments
365 static int sqlite3_config( int op, sqlite3_pcache_methods ap )
366 { // va_list ap;
367 int rc = SQLITE_OK;
368 switch ( op )
369 {
370 case SQLITE_CONFIG_PCACHE:
371 {
372 /* Specify an alternative malloc implementation */
373 sqlite3GlobalConfig.pcache = ap; //sqlite3GlobalConfig.pcache = (sqlite3_pcache_methods)va_arg(ap, "sqlite3_pcache_methods");
374 break;
375 }
376 }
377 return rc;
378 }
379  
380 static int sqlite3_config( int op, ref sqlite3_pcache_methods ap )
381 { // va_list ap;
382 int rc = SQLITE_OK;
383 switch ( op )
384 {
385 case SQLITE_CONFIG_GETPCACHE:
386 {
387 if ( sqlite3GlobalConfig.pcache.xInit == null )
388 {
389 sqlite3PCacheSetDefault();
390 }
391 ap = sqlite3GlobalConfig.pcache;//va_arg(ap, sqlite3_pcache_methods) = sqlite3GlobalConfig.pcache;
392 break;
393 }
394 }
395 return rc;
396 }
397  
398 static int sqlite3_config( int op, sqlite3_mem_methods ap )
399 { // va_list ap;
400 int rc = SQLITE_OK;
401 switch ( op )
402 {
403 case SQLITE_CONFIG_MALLOC:
404 {
405 /* Specify an alternative malloc implementation */
406 sqlite3GlobalConfig.m = ap;// (sqlite3_mem_methods)va_arg( ap, "sqlite3_mem_methods" );
407 break;
408 }
409 }
410 return rc;
411 }
412  
413 static int sqlite3_config( int op, ref sqlite3_mem_methods ap )
414 { // va_list ap;
415 int rc = SQLITE_OK;
416 switch ( op )
417 {
418 case SQLITE_CONFIG_GETMALLOC:
419 {
420 /* Retrieve the current malloc() implementation */
421 //if ( sqlite3GlobalConfig.m.xMalloc == null ) sqlite3MemSetDefault();
422 ap = sqlite3GlobalConfig.m;//va_arg(ap, sqlite3_mem_methods) = sqlite3GlobalConfig.m;
423 break;
424 }
425 }
426 return rc;
427 }
428  
429 #if SQLITE_THREADSAFE // && SQLITE_THREADSAFE>0
430 static int sqlite3_config( int op, sqlite3_mutex_methods ap )
431 {
432 // va_list ap;
433 int rc = SQLITE_OK;
434 switch ( op )
435 {
436 case SQLITE_CONFIG_MUTEX:
437 {
438 /* Specify an alternative mutex implementation */
439 sqlite3GlobalConfig.mutex = ap;// (sqlite3_mutex_methods)va_arg( ap, "sqlite3_mutex_methods" );
440 break;
441 }
442 }
443 return rc;
444 }
445  
446 static int sqlite3_config( int op, ref sqlite3_mutex_methods ap )
447 {
448 // va_list ap;
449 int rc = SQLITE_OK;
450 switch ( op )
451 {
452 case SQLITE_CONFIG_GETMUTEX:
453 {
454 /* Retrieve the current mutex implementation */
455 ap = sqlite3GlobalConfig.mutex;// *va_arg(ap, sqlite3_mutex_methods) = sqlite3GlobalConfig.mutex;
456 break;
457 }
458 }
459 return rc;
460 }
461 #endif
462  
463 static int sqlite3_config( int op, params object[] ap )
464 {
465 // va_list ap;
466 int rc = SQLITE_OK;
467  
468 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
469 ** the SQLite library is in use. */
470 if ( sqlite3GlobalConfig.isInit != 0 )
471 return SQLITE_MISUSE_BKPT();
472  
473 lock ( lock_va_list )
474 {
475 va_start( ap, null );
476 switch ( op )
477 {
478  
479 /* Mutex configuration options are only available in a threadsafe
480 ** compile.
481 */
482 #if SQLITE_THREADSAFE
483 case SQLITE_CONFIG_SINGLETHREAD:
484 {
485 /* Disable all mutexing */
486 sqlite3GlobalConfig.bCoreMutex = false;
487 sqlite3GlobalConfig.bFullMutex = false;
488 break;
489 }
490 case SQLITE_CONFIG_MULTITHREAD:
491 {
492 /* Disable mutexing of database connections */
493 /* Enable mutexing of core data structures */
494 sqlite3GlobalConfig.bCoreMutex = true;
495 sqlite3GlobalConfig.bFullMutex = false;
496 break;
497 }
498 case SQLITE_CONFIG_SERIALIZED:
499 {
500 /* Enable all mutexing */
501 sqlite3GlobalConfig.bCoreMutex = true;
502 sqlite3GlobalConfig.bFullMutex = true;
503 break;
504 }
505 case SQLITE_CONFIG_MUTEX:
506 {
507 /* Specify an alternative mutex implementation */
508 sqlite3GlobalConfig.mutex = va_arg( ap, (sqlite3_mutex_methods)null );
509 break;
510 }
511 case SQLITE_CONFIG_GETMUTEX:
512 {
513 /* Retrieve the current mutex implementation */
514 Debugger.Break(); // TODO -- *va_arg(ap, sqlite3_mutex_methods) = sqlite3GlobalConfig.mutex;
515 break;
516 }
517 #endif
518 case SQLITE_CONFIG_MALLOC:
519 {
520 Debugger.Break(); // TODO --
521 /* Specify an alternative malloc implementation */
522 sqlite3GlobalConfig.m = va_arg( ap, (sqlite3_mem_methods)null );
523 break;
524 }
525 case SQLITE_CONFIG_GETMALLOC:
526 {
527 /* Retrieve the current malloc() implementation */
528 //if ( sqlite3GlobalConfig.m.xMalloc == null ) sqlite3MemSetDefault();
529 //Debugger.Break(); // TODO --//va_arg(ap, sqlite3_mem_methods) = sqlite3GlobalConfig.m;
530 break;
531 }
532 case SQLITE_CONFIG_MEMSTATUS:
533 {
534 /* Enable or disable the malloc status collection */
535 sqlite3GlobalConfig.bMemstat = va_arg( ap, ( Int32 ) 0 ) != 0;
536 break;
537 }
538 case SQLITE_CONFIG_SCRATCH:
539 {
540 /* Designate a buffer for scratch memory space */
541 sqlite3GlobalConfig.pScratch = va_arg( ap, (Byte[][])null );
542 sqlite3GlobalConfig.szScratch = va_arg( ap, (Int32)0 );
543 sqlite3GlobalConfig.nScratch = va_arg( ap, ( Int32 ) 0 );
544 break;
545 }
546  
547 case SQLITE_CONFIG_PAGECACHE:
548 {
549 /* Designate a buffer for page cache memory space */
550 sqlite3GlobalConfig.pPage = va_arg( ap, (MemPage) null );
551 sqlite3GlobalConfig.szPage = va_arg( ap, ( Int32 ) 0 );
552 sqlite3GlobalConfig.nPage = va_arg( ap, ( Int32 ) 0 );
553 break;
554 }
555  
556 case SQLITE_CONFIG_PCACHE:
557 {
558 /* Specify an alternative page cache implementation */
559 Debugger.Break(); // TODO --sqlite3GlobalConfig.pcache = (sqlite3_pcache_methods)va_arg(ap, "sqlite3_pcache_methods");
560 break;
561 }
562  
563 case SQLITE_CONFIG_GETPCACHE:
564 {
565 if ( sqlite3GlobalConfig.pcache.xInit == null )
566 {
567 sqlite3PCacheSetDefault();
568 }
569 Debugger.Break(); // TODO -- *va_arg(ap, sqlite3_pcache_methods) = sqlite3GlobalConfig.pcache;
570 break;
571 }
572  
573 #if SQLITE_ENABLE_MEMSYS3 || SQLITE_ENABLE_MEMSYS5
574 case SQLITE_CONFIG_HEAP: {
575 /* Designate a buffer for heap memory space */
576 sqlite3GlobalConfig.pHeap = va_arg(ap, void);
577 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
578 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
579  
580 if( sqlite3GlobalConfig.mnReq<1 ){
581 sqlite3GlobalConfig.mnReq = 1;
582 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
583 /* cap min request size at 2^12 */
584 sqlite3GlobalConfig.mnReq = (1<<12);
585 }
586  
587 if( sqlite3GlobalConfig.pHeap==0 ){
588 /* If the heap pointer is NULL, then restore the malloc implementation
589 ** back to NULL pointers too. This will cause the malloc to go
590 ** back to its default implementation when sqlite3_initialize() is
591 ** run.
592 */
593 memset(& sqlite3GlobalConfig.m, 0, sizeof( sqlite3GlobalConfig.m));
594 }else{
595 /* The heap pointer is not NULL, then install one of the
596 ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
597 ** ENABLE_MEMSYS5 is defined, return an error.
598 */
599 #if SQLITE_ENABLE_MEMSYS3
600 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
601 #endif
602 #if SQLITE_ENABLE_MEMSYS5
603 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
604 #endif
605 }
606 break;
607 }
608 #endif
609  
610 case SQLITE_CONFIG_LOOKASIDE:
611 {
612 sqlite3GlobalConfig.szLookaside = va_arg( ap, ( Int32 ) 0 );
613 sqlite3GlobalConfig.nLookaside = va_arg( ap, ( Int32 ) 0 );
614 break;
615 }
616  
617 /* Record a pointer to the logger funcction and its first argument.
618 ** The default is NULL. Logging is disabled if the function pointer is
619 ** NULL.
620 */
621 case SQLITE_CONFIG_LOG:
622 {
623 /* MSVC is picky about pulling func ptrs from va lists.
624 ** http://support.microsoft.com/kb/47961
625 ** sqlite3GlobalConfig.xLog = va_arg(ap, void()(void*,int,const char));
626 */
627 //typedef void(*LOGFUNC_t)(void*,int,const char);
628 sqlite3GlobalConfig.xLog = va_arg( ap, (dxLog)null );//"LOGFUNC_t" );
629 sqlite3GlobalConfig.pLogArg = va_arg( ap, (Object)null );
630 break;
631 }
632 case SQLITE_CONFIG_URI: {
633 sqlite3GlobalConfig.bOpenUri = va_arg( ap, (Boolean)true );
634 break;
635 }
636 default:
637 {
638 rc = SQLITE_ERROR;
639 break;
640 }
641 }
642 va_end( ref ap );
643 }
644 return rc;
645 }
646  
647 /*
648 ** Set up the lookaside buffers for a database connection.
649 ** Return SQLITE_OK on success.
650 ** If lookaside is already active, return SQLITE_BUSY.
651 **
652 ** The sz parameter is the number of bytes in each lookaside slot.
653 ** The cnt parameter is the number of slots. If pStart is NULL the
654 ** space for the lookaside memory is obtained from sqlite3_malloc().
655 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
656 ** the lookaside memory.
657 */
658 static int setupLookaside( sqlite3 db, byte[] pBuf, int sz, int cnt )
659 {
660 //void* pStart;
661 //if ( db.lookaside.nOut )
662 //{
663 // return SQLITE_BUSY;
664 //}
665 ///* Free any existing lookaside buffer for this handle before
666 //** allocating a new one so we don't have to have space for
667 //** both at the same time.
668 //*/
669 //if ( db.lookaside.bMalloced )
670 //{
671 // //sqlite3_free( db.lookaside.pStart );
672 //}
673 ///* The size of a lookaside slot needs to be larger than a pointer
674 //** to be useful.
675 //*/
676 //if ( sz <= (int)sizeof( LookasideSlot* ) ) sz = 0;
677 //if ( cnt < 0 ) cnt = 0;
678 //if ( sz == 0 || cnt == 0 )
679 //{
680 // sz = 0;
681 // pStart = 0;
682 //}
683 //else if ( pBuf == 0 )
684 //{
685 // sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
686 // sqlite3BeginBenignMalloc();
687 // pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
688 // sqlite3EndBenignMalloc();
689 //}else{
690 // sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
691 // pStart = pBuf;
692 //}
693 //db.lookaside.pStart = pStart;
694 //db.lookaside.pFree = 0;
695 //db.lookaside.sz = (u16)sz;
696 //if ( pStart )
697 //{
698 // int i;
699 // LookasideSlot* p;
700 // Debug.Assert( sz > sizeof( LookasideSlot* ) );
701 // p = (LookasideSlot)pStart;
702 // for ( i = cnt - 1 ; i >= 0 ; i-- )
703 // {
704 // p.pNext = db.lookaside.pFree;
705 // db.lookaside.pFree = p;
706 // p = (LookasideSlot)&( (u8)p )[sz];
707 // }
708 // db.lookaside.pEnd = p;
709 // db.lookaside.bEnabled = 1;
710 // db.lookaside.bMalloced = pBuf == 0 ? 1 : 0;
711 //}
712 //else
713 //{
714 // db.lookaside.pEnd = 0;
715 // db.lookaside.bEnabled = 0;
716 // db.lookaside.bMalloced = 0;
717 //}
718 return SQLITE_OK;
719 }
720  
721 /*
722 ** Return the mutex associated with a database connection.
723 */
724 static sqlite3_mutex sqlite3_db_mutex( sqlite3 db )
725 {
726 return db.mutex;
727 }
728  
729 public class _aFlagOp
730 {
731 public int op; /* The opcode */
732 public u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
733  
734 public _aFlagOp( int op, u32 mask )
735 {
736 this.op = op;
737 this.mask = mask;
738 }
739 }
740  
741 /*
742 ** Configuration settings for an individual database connection
743 */
744 static int sqlite3_db_config( sqlite3 db, int op, params object[] ap )
745 {
746 int rc;
747 //va_list ap;
748 lock ( lock_va_list )
749 {
750 va_start( ap, string.Empty );
751 switch ( op )
752 {
753 case SQLITE_DBCONFIG_LOOKASIDE:
754 {
755 byte[] pBuf = va_arg( ap, (byte[])null ); /* IMP: R-26835-10964 */
756 int sz = va_arg( ap, (Int32)0 ); /* IMP: R-47871-25994 */
757 int cnt = va_arg( ap, ( Int32 ) 0 ); /* IMP: R-04460-53386 */
758 rc = setupLookaside( db, pBuf, sz, cnt );
759 break;
760 }
761 default:
762 {
763 _aFlagOp[] aFlagOp = new _aFlagOp[]{
764 new _aFlagOp( SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys ),
765 new _aFlagOp( SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger ),
766 };
767 uint i;
768  
769 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
770 for ( i = 0; i < ArraySize( aFlagOp ); i++ )
771 {
772 if ( aFlagOp[i].op == op )
773 {
774 int onoff = va_arg( ap, ( Int32 ) 0 );
775 int pRes = va_arg( ap, (Int32)0 );
776 int oldFlags = db.flags;
777 if ( onoff > 0 )
778 {
779 db.flags = (int)( (u32)db.flags | aFlagOp[i].mask );
780 }
781 else if ( onoff == 0 )
782 {
783 db.flags = (int)( db.flags & ~aFlagOp[i].mask );
784 }
785 if ( oldFlags != db.flags )
786 {
787 sqlite3ExpirePreparedStatements( db );
788 }
789 if ( pRes != 0 )
790 {
791 pRes = ( db.flags & aFlagOp[i].mask ) != 0 ? 1 : 0;
792 }
793 rc = SQLITE_OK;
794 break;
795 }
796 }
797 break;
798 }
799 }
800 va_end( ref ap );
801 }
802 return rc;
803 }
804  
805  
806 /*
807 ** Return true if the buffer z[0..n-1] contains all spaces.
808 */
809 static bool allSpaces( string z, int iStart, int n )
810 {
811 while ( n > 0 && z[iStart + n - 1] == ' ' )
812 {
813 n--;
814 }
815 return n == 0;
816 }
817  
818 /*
819 ** This is the default collating function named "BINARY" which is always
820 ** available.
821 **
822 ** If the padFlag argument is not NULL then space padding at the end
823 ** of strings is ignored. This implements the RTRIM collation.
824 */
825 static int binCollFunc(
826 object padFlag,
827 int nKey1, string pKey1,
828 int nKey2, string pKey2
829 )
830 {
831 int rc, n;
832 n = nKey1 < nKey2 ? nKey1 : nKey2;
833 rc = memcmp( pKey1, pKey2, n );
834 if ( rc == 0 )
835 {
836 if ( (int)padFlag != 0 && allSpaces( pKey1, n, nKey1 - n ) && allSpaces( pKey2, n, nKey2 - n ) )
837 {
838 /* Leave rc unchanged at 0 */
839 }
840 else
841 {
842 rc = nKey1 - nKey2;
843 }
844 }
845 return rc;
846 }
847  
848 /*
849 ** Another built-in collating sequence: NOCASE.
850 **
851 ** This collating sequence is intended to be used for "case independant
852 ** comparison". SQLite's knowledge of upper and lower case equivalents
853 ** extends only to the 26 characters used in the English language.
854 **
855 ** At the moment there is only a UTF-8 implementation.
856 */
857 static int nocaseCollatingFunc(
858 object NotUsed,
859 int nKey1, string pKey1,
860 int nKey2, string pKey2
861 )
862 {
863 ////int n = ( nKey1 < nKey2 ) ? nKey1 : nKey2;
864 int r = sqlite3StrNICmp( pKey1, pKey2, ( nKey1 < nKey2 ) ? nKey1 : nKey2 );
865 UNUSED_PARAMETER( NotUsed );
866 if ( 0 == r )
867 {
868 r = nKey1 - nKey2;
869 }
870 return r;
871 }
872  
873 /*
874 ** Return the ROWID of the most recent insert
875 */
876 static public sqlite_int64 sqlite3_last_insert_rowid( sqlite3 db )
877 {
878 return db.lastRowid;
879 }
880  
881 /*
882 ** Return the number of changes in the most recent call to sqlite3_exec().
883 */
884 static public int sqlite3_changes( sqlite3 db )
885 {
886 return db.nChange;
887 }
888  
889 /*
890 ** Return the number of changes since the database handle was opened.
891 */
892 static public int sqlite3_total_changes( sqlite3 db )
893 {
894 return db.nTotalChange;
895 }
896  
897 /*
898 ** Close all open savepoints. This function only manipulates fields of the
899 ** database handle object, it does not close any savepoints that may be open
900 ** at the b-tree/pager level.
901 */
902 static void sqlite3CloseSavepoints( sqlite3 db )
903 {
904 while ( db.pSavepoint != null )
905 {
906 Savepoint pTmp = db.pSavepoint;
907 db.pSavepoint = pTmp.pNext;
908 sqlite3DbFree( db, ref pTmp );
909 }
910 db.nSavepoint = 0;
911 db.nStatement = 0;
912 db.isTransactionSavepoint = 0;
913 }
914  
915 /*
916 ** Invoke the destructor function associated with FuncDef p, if any. Except,
917 ** if this is not the last copy of the function, do not invoke it. Multiple
918 ** copies of a single function are created when create_function() is called
919 ** with SQLITE_ANY as the encoding.
920 */
921 static void functionDestroy( sqlite3 db, FuncDef p )
922 {
923 FuncDestructor pDestructor = p.pDestructor;
924 if ( pDestructor != null )
925 {
926 pDestructor.nRef--;
927 if ( pDestructor.nRef == 0 )
928 {
929 //pDestructor.xDestroy( pDestructor.pUserData );
930 sqlite3DbFree( db, ref pDestructor );
931 }
932 }
933 }
934  
935 /*
936 ** Close an existing SQLite database
937 */
938 public static int sqlite3_close( sqlite3 db )
939 {
940 HashElem i; /* Hash table iterator */
941 int j;
942  
943 if ( db == null )
944 {
945 return SQLITE_OK;
946 }
947 if ( !sqlite3SafetyCheckSickOrOk( db ) )
948 {
949 return SQLITE_MISUSE_BKPT();
950 }
951 sqlite3_mutex_enter( db.mutex );
952  
953 /* Force xDestroy calls on all virtual tables */
954 sqlite3ResetInternalSchema( db, -1 );
955  
956 /* If a transaction is open, the ResetInternalSchema() call above
957 ** will not have called the xDisconnect() method on any virtual
958 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
959 ** call will do so. We need to do this before the check for active
960 ** SQL statements below, as the v-table implementation may be storing
961 ** some prepared statements internally.
962 */
963 sqlite3VtabRollback( db );
964  
965 /* If there are any outstanding VMs, return SQLITE_BUSY. */
966  
967 if ( db.pVdbe != null )
968 {
969 sqlite3Error( db, SQLITE_BUSY,
970 "unable to close due to unfinalised statements" );
971 sqlite3_mutex_leave( db.mutex );
972 return SQLITE_BUSY;
973 }
974 Debug.Assert( sqlite3SafetyCheckSickOrOk( db ) );
975  
976 for ( j = 0; j < db.nDb; j++ )
977 {
978 Btree pBt = db.aDb[j].pBt;
979 if ( pBt != null && sqlite3BtreeIsInBackup( pBt ) )
980 {
981 sqlite3Error( db, SQLITE_BUSY,
982 "unable to close due to unfinished backup operation" );
983 sqlite3_mutex_leave( db.mutex );
984 return SQLITE_BUSY;
985 }
986 }
987  
988 /* Free any outstanding Savepoint structures. */
989 sqlite3CloseSavepoints( db );
990  
991 for ( j = 0; j < db.nDb; j++ )
992 {
993 Db pDb = db.aDb[j];
994 if ( pDb.pBt != null )
995 {
996 sqlite3BtreeClose( ref pDb.pBt );
997 pDb.pBt = null;
998 if ( j != 1 )
999 {
1000 pDb.pSchema = null;
1001 }
1002 }
1003 }
1004 sqlite3ResetInternalSchema( db, -1 );
1005  
1006 /* Tell the code in notify.c that the connection no longer holds any
1007 ** locks and does not require any further unlock-notify callbacks.
1008 */
1009 sqlite3ConnectionClosed( db );
1010  
1011 Debug.Assert( db.nDb <= 2 );
1012 Debug.Assert( db.aDb[0].Equals( db.aDbStatic[0] ) );
1013 for ( j = 0; j < ArraySize( db.aFunc.a ); j++ )
1014 {
1015 FuncDef pNext, pHash, p;
1016 for ( p = db.aFunc.a[j]; p != null; p = pHash )
1017 {
1018 pHash = p.pHash;
1019 while ( p != null )
1020 {
1021 functionDestroy( db, p );
1022 pNext = p.pNext;
1023 sqlite3DbFree( db, ref p );
1024 p = pNext;
1025 }
1026  
1027 }
1028 }
1029  
1030 for ( i = db.aCollSeq.first; i != null; i = i.next )
1031 {//sqliteHashFirst(db.aCollSeq); i!=null; i=sqliteHashNext(i)){
1032 CollSeq[] pColl = (CollSeq[])i.data;// sqliteHashData(i);
1033 /* Invoke any destructors registered for collation sequence user data. */
1034 for ( j = 0; j < 3; j++ )
1035 {
1036 if ( pColl[j].xDel != null )
1037 {
1038 pColl[j].xDel( ref pColl[j].pUser );
1039 }
1040 }
1041 sqlite3DbFree( db, ref pColl );
1042 }
1043 sqlite3HashClear( db.aCollSeq );
1044 #if !SQLITE_OMIT_VIRTUALTABLE
1045 for ( i = sqliteHashFirst( db.aModule ); i != null; i = sqliteHashNext( i ) )
1046 {
1047 Module pMod = (Module)sqliteHashData( i );
1048 if ( pMod.xDestroy != null )
1049 {
1050 pMod.xDestroy( ref pMod.pAux );
1051 }
1052 sqlite3DbFree( db, ref pMod );
1053 }
1054 sqlite3HashClear( db.aModule );
1055 #endif
1056  
1057 sqlite3Error( db, SQLITE_OK, 0 ); /* Deallocates any cached error strings. */
1058 if ( db.pErr != null )
1059 {
1060 sqlite3ValueFree( ref db.pErr );
1061 }
1062 #if !SQLITE_OMIT_LOAD_EXTENSION
1063 sqlite3CloseExtensions( db );
1064 #endif
1065  
1066 db.magic = SQLITE_MAGIC_ERROR;
1067  
1068 /* The temp.database schema is allocated differently from the other schema
1069 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
1070 ** So it needs to be freed here. Todo: Why not roll the temp schema into
1071 ** the same sqliteMalloc() as the one that allocates the database
1072 ** structure?
1073 */
1074 sqlite3DbFree( db, ref db.aDb[1].pSchema );
1075 sqlite3_mutex_leave( db.mutex );
1076 db.magic = SQLITE_MAGIC_CLOSED;
1077 sqlite3_mutex_free( db.mutex );
1078 Debug.Assert( db.lookaside.nOut == 0 ); /* Fails on a lookaside memory leak */
1079 //if ( db.lookaside.bMalloced )
1080 //{
1081 // sqlite3_free( ref db.lookaside.pStart );
1082 //}
1083 //sqlite3_free( ref db );
1084 return SQLITE_OK;
1085 }
1086  
1087 /*
1088 ** Rollback all database files.
1089 */
1090 static void sqlite3RollbackAll( sqlite3 db )
1091 {
1092 int i;
1093 int inTrans = 0;
1094 Debug.Assert( sqlite3_mutex_held( db.mutex ) );
1095 sqlite3BeginBenignMalloc();
1096 for ( i = 0; i < db.nDb; i++ )
1097 {
1098 if ( db.aDb[i].pBt != null )
1099 {
1100 if ( sqlite3BtreeIsInTrans( db.aDb[i].pBt ) )
1101 {
1102 inTrans = 1;
1103 }
1104 sqlite3BtreeRollback( db.aDb[i].pBt );
1105 db.aDb[i].inTrans = 0;
1106 }
1107 }
1108  
1109 sqlite3VtabRollback( db );
1110 sqlite3EndBenignMalloc();
1111 if ( ( db.flags & SQLITE_InternChanges ) != 0 )
1112 {
1113 sqlite3ExpirePreparedStatements( db );
1114 sqlite3ResetInternalSchema( db, -1 );
1115 }
1116  
1117 /* Any deferred constraint violations have now been resolved. */
1118 db.nDeferredCons = 0;
1119  
1120 /* If one has been configured, invoke the rollback-hook callback */
1121 if ( db.xRollbackCallback != null && ( inTrans != 0 || 0 == db.autoCommit ) )
1122 {
1123 db.xRollbackCallback( db.pRollbackArg );
1124 }
1125 }
1126  
1127 /*
1128 ** Return a static string that describes the kind of error specified in the
1129 ** argument.
1130 */
1131 static string sqlite3ErrStr( int rc )
1132 {
1133 string[] aMsg = new string[]{
1134 /* SQLITE_OK */ "not an error",
1135 /* SQLITE_ERROR */ "SQL logic error or missing database",
1136 /* SQLITE_INTERNAL */ "",
1137 /* SQLITE_PERM */ "access permission denied",
1138 /* SQLITE_ABORT */ "callback requested query abort",
1139 /* SQLITE_BUSY */ "database is locked",
1140 /* SQLITE_LOCKED */ "database table is locked",
1141 /* SQLITE_NOMEM */ "out of memory",
1142 /* SQLITE_READONLY */ "attempt to write a readonly database",
1143 /* SQLITE_INTERRUPT */ "interrupted",
1144 /* SQLITE_IOERR */ "disk I/O error",
1145 /* SQLITE_CORRUPT */ "database disk image is malformed",
1146 /* SQLITE_NOTFOUND */ "unknown operation",
1147 /* SQLITE_FULL */ "database or disk is full",
1148 /* SQLITE_CANTOPEN */ "unable to open database file",
1149 /* SQLITE_PROTOCOL */ "locking protocol",
1150 /* SQLITE_EMPTY */ "table contains no data",
1151 /* SQLITE_SCHEMA */ "database schema has changed",
1152 /* SQLITE_TOOBIG */ "string or blob too big",
1153 /* SQLITE_CONSTRAINT */ "constraint failed",
1154 /* SQLITE_MISMATCH */ "datatype mismatch",
1155 /* SQLITE_MISUSE */ "library routine called out of sequence",
1156 /* SQLITE_NOLFS */ "large file support is disabled",
1157 /* SQLITE_AUTH */ "authorization denied",
1158 /* SQLITE_FORMAT */ "auxiliary database format error",
1159 /* SQLITE_RANGE */ "bind or column index out of range",
1160 /* SQLITE_NOTADB */ "file is encrypted or is not a database",
1161 };
1162 rc &= 0xff;
1163 if ( ALWAYS( rc >= 0 ) && rc < aMsg.Length && aMsg[rc].Length > 0 )//(int)(sizeof(aMsg)/sizeof(aMsg[0]))
1164 {
1165 return aMsg[rc];
1166 }
1167 else
1168 {
1169 return "unknown error";
1170 }
1171 }
1172  
1173 /*
1174 ** This routine implements a busy callback that sleeps and tries
1175 ** again until a timeout value is reached. The timeout value is
1176 ** an integer number of milliseconds passed in as the first
1177 ** argument.
1178 */
1179 static int sqliteDefaultBusyCallback(
1180 object ptr, /* Database connection */
1181 int count /* Number of times table has been busy */
1182 )
1183 {
1184 #if SQLITE_OS_WIN || HAVE_USLEEP
1185 u8[] delays = new u8[] { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
1186 u8[] totals = new u8[] { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
1187 //# define NDELAY ArraySize(delays)
1188 int NDELAY = ArraySize( delays );
1189 sqlite3 db = (sqlite3)ptr;
1190 int timeout = db.busyTimeout;
1191 int delay, prior;
1192  
1193 Debug.Assert( count >= 0 );
1194 if ( count < NDELAY )
1195 {
1196 delay = delays[count];
1197 prior = totals[count];
1198 }
1199 else
1200 {
1201 delay = delays[NDELAY - 1];
1202 prior = totals[NDELAY - 1] + delay * ( count - ( NDELAY - 1 ) );
1203 }
1204 if ( prior + delay > timeout )
1205 {
1206 delay = timeout - prior;
1207 if ( delay <= 0 )
1208 return 0;
1209 }
1210 sqlite3OsSleep( db.pVfs, delay * 1000 );
1211 return 1;
1212 #else
1213 sqlite3 db = (sqlite3)ptr;
1214 int timeout = ( (sqlite3)ptr ).busyTimeout;
1215 if ( ( count + 1 ) * 1000 > timeout )
1216 {
1217 return 0;
1218 }
1219 sqlite3OsSleep( db.pVfs, 1000000 );
1220 return 1;
1221 #endif
1222 }
1223  
1224 /*
1225 ** Invoke the given busy handler.
1226 **
1227 ** This routine is called when an operation failed with a lock.
1228 ** If this routine returns non-zero, the lock is retried. If it
1229 ** returns 0, the operation aborts with an SQLITE_BUSY error.
1230 */
1231 static int sqlite3InvokeBusyHandler( BusyHandler p )
1232 {
1233 int rc;
1234 if ( NEVER( p == null ) || p.xFunc == null || p.nBusy < 0 )
1235 return 0;
1236 rc = p.xFunc( p.pArg, p.nBusy );
1237 if ( rc == 0 )
1238 {
1239 p.nBusy = -1;
1240 }
1241 else
1242 {
1243 p.nBusy++;
1244 }
1245 return rc;
1246 }
1247  
1248 /*
1249 ** This routine sets the busy callback for an Sqlite database to the
1250 ** given callback function with the given argument.
1251 */
1252 static int sqlite3_busy_handler(
1253 sqlite3 db,
1254 dxBusy xBusy,
1255 object pArg
1256 )
1257 {
1258 sqlite3_mutex_enter( db.mutex );
1259 db.busyHandler.xFunc = xBusy;
1260 db.busyHandler.pArg = pArg;
1261 db.busyHandler.nBusy = 0;
1262 sqlite3_mutex_leave( db.mutex );
1263 return SQLITE_OK;
1264 }
1265  
1266 #if !SQLITE_OMIT_PROGRESS_CALLBACK
1267 /*
1268 ** This routine sets the progress callback for an Sqlite database to the
1269 ** given callback function with the given argument. The progress callback will
1270 ** be invoked every nOps opcodes.
1271 */
1272 static void sqlite3_progress_handler(
1273 sqlite3 db,
1274 int nOps,
1275 dxProgress xProgress, //int (xProgress)(void),
1276 object pArg
1277 )
1278 {
1279 sqlite3_mutex_enter( db.mutex );
1280 if ( nOps > 0 )
1281 {
1282 db.xProgress = xProgress;
1283 db.nProgressOps = nOps;
1284 db.pProgressArg = pArg;
1285 }
1286 else
1287 {
1288 db.xProgress = null;
1289 db.nProgressOps = 0;
1290 db.pProgressArg = null;
1291 }
1292 sqlite3_mutex_leave( db.mutex );
1293 }
1294 #endif
1295  
1296  
1297 /*
1298 ** This routine installs a default busy handler that waits for the
1299 ** specified number of milliseconds before returning 0.
1300 */
1301 static public int sqlite3_busy_timeout( sqlite3 db, int ms )
1302 {
1303 if ( ms > 0 )
1304 {
1305 db.busyTimeout = ms;
1306 sqlite3_busy_handler( db, sqliteDefaultBusyCallback, db );
1307 }
1308 else
1309 {
1310 sqlite3_busy_handler( db, null, null );
1311 }
1312 return SQLITE_OK;
1313 }
1314  
1315 /*
1316 ** Cause any pending operation to stop at its earliest opportunity.
1317 */
1318 static void sqlite3_interrupt( sqlite3 db )
1319 {
1320 db.u1.isInterrupted = true;
1321 }
1322  
1323  
1324 /*
1325 ** This function is exactly the same as sqlite3_create_function(), except
1326 ** that it is designed to be called by internal code. The difference is
1327 ** that if a malloc() fails in sqlite3_create_function(), an error code
1328 ** is returned and the mallocFailed flag cleared.
1329 */
1330 static int sqlite3CreateFunc(
1331 sqlite3 db,
1332 string zFunctionName,
1333 int nArg,
1334 u8 enc,
1335 object pUserData,
1336 dxFunc xFunc, //)(sqlite3_context*,int,sqlite3_value *),
1337 dxStep xStep,//)(sqlite3_context*,int,sqlite3_value *),
1338 dxFinal xFinal, //)(sqlite3_context),
1339 FuncDestructor pDestructor
1340 )
1341 {
1342 FuncDef p;
1343 int nName;
1344  
1345 Debug.Assert( sqlite3_mutex_held( db.mutex ) );
1346 if ( zFunctionName == null ||
1347 ( xFunc != null && ( xFinal != null || xStep != null ) ) ||
1348 ( xFunc == null && ( xFinal != null && xStep == null ) ) ||
1349 ( xFunc == null && ( xFinal == null && xStep != null ) ) ||
1350 ( nArg < -1 || nArg > SQLITE_MAX_FUNCTION_ARG ) ||
1351 ( 255 < ( nName = sqlite3Strlen30( zFunctionName ) ) ) )
1352 {
1353 return SQLITE_MISUSE_BKPT();
1354 }
1355  
1356 #if !SQLITE_OMIT_UTF16
1357 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1358 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1359 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1360 **
1361 ** If SQLITE_ANY is specified, add three versions of the function
1362 ** to the hash table.
1363 */
1364 if( enc==SQLITE_UTF16 ){
1365 enc = SQLITE_UTF16NATIVE;
1366 }else if( enc==SQLITE_ANY ){
1367 int rc;
1368 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
1369 pUserData, xFunc, xStep, xFinal, pDestructor);
1370 if( rc==SQLITE_OK ){
1371 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
1372 pUserData, xFunc, xStep, xFinal, pDestructor);
1373 }
1374 if( rc!=SQLITE_OK ){
1375 return rc;
1376 }
1377 enc = SQLITE_UTF16BE;
1378 }
1379 #else
1380 enc = SQLITE_UTF8;
1381 #endif
1382  
1383 /* Check if an existing function is being overridden or deleted. If so,
1384 ** and there are active VMs, then return SQLITE_BUSY. If a function
1385 ** is being overridden/deleted but there are no active VMs, allow the
1386 ** operation to continue but invalidate all precompiled statements.
1387 */
1388 p = sqlite3FindFunction( db, zFunctionName, nName, nArg, enc, 0 );
1389 if ( p != null && p.iPrefEnc == enc && p.nArg == nArg )
1390 {
1391 if ( db.activeVdbeCnt != 0 )
1392 {
1393 sqlite3Error( db, SQLITE_BUSY,
1394 "unable to delete/modify user-function due to active statements" );
1395 //Debug.Assert( 0 == db.mallocFailed );
1396 return SQLITE_BUSY;
1397 }
1398 else
1399 {
1400 sqlite3ExpirePreparedStatements( db );
1401 }
1402 }
1403  
1404 p = sqlite3FindFunction( db, zFunctionName, nName, nArg, enc, 1 );
1405 Debug.Assert( p != null /*|| db.mallocFailed != 0 */ );
1406 //if ( p == null )
1407 //{
1408 // return SQLITE_NOMEM;
1409 //}
1410  
1411 /* If an older version of the function with a configured destructor is
1412 ** being replaced invoke the destructor function here. */
1413 functionDestroy( db, p );
1414  
1415 if ( pDestructor != null )
1416 {
1417 pDestructor.nRef++;
1418 }
1419 p.pDestructor = pDestructor;
1420 p.flags = 0;
1421 p.xFunc = xFunc;
1422 p.xStep = xStep;
1423 p.xFinalize = xFinal;
1424 p.pUserData = pUserData;
1425 p.nArg = (i16)nArg;
1426 return SQLITE_OK;
1427 }
1428  
1429 /*
1430 ** Create new user functions.
1431 */
1432 static public int sqlite3_create_function(
1433 sqlite3 db,
1434 string zFunc,
1435 int nArg,
1436 u8 enc,
1437 object p,
1438 dxFunc xFunc, //)(sqlite3_context*,int,sqlite3_value *),
1439 dxStep xStep,//)(sqlite3_context*,int,sqlite3_value *),
1440 dxFinal xFinal//)(sqlite3_context)
1441 )
1442 {
1443 return sqlite3_create_function_v2( db, zFunc, nArg, enc, p, xFunc, xStep,
1444 xFinal, null );
1445 }
1446  
1447 static int sqlite3_create_function_v2(
1448 sqlite3 db,
1449 string zFunc,
1450 int nArg,
1451 int enc,
1452 object p,
1453 dxFunc xFunc, //)(sqlite3_context*,int,sqlite3_value *),
1454 dxStep xStep,//)(sqlite3_context*,int,sqlite3_value *),
1455 dxFinal xFinal,//)(sqlite3_context)
1456 dxFDestroy xDestroy//)(void )
1457 )
1458 {
1459 int rc = SQLITE_ERROR;
1460 FuncDestructor pArg = null;
1461 sqlite3_mutex_enter( db.mutex );
1462 if ( xDestroy != null )
1463 {
1464 pArg = new FuncDestructor();//(FuncDestructor )sqlite3DbMallocZero(db, sizeof(FuncDestructor));
1465 //if( null==pArg ){
1466 // xDestroy(p);
1467 // goto out;
1468 //}
1469 pArg.xDestroy = xDestroy;
1470 pArg.pUserData = p;
1471 }
1472 rc = sqlite3CreateFunc( db, zFunc, nArg, (byte)enc, p, xFunc, xStep, xFinal, pArg );
1473 if ( pArg != null && pArg.nRef == 0 )
1474 {
1475 Debug.Assert( rc != SQLITE_OK );
1476 //xDestroy(p);
1477 sqlite3DbFree( db, ref pArg );
1478 }
1479 //_out:
1480 rc = sqlite3ApiExit( db, rc );
1481 sqlite3_mutex_leave( db.mutex );
1482 return rc;
1483 }
1484  
1485 #if !SQLITE_OMIT_UTF16
1486 static int sqlite3_create_function16(
1487 sqlite3 db,
1488 string zFunctionName,
1489 int nArg,
1490 int eTextRep,
1491 object p,
1492 dxFunc xFunc, //)(sqlite3_context*,int,sqlite3_value*),
1493 dxStep xStep, //)(sqlite3_context*,int,sqlite3_value*),
1494 dxFinal xFinal //)(sqlite3_context)
1495 ){
1496 int rc;
1497 string zFunc8;
1498 sqlite3_mutex_enter(db.mutex);
1499 Debug.Assert( 0==db.mallocFailed );
1500 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
1501 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal, null);
1502 sqlite3DbFree(db,ref zFunc8);
1503 rc = sqlite3ApiExit(db, rc);
1504 sqlite3_mutex_leave(db.mutex);
1505 return rc;
1506 }
1507 #endif
1508  
1509  
1510 /*
1511 ** Declare that a function has been overloaded by a virtual table.
1512 **
1513 ** If the function already exists as a regular global function, then
1514 ** this routine is a no-op. If the function does not exist, then create
1515 ** a new one that always throws a run-time error.
1516 **
1517 ** When virtual tables intend to provide an overloaded function, they
1518 ** should call this routine to make sure the global function exists.
1519 ** A global function must exist in order for name resolution to work
1520 ** properly.
1521 */
1522 static int sqlite3_overload_function(
1523 sqlite3 db,
1524 string zName,
1525 int nArg
1526 )
1527 {
1528 int nName = sqlite3Strlen30( zName );
1529 int rc;
1530 sqlite3_mutex_enter( db.mutex );
1531 if ( sqlite3FindFunction( db, zName, nName, nArg, SQLITE_UTF8, 0 ) == null )
1532 {
1533 sqlite3CreateFunc( db, zName, nArg, SQLITE_UTF8,
1534 0, (dxFunc)sqlite3InvalidFunction, null, null, null );
1535 }
1536 rc = sqlite3ApiExit( db, SQLITE_OK );
1537 sqlite3_mutex_leave( db.mutex );
1538 return rc;
1539 }
1540  
1541 #if !SQLITE_OMIT_TRACE
1542 /*
1543 ** Register a trace function. The pArg from the previously registered trace
1544 ** is returned.
1545 **
1546 ** A NULL trace function means that no tracing is executes. A non-NULL
1547 ** trace is a pointer to a function that is invoked at the start of each
1548 ** SQL statement.
1549 */
1550 static object sqlite3_trace( sqlite3 db, dxTrace xTrace, object pArg )
1551 {// (*xTrace)(void*,const char), object pArg){
1552 object pOld;
1553 sqlite3_mutex_enter( db.mutex );
1554 pOld = db.pTraceArg;
1555 db.xTrace = xTrace;
1556 db.pTraceArg = pArg;
1557 sqlite3_mutex_leave( db.mutex );
1558 return pOld;
1559 }
1560 /*
1561 ** Register a profile function. The pArg from the previously registered
1562 ** profile function is returned.
1563 **
1564 ** A NULL profile function means that no profiling is executes. A non-NULL
1565 ** profile is a pointer to a function that is invoked at the conclusion of
1566 ** each SQL statement that is run.
1567 */
1568 static object sqlite3_profile(
1569 sqlite3 db,
1570 dxProfile xProfile,//void (*xProfile)(void*,const char*,sqlite_u3264),
1571 object pArg
1572 )
1573 {
1574 object pOld;
1575 sqlite3_mutex_enter( db.mutex );
1576 pOld = db.pProfileArg;
1577 db.xProfile = xProfile;
1578 db.pProfileArg = pArg;
1579 sqlite3_mutex_leave( db.mutex );
1580 return pOld;
1581 }
1582 #endif // * SQLITE_OMIT_TRACE */
1583  
1584 /*** EXPERIMENTAL ***
1585 **
1586 ** Register a function to be invoked when a transaction comments.
1587 ** If the invoked function returns non-zero, then the commit becomes a
1588 ** rollback.
1589 */
1590 static object sqlite3_commit_hook(
1591 sqlite3 db, /* Attach the hook to this database */
1592 dxCommitCallback xCallback, //int (*xCallback)(void), /* Function to invoke on each commit */
1593 object pArg /* Argument to the function */
1594 )
1595 {
1596 object pOld;
1597 sqlite3_mutex_enter( db.mutex );
1598 pOld = db.pCommitArg;
1599 db.xCommitCallback = xCallback;
1600 db.pCommitArg = pArg;
1601 sqlite3_mutex_leave( db.mutex );
1602 return pOld;
1603 }
1604  
1605 /*
1606 ** Register a callback to be invoked each time a row is updated,
1607 ** inserted or deleted using this database connection.
1608 */
1609 static object sqlite3_update_hook(
1610 sqlite3 db, /* Attach the hook to this database */
1611 dxUpdateCallback xCallback, //void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
1612 object pArg /* Argument to the function */
1613 )
1614 {
1615 object pRet;
1616 sqlite3_mutex_enter( db.mutex );
1617 pRet = db.pUpdateArg;
1618 db.xUpdateCallback = xCallback;
1619 db.pUpdateArg = pArg;
1620 sqlite3_mutex_leave( db.mutex );
1621 return pRet;
1622 }
1623  
1624 /*
1625 ** Register a callback to be invoked each time a transaction is rolled
1626 ** back by this database connection.
1627 */
1628 static object sqlite3_rollback_hook(
1629 sqlite3 db, /* Attach the hook to this database */
1630 dxRollbackCallback xCallback, //void (*xCallback)(void), /* Callback function */
1631 object pArg /* Argument to the function */
1632 )
1633 {
1634 object pRet;
1635 sqlite3_mutex_enter( db.mutex );
1636 pRet = db.pRollbackArg;
1637 db.xRollbackCallback = xCallback;
1638 db.pRollbackArg = pArg;
1639 sqlite3_mutex_leave( db.mutex );
1640 return pRet;
1641 }
1642  
1643 #if !SQLITE_OMIT_WAL
1644 /*
1645 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
1646 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
1647 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
1648 ** wal_autocheckpoint()).
1649 */
1650 int sqlite3WalDefaultHook(
1651 void *pClientData, /* Argument */
1652 sqlite3 db, /* Connection */
1653 const string zDb, /* Database */
1654 int nFrame /* Size of WAL */
1655 ){
1656 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
1657 sqlite3BeginBenignMalloc();
1658 sqlite3_wal_checkpoint(db, zDb);
1659 sqlite3EndBenignMalloc();
1660 }
1661 return SQLITE_OK;
1662 }
1663 #endif //* SQLITE_OMIT_WAL */
1664  
1665 /*
1666 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
1667 ** a database after committing a transaction if there are nFrame or
1668 ** more frames in the log file. Passing zero or a negative value as the
1669 ** nFrame parameter disables automatic checkpoints entirely.
1670 **
1671 ** The callback registered by this function replaces any existing callback
1672 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
1673 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
1674 ** configured by this function.
1675 */
1676 static int sqlite3_wal_autocheckpoint( sqlite3 db, int nFrame )
1677 {
1678 #if SQLITE_OMIT_WAL
1679 UNUSED_PARAMETER( db );
1680 UNUSED_PARAMETER( nFrame );
1681 #else
1682 if( nFrame>0 ){
1683 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
1684 }else{
1685 sqlite3_wal_hook(db, 0, 0);
1686 }
1687 #endif
1688 return SQLITE_OK;
1689 }
1690  
1691 /*
1692 ** Register a callback to be invoked each time a transaction is written
1693 ** into the write-ahead-log by this database connection.
1694 */
1695 static object sqlite3_wal_hook(
1696 sqlite3 db, /* Attach the hook to this db handle */
1697 dxWalCallback xCallback, //int(*xCallback)(void *, sqlite3*, const char*, int),
1698 object pArg /* First argument passed to xCallback() */
1699 )
1700 {
1701 #if !SQLITE_OMIT_WAL
1702 void *pRet;
1703 sqlite3_mutex_enter(db.mutex);
1704 pRet = db.pWalArg;
1705 db.xWalCallback = xCallback;
1706 db.pWalArg = pArg;
1707 sqlite3_mutex_leave(db.mutex);
1708 return pRet;
1709 #else
1710 return null;
1711 #endif
1712 }
1713  
1714  
1715 /*
1716 ** Checkpoint database zDb.
1717 */
1718 static int sqlite3_wal_checkpoint_v2(
1719 sqlite3 db, /* Database handle */
1720 string zDb, /* Name of attached database (or NULL) */
1721 int eMode, /* SQLITE_CHECKPOINT_* value */
1722 out int pnLog, /* OUT: Size of WAL log in frames */
1723 out int pnCkpt /* OUT: Total number of frames checkpointed */
1724 )
1725 {
1726 #if SQLITE_OMIT_WAL
1727 pnLog = 0;
1728 pnCkpt = 0;
1729 return SQLITE_OK;
1730 #else
1731 int rc; /* Return code */
1732 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
1733  
1734 /* Initialize the output variables to -1 in case an error occurs. */
1735 if( pnLog ) *pnLog = -1;
1736 if( pnCkpt ) *pnCkpt = -1;
1737  
1738 Debug.Assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
1739 Debug.Assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
1740 Debug.Assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
1741 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
1742 return SQLITE_MISUSE;
1743 }
1744  
1745 sqlite3_mutex_enter(db->mutex);
1746 if( zDb && zDb[0] ){
1747 iDb = sqlite3FindDbName(db, zDb);
1748 }
1749 if( iDb<0 ){
1750 rc = SQLITE_ERROR;
1751 sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
1752 }else{
1753 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
1754 sqlite3Error(db, rc, 0);
1755 }
1756 rc = sqlite3ApiExit(db, rc);
1757 sqlite3_mutex_leave(db->mutex);
1758 return rc;
1759 #endif
1760 }
1761  
1762  
1763 /*
1764 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
1765 ** to contains a zero-length string, all attached databases are
1766 ** checkpointed.
1767 */
1768 static int sqlite3_wal_checkpoint( sqlite3 db, string zDb )
1769 {
1770 int dummy;
1771 return sqlite3_wal_checkpoint_v2( db, zDb, SQLITE_CHECKPOINT_PASSIVE, out dummy, out dummy );
1772 }
1773  
1774 #if !SQLITE_OMIT_WAL
1775 /*
1776 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
1777 ** not currently open in WAL mode.
1778 **
1779 ** If a transaction is open on the database being checkpointed, this
1780 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
1781 ** an error occurs while running the checkpoint, an SQLite error code is
1782 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
1783 **
1784 ** The mutex on database handle db should be held by the caller. The mutex
1785 ** associated with the specific b-tree being checkpointed is taken by
1786 ** this function while the checkpoint is running.
1787 **
1788 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
1789 ** checkpointed. If an error is encountered it is returned immediately -
1790 ** no attempt is made to checkpoint any remaining databases.
1791 **
1792 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
1793 */
1794 int sqlite3Checkpoint(sqlite3 db, int iDb, int eMode, int *pnLog, int *pnCkpt){
1795 int rc = SQLITE_OK; /* Return code */
1796 int i; /* Used to iterate through attached dbs */
1797 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
1798  
1799 Debug.Assert( sqlite3_mutex_held(db->mutex) );
1800 Debug.Assert( !pnLog || *pnLog==-1 );
1801 Debug.Assert( !pnCkpt || *pnCkpt==-1 );
1802  
1803 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
1804 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
1805 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
1806 pnLog = 0;
1807 pnCkpt = 0;
1808 if( rc==SQLITE_BUSY ){
1809 bBusy = 1;
1810 rc = SQLITE_OK;
1811 }
1812 }
1813 }
1814  
1815 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
1816 }
1817 #endif //* SQLITE_OMIT_WAL */
1818  
1819 /*
1820 /*
1821 ** This function returns true if main-memory should be used instead of
1822 ** a temporary file for transient pager files and statement journals.
1823 ** The value returned depends on the value of db->temp_store (runtime
1824 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
1825 ** following table describes the relationship between these two values
1826 ** and this functions return value.
1827 **
1828 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
1829 ** ----------------- -------------- ------------------------------
1830 ** 0 any file (return 0)
1831 ** 1 1 file (return 0)
1832 ** 1 2 memory (return 1)
1833 ** 1 0 file (return 0)
1834 ** 2 1 file (return 0)
1835 ** 2 2 memory (return 1)
1836 ** 2 0 memory (return 1)
1837 ** 3 any memory (return 1)
1838 */
1839 static bool sqlite3TempInMemory( sqlite3 db )
1840 {
1841 //#if SQLITE_TEMP_STORE==1
1842 if ( SQLITE_TEMP_STORE == 1 )
1843 return ( db.temp_store == 2 );
1844 //#endif
1845 //#if SQLITE_TEMP_STORE==2
1846 if ( SQLITE_TEMP_STORE == 2 )
1847 return ( db.temp_store != 1 );
1848 //#endif
1849 //#if SQLITE_TEMP_STORE==3
1850 if ( SQLITE_TEMP_STORE == 3 )
1851 return true;
1852 //#endif
1853 //#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
1854 if ( SQLITE_TEMP_STORE < 1 || SQLITE_TEMP_STORE > 3 )
1855 return false;
1856 //#endif
1857 return false;
1858 }
1859  
1860 /*
1861 ** Return UTF-8 encoded English language explanation of the most recent
1862 ** error.
1863 */
1864 public static string sqlite3_errmsg( sqlite3 db )
1865 {
1866 string z;
1867 if ( db == null )
1868 {
1869 return sqlite3ErrStr( SQLITE_NOMEM );
1870 }
1871 if ( !sqlite3SafetyCheckSickOrOk( db ) )
1872 {
1873 return sqlite3ErrStr( SQLITE_MISUSE_BKPT() );
1874 }
1875 sqlite3_mutex_enter( db.mutex );
1876 //if ( db.mallocFailed != 0 )
1877 //{
1878 // z = sqlite3ErrStr( SQLITE_NOMEM );
1879 //}
1880 //else
1881 {
1882 z = sqlite3_value_text( db.pErr );
1883 //Debug.Assert( 0 == db.mallocFailed );
1884 if ( string.IsNullOrEmpty( z ) )
1885 {
1886 z = sqlite3ErrStr( db.errCode );
1887 }
1888 }
1889 sqlite3_mutex_leave( db.mutex );
1890 return z;
1891 }
1892  
1893 #if !SQLITE_OMIT_UTF16
1894 /*
1895 ** Return UTF-16 encoded English language explanation of the most recent
1896 ** error.
1897 */
1898 const void *sqlite3_errmsg16(sqlite3 db){
1899 static const u16 outOfMem[] = {
1900 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
1901 };
1902 static const u16 misuse[] = {
1903 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
1904 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
1905 'c', 'a', 'l', 'l', 'e', 'd', ' ',
1906 'o', 'u', 't', ' ',
1907 'o', 'f', ' ',
1908 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
1909 };
1910  
1911 string z;
1912 if( null==db ){
1913 return (void )outOfMem;
1914 }
1915 if( null==sqlite3SafetyCheckSickOrOk(db) ){
1916 return (void )misuse;
1917 }
1918 sqlite3_mutex_enter(db->mutex);
1919 if( db->mallocFailed ){
1920 z = (void )outOfMem;
1921 }else{
1922 z = sqlite3_value_text16(db->pErr);
1923 if( z==0 ){
1924 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
1925 SQLITE_UTF8, SQLITE_STATIC);
1926 z = sqlite3_value_text16(db->pErr);
1927 }
1928 /* A malloc() may have failed within the call to sqlite3_value_text16()
1929 ** above. If this is the case, then the db->mallocFailed flag needs to
1930 ** be cleared before returning. Do this directly, instead of via
1931 ** sqlite3ApiExit(), to avoid setting the database handle error message.
1932 */
1933 db->mallocFailed = 0;
1934 }
1935 sqlite3_mutex_leave(db->mutex);
1936 return z;
1937 }
1938 #endif // * SQLITE_OMIT_UTF16 */
1939  
1940 /*
1941 ** Return the most recent error code generated by an SQLite routine. If NULL is
1942 ** passed to this function, we assume a malloc() failed during sqlite3_open().
1943 */
1944 static public int sqlite3_errcode( sqlite3 db )
1945 {
1946 if ( db != null && !sqlite3SafetyCheckSickOrOk( db ) )
1947 {
1948 return SQLITE_MISUSE_BKPT();
1949 }
1950 if ( null == db /*|| db.mallocFailed != 0 */ )
1951 {
1952 return SQLITE_NOMEM;
1953 }
1954 return db.errCode & db.errMask;
1955 }
1956 static int sqlite3_extended_errcode( sqlite3 db )
1957 {
1958 if ( db != null && !sqlite3SafetyCheckSickOrOk( db ) )
1959 {
1960 return SQLITE_MISUSE_BKPT();
1961 }
1962 if ( null == db /*|| db.mallocFailed != 0 */ )
1963 {
1964 return SQLITE_NOMEM;
1965 }
1966 return db.errCode;
1967 }
1968 /*
1969 ** Create a new collating function for database "db". The name is zName
1970 ** and the encoding is enc.
1971 */
1972 static int createCollation(
1973 sqlite3 db,
1974 string zName,
1975 u8 enc,
1976 u8 collType,
1977 object pCtx,
1978 dxCompare xCompare,//)(void*,int,const void*,int,const void),
1979 dxDelCollSeq xDel//)(void)
1980 )
1981 {
1982 CollSeq pColl;
1983 int enc2;
1984 int nName = sqlite3Strlen30( zName );
1985  
1986 Debug.Assert( sqlite3_mutex_held( db.mutex ) );
1987  
1988 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1989 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1990 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1991 */
1992 enc2 = enc;
1993 testcase( enc2 == SQLITE_UTF16 );
1994 testcase( enc2 == SQLITE_UTF16_ALIGNED );
1995 if ( enc2 == SQLITE_UTF16 || enc2 == SQLITE_UTF16_ALIGNED )
1996 {
1997 enc2 = SQLITE_UTF16NATIVE;
1998 }
1999 if ( enc2 < SQLITE_UTF8 || enc2 > SQLITE_UTF16BE )
2000 {
2001 return SQLITE_MISUSE_BKPT();
2002 }
2003  
2004 /* Check if this call is removing or replacing an existing collation
2005 ** sequence. If so, and there are active VMs, return busy. If there
2006 ** are no active VMs, invalidate any pre-compiled statements.
2007 */
2008 pColl = sqlite3FindCollSeq( db, (u8)enc2, zName, 0 );
2009 if ( pColl != null && pColl.xCmp != null )
2010 {
2011 if ( db.activeVdbeCnt != 0 )
2012 {
2013 sqlite3Error( db, SQLITE_BUSY,
2014 "unable to delete/modify collation sequence due to active statements" );
2015 return SQLITE_BUSY;
2016 }
2017 sqlite3ExpirePreparedStatements( db );
2018  
2019 /* If collation sequence pColl was created directly by a call to
2020 ** sqlite3_create_collation, and not generated by synthCollSeq(),
2021 ** then any copies made by synthCollSeq() need to be invalidated.
2022 ** Also, collation destructor - CollSeq.xDel() - function may need
2023 ** to be called.
2024 */
2025 if ( ( pColl.enc & ~SQLITE_UTF16_ALIGNED ) == enc2 )
2026 {
2027 CollSeq[] aColl = sqlite3HashFind( db.aCollSeq, zName, nName, (CollSeq[])null );
2028 int j;
2029 for ( j = 0; j < 3; j++ )
2030 {
2031 CollSeq p = aColl[j];
2032 if ( p.enc == pColl.enc )
2033 {
2034 if ( p.xDel != null )
2035 {
2036 p.xDel( ref p.pUser );
2037 }
2038 p.xCmp = null;
2039 }
2040 }
2041 }
2042 }
2043  
2044 pColl = sqlite3FindCollSeq( db, (u8)enc2, zName, 1 );
2045 //if ( pColl == null )
2046 // return SQLITE_NOMEM;
2047 pColl.xCmp = xCompare;
2048 pColl.pUser = pCtx;
2049 pColl.xDel = xDel;
2050 pColl.enc = (u8)( enc2 | ( enc & SQLITE_UTF16_ALIGNED ) );
2051 pColl.type = collType;
2052 sqlite3Error( db, SQLITE_OK, 0 );
2053 return SQLITE_OK;
2054 }
2055  
2056 /*
2057 ** This array defines hard upper bounds on limit values. The
2058 ** initializer must be kept in sync with the SQLITE_LIMIT_*
2059 ** #defines in sqlite3.h.
2060 */
2061 static int[] aHardLimit = new int[] {
2062 SQLITE_MAX_LENGTH,
2063 SQLITE_MAX_SQL_LENGTH,
2064 SQLITE_MAX_COLUMN,
2065 SQLITE_MAX_EXPR_DEPTH,
2066 SQLITE_MAX_COMPOUND_SELECT,
2067 SQLITE_MAX_VDBE_OP,
2068 SQLITE_MAX_FUNCTION_ARG,
2069 SQLITE_MAX_ATTACHED,
2070 SQLITE_MAX_LIKE_PATTERN_LENGTH,
2071 SQLITE_MAX_VARIABLE_NUMBER,
2072 SQLITE_MAX_TRIGGER_DEPTH,
2073 };
2074  
2075 /*
2076 ** Make sure the hard limits are set to reasonable values
2077 */
2078 //#if SQLITE_MAX_LENGTH<100
2079 //# error SQLITE_MAX_LENGTH must be at least 100
2080 //#endif
2081 //#if SQLITE_MAX_SQL_LENGTH<100
2082 //# error SQLITE_MAX_SQL_LENGTH must be at least 100
2083 //#endif
2084 //#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
2085 //# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
2086 //#endif
2087 //#if SQLITE_MAX_COMPOUND_SELECT<2
2088 //# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
2089 //#endif
2090 //#if SQLITE_MAX_VDBE_OP<40
2091 //# error SQLITE_MAX_VDBE_OP must be at least 40
2092 //#endif
2093 //#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
2094 //# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
2095 //#endif
2096 //#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
2097 //# error SQLITE_MAX_ATTACHED must be between 0 and 62
2098 //#endif
2099 //#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
2100 //# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
2101 //#endif
2102 //#if SQLITE_MAX_COLUMN>32767
2103 //# error SQLITE_MAX_COLUMN must not exceed 32767
2104 //#endif
2105 //#if SQLITE_MAX_TRIGGER_DEPTH<1
2106 //# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
2107 //#endif
2108  
2109 /*
2110 ** Change the value of a limit. Report the old value.
2111 ** If an invalid limit index is supplied, report -1.
2112 ** Make no changes but still report the old value if the
2113 ** new limit is negative.
2114 **
2115 ** A new lower limit does not shrink existing constructs.
2116 ** It merely prevents new constructs that exceed the limit
2117 ** from forming.
2118 */
2119 static int sqlite3_limit( sqlite3 db, int limitId, int newLimit )
2120 {
2121 int oldLimit;
2122  
2123  
2124 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
2125 ** there is a hard upper bound set at compile-time by a C preprocessor
2126 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
2127 ** "_MAX_".)
2128 */
2129 Debug.Assert( aHardLimit[SQLITE_LIMIT_LENGTH] == SQLITE_MAX_LENGTH );
2130 Debug.Assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH] == SQLITE_MAX_SQL_LENGTH );
2131 Debug.Assert( aHardLimit[SQLITE_LIMIT_COLUMN] == SQLITE_MAX_COLUMN );
2132 Debug.Assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH] == SQLITE_MAX_EXPR_DEPTH );
2133 Debug.Assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT] == SQLITE_MAX_COMPOUND_SELECT );
2134 Debug.Assert( aHardLimit[SQLITE_LIMIT_VDBE_OP] == SQLITE_MAX_VDBE_OP );
2135 Debug.Assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG] == SQLITE_MAX_FUNCTION_ARG );
2136 Debug.Assert( aHardLimit[SQLITE_LIMIT_ATTACHED] == SQLITE_MAX_ATTACHED );
2137 Debug.Assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ==
2138 SQLITE_MAX_LIKE_PATTERN_LENGTH );
2139 Debug.Assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER] == SQLITE_MAX_VARIABLE_NUMBER );
2140 Debug.Assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH] == SQLITE_MAX_TRIGGER_DEPTH );
2141 Debug.Assert( SQLITE_LIMIT_TRIGGER_DEPTH == ( SQLITE_N_LIMIT - 1 ) );
2142  
2143  
2144 if ( limitId < 0 || limitId >= SQLITE_N_LIMIT )
2145 {
2146 return -1;
2147 }
2148 oldLimit = db.aLimit[limitId];
2149 if ( newLimit >= 0 ) /* IMP: R-52476-28732 */
2150 {
2151 if ( newLimit > aHardLimit[limitId] )
2152 {
2153 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
2154 }
2155 db.aLimit[limitId] = newLimit;
2156 }
2157 return oldLimit; /* IMP: R-53341-35419 */
2158 }
2159  
2160 class OpenMode {
2161 public string z;
2162 public int mode;
2163  
2164 public OpenMode(string z, int mode)
2165 {
2166 this.z=z;
2167 this.mode=mode;
2168 }
2169 }
2170 /*
2171 ** This function is used to parse both URIs and non-URI filenames passed by the
2172 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
2173 ** URIs specified as part of ATTACH statements.
2174 **
2175 ** The first argument to this function is the name of the VFS to use (or
2176 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
2177 ** query parameter. The second argument contains the URI (or non-URI filename)
2178 ** itself. When this function is called the *pFlags variable should contain
2179 ** the default flags to open the database handle with. The value stored in
2180 ** *pFlags may be updated before returning if the URI filename contains
2181 ** "cache=xxx" or "mode=xxx" query parameters.
2182 **
2183 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
2184 ** the VFS that should be used to open the database file. *pzFile is set to
2185 ** point to a buffer containing the name of the file to open. It is the
2186 ** responsibility of the caller to eventually call sqlite3_free() to release
2187 ** this buffer.
2188 **
2189 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
2190 ** may be set to point to a buffer containing an English language error
2191 ** message. It is the responsibility of the caller to eventually release
2192 ** this buffer by calling sqlite3_free().
2193 */
2194 static int sqlite3ParseUri(
2195 string zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
2196 string zUri, /* Nul-terminated URI to parse */
2197 ref int pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
2198 ref sqlite3_vfs ppVfs, /* OUT: VFS to use */
2199 ref string pzFile, /* OUT: Filename component of URI */
2200 ref string pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
2201 ){
2202 int rc = SQLITE_OK;
2203 int flags = pFlags;
2204 string zVfs = zDefaultVfs;
2205 StringBuilder zFile = null;
2206 char c;
2207 int nUri = sqlite3Strlen30(zUri);
2208 pzErrMsg = null;
2209 ppVfs = null;
2210  
2211 if( ((flags & SQLITE_OPEN_URI) != 0 || sqlite3GlobalConfig.bOpenUri)
2212 && nUri>=5 && memcmp(zUri, "file:", 5)==0
2213 ){
2214 string zOpt;
2215 int eState; /* Parser state when parsing URI */
2216 int iIn; /* Input character index */
2217 //int iOut = 0; /* Output character index */
2218 int nByte = nUri+2; /* Bytes of space to allocate */
2219  
2220 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
2221 ** method that there may be extra parameters following the file-name. */
2222 flags |= SQLITE_OPEN_URI;
2223  
2224 for ( iIn = 0; iIn < nUri; iIn++ )
2225 nByte += ( zUri[iIn] == '&' ) ? 1 : 0;
2226 //zFile = sqlite3_malloc(nByte);
2227 //if( null==zFile ) return SQLITE_NOMEM;
2228 zFile = new StringBuilder( nByte );
2229  
2230 /* Discard the scheme and authority segments of the URI. */
2231 if( zUri[5]=='/' && zUri[6]=='/' ){
2232 iIn = 7;
2233 while ( iIn < nUri && zUri[iIn] != '/' )
2234 iIn++;
2235  
2236 if ( iIn != 7 && ( iIn != 16 || String.Compare( "localhost", zUri.Substring( 7, 9 ), StringComparison.OrdinalIgnoreCase ) != 0 ) )//memcmp("localhost", &zUri[7], 9)) )
2237 {
2238 pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
2239 iIn-7, zUri.Substring(7));
2240 rc = SQLITE_ERROR;
2241 goto parse_uri_out;
2242 }
2243 }else{
2244 iIn = 5;
2245 }
2246  
2247 /* Copy the filename and any query parameters into the zFile buffer.
2248 ** Decode %HH escape codes along the way.
2249 **
2250 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
2251 ** on the parsing context. As follows:
2252 **
2253 ** 0: Parsing file-name.
2254 ** 1: Parsing name section of a name=value query parameter.
2255 ** 2: Parsing value section of a name=value query parameter.
2256 */
2257 eState = 0;
2258 while ( iIn < nUri&& ( c = zUri[iIn] ) != 0 && c != '#' )
2259 {
2260 iIn++;
2261 if( c=='%'
2262 && sqlite3Isxdigit(zUri[iIn])
2263 && sqlite3Isxdigit(zUri[iIn+1])
2264 ){
2265 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
2266 octet += sqlite3HexToInt(zUri[iIn++]);
2267  
2268 Debug.Assert( octet >= 0 && octet < 256 );
2269 if ( octet == 0 )
2270 {
2271 /* This branch is taken when "%00" appears within the URI. In this
2272 ** case we ignore all text in the remainder of the path, name or
2273 ** value currently being parsed. So ignore the current character
2274 ** and skip to the next "?", "=" or "&", as appropriate. */
2275 while ( iIn < nUri && ( c = zUri[iIn] ) != 0 && c != '#'
2276 && (eState!=0 || c!='?')
2277 && (eState!=1 || (c!='=' && c!='&'))
2278 && (eState!=2 || c!='&')
2279 ){
2280 iIn++;
2281 }
2282 continue;
2283 }
2284 c = (char)octet;
2285 }else if( eState==1 && (c=='&' || c=='=') ){
2286 if ( zFile[zFile.Length-1] == '\0' )
2287 {
2288 /* An empty option name. Ignore this option altogether. */
2289 while( zUri[iIn] != '\0' && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
2290 continue;
2291 }
2292 if( c=='&' ){
2293 zFile.Append('\0');//[iOut++] = '\0';
2294 }else{
2295 eState = 2;
2296 }
2297 c = '\0';
2298 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
2299 c = '\0';
2300 eState = 1;
2301 }
2302 zFile.Append(c);// zFile[iOut++] = c;
2303 }
2304 if ( eState == 1 )
2305 zFile.Append( '\0' );//[iOut++] = '\0';
2306 zFile.Append( '\0' );//[iOut++] = '\0';
2307 zFile.Append( '\0' );//[iOut++] = '\0';
2308  
2309 /* Check if there were any options specified that should be interpreted
2310 ** here. Options that are interpreted here include "vfs" and those that
2311 ** correspond to flags that may be passed to the sqlite3_open_v2()
2312 ** method. */
2313 zOpt = zFile.ToString().Substring(sqlite3Strlen30( zFile ) + 1);
2314 while( zOpt.Length>0 ){
2315 int nOpt = sqlite3Strlen30(zOpt);
2316 string zVal = zOpt.Substring( nOpt );//zOpt[nOpt + 1];
2317 int nVal = sqlite3Strlen30(zVal);
2318  
2319 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
2320 zVfs = zVal;
2321 }else{
2322 OpenMode[] aMode = null;
2323 string zModeType = string.Empty;
2324 int mask = 0;
2325 int limit = 0;
2326  
2327 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
2328 OpenMode[] aCacheMode = new OpenMode[] {
2329 new OpenMode( "shared", SQLITE_OPEN_SHAREDCACHE ),
2330 new OpenMode( "private", SQLITE_OPEN_PRIVATECACHE ),
2331 new OpenMode( null, 0 )
2332 };
2333  
2334 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
2335 aMode = aCacheMode;
2336 limit = mask;
2337 zModeType = "cache";
2338 }
2339 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
2340 OpenMode[] aOpenMode = new OpenMode[] {
2341 new OpenMode( "ro", SQLITE_OPEN_READONLY ),
2342 new OpenMode( "rw", SQLITE_OPEN_READWRITE ),
2343 new OpenMode( "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE ),
2344 new OpenMode( null, 0 )
2345 };
2346  
2347 mask = SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
2348 aMode = aOpenMode;
2349 limit = mask & flags;
2350 zModeType = "access";
2351 }
2352  
2353 if( aMode != null){
2354 int i;
2355 int mode = 0;
2356 for(i=0; aMode[i].z!= null; i++){
2357 string z = aMode[i].z;
2358 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
2359 mode = aMode[i].mode;
2360 break;
2361 }
2362 }
2363 if( mode==0 ){
2364 pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
2365 rc = SQLITE_ERROR;
2366 goto parse_uri_out;
2367 }
2368 if( mode>limit ){
2369 pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
2370 zModeType, zVal);
2371 rc = SQLITE_PERM;
2372 goto parse_uri_out;
2373 }
2374 flags = ((flags & ~mask) | mode);
2375 }
2376 }
2377  
2378 zOpt = zVal.Substring(nVal+1);
2379 }
2380  
2381 }else{
2382 //zFile = sqlite3_malloc(nUri+2);
2383 //if( null==zFile ) return SQLITE_NOMEM;
2384 //memcpy(zFile, zUri, nUri);
2385 zFile = zUri == null ? new StringBuilder() : new StringBuilder(zUri.Substring( 0, nUri ));
2386 zFile.Append( '\0' );//[iOut++] = '\0';
2387 zFile.Append( '\0' );//[iOut++] = '\0';
2388 }
2389  
2390 ppVfs = sqlite3_vfs_find(zVfs);
2391 if( ppVfs==null ){
2392 pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
2393 rc = SQLITE_ERROR;
2394 }
2395 parse_uri_out:
2396 if( rc!=SQLITE_OK ){
2397 //sqlite3_free(zFile);
2398 zFile = null;
2399 }
2400 pFlags = flags;
2401 pzFile = zFile == null ? null : zFile.ToString().Substring( 0, sqlite3Strlen30( zFile.ToString() ) );
2402 return rc;
2403 }
2404  
2405 /*
2406 ** This routine does the work of opening a database on behalf of
2407 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
2408 ** is UTF-8 encoded.
2409 */
2410 static int openDatabase(
2411 string zFilename, /* Database filename UTF-8 encoded */
2412 out sqlite3 ppDb, /* OUT: Returned database handle */
2413 int flags, /* Operational flags */
2414 string zVfs /* Name of the VFS to use */
2415 )
2416 {
2417 sqlite3 db; /* Store allocated handle here */
2418 int rc; /* Return code */
2419 int isThreadsafe; /* True for threadsafe connections */
2420 string zOpen = string.Empty; /* Filename argument to pass to BtreeOpen() */
2421 string zErrMsg = string.Empty; /* Error message from sqlite3ParseUri() */
2422  
2423 ppDb = null;
2424 #if !SQLITE_OMIT_AUTOINIT
2425 rc = sqlite3_initialize();
2426 if ( rc != 0 )
2427 return rc;
2428 #endif
2429  
2430 /* Only allow sensible combinations of bits in the flags argument.
2431 ** Throw an error if any non-sense combination is used. If we
2432 ** do not block illegal combinations here, it could trigger
2433 ** Debug.Assert() statements in deeper layers. Sensible combinations
2434 ** are:
2435 **
2436 ** 1: SQLITE_OPEN_READONLY
2437 ** 2: SQLITE_OPEN_READWRITE
2438 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
2439 */
2440 Debug.Assert( SQLITE_OPEN_READONLY == 0x01 );
2441 Debug.Assert( SQLITE_OPEN_READWRITE == 0x02 );
2442 Debug.Assert( SQLITE_OPEN_CREATE == 0x04 );
2443 testcase( ( 1 << ( flags & 7 ) ) == 0x02 ); /* READONLY */
2444 testcase( ( 1 << ( flags & 7 ) ) == 0x04 ); /* READWRITE */
2445 testcase( ( 1 << ( flags & 7 ) ) == 0x40 ); /* READWRITE | CREATE */
2446 if ( ( ( 1 << ( flags & 7 ) ) & 0x46 ) == 0 ) return SQLITE_MISUSE_BKPT();
2447  
2448 if ( sqlite3GlobalConfig.bCoreMutex == false )
2449 {
2450 isThreadsafe = 0;
2451 }
2452 else if ( ( flags & SQLITE_OPEN_NOMUTEX ) != 0 )
2453 {
2454 isThreadsafe = 0;
2455 }
2456 else if ( ( flags & SQLITE_OPEN_FULLMUTEX ) != 0 )
2457 {
2458 isThreadsafe = 1;
2459 }
2460 else
2461 {
2462 isThreadsafe = sqlite3GlobalConfig.bFullMutex ? 1 : 0;
2463 }
2464 if ( ( flags & SQLITE_OPEN_PRIVATECACHE ) != 0 )
2465 {
2466 flags &= ~SQLITE_OPEN_SHAREDCACHE;
2467 }
2468 else if ( sqlite3GlobalConfig.sharedCacheEnabled )
2469 {
2470 flags |= SQLITE_OPEN_SHAREDCACHE;
2471 }
2472  
2473 /* Remove harmful bits from the flags parameter
2474 **
2475 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
2476 ** dealt with in the previous code block. Besides these, the only
2477 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
2478 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
2479 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
2480 ** off all other flags.
2481 */
2482 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
2483 SQLITE_OPEN_EXCLUSIVE |
2484 SQLITE_OPEN_MAIN_DB |
2485 SQLITE_OPEN_TEMP_DB |
2486 SQLITE_OPEN_TRANSIENT_DB |
2487 SQLITE_OPEN_MAIN_JOURNAL |
2488 SQLITE_OPEN_TEMP_JOURNAL |
2489 SQLITE_OPEN_SUBJOURNAL |
2490 SQLITE_OPEN_MASTER_JOURNAL |
2491 SQLITE_OPEN_NOMUTEX |
2492 SQLITE_OPEN_FULLMUTEX |
2493 SQLITE_OPEN_WAL
2494 );
2495  
2496  
2497 /* Allocate the sqlite data structure */
2498 db = new sqlite3();//sqlite3MallocZero( sqlite3.Length );
2499 if ( db == null )
2500 goto opendb_out;
2501 if ( sqlite3GlobalConfig.bFullMutex && isThreadsafe != 0 )
2502 {
2503 db.mutex = sqlite3MutexAlloc( SQLITE_MUTEX_RECURSIVE );
2504 if ( db.mutex == null )
2505 {
2506 //sqlite3_free( ref db );
2507 goto opendb_out;
2508 }
2509 }
2510 sqlite3_mutex_enter( db.mutex );
2511 db.errMask = 0xff;
2512 db.nDb = 2;
2513 db.magic = SQLITE_MAGIC_BUSY;
2514 Array.Copy( db.aDbStatic, db.aDb, db.aDbStatic.Length );// db.aDb = db.aDbStatic;
2515 Debug.Assert( db.aLimit.Length == aHardLimit.Length );
2516 Buffer.BlockCopy( aHardLimit, 0, db.aLimit, 0, aHardLimit.Length * sizeof( int ) );//memcpy(db.aLimit, aHardLimit, sizeof(db.aLimit));
2517 db.autoCommit = 1;
2518 db.nextAutovac = -1;
2519 db.nextPagesize = 0;
2520 db.flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger;
2521 if ( SQLITE_DEFAULT_FILE_FORMAT < 4 )
2522 db.flags |= SQLITE_LegacyFileFmt
2523 #if SQLITE_ENABLE_LOAD_EXTENSION
2524 | SQLITE_LoadExtension
2525 #endif
2526 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
2527 | SQLITE_RecTriggers
2528 #endif
2529 #if (SQLITE_DEFAULT_FOREIGN_KEYS) //&& SQLITE_DEFAULT_FOREIGN_KEYS
2530 | SQLITE_ForeignKeys
2531 #endif
2532 ;
2533 sqlite3HashInit( db.aCollSeq );
2534 #if !SQLITE_OMIT_VIRTUALTABLE
2535 db.aModule = new Hash();
2536 sqlite3HashInit( db.aModule );
2537 #endif
2538  
2539 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
2540 ** and UTF-16, so add a version for each to avoid any unnecessary
2541 ** conversions. The only error that can occur here is a malloc() failure.
2542 */
2543 createCollation( db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
2544 binCollFunc, null );
2545 createCollation( db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
2546 binCollFunc, null );
2547 createCollation( db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
2548 binCollFunc, null );
2549 createCollation( db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, 1,
2550 binCollFunc, null );
2551 //if ( db.mallocFailed != 0 )
2552 //{
2553 // goto opendb_out;
2554 //}
2555 db.pDfltColl = sqlite3FindCollSeq( db, SQLITE_UTF8, "BINARY", 0 );
2556 Debug.Assert( db.pDfltColl != null );
2557  
2558 /* Also add a UTF-8 case-insensitive collation sequence. */
2559 createCollation( db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
2560 nocaseCollatingFunc, null );
2561  
2562 /* Parse the filename/URI argument. */
2563 db.openFlags = flags;
2564 rc = sqlite3ParseUri( zVfs, zFilename, ref flags, ref db.pVfs, ref zOpen, ref zErrMsg );
2565 if( rc!=SQLITE_OK ){
2566 //if( rc==SQLITE_NOMEM ) db.mallocFailed = 1;
2567 sqlite3Error(db, rc, "%s", zErrMsg);
2568 //sqlite3_free(zErrMsg);
2569 goto opendb_out;
2570 }
2571  
2572 /* Open the backend database driver */
2573 rc = sqlite3BtreeOpen(db.pVfs, zOpen, db, ref db.aDb[0].pBt, 0,
2574 flags | SQLITE_OPEN_MAIN_DB);
2575 if ( rc != SQLITE_OK )
2576 {
2577 if ( rc == SQLITE_IOERR_NOMEM )
2578 {
2579 rc = SQLITE_NOMEM;
2580 }
2581 sqlite3Error( db, rc, 0 );
2582 goto opendb_out;
2583 }
2584 db.aDb[0].pSchema = sqlite3SchemaGet( db, db.aDb[0].pBt );
2585 db.aDb[1].pSchema = sqlite3SchemaGet( db, null );
2586  
2587  
2588 /* The default safety_level for the main database is 'full'; for the temp
2589 ** database it is 'NONE'. This matches the pager layer defaults.
2590 */
2591 db.aDb[0].zName = "main";
2592 db.aDb[0].safety_level = 3;
2593 db.aDb[1].zName = "temp";
2594 db.aDb[1].safety_level = 1;
2595  
2596 db.magic = SQLITE_MAGIC_OPEN;
2597 //if ( db.mallocFailed != 0 )
2598 //{
2599 // goto opendb_out;
2600 //}
2601  
2602 /* Register all built-in functions, but do not attempt to read the
2603 ** database schema yet. This is delayed until the first time the database
2604 ** is accessed.
2605 */
2606 sqlite3Error( db, SQLITE_OK, 0 );
2607 sqlite3RegisterBuiltinFunctions( db );
2608  
2609 /* Load automatic extensions - extensions that have been registered
2610 ** using the sqlite3_automatic_extension() API.
2611 */
2612 sqlite3AutoLoadExtensions( db );
2613 rc = sqlite3_errcode( db );
2614 if ( rc != SQLITE_OK )
2615 {
2616 goto opendb_out;
2617 }
2618  
2619  
2620 #if SQLITE_ENABLE_FTS1
2621 if( 0==db.mallocFailed ){
2622 extern int sqlite3Fts1Init(sqlite3);
2623 rc = sqlite3Fts1Init(db);
2624 }
2625 #endif
2626  
2627 #if SQLITE_ENABLE_FTS2
2628 if( 0==db.mallocFailed && rc==SQLITE_OK ){
2629 extern int sqlite3Fts2Init(sqlite3);
2630 rc = sqlite3Fts2Init(db);
2631 }
2632 #endif
2633  
2634 #if SQLITE_ENABLE_FTS3
2635 if( 0==db.mallocFailed && rc==SQLITE_OK ){
2636 rc = sqlite3Fts3Init(db);
2637 }
2638 #endif
2639  
2640 #if SQLITE_ENABLE_ICU
2641 if( 0==db.mallocFailed && rc==SQLITE_OK ){
2642 extern int sqlite3IcuInit(sqlite3);
2643 rc = sqlite3IcuInit(db);
2644 }
2645 #endif
2646  
2647 #if SQLITE_ENABLE_RTREE
2648 if( 0==db.mallocFailed && rc==SQLITE_OK){
2649 rc = sqlite3RtreeInit(db);
2650 }
2651 #endif
2652  
2653 sqlite3Error( db, rc, 0 );
2654  
2655 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
2656 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
2657 ** mode. Doing nothing at all also makes NORMAL the default.
2658 */
2659 #if SQLITE_DEFAULT_LOCKING_MODE
2660 db.dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
2661 sqlite3PagerLockingMode(sqlite3BtreePager(db.aDb[0].pBt),
2662 SQLITE_DEFAULT_LOCKING_MODE);
2663 #endif
2664  
2665 /* Enable the lookaside-malloc subsystem */
2666 setupLookaside( db, null, sqlite3GlobalConfig.szLookaside,
2667 sqlite3GlobalConfig.nLookaside );
2668  
2669 sqlite3_wal_autocheckpoint( db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT );
2670  
2671 opendb_out:
2672 //sqlite3_free(zOpen);
2673 if ( db != null )
2674 {
2675 Debug.Assert( db.mutex != null || isThreadsafe == 0 || !sqlite3GlobalConfig.bFullMutex );
2676 sqlite3_mutex_leave( db.mutex );
2677 }
2678 rc = sqlite3_errcode( db );
2679 if ( rc == SQLITE_NOMEM )
2680 {
2681 sqlite3_close( db );
2682 db = null;
2683 }
2684 else if ( rc != SQLITE_OK )
2685 {
2686 db.magic = SQLITE_MAGIC_SICK;
2687 }
2688 ppDb = db;
2689 return sqlite3ApiExit( 0, rc );
2690 }
2691  
2692 /*
2693 ** Open a new database handle.
2694 */
2695 static public int sqlite3_open(
2696 string zFilename,
2697 out sqlite3 ppDb
2698 )
2699 {
2700 return openDatabase( zFilename, out ppDb,
2701 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, null );
2702 }
2703  
2704 static public int sqlite3_open_v2(
2705 string filename, /* Database filename (UTF-8) */
2706 out sqlite3 ppDb, /* OUT: SQLite db handle */
2707 int flags, /* Flags */
2708 string zVfs /* Name of VFS module to use */
2709 )
2710 {
2711 return openDatabase( filename, out ppDb, flags, zVfs );
2712 }
2713  
2714 #if !SQLITE_OMIT_UTF16
2715  
2716 /*
2717 ** Open a new database handle.
2718 */
2719 int sqlite3_open16(
2720 string zFilename,
2721 sqlite3 **ppDb
2722 ){
2723 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
2724 sqlite3_value pVal;
2725 int rc;
2726  
2727 Debug.Assert(zFilename );
2728 Debug.Assert(ppDb );
2729 *ppDb = 0;
2730 #if !SQLITE_OMIT_AUTOINIT
2731 rc = sqlite3_initialize();
2732 if( rc !=0) return rc;
2733 #endif
2734 pVal = sqlite3ValueNew(0);
2735 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
2736 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
2737 if( zFilename8 ){
2738 rc = openDatabase(zFilename8, ppDb,
2739 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
2740 Debug.Assert(*ppDb || rc==SQLITE_NOMEM );
2741 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
2742 ENC(*ppDb) = SQLITE_UTF16NATIVE;
2743 }
2744 }else{
2745 rc = SQLITE_NOMEM;
2746 }
2747 sqlite3ValueFree(pVal);
2748  
2749 return sqlite3ApiExit(0, rc);
2750 }
2751 #endif // * SQLITE_OMIT_UTF16 */
2752  
2753 /*
2754 ** Register a new collation sequence with the database handle db.
2755 */
2756 static int sqlite3_create_collation(
2757 sqlite3 db,
2758 string zName,
2759 int enc,
2760 object pCtx,
2761 dxCompare xCompare
2762 )
2763 {
2764 int rc;
2765 sqlite3_mutex_enter( db.mutex );
2766 //Debug.Assert( 0 == db.mallocFailed );
2767 rc = createCollation( db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, null );
2768 rc = sqlite3ApiExit( db, rc );
2769 sqlite3_mutex_leave( db.mutex );
2770 return rc;
2771 }
2772  
2773 /*
2774 ** Register a new collation sequence with the database handle db.
2775 */
2776 static int sqlite3_create_collation_v2(
2777 sqlite3 db,
2778 string zName,
2779 int enc,
2780 object pCtx,
2781 dxCompare xCompare, //int(*xCompare)(void*,int,const void*,int,const void),
2782 dxDelCollSeq xDel //void(*xDel)(void)
2783 )
2784 {
2785 int rc;
2786 sqlite3_mutex_enter( db.mutex );
2787 //Debug.Assert( 0 == db.mallocFailed );
2788 rc = createCollation( db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel );
2789 rc = sqlite3ApiExit( db, rc );
2790 sqlite3_mutex_leave( db.mutex );
2791 return rc;
2792 }
2793  
2794 #if !SQLITE_OMIT_UTF16
2795 /*
2796 ** Register a new collation sequence with the database handle db.
2797 */
2798 //int sqlite3_create_collation16(
2799 // sqlite3* db,
2800 // string zName,
2801 // int enc,
2802 // void* pCtx,
2803 // int(*xCompare)(void*,int,const void*,int,const void)
2804 //){
2805 // int rc = SQLITE_OK;
2806 // string zName8;
2807 // sqlite3_mutex_enter(db.mutex);
2808 // Debug.Assert( 0==db.mallocFailed );
2809 // zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
2810 // if( zName8 ){
2811 // rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
2812 // sqlite3DbFree(db,ref zName8);
2813 // }
2814 // rc = sqlite3ApiExit(db, rc);
2815 // sqlite3_mutex_leave(db.mutex);
2816 // return rc;
2817 //}
2818 #endif // * SQLITE_OMIT_UTF16 */
2819  
2820 /*
2821 ** Register a collation sequence factory callback with the database handle
2822 ** db. Replace any previously installed collation sequence factory.
2823 */
2824 static int sqlite3_collation_needed(
2825 sqlite3 db,
2826 object pCollNeededArg,
2827 dxCollNeeded xCollNeeded
2828 )
2829 {
2830 sqlite3_mutex_enter( db.mutex );
2831 db.xCollNeeded = xCollNeeded;
2832 db.xCollNeeded16 = null;
2833 db.pCollNeededArg = pCollNeededArg;
2834 sqlite3_mutex_leave( db.mutex );
2835 return SQLITE_OK;
2836 }
2837  
2838 #if !SQLITE_OMIT_UTF16
2839 /*
2840 ** Register a collation sequence factory callback with the database handle
2841 ** db. Replace any previously installed collation sequence factory.
2842 */
2843 //int sqlite3_collation_needed16(
2844 // sqlite3 db,
2845 // void pCollNeededArg,
2846 // void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void)
2847 //){
2848 // sqlite3_mutex_enter(db.mutex);
2849 // db.xCollNeeded = 0;
2850 // db.xCollNeeded16 = xCollNeeded16;
2851 // db.pCollNeededArg = pCollNeededArg;
2852 // sqlite3_mutex_leave(db.mutex);
2853 // return SQLITE_OK;
2854 //}
2855 #endif // * SQLITE_OMIT_UTF16 */
2856  
2857 #if !SQLITE_OMIT_DEPRECATED
2858 /*
2859 ** This function is now an anachronism. It used to be used to recover from a
2860 ** malloc() failure, but SQLite now does this automatically.
2861 */
2862 static int sqlite3_global_recover()
2863 {
2864 return SQLITE_OK;
2865 }
2866 #endif
2867  
2868 /*
2869 ** Test to see whether or not the database connection is in autocommit
2870 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
2871 ** by default. Autocommit is disabled by a BEGIN statement and reenabled
2872 ** by the next COMMIT or ROLLBACK.
2873 **
2874 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
2875 */
2876 static u8 sqlite3_get_autocommit( sqlite3 db )
2877 {
2878 return db.autoCommit;
2879 }
2880  
2881 /*
2882 ** The following routines are subtitutes for constants SQLITE_CORRUPT,
2883 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
2884 ** constants. They server two purposes:
2885 **
2886 ** 1. Serve as a convenient place to set a breakpoint in a debugger
2887 ** to detect when version error conditions occurs.
2888 **
2889 ** 2. Invoke sqlite3_log() to provide the source code location where
2890 ** a low-level error is first detected.
2891 */
2892 static int sqlite3CorruptError( int lineno )
2893 {
2894 testcase( sqlite3GlobalConfig.xLog != null );
2895 sqlite3_log( SQLITE_CORRUPT,
2896 "database corruption at line %d of [%.10s]",
2897 lineno, 20 + sqlite3_sourceid() );
2898 return SQLITE_CORRUPT;
2899 }
2900 static int sqlite3MisuseError( int lineno )
2901 {
2902 testcase( sqlite3GlobalConfig.xLog != null );
2903 sqlite3_log( SQLITE_MISUSE,
2904 "misuse at line %d of [%.10s]",
2905 lineno, 20 + sqlite3_sourceid() );
2906 return SQLITE_MISUSE;
2907 }
2908 static int sqlite3CantopenError( int lineno )
2909 {
2910 testcase( sqlite3GlobalConfig.xLog != null );
2911 sqlite3_log( SQLITE_CANTOPEN,
2912 "cannot open file at line %d of [%.10s]",
2913 lineno, 20 + sqlite3_sourceid() );
2914 return SQLITE_CANTOPEN;
2915 }
2916  
2917 #if !SQLITE_OMIT_DEPRECATED
2918 /*
2919 ** This is a convenience routine that makes sure that all thread-specific
2920 ** data for this thread has been deallocated.
2921 **
2922 ** SQLite no longer uses thread-specific data so this routine is now a
2923 ** no-op. It is retained for historical compatibility.
2924 */
2925 void sqlite3_thread_cleanup()
2926 {
2927 }
2928 #endif
2929 /*
2930 ** Return meta information about a specific column of a database table.
2931 ** See comment in sqlite3.h (sqlite.h.in) for details.
2932 */
2933 #if SQLITE_ENABLE_COLUMN_METADATA
2934  
2935 public static int sqlite3_table_column_metadata(
2936 sqlite3 db, /* Connection handle */
2937 string zDbName, /* Database name or NULL */
2938 string zTableName, /* Table name */
2939 string zColumnName, /* Column name */
2940 ref string pzDataType, /* OUTPUT: Declared data type */
2941 ref string pzCollSeq, /* OUTPUT: Collation sequence name */
2942 ref int pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
2943 ref int pPrimaryKey, /* OUTPUT: True if column part of PK */
2944 ref int pAutoinc /* OUTPUT: True if column is auto-increment */
2945 )
2946 {
2947 int rc;
2948 string zErrMsg = string.Empty;
2949 Table pTab = null;
2950 Column pCol = null;
2951 int iCol;
2952  
2953 string zDataType = null;
2954 string zCollSeq = null;
2955 int notnull = 0;
2956 int primarykey = 0;
2957 int autoinc = 0;
2958  
2959 /* Ensure the database schema has been loaded */
2960 sqlite3_mutex_enter( db.mutex );
2961 sqlite3BtreeEnterAll( db );
2962 rc = sqlite3Init( db, ref zErrMsg );
2963 if ( SQLITE_OK != rc )
2964 {
2965 goto error_out;
2966 }
2967  
2968 /* Locate the table in question */
2969 pTab = sqlite3FindTable( db, zTableName, zDbName );
2970 if ( null == pTab || pTab.pSelect != null )
2971 {
2972 pTab = null;
2973 goto error_out;
2974 }
2975  
2976 /* Find the column for which info is requested */
2977 if ( sqlite3IsRowid( zColumnName ) )
2978 {
2979 iCol = pTab.iPKey;
2980 if ( iCol >= 0 )
2981 {
2982 pCol = pTab.aCol[iCol];
2983 }
2984 }
2985 else
2986 {
2987 for ( iCol = 0; iCol < pTab.nCol; iCol++ )
2988 {
2989 pCol = pTab.aCol[iCol];
2990 if ( pCol.zName.Equals( zColumnName, StringComparison.OrdinalIgnoreCase ) )
2991 {
2992 break;
2993 }
2994 }
2995 if ( iCol == pTab.nCol )
2996 {
2997 pTab = null;
2998 goto error_out;
2999 }
3000 }
3001  
3002 /* The following block stores the meta information that will be returned
3003 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
3004 ** and autoinc. At this point there are two possibilities:
3005 **
3006 ** 1. The specified column name was rowid", "oid" or "_rowid_"
3007 ** and there is no explicitly declared IPK column.
3008 **
3009 ** 2. The table is not a view and the column name identified an
3010 ** explicitly declared column. Copy meta information from pCol.
3011 */
3012 if ( pCol != null )
3013 {
3014 zDataType = pCol.zType;
3015 zCollSeq = pCol.zColl;
3016 notnull = pCol.notNull != 0 ? 1 : 0;
3017 primarykey = pCol.isPrimKey != 0 ? 1 : 0;
3018 autoinc = ( pTab.iPKey == iCol && ( pTab.tabFlags & TF_Autoincrement ) != 0 ) ? 1 : 0;
3019 }
3020 else
3021 {
3022 zDataType = "INTEGER";
3023 primarykey = 1;
3024 }
3025 if ( string.IsNullOrEmpty( zCollSeq ) )
3026 {
3027 zCollSeq = "BINARY";
3028 }
3029  
3030 error_out:
3031 sqlite3BtreeLeaveAll( db );
3032  
3033 /* Whether the function call succeeded or failed, set the output parameters
3034 ** to whatever their local counterparts contain. If an error did occur,
3035 ** this has the effect of zeroing all output parameters.
3036 */
3037 //if ( pzDataType )
3038 pzDataType = zDataType;
3039 //if ( pzCollSeq )
3040 pzCollSeq = zCollSeq;
3041 //if ( pNotNull )
3042 pNotNull = notnull;
3043 //if ( pPrimaryKey )
3044 pPrimaryKey = primarykey;
3045 //if ( pAutoinc )
3046 pAutoinc = autoinc;
3047  
3048 if ( SQLITE_OK == rc && null == pTab )
3049 {
3050 sqlite3DbFree( db, ref zErrMsg );
3051 zErrMsg = sqlite3MPrintf( db, "no such table column: %s.%s", zTableName,
3052 zColumnName );
3053 rc = SQLITE_ERROR;
3054 }
3055 sqlite3Error( db, rc, ( !string.IsNullOrEmpty( zErrMsg ) ? "%s" : null ), zErrMsg );
3056 sqlite3DbFree( db, ref zErrMsg );
3057 rc = sqlite3ApiExit( db, rc );
3058 sqlite3_mutex_leave( db.mutex );
3059 return rc;
3060 }
3061 #endif
3062  
3063 /*
3064 ** Sleep for a little while. Return the amount of time slept.
3065 */
3066 static public int sqlite3_sleep( int ms )
3067 {
3068 sqlite3_vfs pVfs;
3069 int rc;
3070 pVfs = sqlite3_vfs_find( null );
3071 if ( pVfs == null )
3072 return 0;
3073  
3074 /* This function works in milliseconds, but the underlying OsSleep()
3075 ** API uses microseconds. Hence the 1000's.
3076 */
3077 rc = ( sqlite3OsSleep( pVfs, 1000 * ms ) / 1000 );
3078 return rc;
3079 }
3080  
3081 /*
3082 ** Enable or disable the extended result codes.
3083 */
3084 static int sqlite3_extended_result_codes( sqlite3 db, bool onoff )
3085 {
3086 sqlite3_mutex_enter( db.mutex );
3087 db.errMask = (int)( onoff ? 0xffffffff : 0xff );
3088 sqlite3_mutex_leave( db.mutex );
3089 return SQLITE_OK;
3090 }
3091  
3092 /*
3093 ** Invoke the xFileControl method on a particular database.
3094 */
3095 static int sqlite3_file_control( sqlite3 db, string zDbName, int op, ref sqlite3_int64 pArg )
3096 {
3097 int rc = SQLITE_ERROR;
3098 int iDb;
3099 sqlite3_mutex_enter( db.mutex );
3100 if ( zDbName == null )
3101 {
3102 iDb = 0;
3103 }
3104 else
3105 {
3106 for ( iDb = 0; iDb < db.nDb; iDb++ )
3107 {
3108 if ( db.aDb[iDb].zName == zDbName )
3109 break;
3110 }
3111 }
3112 if ( iDb < db.nDb )
3113 {
3114 Btree pBtree = db.aDb[iDb].pBt;
3115 if ( pBtree != null )
3116 {
3117 Pager pPager;
3118 sqlite3_file fd;
3119 sqlite3BtreeEnter( pBtree );
3120 pPager = sqlite3BtreePager( pBtree );
3121 Debug.Assert( pPager != null );
3122 fd = sqlite3PagerFile( pPager );
3123 Debug.Assert( fd != null );
3124 if ( op == SQLITE_FCNTL_FILE_POINTER )
3125 {
3126 #if (SQLITE_SILVERLIGHT || WINDOWS_MOBILE)
3127 pArg = (long)-1; // not supported
3128 #else
3129 pArg = (long)fd.fs.Handle;
3130 #endif
3131 rc = SQLITE_OK;
3132 }
3133 else if ( fd.pMethods != null )
3134 {
3135 rc = sqlite3OsFileControl( fd, (u32)op, ref pArg );
3136 }
3137 else
3138 {
3139 rc = SQLITE_NOTFOUND;
3140 }
3141 sqlite3BtreeLeave( pBtree );
3142 }
3143 }
3144 sqlite3_mutex_leave( db.mutex );
3145 return rc;
3146 }
3147  
3148 /*
3149 ** Interface to the testing logic.
3150 */
3151 static int sqlite3_test_control( int op, params object[] ap )
3152 {
3153 int rc = 0;
3154 #if !SQLITE_OMIT_BUILTIN_TEST
3155 // va_list ap;
3156 lock ( lock_va_list )
3157 {
3158 va_start( ap, "op" );
3159 switch ( op )
3160 {
3161  
3162 /*
3163 ** Save the current state of the PRNG.
3164 */
3165 case SQLITE_TESTCTRL_PRNG_SAVE:
3166 {
3167 sqlite3PrngSaveState();
3168 break;
3169 }
3170  
3171 /*
3172 ** Restore the state of the PRNG to the last state saved using
3173 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
3174 ** this verb acts like PRNG_RESET.
3175 */
3176 case SQLITE_TESTCTRL_PRNG_RESTORE:
3177 {
3178 sqlite3PrngRestoreState();
3179 break;
3180 }
3181  
3182 /*
3183 ** Reset the PRNG back to its uninitialized state. The next call
3184 ** to sqlite3_randomness() will reseed the PRNG using a single call
3185 ** to the xRandomness method of the default VFS.
3186 */
3187 case SQLITE_TESTCTRL_PRNG_RESET:
3188 {
3189 sqlite3PrngResetState();
3190 break;
3191 }
3192  
3193 /*
3194 ** sqlite3_test_control(BITVEC_TEST, size, program)
3195 **
3196 ** Run a test against a Bitvec object of size. The program argument
3197 ** is an array of integers that defines the test. Return -1 on a
3198 ** memory allocation error, 0 on success, or non-zero for an error.
3199 ** See the sqlite3BitvecBuiltinTest() for additional information.
3200 */
3201 case SQLITE_TESTCTRL_BITVEC_TEST:
3202 {
3203 int sz = va_arg( ap, ( Int32 ) 0 );
3204 int[] aProg = va_arg( ap, (Int32[])null );
3205 rc = sqlite3BitvecBuiltinTest( (u32)sz, aProg );
3206 break;
3207 }
3208  
3209 /*
3210 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
3211 **
3212 ** Register hooks to call to indicate which malloc() failures
3213 ** are benign.
3214 */
3215 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
3216 {
3217 //typedef void (*void_function)(void);
3218 void_function xBenignBegin;
3219 void_function xBenignEnd;
3220 xBenignBegin = va_arg( ap, (void_function)null );
3221 xBenignEnd = va_arg( ap, (void_function)null );
3222 sqlite3BenignMallocHooks( xBenignBegin, xBenignEnd );
3223 break;
3224 }
3225 /*
3226 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
3227 **
3228 ** Set the PENDING byte to the value in the argument, if X>0.
3229 ** Make no changes if X==0. Return the value of the pending byte
3230 ** as it existing before this routine was called.
3231 **
3232 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
3233 ** an incompatible database file format. Changing the PENDING byte
3234 ** while any database connection is open results in undefined and
3235 ** dileterious behavior.
3236 */
3237 case SQLITE_TESTCTRL_PENDING_BYTE:
3238 {
3239 rc = PENDING_BYTE;
3240 #if !SQLITE_OMIT_WSD
3241 {
3242 u32 newVal = va_arg( ap, (UInt32)0 );
3243 if ( newVal != 0 )
3244 {
3245 if ( sqlite3PendingByte != newVal )
3246 sqlite3PendingByte = (int)newVal;
3247 #if DEBUG && TCLSH
3248 TCLsqlite3PendingByte.iValue = sqlite3PendingByte;
3249 #endif
3250 PENDING_BYTE = sqlite3PendingByte;
3251 }
3252 }
3253 #endif
3254 break;
3255 }
3256  
3257 /*
3258 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
3259 **
3260 ** This action provides a run-time test to see whether or not
3261 ** Debug.Assert() was enabled at compile-time. If X is true and Debug.Assert()
3262 ** is enabled, then the return value is true. If X is true and
3263 ** Debug.Assert() is disabled, then the return value is zero. If X is
3264 ** false and Debug.Assert() is enabled, then the assertion fires and the
3265 ** process aborts. If X is false and Debug.Assert() is disabled, then the
3266 ** return value is zero.
3267 */
3268 case SQLITE_TESTCTRL_ASSERT:
3269 {
3270 int x = 0;
3271 Debug.Assert( ( x = va_arg( ap, ( Int32 ) 0 ) ) != 0 );
3272 rc = x;
3273 break;
3274 }
3275  
3276  
3277 /*
3278 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
3279 **
3280 ** This action provides a run-time test to see how the ALWAYS and
3281 ** NEVER macros were defined at compile-time.
3282 **
3283 ** The return value is ALWAYS(X).
3284 **
3285 ** The recommended test is X==2. If the return value is 2, that means
3286 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
3287 ** default setting. If the return value is 1, then ALWAYS() is either
3288 ** hard-coded to true or else it asserts if its argument is false.
3289 ** The first behavior (hard-coded to true) is the case if
3290 ** SQLITE_TESTCTRL_ASSERT shows that Debug.Assert() is disabled and the second
3291 ** behavior (assert if the argument to ALWAYS() is false) is the case if
3292 ** SQLITE_TESTCTRL_ASSERT shows that Debug.Assert() is enabled.
3293 **
3294 ** The run-time test procedure might look something like this:
3295 **
3296 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
3297 ** // ALWAYS() and NEVER() are no-op pass-through macros
3298 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
3299 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
3300 ** }else{
3301 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
3302 ** }
3303 */
3304 case SQLITE_TESTCTRL_ALWAYS:
3305 {
3306 int x = va_arg( ap, ( Int32 ) 0 );
3307 rc = ALWAYS( x );
3308 break;
3309 }
3310  
3311 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 db, int N)
3312 **
3313 ** Set the nReserve size to N for the main database on the database
3314 ** connection db.
3315 */
3316 case SQLITE_TESTCTRL_RESERVE:
3317 {
3318 sqlite3 db = va_arg( ap, (sqlite3)null );
3319 int x = va_arg( ap, ( Int32 ) 0 );
3320 sqlite3_mutex_enter( db.mutex );
3321 sqlite3BtreeSetPageSize( db.aDb[0].pBt, 0, x, 0 );
3322 sqlite3_mutex_leave( db.mutex );
3323 break;
3324 }
3325  
3326 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 db, int N)
3327 **
3328 ** Enable or disable various optimizations for testing purposes. The
3329 ** argument N is a bitmask of optimizations to be disabled. For normal
3330 ** operation N should be 0. The idea is that a test program (like the
3331 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
3332 ** with various optimizations disabled to verify that the same answer
3333 ** is obtained in every case.
3334 */
3335 case SQLITE_TESTCTRL_OPTIMIZATIONS:
3336 {
3337 sqlite3 db = va_arg( ap, (sqlite3)null );//sqlite3 db = va_arg(ap, sqlite3);
3338 int x = va_arg( ap, (Int32)0 );//int x = va_arg(ap,int);
3339 db.flags = ( x & SQLITE_OptMask ) | ( db.flags & ~SQLITE_OptMask );
3340 break;
3341 }
3342  
3343 //#if SQLITE_N_KEYWORD
3344 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const string zWord)
3345 **
3346 ** If zWord is a keyword recognized by the parser, then return the
3347 ** number of keywords. Or if zWord is not a keyword, return 0.
3348 **
3349 ** This test feature is only available in the amalgamation since
3350 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
3351 ** is built using separate source files.
3352 */
3353 case SQLITE_TESTCTRL_ISKEYWORD:
3354 {
3355 string zWord = (string)va_arg( ap, "char*" );
3356 int n = sqlite3Strlen30( zWord );
3357 rc = ( sqlite3KeywordCode( zWord, n ) != TK_ID ) ? SQLITE_N_KEYWORD : 0;
3358 break;
3359 }
3360 //#endif
3361 /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
3362 **
3363 ** Return the size of a pcache header in bytes.
3364 */
3365 case SQLITE_TESTCTRL_PGHDRSZ:
3366 {
3367 rc = -1;// sizeof(PgHdr);
3368 break;
3369 }
3370  
3371 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
3372 **
3373 ** Pass pFree into sqlite3ScratchFree().
3374 ** If sz>0 then allocate a scratch buffer into pNew.
3375 */
3376 case SQLITE_TESTCTRL_SCRATCHMALLOC:
3377 {
3378 //void pFree, *ppNew;
3379 //int sz;
3380 //sz = va_arg(ap, int);
3381 //ppNew = va_arg(ap, void*);
3382 //pFree = va_arg(ap, void);
3383 //if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
3384 //sqlite3ScratchFree(pFree);
3385 break;
3386 }
3387 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
3388 **
3389 ** If parameter onoff is non-zero, configure the wrappers so that all
3390 ** subsequent calls to localtime() and variants fail. If onoff is zero,
3391 ** undo this setting.
3392 */
3393 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
3394 sqlite3GlobalConfig.bLocaltimeFault = va_arg( ap, (Boolean)true );
3395 break;
3396 }
3397  
3398 }
3399 va_end( ref ap );
3400 }
3401 #endif //* SQLITE_OMIT_BUILTIN_TEST */
3402 return rc;
3403 }
3404  
3405  
3406 /*
3407 ** This is a utility routine, useful to VFS implementations, that checks
3408 ** to see if a database file was a URI that contained a specific query
3409 ** parameter, and if so obtains the value of the query parameter.
3410 **
3411 ** The zFilename argument is the filename pointer passed into the xOpen()
3412 ** method of a VFS implementation. The zParam argument is the name of the
3413 ** query parameter we seek. This routine returns the value of the zParam
3414 ** parameter if it exists. If the parameter does not exist, this routine
3415 ** returns a NULL pointer.
3416 */
3417 static string sqlite3_uri_parameter(string zFilename, string zParam){
3418 Debugger.Break();
3419 //zFilename += sqlite3Strlen30(zFilename) + 1;
3420 //while( zFilename[0] ){
3421 // int x = strcmp(zFilename, zParam);
3422 // zFilename += sqlite3Strlen30(zFilename) + 1;
3423 // if( x==0 ) return zFilename;
3424 // zFilename += sqlite3Strlen30(zFilename) + 1;
3425 //}
3426 return null;
3427 }
3428  
3429 }
3430 }