Fossil SCM

Update the built-in SQLite to the latest version from the trunk of the SQLite tree. The diff of this SQLite change will be very slow without the previous check-in that enhances the diff performance.

drh 2012-06-22 23:32 trunk
Commit c5b835ddc0a66e9b833e8c450199997bf6a23e1c
3 files changed +11 -17 +322 -2554 +42 -23
+11 -17
--- src/shell.c
+++ src/shell.c
@@ -34,23 +34,19 @@
3434
#include <assert.h>
3535
#include "sqlite3.h"
3636
#include <ctype.h>
3737
#include <stdarg.h>
3838
39
-#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
39
+#if !defined(_WIN32) && !defined(WIN32)
4040
# include <signal.h>
4141
# if !defined(__RTP__) && !defined(_WRS_KERNEL)
4242
# include <pwd.h>
4343
# endif
4444
# include <unistd.h>
4545
# include <sys/types.h>
4646
#endif
4747
48
-#ifdef __OS2__
49
-# include <unistd.h>
50
-#endif
51
-
5248
#ifdef HAVE_EDITLINE
5349
# include <editline/editline.h>
5450
#endif
5551
#if defined(HAVE_READLINE) && HAVE_READLINE==1
5652
# include <readline/readline.h>
@@ -90,11 +86,11 @@
9086
/* ctype macros that work with signed characters */
9187
#define IsSpace(X) isspace((unsigned char)X)
9288
#define IsDigit(X) isdigit((unsigned char)X)
9389
#define ToLower(X) (char)tolower((unsigned char)X)
9490
95
-#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
91
+#if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL)
9692
#include <sys/time.h>
9793
#include <sys/resource.h>
9894
9995
/* Saved resource information for the beginning of an operation */
10096
static struct rusage sBegin;
@@ -2694,35 +2690,37 @@
26942690
*/
26952691
static char *find_home_dir(void){
26962692
static char *home_dir = NULL;
26972693
if( home_dir ) return home_dir;
26982694
2699
-#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2700
- struct passwd *pwent;
2701
- uid_t uid = getuid();
2702
- if( (pwent=getpwuid(uid)) != NULL) {
2703
- home_dir = pwent->pw_dir;
2695
+#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2696
+ {
2697
+ struct passwd *pwent;
2698
+ uid_t uid = getuid();
2699
+ if( (pwent=getpwuid(uid)) != NULL) {
2700
+ home_dir = pwent->pw_dir;
2701
+ }
27042702
}
27052703
#endif
27062704
27072705
#if defined(_WIN32_WCE)
27082706
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
27092707
*/
27102708
home_dir = "/";
27112709
#else
27122710
2713
-#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2711
+#if defined(_WIN32) || defined(WIN32)
27142712
if (!home_dir) {
27152713
home_dir = getenv("USERPROFILE");
27162714
}
27172715
#endif
27182716
27192717
if (!home_dir) {
27202718
home_dir = getenv("HOME");
27212719
}
27222720
2723
-#if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2721
+#if defined(_WIN32) || defined(WIN32)
27242722
if (!home_dir) {
27252723
char *zDrive, *zPath;
27262724
int n;
27272725
zDrive = getenv("HOMEDRIVE");
27282726
zPath = getenv("HOMEPATH");
@@ -2934,15 +2932,11 @@
29342932
exit(1);
29352933
}
29362934
}
29372935
}
29382936
if( i<argc ){
2939
-#if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
2940
- data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
2941
-#else
29422937
data.zDbFilename = argv[i++];
2943
-#endif
29442938
}else{
29452939
#ifndef SQLITE_OMIT_MEMORYDB
29462940
data.zDbFilename = ":memory:";
29472941
#else
29482942
data.zDbFilename = 0;
29492943
--- src/shell.c
+++ src/shell.c
@@ -34,23 +34,19 @@
34 #include <assert.h>
35 #include "sqlite3.h"
36 #include <ctype.h>
37 #include <stdarg.h>
38
39 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
40 # include <signal.h>
41 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
42 # include <pwd.h>
43 # endif
44 # include <unistd.h>
45 # include <sys/types.h>
46 #endif
47
48 #ifdef __OS2__
49 # include <unistd.h>
50 #endif
51
52 #ifdef HAVE_EDITLINE
53 # include <editline/editline.h>
54 #endif
55 #if defined(HAVE_READLINE) && HAVE_READLINE==1
56 # include <readline/readline.h>
@@ -90,11 +86,11 @@
90 /* ctype macros that work with signed characters */
91 #define IsSpace(X) isspace((unsigned char)X)
92 #define IsDigit(X) isdigit((unsigned char)X)
93 #define ToLower(X) (char)tolower((unsigned char)X)
94
95 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
96 #include <sys/time.h>
97 #include <sys/resource.h>
98
99 /* Saved resource information for the beginning of an operation */
100 static struct rusage sBegin;
@@ -2694,35 +2690,37 @@
2694 */
2695 static char *find_home_dir(void){
2696 static char *home_dir = NULL;
2697 if( home_dir ) return home_dir;
2698
2699 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2700 struct passwd *pwent;
2701 uid_t uid = getuid();
2702 if( (pwent=getpwuid(uid)) != NULL) {
2703 home_dir = pwent->pw_dir;
 
 
2704 }
2705 #endif
2706
2707 #if defined(_WIN32_WCE)
2708 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
2709 */
2710 home_dir = "/";
2711 #else
2712
2713 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2714 if (!home_dir) {
2715 home_dir = getenv("USERPROFILE");
2716 }
2717 #endif
2718
2719 if (!home_dir) {
2720 home_dir = getenv("HOME");
2721 }
2722
2723 #if defined(_WIN32) || defined(WIN32) || defined(__OS2__)
2724 if (!home_dir) {
2725 char *zDrive, *zPath;
2726 int n;
2727 zDrive = getenv("HOMEDRIVE");
2728 zPath = getenv("HOMEPATH");
@@ -2934,15 +2932,11 @@
2934 exit(1);
2935 }
2936 }
2937 }
2938 if( i<argc ){
2939 #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
2940 data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
2941 #else
2942 data.zDbFilename = argv[i++];
2943 #endif
2944 }else{
2945 #ifndef SQLITE_OMIT_MEMORYDB
2946 data.zDbFilename = ":memory:";
2947 #else
2948 data.zDbFilename = 0;
2949
--- src/shell.c
+++ src/shell.c
@@ -34,23 +34,19 @@
34 #include <assert.h>
35 #include "sqlite3.h"
36 #include <ctype.h>
37 #include <stdarg.h>
38
39 #if !defined(_WIN32) && !defined(WIN32)
40 # include <signal.h>
41 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
42 # include <pwd.h>
43 # endif
44 # include <unistd.h>
45 # include <sys/types.h>
46 #endif
47
 
 
 
 
48 #ifdef HAVE_EDITLINE
49 # include <editline/editline.h>
50 #endif
51 #if defined(HAVE_READLINE) && HAVE_READLINE==1
52 # include <readline/readline.h>
@@ -90,11 +86,11 @@
86 /* ctype macros that work with signed characters */
87 #define IsSpace(X) isspace((unsigned char)X)
88 #define IsDigit(X) isdigit((unsigned char)X)
89 #define ToLower(X) (char)tolower((unsigned char)X)
90
91 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL)
92 #include <sys/time.h>
93 #include <sys/resource.h>
94
95 /* Saved resource information for the beginning of an operation */
96 static struct rusage sBegin;
@@ -2694,35 +2690,37 @@
2690 */
2691 static char *find_home_dir(void){
2692 static char *home_dir = NULL;
2693 if( home_dir ) return home_dir;
2694
2695 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL)
2696 {
2697 struct passwd *pwent;
2698 uid_t uid = getuid();
2699 if( (pwent=getpwuid(uid)) != NULL) {
2700 home_dir = pwent->pw_dir;
2701 }
2702 }
2703 #endif
2704
2705 #if defined(_WIN32_WCE)
2706 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
2707 */
2708 home_dir = "/";
2709 #else
2710
2711 #if defined(_WIN32) || defined(WIN32)
2712 if (!home_dir) {
2713 home_dir = getenv("USERPROFILE");
2714 }
2715 #endif
2716
2717 if (!home_dir) {
2718 home_dir = getenv("HOME");
2719 }
2720
2721 #if defined(_WIN32) || defined(WIN32)
2722 if (!home_dir) {
2723 char *zDrive, *zPath;
2724 int n;
2725 zDrive = getenv("HOMEDRIVE");
2726 zPath = getenv("HOMEPATH");
@@ -2934,15 +2932,11 @@
2932 exit(1);
2933 }
2934 }
2935 }
2936 if( i<argc ){
 
 
 
2937 data.zDbFilename = argv[i++];
 
2938 }else{
2939 #ifndef SQLITE_OMIT_MEMORYDB
2940 data.zDbFilename = ":memory:";
2941 #else
2942 data.zDbFilename = 0;
2943
+322 -2554
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.7.13. By combining all the individual C code files into this
3
+** version 3.7.14. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -387,10 +387,11 @@
387387
** Exactly one of the following macros must be defined in order to
388388
** specify which memory allocation subsystem to use.
389389
**
390390
** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
391391
** SQLITE_WIN32_MALLOC // Use Win32 native heap API
392
+** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
392393
** SQLITE_MEMDEBUG // Debugging version of system malloc()
393394
**
394395
** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
395396
** assert() macro is enabled, each call into the Win32 native heap subsystem
396397
** will cause HeapValidate to be called. If heap validation should fail, an
@@ -400,15 +401,23 @@
400401
** pared it down to just these three.)
401402
**
402403
** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
403404
** the default.
404405
*/
405
-#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
406
-# error "At most one of the following compile-time configuration options\
407
- is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
406
+#if defined(SQLITE_SYSTEM_MALLOC) \
407
+ + defined(SQLITE_WIN32_MALLOC) \
408
+ + defined(SQLITE_ZERO_MALLOC) \
409
+ + defined(SQLITE_MEMDEBUG)>1
410
+# error "Two or more of the following compile-time configuration options\
411
+ are defined but at most one is allowed:\
412
+ SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
413
+ SQLITE_ZERO_MALLOC"
408414
#endif
409
-#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
415
+#if defined(SQLITE_SYSTEM_MALLOC) \
416
+ + defined(SQLITE_WIN32_MALLOC) \
417
+ + defined(SQLITE_ZERO_MALLOC) \
418
+ + defined(SQLITE_MEMDEBUG)==0
410419
# define SQLITE_SYSTEM_MALLOC 1
411420
#endif
412421
413422
/*
414423
** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -662,13 +671,13 @@
662671
**
663672
** See also: [sqlite3_libversion()],
664673
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
665674
** [sqlite_version()] and [sqlite_source_id()].
666675
*/
667
-#define SQLITE_VERSION "3.7.13"
668
-#define SQLITE_VERSION_NUMBER 3007013
669
-#define SQLITE_SOURCE_ID "2012-06-07 07:24:04 506008f000ba4af0b35da023b8c52f7a3f5033bd"
676
+#define SQLITE_VERSION "3.7.14"
677
+#define SQLITE_VERSION_NUMBER 3007014
678
+#define SQLITE_SOURCE_ID "2012-06-21 17:21:52 d5e6880279210ca63e2d5e7f6d009f30566f1242"
670679
671680
/*
672681
** CAPI3REF: Run-Time Library Version Numbers
673682
** KEYWORDS: sqlite3_version, sqlite3_sourceid
674683
**
@@ -774,11 +783,12 @@
774783
**
775784
** Each open SQLite database is represented by a pointer to an instance of
776785
** the opaque structure named "sqlite3". It is useful to think of an sqlite3
777786
** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
778787
** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
779
-** is its destructor. There are many other interfaces (such as
788
+** and [sqlite3_close_v2()] are its destructors. There are many other
789
+** interfaces (such as
780790
** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
781791
** [sqlite3_busy_timeout()] to name but three) that are methods on an
782792
** sqlite3 object.
783793
*/
784794
typedef struct sqlite3 sqlite3;
@@ -821,32 +831,50 @@
821831
#endif
822832
823833
/*
824834
** CAPI3REF: Closing A Database Connection
825835
**
826
-** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
827
-** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
828
-** successfully destroyed and all associated resources are deallocated.
836
+** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
837
+** for the [sqlite3] object.
838
+** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
839
+** the [sqlite3] object is successfully destroyed and all associated
840
+** resources are deallocated.
829841
**
830
-** Applications must [sqlite3_finalize | finalize] all [prepared statements]
831
-** and [sqlite3_blob_close | close] all [BLOB handles] associated with
832
-** the [sqlite3] object prior to attempting to close the object. ^If
842
+** ^If the database connection is associated with unfinalized prepared
843
+** statements or unfinished sqlite3_backup objects then sqlite3_close()
844
+** will leave the database connection open and return [SQLITE_BUSY].
845
+** ^If sqlite3_close_v2() is called with unfinalized prepared statements
846
+** and unfinished sqlite3_backups, then the database connection becomes
847
+** an unusable "zombie" which will automatically be deallocated when the
848
+** last prepared statement is finalized or the last sqlite3_backup is
849
+** finished. The sqlite3_close_v2() interface is intended for use with
850
+** host languages that are garbage collected, and where the order in which
851
+** destructors are called is arbitrary.
852
+**
853
+** Applications should [sqlite3_finalize | finalize] all [prepared statements],
854
+** [sqlite3_blob_close | close] all [BLOB handles], and
855
+** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
856
+** with the [sqlite3] object prior to attempting to close the object. ^If
833857
** sqlite3_close() is called on a [database connection] that still has
834
-** outstanding [prepared statements] or [BLOB handles], then it returns
835
-** SQLITE_BUSY.
858
+** outstanding [prepared statements], [BLOB handles], and/or
859
+** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
860
+** of resources is deferred until all [prepared statements], [BLOB handles],
861
+** and [sqlite3_backup] objects are also destroyed.
836862
**
837
-** ^If [sqlite3_close()] is invoked while a transaction is open,
863
+** ^If an [sqlite3] object is destroyed while a transaction is open,
838864
** the transaction is automatically rolled back.
839865
**
840
-** The C parameter to [sqlite3_close(C)] must be either a NULL
866
+** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
867
+** must be either a NULL
841868
** pointer or an [sqlite3] object pointer obtained
842869
** from [sqlite3_open()], [sqlite3_open16()], or
843870
** [sqlite3_open_v2()], and not previously closed.
844
-** ^Calling sqlite3_close() with a NULL pointer argument is a
845
-** harmless no-op.
871
+** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
872
+** argument is a harmless no-op.
846873
*/
847
-SQLITE_API int sqlite3_close(sqlite3 *);
874
+SQLITE_API int sqlite3_close(sqlite3*);
875
+SQLITE_API int sqlite3_close_v2(sqlite3*);
848876
849877
/*
850878
** The type for a callback function.
851879
** This is legacy and deprecated. It is included for historical
852880
** compatibility and is not documented.
@@ -5013,15 +5041,16 @@
50135041
** CAPI3REF: Name Of The Folder Holding Database Files
50145042
**
50155043
** ^(If this global variable is made to point to a string which is
50165044
** the name of a folder (a.k.a. directory), then all database files
50175045
** specified with a relative pathname and created or accessed by
5018
-** SQLite when using a built-in [sqlite3_vfs | VFS] will be assumed
5046
+** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
50195047
** to be relative to that directory.)^ ^If this variable is a NULL
50205048
** pointer, then SQLite assumes that all database files specified
50215049
** with a relative pathname are relative to the current directory
5022
-** for the process.
5050
+** for the process. Only the windows VFS makes use of this global
5051
+** variable; it is ignored by the unix VFS.
50235052
**
50245053
** Changing the value of this variable while a database connection is
50255054
** open can result in a corrupt database.
50265055
**
50275056
** It is not safe to read or modify this variable in more than one
@@ -6048,21 +6077,20 @@
60486077
** of these mutex routines. An appropriate implementation
60496078
** is selected automatically at compile-time. ^(The following
60506079
** implementations are available in the SQLite core:
60516080
**
60526081
** <ul>
6053
-** <li> SQLITE_MUTEX_OS2
60546082
** <li> SQLITE_MUTEX_PTHREADS
60556083
** <li> SQLITE_MUTEX_W32
60566084
** <li> SQLITE_MUTEX_NOOP
60576085
** </ul>)^
60586086
**
60596087
** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
60606088
** that does no real locking and is appropriate for use in
6061
-** a single-threaded application. ^The SQLITE_MUTEX_OS2,
6062
-** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
6063
-** are appropriate for use on OS/2, Unix, and Windows.
6089
+** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and
6090
+** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
6091
+** and Windows.
60646092
**
60656093
** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
60666094
** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
60676095
** implementation is included with the library. In this case the
60686096
** application must supply a custom mutex implementation using the
@@ -9287,22 +9315,20 @@
92879315
#define _SQLITE_OS_H_
92889316
92899317
/*
92909318
** Figure out if we are dealing with Unix, Windows, or some other
92919319
** operating system. After the following block of preprocess macros,
9292
-** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
9320
+** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
92939321
** will defined to either 1 or 0. One of the four will be 1. The other
92949322
** three will be 0.
92959323
*/
92969324
#if defined(SQLITE_OS_OTHER)
92979325
# if SQLITE_OS_OTHER==1
92989326
# undef SQLITE_OS_UNIX
92999327
# define SQLITE_OS_UNIX 0
93009328
# undef SQLITE_OS_WIN
93019329
# define SQLITE_OS_WIN 0
9302
-# undef SQLITE_OS_OS2
9303
-# define SQLITE_OS_OS2 0
93049330
# else
93059331
# undef SQLITE_OS_OTHER
93069332
# endif
93079333
#endif
93089334
#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
@@ -9309,23 +9335,16 @@
93099335
# define SQLITE_OS_OTHER 0
93109336
# ifndef SQLITE_OS_WIN
93119337
# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
93129338
# define SQLITE_OS_WIN 1
93139339
# define SQLITE_OS_UNIX 0
9314
-# define SQLITE_OS_OS2 0
9315
-# elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
9316
-# define SQLITE_OS_WIN 0
9317
-# define SQLITE_OS_UNIX 0
9318
-# define SQLITE_OS_OS2 1
93199340
# else
93209341
# define SQLITE_OS_WIN 0
93219342
# define SQLITE_OS_UNIX 1
9322
-# define SQLITE_OS_OS2 0
93239343
# endif
93249344
# else
93259345
# define SQLITE_OS_UNIX 0
9326
-# define SQLITE_OS_OS2 0
93279346
# endif
93289347
#else
93299348
# ifndef SQLITE_OS_WIN
93309349
# define SQLITE_OS_WIN 0
93319350
# endif
@@ -9333,25 +9352,10 @@
93339352
93349353
#if SQLITE_OS_WIN
93359354
# include <windows.h>
93369355
#endif
93379356
9338
-#if SQLITE_OS_OS2
9339
-# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
9340
-# include <os2safe.h> /* has to be included before os2.h for linking to work */
9341
-# endif
9342
-# define INCL_DOSDATETIME
9343
-# define INCL_DOSFILEMGR
9344
-# define INCL_DOSERRORS
9345
-# define INCL_DOSMISC
9346
-# define INCL_DOSPROCESS
9347
-# define INCL_DOSMODULEMGR
9348
-# define INCL_DOSSEMAPHORES
9349
-# include <os2.h>
9350
-# include <uconv.h>
9351
-#endif
9352
-
93539357
/*
93549358
** Determine if we are dealing with Windows NT.
93559359
**
93569360
** We ought to be able to determine if we are compiling for win98 or winNT
93579361
** using the _WIN32_WINNT macro as follows:
@@ -9617,23 +9621,19 @@
96179621
** start-time.
96189622
**
96199623
** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
96209624
**
96219625
** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
9622
-**
9623
-** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2.
96249626
*/
96259627
#if !SQLITE_THREADSAFE
96269628
# define SQLITE_MUTEX_OMIT
96279629
#endif
96289630
#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
96299631
# if SQLITE_OS_UNIX
96309632
# define SQLITE_MUTEX_PTHREADS
96319633
# elif SQLITE_OS_WIN
96329634
# define SQLITE_MUTEX_W32
9633
-# elif SQLITE_OS_OS2
9634
-# define SQLITE_MUTEX_OS2
96359635
# else
96369636
# define SQLITE_MUTEX_NOOP
96379637
# endif
96389638
#endif
96399639
@@ -9950,10 +9950,11 @@
99509950
#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
99519951
#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
99529952
#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
99539953
#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
99549954
#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
9955
+#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
99559956
99569957
/*
99579958
** Each SQL function is defined by an instance of the following
99589959
** structure. A pointer to this structure is stored in the sqlite.aFunc
99599960
** hash table. When multiple functions have the same name, the hash table
@@ -11811,10 +11812,11 @@
1181111812
SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
1181211813
SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
1181311814
SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
1181411815
SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
1181511816
SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
11817
+SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
1181611818
SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
1181711819
SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
1181811820
SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
1181911821
SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
1182011822
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
@@ -17716,286 +17718,10 @@
1771617718
}
1771717719
#endif /* defined(SQLITE_MUTEX_NOOP) */
1771817720
#endif /* !defined(SQLITE_MUTEX_OMIT) */
1771917721
1772017722
/************** End of mutex_noop.c ******************************************/
17721
-/************** Begin file mutex_os2.c ***************************************/
17722
-/*
17723
-** 2007 August 28
17724
-**
17725
-** The author disclaims copyright to this source code. In place of
17726
-** a legal notice, here is a blessing:
17727
-**
17728
-** May you do good and not evil.
17729
-** May you find forgiveness for yourself and forgive others.
17730
-** May you share freely, never taking more than you give.
17731
-**
17732
-*************************************************************************
17733
-** This file contains the C functions that implement mutexes for OS/2
17734
-*/
17735
-
17736
-/*
17737
-** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
17738
-** See the mutex.h file for details.
17739
-*/
17740
-#ifdef SQLITE_MUTEX_OS2
17741
-
17742
-/********************** OS/2 Mutex Implementation **********************
17743
-**
17744
-** This implementation of mutexes is built using the OS/2 API.
17745
-*/
17746
-
17747
-/*
17748
-** The mutex object
17749
-** Each recursive mutex is an instance of the following structure.
17750
-*/
17751
-struct sqlite3_mutex {
17752
- HMTX mutex; /* Mutex controlling the lock */
17753
- int id; /* Mutex type */
17754
-#ifdef SQLITE_DEBUG
17755
- int trace; /* True to trace changes */
17756
-#endif
17757
-};
17758
-
17759
-#ifdef SQLITE_DEBUG
17760
-#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
17761
-#else
17762
-#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
17763
-#endif
17764
-
17765
-/*
17766
-** Initialize and deinitialize the mutex subsystem.
17767
-*/
17768
-static int os2MutexInit(void){ return SQLITE_OK; }
17769
-static int os2MutexEnd(void){ return SQLITE_OK; }
17770
-
17771
-/*
17772
-** The sqlite3_mutex_alloc() routine allocates a new
17773
-** mutex and returns a pointer to it. If it returns NULL
17774
-** that means that a mutex could not be allocated.
17775
-** SQLite will unwind its stack and return an error. The argument
17776
-** to sqlite3_mutex_alloc() is one of these integer constants:
17777
-**
17778
-** <ul>
17779
-** <li> SQLITE_MUTEX_FAST
17780
-** <li> SQLITE_MUTEX_RECURSIVE
17781
-** <li> SQLITE_MUTEX_STATIC_MASTER
17782
-** <li> SQLITE_MUTEX_STATIC_MEM
17783
-** <li> SQLITE_MUTEX_STATIC_MEM2
17784
-** <li> SQLITE_MUTEX_STATIC_PRNG
17785
-** <li> SQLITE_MUTEX_STATIC_LRU
17786
-** <li> SQLITE_MUTEX_STATIC_LRU2
17787
-** </ul>
17788
-**
17789
-** The first two constants cause sqlite3_mutex_alloc() to create
17790
-** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17791
-** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17792
-** The mutex implementation does not need to make a distinction
17793
-** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17794
-** not want to. But SQLite will only request a recursive mutex in
17795
-** cases where it really needs one. If a faster non-recursive mutex
17796
-** implementation is available on the host platform, the mutex subsystem
17797
-** might return such a mutex in response to SQLITE_MUTEX_FAST.
17798
-**
17799
-** The other allowed parameters to sqlite3_mutex_alloc() each return
17800
-** a pointer to a static preexisting mutex. Six static mutexes are
17801
-** used by the current version of SQLite. Future versions of SQLite
17802
-** may add additional static mutexes. Static mutexes are for internal
17803
-** use by SQLite only. Applications that use SQLite mutexes should
17804
-** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17805
-** SQLITE_MUTEX_RECURSIVE.
17806
-**
17807
-** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17808
-** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17809
-** returns a different mutex on every call. But for the static
17810
-** mutex types, the same mutex is returned on every call that has
17811
-** the same type number.
17812
-*/
17813
-static sqlite3_mutex *os2MutexAlloc(int iType){
17814
- sqlite3_mutex *p = NULL;
17815
- switch( iType ){
17816
- case SQLITE_MUTEX_FAST:
17817
- case SQLITE_MUTEX_RECURSIVE: {
17818
- p = sqlite3MallocZero( sizeof(*p) );
17819
- if( p ){
17820
- p->id = iType;
17821
- if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
17822
- sqlite3_free( p );
17823
- p = NULL;
17824
- }
17825
- }
17826
- break;
17827
- }
17828
- default: {
17829
- static volatile int isInit = 0;
17830
- static sqlite3_mutex staticMutexes[6] = {
17831
- SQLITE3_MUTEX_INITIALIZER,
17832
- SQLITE3_MUTEX_INITIALIZER,
17833
- SQLITE3_MUTEX_INITIALIZER,
17834
- SQLITE3_MUTEX_INITIALIZER,
17835
- SQLITE3_MUTEX_INITIALIZER,
17836
- SQLITE3_MUTEX_INITIALIZER,
17837
- };
17838
- if ( !isInit ){
17839
- APIRET rc;
17840
- PTIB ptib;
17841
- PPIB ppib;
17842
- HMTX mutex;
17843
- char name[32];
17844
- DosGetInfoBlocks( &ptib, &ppib );
17845
- sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
17846
- ppib->pib_ulpid );
17847
- while( !isInit ){
17848
- mutex = 0;
17849
- rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
17850
- if( rc == NO_ERROR ){
17851
- unsigned int i;
17852
- if( !isInit ){
17853
- for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
17854
- DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
17855
- }
17856
- isInit = 1;
17857
- }
17858
- DosCloseMutexSem( mutex );
17859
- }else if( rc == ERROR_DUPLICATE_NAME ){
17860
- DosSleep( 1 );
17861
- }else{
17862
- return p;
17863
- }
17864
- }
17865
- }
17866
- assert( iType-2 >= 0 );
17867
- assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
17868
- p = &staticMutexes[iType-2];
17869
- p->id = iType;
17870
- break;
17871
- }
17872
- }
17873
- return p;
17874
-}
17875
-
17876
-
17877
-/*
17878
-** This routine deallocates a previously allocated mutex.
17879
-** SQLite is careful to deallocate every mutex that it allocates.
17880
-*/
17881
-static void os2MutexFree(sqlite3_mutex *p){
17882
-#ifdef SQLITE_DEBUG
17883
- TID tid;
17884
- PID pid;
17885
- ULONG ulCount;
17886
- DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17887
- assert( ulCount==0 );
17888
- assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17889
-#endif
17890
- DosCloseMutexSem( p->mutex );
17891
- sqlite3_free( p );
17892
-}
17893
-
17894
-#ifdef SQLITE_DEBUG
17895
-/*
17896
-** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17897
-** intended for use inside assert() statements.
17898
-*/
17899
-static int os2MutexHeld(sqlite3_mutex *p){
17900
- TID tid;
17901
- PID pid;
17902
- ULONG ulCount;
17903
- PTIB ptib;
17904
- DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17905
- if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
17906
- return 0;
17907
- DosGetInfoBlocks(&ptib, NULL);
17908
- return tid==ptib->tib_ptib2->tib2_ultid;
17909
-}
17910
-static int os2MutexNotheld(sqlite3_mutex *p){
17911
- TID tid;
17912
- PID pid;
17913
- ULONG ulCount;
17914
- PTIB ptib;
17915
- DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17916
- if( ulCount==0 )
17917
- return 1;
17918
- DosGetInfoBlocks(&ptib, NULL);
17919
- return tid!=ptib->tib_ptib2->tib2_ultid;
17920
-}
17921
-static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
17922
- TID tid;
17923
- PID pid;
17924
- ULONG ulCount;
17925
- DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17926
- printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
17927
-}
17928
-#endif
17929
-
17930
-/*
17931
-** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17932
-** to enter a mutex. If another thread is already within the mutex,
17933
-** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17934
-** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
17935
-** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
17936
-** be entered multiple times by the same thread. In such cases the,
17937
-** mutex must be exited an equal number of times before another thread
17938
-** can enter. If the same thread tries to enter any other kind of mutex
17939
-** more than once, the behavior is undefined.
17940
-*/
17941
-static void os2MutexEnter(sqlite3_mutex *p){
17942
- assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17943
- DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
17944
-#ifdef SQLITE_DEBUG
17945
- if( p->trace ) os2MutexTrace(p, "enter");
17946
-#endif
17947
-}
17948
-static int os2MutexTry(sqlite3_mutex *p){
17949
- int rc = SQLITE_BUSY;
17950
- assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17951
- if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
17952
- rc = SQLITE_OK;
17953
-#ifdef SQLITE_DEBUG
17954
- if( p->trace ) os2MutexTrace(p, "try");
17955
-#endif
17956
- }
17957
- return rc;
17958
-}
17959
-
17960
-/*
17961
-** The sqlite3_mutex_leave() routine exits a mutex that was
17962
-** previously entered by the same thread. The behavior
17963
-** is undefined if the mutex is not currently entered or
17964
-** is not currently allocated. SQLite will never do either.
17965
-*/
17966
-static void os2MutexLeave(sqlite3_mutex *p){
17967
- assert( os2MutexHeld(p) );
17968
- DosReleaseMutexSem(p->mutex);
17969
-#ifdef SQLITE_DEBUG
17970
- if( p->trace ) os2MutexTrace(p, "leave");
17971
-#endif
17972
-}
17973
-
17974
-SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17975
- static const sqlite3_mutex_methods sMutex = {
17976
- os2MutexInit,
17977
- os2MutexEnd,
17978
- os2MutexAlloc,
17979
- os2MutexFree,
17980
- os2MutexEnter,
17981
- os2MutexTry,
17982
- os2MutexLeave,
17983
-#ifdef SQLITE_DEBUG
17984
- os2MutexHeld,
17985
- os2MutexNotheld
17986
-#else
17987
- 0,
17988
- 0
17989
-#endif
17990
- };
17991
-
17992
- return &sMutex;
17993
-}
17994
-#endif /* SQLITE_MUTEX_OS2 */
17995
-
17996
-/************** End of mutex_os2.c *******************************************/
1799717723
/************** Begin file mutex_unix.c **************************************/
1799817724
/*
1799917725
** 2007 August 28
1800017726
**
1800117727
** The author disclaims copyright to this source code. In place of
@@ -18456,11 +18182,11 @@
1845618182
** processing, the "interlocked" magic is probably not
1845718183
** strictly necessary.
1845818184
*/
1845918185
static long winMutex_lock = 0;
1846018186
18461
-SQLITE_API extern void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18187
+SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
1846218188
1846318189
static int winMutexInit(void){
1846418190
/* The first to increment to 1 does actual initialization */
1846518191
if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
1846618192
int i;
@@ -19599,11 +19325,12 @@
1959919325
** always returned.
1960019326
*/
1960119327
static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
1960219328
int digit;
1960319329
LONGDOUBLE_TYPE d;
19604
- if( (*cnt)++ >= 16 ) return '0';
19330
+ if( (*cnt)<=0 ) return '0';
19331
+ (*cnt)--;
1960519332
digit = (int)*val;
1960619333
d = digit;
1960719334
digit += '0';
1960819335
*val = (*val - d)*10.0;
1960919336
return (char)digit;
@@ -19903,13 +19630,16 @@
1990319630
bufpt = "NaN";
1990419631
length = 3;
1990519632
break;
1990619633
}
1990719634
if( realvalue>0.0 ){
19908
- while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
19909
- while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
19910
- while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
19635
+ LONGDOUBLE_TYPE scale = 1.0;
19636
+ while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
19637
+ while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
19638
+ while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
19639
+ while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
19640
+ realvalue /= scale;
1991119641
while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
1991219642
while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
1991319643
if( exp>350 ){
1991419644
if( prefix=='-' ){
1991519645
bufpt = "-Inf";
@@ -19938,11 +19668,11 @@
1993819668
}else{
1993919669
precision = precision - exp;
1994019670
xtype = etFLOAT;
1994119671
}
1994219672
}else{
19943
- flag_rtz = 0;
19673
+ flag_rtz = flag_altform2;
1994419674
}
1994519675
if( xtype==etEXP ){
1994619676
e2 = 0;
1994719677
}else{
1994819678
e2 = exp;
@@ -19953,11 +19683,11 @@
1995319683
pAccum->mallocFailed = 1;
1995419684
return;
1995519685
}
1995619686
}
1995719687
zOut = bufpt;
19958
- nsd = 0;
19688
+ nsd = 16 + flag_altform2*10;
1995919689
flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
1996019690
/* The sign in front of the number */
1996119691
if( prefix ){
1996219692
*(bufpt++) = prefix;
1996319693
}
@@ -21526,11 +21256,11 @@
2152621256
s = sign<0 ? -s : s;
2152721257
2152821258
/* if exponent, scale significand as appropriate
2152921259
** and store in result. */
2153021260
if( e ){
21531
- double scale = 1.0;
21261
+ LONGDOUBLE_TYPE scale = 1.0;
2153221262
/* attempt to handle extremely small/large numbers better */
2153321263
if( e>307 && e<342 ){
2153421264
while( e%308 ) { scale *= 1.0e+1; e -= 1; }
2153521265
if( esign<0 ){
2153621266
result = s / scale;
@@ -22781,2144 +22511,10 @@
2278122511
return azName[i];
2278222512
}
2278322513
#endif
2278422514
2278522515
/************** End of opcodes.c *********************************************/
22786
-/************** Begin file os_os2.c ******************************************/
22787
-/*
22788
-** 2006 Feb 14
22789
-**
22790
-** The author disclaims copyright to this source code. In place of
22791
-** a legal notice, here is a blessing:
22792
-**
22793
-** May you do good and not evil.
22794
-** May you find forgiveness for yourself and forgive others.
22795
-** May you share freely, never taking more than you give.
22796
-**
22797
-******************************************************************************
22798
-**
22799
-** This file contains code that is specific to OS/2.
22800
-*/
22801
-
22802
-
22803
-#if SQLITE_OS_OS2
22804
-
22805
-/*
22806
-** A Note About Memory Allocation:
22807
-**
22808
-** This driver uses malloc()/free() directly rather than going through
22809
-** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
22810
-** are designed for use on embedded systems where memory is scarce and
22811
-** malloc failures happen frequently. OS/2 does not typically run on
22812
-** embedded systems, and when it does the developers normally have bigger
22813
-** problems to worry about than running out of memory. So there is not
22814
-** a compelling need to use the wrappers.
22815
-**
22816
-** But there is a good reason to not use the wrappers. If we use the
22817
-** wrappers then we will get simulated malloc() failures within this
22818
-** driver. And that causes all kinds of problems for our tests. We
22819
-** could enhance SQLite to deal with simulated malloc failures within
22820
-** the OS driver, but the code to deal with those failure would not
22821
-** be exercised on Linux (which does not need to malloc() in the driver)
22822
-** and so we would have difficulty writing coverage tests for that
22823
-** code. Better to leave the code out, we think.
22824
-**
22825
-** The point of this discussion is as follows: When creating a new
22826
-** OS layer for an embedded system, if you use this file as an example,
22827
-** avoid the use of malloc()/free(). Those routines work ok on OS/2
22828
-** desktops but not so well in embedded systems.
22829
-*/
22830
-
22831
-/*
22832
-** Macros used to determine whether or not to use threads.
22833
-*/
22834
-#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
22835
-# define SQLITE_OS2_THREADS 1
22836
-#endif
22837
-
22838
-/*
22839
-** Include code that is common to all os_*.c files
22840
-*/
22841
-/************** Include os_common.h in the middle of os_os2.c ****************/
22842
-/************** Begin file os_common.h ***************************************/
22843
-/*
22844
-** 2004 May 22
22845
-**
22846
-** The author disclaims copyright to this source code. In place of
22847
-** a legal notice, here is a blessing:
22848
-**
22849
-** May you do good and not evil.
22850
-** May you find forgiveness for yourself and forgive others.
22851
-** May you share freely, never taking more than you give.
22852
-**
22853
-******************************************************************************
22854
-**
22855
-** This file contains macros and a little bit of code that is common to
22856
-** all of the platform-specific files (os_*.c) and is #included into those
22857
-** files.
22858
-**
22859
-** This file should be #included by the os_*.c files only. It is not a
22860
-** general purpose header file.
22861
-*/
22862
-#ifndef _OS_COMMON_H_
22863
-#define _OS_COMMON_H_
22864
-
22865
-/*
22866
-** At least two bugs have slipped in because we changed the MEMORY_DEBUG
22867
-** macro to SQLITE_DEBUG and some older makefiles have not yet made the
22868
-** switch. The following code should catch this problem at compile-time.
22869
-*/
22870
-#ifdef MEMORY_DEBUG
22871
-# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22872
-#endif
22873
-
22874
-#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
22875
-# ifndef SQLITE_DEBUG_OS_TRACE
22876
-# define SQLITE_DEBUG_OS_TRACE 0
22877
-# endif
22878
- int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22879
-# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
22880
-#else
22881
-# define OSTRACE(X)
22882
-#endif
22883
-
22884
-/*
22885
-** Macros for performance tracing. Normally turned off. Only works
22886
-** on i486 hardware.
22887
-*/
22888
-#ifdef SQLITE_PERFORMANCE_TRACE
22889
-
22890
-/*
22891
-** hwtime.h contains inline assembler code for implementing
22892
-** high-performance timing routines.
22893
-*/
22894
-/************** Include hwtime.h in the middle of os_common.h ****************/
22895
-/************** Begin file hwtime.h ******************************************/
22896
-/*
22897
-** 2008 May 27
22898
-**
22899
-** The author disclaims copyright to this source code. In place of
22900
-** a legal notice, here is a blessing:
22901
-**
22902
-** May you do good and not evil.
22903
-** May you find forgiveness for yourself and forgive others.
22904
-** May you share freely, never taking more than you give.
22905
-**
22906
-******************************************************************************
22907
-**
22908
-** This file contains inline asm code for retrieving "high-performance"
22909
-** counters for x86 class CPUs.
22910
-*/
22911
-#ifndef _HWTIME_H_
22912
-#define _HWTIME_H_
22913
-
22914
-/*
22915
-** The following routine only works on pentium-class (or newer) processors.
22916
-** It uses the RDTSC opcode to read the cycle count value out of the
22917
-** processor and returns that value. This can be used for high-res
22918
-** profiling.
22919
-*/
22920
-#if (defined(__GNUC__) || defined(_MSC_VER)) && \
22921
- (defined(i386) || defined(__i386__) || defined(_M_IX86))
22922
-
22923
- #if defined(__GNUC__)
22924
-
22925
- __inline__ sqlite_uint64 sqlite3Hwtime(void){
22926
- unsigned int lo, hi;
22927
- __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
22928
- return (sqlite_uint64)hi << 32 | lo;
22929
- }
22930
-
22931
- #elif defined(_MSC_VER)
22932
-
22933
- __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
22934
- __asm {
22935
- rdtsc
22936
- ret ; return value at EDX:EAX
22937
- }
22938
- }
22939
-
22940
- #endif
22941
-
22942
-#elif (defined(__GNUC__) && defined(__x86_64__))
22943
-
22944
- __inline__ sqlite_uint64 sqlite3Hwtime(void){
22945
- unsigned long val;
22946
- __asm__ __volatile__ ("rdtsc" : "=A" (val));
22947
- return val;
22948
- }
22949
-
22950
-#elif (defined(__GNUC__) && defined(__ppc__))
22951
-
22952
- __inline__ sqlite_uint64 sqlite3Hwtime(void){
22953
- unsigned long long retval;
22954
- unsigned long junk;
22955
- __asm__ __volatile__ ("\n\
22956
- 1: mftbu %1\n\
22957
- mftb %L0\n\
22958
- mftbu %0\n\
22959
- cmpw %0,%1\n\
22960
- bne 1b"
22961
- : "=r" (retval), "=r" (junk));
22962
- return retval;
22963
- }
22964
-
22965
-#else
22966
-
22967
- #error Need implementation of sqlite3Hwtime() for your platform.
22968
-
22969
- /*
22970
- ** To compile without implementing sqlite3Hwtime() for your platform,
22971
- ** you can remove the above #error and use the following
22972
- ** stub function. You will lose timing support for many
22973
- ** of the debugging and testing utilities, but it should at
22974
- ** least compile and run.
22975
- */
22976
-SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
22977
-
22978
-#endif
22979
-
22980
-#endif /* !defined(_HWTIME_H_) */
22981
-
22982
-/************** End of hwtime.h **********************************************/
22983
-/************** Continuing where we left off in os_common.h ******************/
22984
-
22985
-static sqlite_uint64 g_start;
22986
-static sqlite_uint64 g_elapsed;
22987
-#define TIMER_START g_start=sqlite3Hwtime()
22988
-#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
22989
-#define TIMER_ELAPSED g_elapsed
22990
-#else
22991
-#define TIMER_START
22992
-#define TIMER_END
22993
-#define TIMER_ELAPSED ((sqlite_uint64)0)
22994
-#endif
22995
-
22996
-/*
22997
-** If we compile with the SQLITE_TEST macro set, then the following block
22998
-** of code will give us the ability to simulate a disk I/O error. This
22999
-** is used for testing the I/O recovery logic.
23000
-*/
23001
-#ifdef SQLITE_TEST
23002
-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
23003
-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
23004
-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
23005
-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
23006
-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
23007
-SQLITE_API int sqlite3_diskfull_pending = 0;
23008
-SQLITE_API int sqlite3_diskfull = 0;
23009
-#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
23010
-#define SimulateIOError(CODE) \
23011
- if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
23012
- || sqlite3_io_error_pending-- == 1 ) \
23013
- { local_ioerr(); CODE; }
23014
-static void local_ioerr(){
23015
- IOTRACE(("IOERR\n"));
23016
- sqlite3_io_error_hit++;
23017
- if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
23018
-}
23019
-#define SimulateDiskfullError(CODE) \
23020
- if( sqlite3_diskfull_pending ){ \
23021
- if( sqlite3_diskfull_pending == 1 ){ \
23022
- local_ioerr(); \
23023
- sqlite3_diskfull = 1; \
23024
- sqlite3_io_error_hit = 1; \
23025
- CODE; \
23026
- }else{ \
23027
- sqlite3_diskfull_pending--; \
23028
- } \
23029
- }
23030
-#else
23031
-#define SimulateIOErrorBenign(X)
23032
-#define SimulateIOError(A)
23033
-#define SimulateDiskfullError(A)
23034
-#endif
23035
-
23036
-/*
23037
-** When testing, keep a count of the number of open files.
23038
-*/
23039
-#ifdef SQLITE_TEST
23040
-SQLITE_API int sqlite3_open_file_count = 0;
23041
-#define OpenCounter(X) sqlite3_open_file_count+=(X)
23042
-#else
23043
-#define OpenCounter(X)
23044
-#endif
23045
-
23046
-#endif /* !defined(_OS_COMMON_H_) */
23047
-
23048
-/************** End of os_common.h *******************************************/
23049
-/************** Continuing where we left off in os_os2.c *********************/
23050
-
23051
-/* Forward references */
23052
-typedef struct os2File os2File; /* The file structure */
23053
-typedef struct os2ShmNode os2ShmNode; /* A shared descritive memory node */
23054
-typedef struct os2ShmLink os2ShmLink; /* A connection to shared-memory */
23055
-
23056
-/*
23057
-** The os2File structure is subclass of sqlite3_file specific for the OS/2
23058
-** protability layer.
23059
-*/
23060
-struct os2File {
23061
- const sqlite3_io_methods *pMethod; /* Always the first entry */
23062
- HFILE h; /* Handle for accessing the file */
23063
- int flags; /* Flags provided to os2Open() */
23064
- int locktype; /* Type of lock currently held on this file */
23065
- int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
23066
- char *zFullPathCp; /* Full path name of this file */
23067
- os2ShmLink *pShmLink; /* Instance of shared memory on this file */
23068
-};
23069
-
23070
-#define LOCK_TIMEOUT 10L /* the default locking timeout */
23071
-
23072
-/*
23073
-** Missing from some versions of the OS/2 toolkit -
23074
-** used to allocate from high memory if possible
23075
-*/
23076
-#ifndef OBJ_ANY
23077
-# define OBJ_ANY 0x00000400
23078
-#endif
23079
-
23080
-/*****************************************************************************
23081
-** The next group of routines implement the I/O methods specified
23082
-** by the sqlite3_io_methods object.
23083
-******************************************************************************/
23084
-
23085
-/*
23086
-** Close a file.
23087
-*/
23088
-static int os2Close( sqlite3_file *id ){
23089
- APIRET rc;
23090
- os2File *pFile = (os2File*)id;
23091
-
23092
- assert( id!=0 );
23093
- OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
23094
-
23095
- rc = DosClose( pFile->h );
23096
-
23097
- if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
23098
- DosForceDelete( (PSZ)pFile->zFullPathCp );
23099
-
23100
- free( pFile->zFullPathCp );
23101
- pFile->zFullPathCp = NULL;
23102
- pFile->locktype = NO_LOCK;
23103
- pFile->h = (HFILE)-1;
23104
- pFile->flags = 0;
23105
-
23106
- OpenCounter( -1 );
23107
- return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23108
-}
23109
-
23110
-/*
23111
-** Read data from a file into a buffer. Return SQLITE_OK if all
23112
-** bytes were read successfully and SQLITE_IOERR if anything goes
23113
-** wrong.
23114
-*/
23115
-static int os2Read(
23116
- sqlite3_file *id, /* File to read from */
23117
- void *pBuf, /* Write content into this buffer */
23118
- int amt, /* Number of bytes to read */
23119
- sqlite3_int64 offset /* Begin reading at this offset */
23120
-){
23121
- ULONG fileLocation = 0L;
23122
- ULONG got;
23123
- os2File *pFile = (os2File*)id;
23124
- assert( id!=0 );
23125
- SimulateIOError( return SQLITE_IOERR_READ );
23126
- OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
23127
- if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
23128
- return SQLITE_IOERR;
23129
- }
23130
- if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
23131
- return SQLITE_IOERR_READ;
23132
- }
23133
- if( got == (ULONG)amt )
23134
- return SQLITE_OK;
23135
- else {
23136
- /* Unread portions of the input buffer must be zero-filled */
23137
- memset(&((char*)pBuf)[got], 0, amt-got);
23138
- return SQLITE_IOERR_SHORT_READ;
23139
- }
23140
-}
23141
-
23142
-/*
23143
-** Write data from a buffer into a file. Return SQLITE_OK on success
23144
-** or some other error code on failure.
23145
-*/
23146
-static int os2Write(
23147
- sqlite3_file *id, /* File to write into */
23148
- const void *pBuf, /* The bytes to be written */
23149
- int amt, /* Number of bytes to write */
23150
- sqlite3_int64 offset /* Offset into the file to begin writing at */
23151
-){
23152
- ULONG fileLocation = 0L;
23153
- APIRET rc = NO_ERROR;
23154
- ULONG wrote;
23155
- os2File *pFile = (os2File*)id;
23156
- assert( id!=0 );
23157
- SimulateIOError( return SQLITE_IOERR_WRITE );
23158
- SimulateDiskfullError( return SQLITE_FULL );
23159
- OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
23160
- if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
23161
- return SQLITE_IOERR;
23162
- }
23163
- assert( amt>0 );
23164
- while( amt > 0 &&
23165
- ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
23166
- wrote > 0
23167
- ){
23168
- amt -= wrote;
23169
- pBuf = &((char*)pBuf)[wrote];
23170
- }
23171
-
23172
- return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
23173
-}
23174
-
23175
-/*
23176
-** Truncate an open file to a specified size
23177
-*/
23178
-static int os2Truncate( sqlite3_file *id, i64 nByte ){
23179
- APIRET rc;
23180
- os2File *pFile = (os2File*)id;
23181
- assert( id!=0 );
23182
- OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
23183
- SimulateIOError( return SQLITE_IOERR_TRUNCATE );
23184
-
23185
- /* If the user has configured a chunk-size for this file, truncate the
23186
- ** file so that it consists of an integer number of chunks (i.e. the
23187
- ** actual file size after the operation may be larger than the requested
23188
- ** size).
23189
- */
23190
- if( pFile->szChunk ){
23191
- nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
23192
- }
23193
-
23194
- rc = DosSetFileSize( pFile->h, nByte );
23195
- return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
23196
-}
23197
-
23198
-#ifdef SQLITE_TEST
23199
-/*
23200
-** Count the number of fullsyncs and normal syncs. This is used to test
23201
-** that syncs and fullsyncs are occuring at the right times.
23202
-*/
23203
-SQLITE_API int sqlite3_sync_count = 0;
23204
-SQLITE_API int sqlite3_fullsync_count = 0;
23205
-#endif
23206
-
23207
-/*
23208
-** Make sure all writes to a particular file are committed to disk.
23209
-*/
23210
-static int os2Sync( sqlite3_file *id, int flags ){
23211
- os2File *pFile = (os2File*)id;
23212
- OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
23213
-#ifdef SQLITE_TEST
23214
- if( flags & SQLITE_SYNC_FULL){
23215
- sqlite3_fullsync_count++;
23216
- }
23217
- sqlite3_sync_count++;
23218
-#endif
23219
- /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
23220
- ** no-op
23221
- */
23222
-#ifdef SQLITE_NO_SYNC
23223
- UNUSED_PARAMETER(pFile);
23224
- return SQLITE_OK;
23225
-#else
23226
- return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23227
-#endif
23228
-}
23229
-
23230
-/*
23231
-** Determine the current size of a file in bytes
23232
-*/
23233
-static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
23234
- APIRET rc = NO_ERROR;
23235
- FILESTATUS3 fsts3FileInfo;
23236
- memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
23237
- assert( id!=0 );
23238
- SimulateIOError( return SQLITE_IOERR_FSTAT );
23239
- rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
23240
- if( rc == NO_ERROR ){
23241
- *pSize = fsts3FileInfo.cbFile;
23242
- return SQLITE_OK;
23243
- }else{
23244
- return SQLITE_IOERR_FSTAT;
23245
- }
23246
-}
23247
-
23248
-/*
23249
-** Acquire a reader lock.
23250
-*/
23251
-static int getReadLock( os2File *pFile ){
23252
- FILELOCK LockArea,
23253
- UnlockArea;
23254
- APIRET res;
23255
- memset(&LockArea, 0, sizeof(LockArea));
23256
- memset(&UnlockArea, 0, sizeof(UnlockArea));
23257
- LockArea.lOffset = SHARED_FIRST;
23258
- LockArea.lRange = SHARED_SIZE;
23259
- UnlockArea.lOffset = 0L;
23260
- UnlockArea.lRange = 0L;
23261
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
23262
- OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
23263
- return res;
23264
-}
23265
-
23266
-/*
23267
-** Undo a readlock
23268
-*/
23269
-static int unlockReadLock( os2File *id ){
23270
- FILELOCK LockArea,
23271
- UnlockArea;
23272
- APIRET res;
23273
- memset(&LockArea, 0, sizeof(LockArea));
23274
- memset(&UnlockArea, 0, sizeof(UnlockArea));
23275
- LockArea.lOffset = 0L;
23276
- LockArea.lRange = 0L;
23277
- UnlockArea.lOffset = SHARED_FIRST;
23278
- UnlockArea.lRange = SHARED_SIZE;
23279
- res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
23280
- OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
23281
- return res;
23282
-}
23283
-
23284
-/*
23285
-** Lock the file with the lock specified by parameter locktype - one
23286
-** of the following:
23287
-**
23288
-** (1) SHARED_LOCK
23289
-** (2) RESERVED_LOCK
23290
-** (3) PENDING_LOCK
23291
-** (4) EXCLUSIVE_LOCK
23292
-**
23293
-** Sometimes when requesting one lock state, additional lock states
23294
-** are inserted in between. The locking might fail on one of the later
23295
-** transitions leaving the lock state different from what it started but
23296
-** still short of its goal. The following chart shows the allowed
23297
-** transitions and the inserted intermediate states:
23298
-**
23299
-** UNLOCKED -> SHARED
23300
-** SHARED -> RESERVED
23301
-** SHARED -> (PENDING) -> EXCLUSIVE
23302
-** RESERVED -> (PENDING) -> EXCLUSIVE
23303
-** PENDING -> EXCLUSIVE
23304
-**
23305
-** This routine will only increase a lock. The os2Unlock() routine
23306
-** erases all locks at once and returns us immediately to locking level 0.
23307
-** It is not possible to lower the locking level one step at a time. You
23308
-** must go straight to locking level 0.
23309
-*/
23310
-static int os2Lock( sqlite3_file *id, int locktype ){
23311
- int rc = SQLITE_OK; /* Return code from subroutines */
23312
- APIRET res = NO_ERROR; /* Result of an OS/2 lock call */
23313
- int newLocktype; /* Set pFile->locktype to this value before exiting */
23314
- int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
23315
- FILELOCK LockArea,
23316
- UnlockArea;
23317
- os2File *pFile = (os2File*)id;
23318
- memset(&LockArea, 0, sizeof(LockArea));
23319
- memset(&UnlockArea, 0, sizeof(UnlockArea));
23320
- assert( pFile!=0 );
23321
- OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
23322
-
23323
- /* If there is already a lock of this type or more restrictive on the
23324
- ** os2File, do nothing. Don't use the end_lock: exit path, as
23325
- ** sqlite3_mutex_enter() hasn't been called yet.
23326
- */
23327
- if( pFile->locktype>=locktype ){
23328
- OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
23329
- return SQLITE_OK;
23330
- }
23331
-
23332
- /* Make sure the locking sequence is correct
23333
- */
23334
- assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
23335
- assert( locktype!=PENDING_LOCK );
23336
- assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
23337
-
23338
- /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
23339
- ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
23340
- ** the PENDING_LOCK byte is temporary.
23341
- */
23342
- newLocktype = pFile->locktype;
23343
- if( pFile->locktype==NO_LOCK
23344
- || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
23345
- ){
23346
- LockArea.lOffset = PENDING_BYTE;
23347
- LockArea.lRange = 1L;
23348
- UnlockArea.lOffset = 0L;
23349
- UnlockArea.lRange = 0L;
23350
-
23351
- /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
23352
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
23353
- if( res == NO_ERROR ){
23354
- gotPendingLock = 1;
23355
- OSTRACE(( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res ));
23356
- }
23357
- }
23358
-
23359
- /* Acquire a shared lock
23360
- */
23361
- if( locktype==SHARED_LOCK && res == NO_ERROR ){
23362
- assert( pFile->locktype==NO_LOCK );
23363
- res = getReadLock(pFile);
23364
- if( res == NO_ERROR ){
23365
- newLocktype = SHARED_LOCK;
23366
- }
23367
- OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
23368
- }
23369
-
23370
- /* Acquire a RESERVED lock
23371
- */
23372
- if( locktype==RESERVED_LOCK && res == NO_ERROR ){
23373
- assert( pFile->locktype==SHARED_LOCK );
23374
- LockArea.lOffset = RESERVED_BYTE;
23375
- LockArea.lRange = 1L;
23376
- UnlockArea.lOffset = 0L;
23377
- UnlockArea.lRange = 0L;
23378
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23379
- if( res == NO_ERROR ){
23380
- newLocktype = RESERVED_LOCK;
23381
- }
23382
- OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
23383
- }
23384
-
23385
- /* Acquire a PENDING lock
23386
- */
23387
- if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
23388
- newLocktype = PENDING_LOCK;
23389
- gotPendingLock = 0;
23390
- OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
23391
- pFile->h ));
23392
- }
23393
-
23394
- /* Acquire an EXCLUSIVE lock
23395
- */
23396
- if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
23397
- assert( pFile->locktype>=SHARED_LOCK );
23398
- res = unlockReadLock(pFile);
23399
- OSTRACE(( "unreadlock = %d\n", res ));
23400
- LockArea.lOffset = SHARED_FIRST;
23401
- LockArea.lRange = SHARED_SIZE;
23402
- UnlockArea.lOffset = 0L;
23403
- UnlockArea.lRange = 0L;
23404
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23405
- if( res == NO_ERROR ){
23406
- newLocktype = EXCLUSIVE_LOCK;
23407
- }else{
23408
- OSTRACE(( "OS/2 error-code = %d\n", res ));
23409
- getReadLock(pFile);
23410
- }
23411
- OSTRACE(( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res ));
23412
- }
23413
-
23414
- /* If we are holding a PENDING lock that ought to be released, then
23415
- ** release it now.
23416
- */
23417
- if( gotPendingLock && locktype==SHARED_LOCK ){
23418
- int r;
23419
- LockArea.lOffset = 0L;
23420
- LockArea.lRange = 0L;
23421
- UnlockArea.lOffset = PENDING_BYTE;
23422
- UnlockArea.lRange = 1L;
23423
- r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23424
- OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
23425
- }
23426
-
23427
- /* Update the state of the lock has held in the file descriptor then
23428
- ** return the appropriate result code.
23429
- */
23430
- if( res == NO_ERROR ){
23431
- rc = SQLITE_OK;
23432
- }else{
23433
- OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
23434
- locktype, newLocktype ));
23435
- rc = SQLITE_BUSY;
23436
- }
23437
- pFile->locktype = newLocktype;
23438
- OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
23439
- return rc;
23440
-}
23441
-
23442
-/*
23443
-** This routine checks if there is a RESERVED lock held on the specified
23444
-** file by this or any other process. If such a lock is held, return
23445
-** non-zero, otherwise zero.
23446
-*/
23447
-static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
23448
- int r = 0;
23449
- os2File *pFile = (os2File*)id;
23450
- assert( pFile!=0 );
23451
- if( pFile->locktype>=RESERVED_LOCK ){
23452
- r = 1;
23453
- OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
23454
- }else{
23455
- FILELOCK LockArea,
23456
- UnlockArea;
23457
- APIRET rc = NO_ERROR;
23458
- memset(&LockArea, 0, sizeof(LockArea));
23459
- memset(&UnlockArea, 0, sizeof(UnlockArea));
23460
- LockArea.lOffset = RESERVED_BYTE;
23461
- LockArea.lRange = 1L;
23462
- UnlockArea.lOffset = 0L;
23463
- UnlockArea.lRange = 0L;
23464
- rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23465
- OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
23466
- if( rc == NO_ERROR ){
23467
- APIRET rcu = NO_ERROR; /* return code for unlocking */
23468
- LockArea.lOffset = 0L;
23469
- LockArea.lRange = 0L;
23470
- UnlockArea.lOffset = RESERVED_BYTE;
23471
- UnlockArea.lRange = 1L;
23472
- rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23473
- OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
23474
- }
23475
- r = !(rc == NO_ERROR);
23476
- OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
23477
- }
23478
- *pOut = r;
23479
- return SQLITE_OK;
23480
-}
23481
-
23482
-/*
23483
-** Lower the locking level on file descriptor id to locktype. locktype
23484
-** must be either NO_LOCK or SHARED_LOCK.
23485
-**
23486
-** If the locking level of the file descriptor is already at or below
23487
-** the requested locking level, this routine is a no-op.
23488
-**
23489
-** It is not possible for this routine to fail if the second argument
23490
-** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
23491
-** might return SQLITE_IOERR;
23492
-*/
23493
-static int os2Unlock( sqlite3_file *id, int locktype ){
23494
- int type;
23495
- os2File *pFile = (os2File*)id;
23496
- APIRET rc = SQLITE_OK;
23497
- APIRET res = NO_ERROR;
23498
- FILELOCK LockArea,
23499
- UnlockArea;
23500
- memset(&LockArea, 0, sizeof(LockArea));
23501
- memset(&UnlockArea, 0, sizeof(UnlockArea));
23502
- assert( pFile!=0 );
23503
- assert( locktype<=SHARED_LOCK );
23504
- OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
23505
- type = pFile->locktype;
23506
- if( type>=EXCLUSIVE_LOCK ){
23507
- LockArea.lOffset = 0L;
23508
- LockArea.lRange = 0L;
23509
- UnlockArea.lOffset = SHARED_FIRST;
23510
- UnlockArea.lRange = SHARED_SIZE;
23511
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23512
- OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
23513
- if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
23514
- /* This should never happen. We should always be able to
23515
- ** reacquire the read lock */
23516
- OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
23517
- rc = SQLITE_IOERR_UNLOCK;
23518
- }
23519
- }
23520
- if( type>=RESERVED_LOCK ){
23521
- LockArea.lOffset = 0L;
23522
- LockArea.lRange = 0L;
23523
- UnlockArea.lOffset = RESERVED_BYTE;
23524
- UnlockArea.lRange = 1L;
23525
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23526
- OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
23527
- }
23528
- if( locktype==NO_LOCK && type>=SHARED_LOCK ){
23529
- res = unlockReadLock(pFile);
23530
- OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
23531
- pFile->h, type, locktype, res ));
23532
- }
23533
- if( type>=PENDING_LOCK ){
23534
- LockArea.lOffset = 0L;
23535
- LockArea.lRange = 0L;
23536
- UnlockArea.lOffset = PENDING_BYTE;
23537
- UnlockArea.lRange = 1L;
23538
- res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23539
- OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
23540
- }
23541
- pFile->locktype = locktype;
23542
- OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
23543
- return rc;
23544
-}
23545
-
23546
-/*
23547
-** Control and query of the open file handle.
23548
-*/
23549
-static int os2FileControl(sqlite3_file *id, int op, void *pArg){
23550
- switch( op ){
23551
- case SQLITE_FCNTL_LOCKSTATE: {
23552
- *(int*)pArg = ((os2File*)id)->locktype;
23553
- OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
23554
- ((os2File*)id)->h, ((os2File*)id)->locktype ));
23555
- return SQLITE_OK;
23556
- }
23557
- case SQLITE_FCNTL_CHUNK_SIZE: {
23558
- ((os2File*)id)->szChunk = *(int*)pArg;
23559
- return SQLITE_OK;
23560
- }
23561
- case SQLITE_FCNTL_SIZE_HINT: {
23562
- sqlite3_int64 sz = *(sqlite3_int64*)pArg;
23563
- SimulateIOErrorBenign(1);
23564
- os2Truncate(id, sz);
23565
- SimulateIOErrorBenign(0);
23566
- return SQLITE_OK;
23567
- }
23568
- case SQLITE_FCNTL_SYNC_OMITTED: {
23569
- return SQLITE_OK;
23570
- }
23571
- }
23572
- return SQLITE_NOTFOUND;
23573
-}
23574
-
23575
-/*
23576
-** Return the sector size in bytes of the underlying block device for
23577
-** the specified file. This is almost always 512 bytes, but may be
23578
-** larger for some devices.
23579
-**
23580
-** SQLite code assumes this function cannot fail. It also assumes that
23581
-** if two files are created in the same file-system directory (i.e.
23582
-** a database and its journal file) that the sector size will be the
23583
-** same for both.
23584
-*/
23585
-static int os2SectorSize(sqlite3_file *id){
23586
- UNUSED_PARAMETER(id);
23587
- return SQLITE_DEFAULT_SECTOR_SIZE;
23588
-}
23589
-
23590
-/*
23591
-** Return a vector of device characteristics.
23592
-*/
23593
-static int os2DeviceCharacteristics(sqlite3_file *id){
23594
- UNUSED_PARAMETER(id);
23595
- return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
23596
-}
23597
-
23598
-
23599
-/*
23600
-** Character set conversion objects used by conversion routines.
23601
-*/
23602
-static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
23603
-static UconvObject uclCp = NULL; /* convert between local codepage and UCS-2 */
23604
-
23605
-/*
23606
-** Helper function to initialize the conversion objects from and to UTF-8.
23607
-*/
23608
-static void initUconvObjects( void ){
23609
- if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
23610
- ucUtf8 = NULL;
23611
- if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
23612
- uclCp = NULL;
23613
-}
23614
-
23615
-/*
23616
-** Helper function to free the conversion objects from and to UTF-8.
23617
-*/
23618
-static void freeUconvObjects( void ){
23619
- if ( ucUtf8 )
23620
- UniFreeUconvObject( ucUtf8 );
23621
- if ( uclCp )
23622
- UniFreeUconvObject( uclCp );
23623
- ucUtf8 = NULL;
23624
- uclCp = NULL;
23625
-}
23626
-
23627
-/*
23628
-** Helper function to convert UTF-8 filenames to local OS/2 codepage.
23629
-** The two-step process: first convert the incoming UTF-8 string
23630
-** into UCS-2 and then from UCS-2 to the current codepage.
23631
-** The returned char pointer has to be freed.
23632
-*/
23633
-static char *convertUtf8PathToCp( const char *in ){
23634
- UniChar tempPath[CCHMAXPATH];
23635
- char *out = (char *)calloc( CCHMAXPATH, 1 );
23636
-
23637
- if( !out )
23638
- return NULL;
23639
-
23640
- if( !ucUtf8 || !uclCp )
23641
- initUconvObjects();
23642
-
23643
- /* determine string for the conversion of UTF-8 which is CP1208 */
23644
- if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23645
- return out; /* if conversion fails, return the empty string */
23646
-
23647
- /* conversion for current codepage which can be used for paths */
23648
- UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
23649
-
23650
- return out;
23651
-}
23652
-
23653
-/*
23654
-** Helper function to convert filenames from local codepage to UTF-8.
23655
-** The two-step process: first convert the incoming codepage-specific
23656
-** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
23657
-** The returned char pointer has to be freed.
23658
-**
23659
-** This function is non-static to be able to use this in shell.c and
23660
-** similar applications that take command line arguments.
23661
-*/
23662
-char *convertCpPathToUtf8( const char *in ){
23663
- UniChar tempPath[CCHMAXPATH];
23664
- char *out = (char *)calloc( CCHMAXPATH, 1 );
23665
-
23666
- if( !out )
23667
- return NULL;
23668
-
23669
- if( !ucUtf8 || !uclCp )
23670
- initUconvObjects();
23671
-
23672
- /* conversion for current codepage which can be used for paths */
23673
- if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23674
- return out; /* if conversion fails, return the empty string */
23675
-
23676
- /* determine string for the conversion of UTF-8 which is CP1208 */
23677
- UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
23678
-
23679
- return out;
23680
-}
23681
-
23682
-
23683
-#ifndef SQLITE_OMIT_WAL
23684
-
23685
-/*
23686
-** Use main database file for interprocess locking. If un-defined
23687
-** a separate file is created for this purpose. The file will be
23688
-** used only to set file locks. There will be no data written to it.
23689
-*/
23690
-#define SQLITE_OS2_NO_WAL_LOCK_FILE
23691
-
23692
-#if 0
23693
-static void _ERR_TRACE( const char *fmt, ... ) {
23694
- va_list ap;
23695
- va_start(ap, fmt);
23696
- vfprintf(stderr, fmt, ap);
23697
- fflush(stderr);
23698
-}
23699
-#define ERR_TRACE(rc, msg) \
23700
- if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
23701
-#else
23702
-#define ERR_TRACE(rc, msg)
23703
-#endif
23704
-
23705
-/*
23706
-** Helper functions to obtain and relinquish the global mutex. The
23707
-** global mutex is used to protect os2ShmNodeList.
23708
-**
23709
-** Function os2ShmMutexHeld() is used to assert() that the global mutex
23710
-** is held when required. This function is only used as part of assert()
23711
-** statements. e.g.
23712
-**
23713
-** os2ShmEnterMutex()
23714
-** assert( os2ShmMutexHeld() );
23715
-** os2ShmLeaveMutex()
23716
-*/
23717
-static void os2ShmEnterMutex(void){
23718
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23719
-}
23720
-static void os2ShmLeaveMutex(void){
23721
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23722
-}
23723
-#ifdef SQLITE_DEBUG
23724
-static int os2ShmMutexHeld(void) {
23725
- return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23726
-}
23727
-int GetCurrentProcessId(void) {
23728
- PPIB pib;
23729
- DosGetInfoBlocks(NULL, &pib);
23730
- return (int)pib->pib_ulpid;
23731
-}
23732
-#endif
23733
-
23734
-/*
23735
-** Object used to represent a the shared memory area for a single log file.
23736
-** When multiple threads all reference the same log-summary, each thread has
23737
-** its own os2File object, but they all point to a single instance of this
23738
-** object. In other words, each log-summary is opened only once per process.
23739
-**
23740
-** os2ShmMutexHeld() must be true when creating or destroying
23741
-** this object or while reading or writing the following fields:
23742
-**
23743
-** nRef
23744
-** pNext
23745
-**
23746
-** The following fields are read-only after the object is created:
23747
-**
23748
-** szRegion
23749
-** hLockFile
23750
-** shmBaseName
23751
-**
23752
-** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
23753
-** os2ShmMutexHeld() is true when reading or writing any other field
23754
-** in this structure.
23755
-**
23756
-*/
23757
-struct os2ShmNode {
23758
- sqlite3_mutex *mutex; /* Mutex to access this object */
23759
- os2ShmNode *pNext; /* Next in list of all os2ShmNode objects */
23760
-
23761
- int szRegion; /* Size of shared-memory regions */
23762
-
23763
- int nRegion; /* Size of array apRegion */
23764
- void **apRegion; /* Array of pointers to shared-memory regions */
23765
-
23766
- int nRef; /* Number of os2ShmLink objects pointing to this */
23767
- os2ShmLink *pFirst; /* First os2ShmLink object pointing to this */
23768
-
23769
- HFILE hLockFile; /* File used for inter-process memory locking */
23770
- char shmBaseName[1]; /* Name of the memory object !!! must last !!! */
23771
-};
23772
-
23773
-
23774
-/*
23775
-** Structure used internally by this VFS to record the state of an
23776
-** open shared memory connection.
23777
-**
23778
-** The following fields are initialized when this object is created and
23779
-** are read-only thereafter:
23780
-**
23781
-** os2Shm.pShmNode
23782
-** os2Shm.id
23783
-**
23784
-** All other fields are read/write. The os2Shm.pShmNode->mutex must be held
23785
-** while accessing any read/write fields.
23786
-*/
23787
-struct os2ShmLink {
23788
- os2ShmNode *pShmNode; /* The underlying os2ShmNode object */
23789
- os2ShmLink *pNext; /* Next os2Shm with the same os2ShmNode */
23790
- u32 sharedMask; /* Mask of shared locks held */
23791
- u32 exclMask; /* Mask of exclusive locks held */
23792
-#ifdef SQLITE_DEBUG
23793
- u8 id; /* Id of this connection with its os2ShmNode */
23794
-#endif
23795
-};
23796
-
23797
-
23798
-/*
23799
-** A global list of all os2ShmNode objects.
23800
-**
23801
-** The os2ShmMutexHeld() must be true while reading or writing this list.
23802
-*/
23803
-static os2ShmNode *os2ShmNodeList = NULL;
23804
-
23805
-/*
23806
-** Constants used for locking
23807
-*/
23808
-#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
23809
-#define OS2_SHM_BASE (PENDING_BYTE + 0x10000) /* first lock byte */
23810
-#else
23811
-#define OS2_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
23812
-#endif
23813
-
23814
-#define OS2_SHM_DMS (OS2_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
23815
-
23816
-/*
23817
-** Apply advisory locks for all n bytes beginning at ofst.
23818
-*/
23819
-#define _SHM_UNLCK 1 /* no lock */
23820
-#define _SHM_RDLCK 2 /* shared lock, no wait */
23821
-#define _SHM_WRLCK 3 /* exlusive lock, no wait */
23822
-#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
23823
-static int os2ShmSystemLock(
23824
- os2ShmNode *pNode, /* Apply locks to this open shared-memory segment */
23825
- int lockType, /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
23826
- int ofst, /* Offset to first byte to be locked/unlocked */
23827
- int nByte /* Number of bytes to lock or unlock */
23828
-){
23829
- APIRET rc;
23830
- FILELOCK area;
23831
- ULONG mode, timeout;
23832
-
23833
- /* Access to the os2ShmNode object is serialized by the caller */
23834
- assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
23835
-
23836
- mode = 1; /* shared lock */
23837
- timeout = 0; /* no wait */
23838
- area.lOffset = ofst;
23839
- area.lRange = nByte;
23840
-
23841
- switch( lockType ) {
23842
- case _SHM_WRLCK_WAIT:
23843
- timeout = (ULONG)-1; /* wait forever */
23844
- case _SHM_WRLCK:
23845
- mode = 0; /* exclusive lock */
23846
- case _SHM_RDLCK:
23847
- rc = DosSetFileLocks(pNode->hLockFile,
23848
- NULL, &area, timeout, mode);
23849
- break;
23850
- /* case _SHM_UNLCK: */
23851
- default:
23852
- rc = DosSetFileLocks(pNode->hLockFile,
23853
- &area, NULL, 0, 0);
23854
- break;
23855
- }
23856
-
23857
- OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
23858
- pNode->hLockFile,
23859
- rc==SQLITE_OK ? "ok" : "failed",
23860
- lockType==_SHM_UNLCK ? "Unlock" : "Lock",
23861
- rc));
23862
-
23863
- ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
23864
-
23865
- return ( rc == 0 ) ? SQLITE_OK : SQLITE_BUSY;
23866
-}
23867
-
23868
-/*
23869
-** Find an os2ShmNode in global list or allocate a new one, if not found.
23870
-**
23871
-** This is not a VFS shared-memory method; it is a utility function called
23872
-** by VFS shared-memory methods.
23873
-*/
23874
-static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
23875
- os2ShmLink *pLink;
23876
- os2ShmNode *pNode;
23877
- int cbShmName, rc = SQLITE_OK;
23878
- char shmName[CCHMAXPATH + 30];
23879
-#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23880
- ULONG action;
23881
-#endif
23882
-
23883
- /* We need some additional space at the end to append the region number */
23884
- cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
23885
- if( cbShmName >= CCHMAXPATH-8 )
23886
- return SQLITE_IOERR_SHMOPEN;
23887
-
23888
- /* Replace colon in file name to form a valid shared memory name */
23889
- shmName[10+1] = '!';
23890
-
23891
- /* Allocate link object (we free it later in case of failure) */
23892
- pLink = sqlite3_malloc( sizeof(*pLink) );
23893
- if( !pLink )
23894
- return SQLITE_NOMEM;
23895
-
23896
- /* Access node list */
23897
- os2ShmEnterMutex();
23898
-
23899
- /* Find node by it's shared memory base name */
23900
- for( pNode = os2ShmNodeList;
23901
- pNode && stricmp(shmName, pNode->shmBaseName) != 0;
23902
- pNode = pNode->pNext ) ;
23903
-
23904
- /* Not found: allocate a new node */
23905
- if( !pNode ) {
23906
- pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
23907
- if( pNode ) {
23908
- memset(pNode, 0, sizeof(*pNode) );
23909
- pNode->szRegion = szRegion;
23910
- pNode->hLockFile = (HFILE)-1;
23911
- strcpy(pNode->shmBaseName, shmName);
23912
-
23913
-#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
23914
- if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
23915
-#else
23916
- sprintf(shmName, "%s-lck", fd->zFullPathCp);
23917
- if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL,
23918
- OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
23919
- OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
23920
- OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
23921
- NULL) != 0 ) {
23922
-#endif
23923
- sqlite3_free(pNode);
23924
- rc = SQLITE_IOERR;
23925
- } else {
23926
- pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
23927
- if( !pNode->mutex ) {
23928
- sqlite3_free(pNode);
23929
- rc = SQLITE_NOMEM;
23930
- }
23931
- }
23932
- } else {
23933
- rc = SQLITE_NOMEM;
23934
- }
23935
-
23936
- if( rc == SQLITE_OK ) {
23937
- pNode->pNext = os2ShmNodeList;
23938
- os2ShmNodeList = pNode;
23939
- } else {
23940
- pNode = NULL;
23941
- }
23942
- } else if( pNode->szRegion != szRegion ) {
23943
- rc = SQLITE_IOERR_SHMSIZE;
23944
- pNode = NULL;
23945
- }
23946
-
23947
- if( pNode ) {
23948
- sqlite3_mutex_enter(pNode->mutex);
23949
-
23950
- memset(pLink, 0, sizeof(*pLink));
23951
-
23952
- pLink->pShmNode = pNode;
23953
- pLink->pNext = pNode->pFirst;
23954
- pNode->pFirst = pLink;
23955
- pNode->nRef++;
23956
-
23957
- fd->pShmLink = pLink;
23958
-
23959
- sqlite3_mutex_leave(pNode->mutex);
23960
-
23961
- } else {
23962
- /* Error occured. Free our link object. */
23963
- sqlite3_free(pLink);
23964
- }
23965
-
23966
- os2ShmLeaveMutex();
23967
-
23968
- ERR_TRACE(rc, ("os2OpenSharedMemory: %d %s\n", rc, fd->zFullPathCp))
23969
-
23970
- return rc;
23971
-}
23972
-
23973
-/*
23974
-** Purge the os2ShmNodeList list of all entries with nRef==0.
23975
-**
23976
-** This is not a VFS shared-memory method; it is a utility function called
23977
-** by VFS shared-memory methods.
23978
-*/
23979
-static void os2PurgeShmNodes( int deleteFlag ) {
23980
- os2ShmNode *pNode;
23981
- os2ShmNode **ppNode;
23982
-
23983
- os2ShmEnterMutex();
23984
-
23985
- ppNode = &os2ShmNodeList;
23986
-
23987
- while( *ppNode ) {
23988
- pNode = *ppNode;
23989
-
23990
- if( pNode->nRef == 0 ) {
23991
- *ppNode = pNode->pNext;
23992
-
23993
- if( pNode->apRegion ) {
23994
- /* Prevent other processes from resizing the shared memory */
23995
- os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23996
-
23997
- while( pNode->nRegion-- ) {
23998
-#ifdef SQLITE_DEBUG
23999
- int rc =
24000
-#endif
24001
- DosFreeMem(pNode->apRegion[pNode->nRegion]);
24002
-
24003
- OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
24004
- (int)GetCurrentProcessId(), pNode->nRegion,
24005
- rc == 0 ? "ok" : "failed"));
24006
- }
24007
-
24008
- /* Allow other processes to resize the shared memory */
24009
- os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
24010
-
24011
- sqlite3_free(pNode->apRegion);
24012
- }
24013
-
24014
- DosClose(pNode->hLockFile);
24015
-
24016
-#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
24017
- if( deleteFlag ) {
24018
- char fileName[CCHMAXPATH];
24019
- /* Skip "\\SHAREMEM\\" */
24020
- sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
24021
- /* restore colon */
24022
- fileName[1] = ':';
24023
-
24024
- DosForceDelete(fileName);
24025
- }
24026
-#endif
24027
-
24028
- sqlite3_mutex_free(pNode->mutex);
24029
-
24030
- sqlite3_free(pNode);
24031
-
24032
- } else {
24033
- ppNode = &pNode->pNext;
24034
- }
24035
- }
24036
-
24037
- os2ShmLeaveMutex();
24038
-}
24039
-
24040
-/*
24041
-** This function is called to obtain a pointer to region iRegion of the
24042
-** shared-memory associated with the database file id. Shared-memory regions
24043
-** are numbered starting from zero. Each shared-memory region is szRegion
24044
-** bytes in size.
24045
-**
24046
-** If an error occurs, an error code is returned and *pp is set to NULL.
24047
-**
24048
-** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
24049
-** region has not been allocated (by any client, including one running in a
24050
-** separate process), then *pp is set to NULL and SQLITE_OK returned. If
24051
-** bExtend is non-zero and the requested shared-memory region has not yet
24052
-** been allocated, it is allocated by this function.
24053
-**
24054
-** If the shared-memory region has already been allocated or is allocated by
24055
-** this call as described above, then it is mapped into this processes
24056
-** address space (if it is not already), *pp is set to point to the mapped
24057
-** memory and SQLITE_OK returned.
24058
-*/
24059
-static int os2ShmMap(
24060
- sqlite3_file *id, /* Handle open on database file */
24061
- int iRegion, /* Region to retrieve */
24062
- int szRegion, /* Size of regions */
24063
- int bExtend, /* True to extend block if necessary */
24064
- void volatile **pp /* OUT: Mapped memory */
24065
-){
24066
- PVOID pvTemp;
24067
- void **apRegion;
24068
- os2ShmNode *pNode;
24069
- int n, rc = SQLITE_OK;
24070
- char shmName[CCHMAXPATH];
24071
- os2File *pFile = (os2File*)id;
24072
-
24073
- *pp = NULL;
24074
-
24075
- if( !pFile->pShmLink )
24076
- rc = os2OpenSharedMemory( pFile, szRegion );
24077
-
24078
- if( rc == SQLITE_OK ) {
24079
- pNode = pFile->pShmLink->pShmNode ;
24080
-
24081
- sqlite3_mutex_enter(pNode->mutex);
24082
-
24083
- assert( szRegion==pNode->szRegion );
24084
-
24085
- /* Unmapped region ? */
24086
- if( iRegion >= pNode->nRegion ) {
24087
- /* Prevent other processes from resizing the shared memory */
24088
- os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
24089
-
24090
- apRegion = sqlite3_realloc(
24091
- pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
24092
-
24093
- if( apRegion ) {
24094
- pNode->apRegion = apRegion;
24095
-
24096
- while( pNode->nRegion <= iRegion ) {
24097
- sprintf(shmName, "%s-%u",
24098
- pNode->shmBaseName, pNode->nRegion);
24099
-
24100
- if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName,
24101
- PAG_READ | PAG_WRITE) != NO_ERROR ) {
24102
- if( !bExtend )
24103
- break;
24104
-
24105
- if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
24106
- PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR &&
24107
- DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
24108
- PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) {
24109
- rc = SQLITE_NOMEM;
24110
- break;
24111
- }
24112
- }
24113
-
24114
- apRegion[pNode->nRegion++] = pvTemp;
24115
- }
24116
-
24117
- /* zero out remaining entries */
24118
- for( n = pNode->nRegion; n <= iRegion; n++ )
24119
- pNode->apRegion[n] = NULL;
24120
-
24121
- /* Return this region (maybe zero) */
24122
- *pp = pNode->apRegion[iRegion];
24123
- } else {
24124
- rc = SQLITE_NOMEM;
24125
- }
24126
-
24127
- /* Allow other processes to resize the shared memory */
24128
- os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
24129
-
24130
- } else {
24131
- /* Region has been mapped previously */
24132
- *pp = pNode->apRegion[iRegion];
24133
- }
24134
-
24135
- sqlite3_mutex_leave(pNode->mutex);
24136
- }
24137
-
24138
- ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n",
24139
- pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
24140
-
24141
- return rc;
24142
-}
24143
-
24144
-/*
24145
-** Close a connection to shared-memory. Delete the underlying
24146
-** storage if deleteFlag is true.
24147
-**
24148
-** If there is no shared memory associated with the connection then this
24149
-** routine is a harmless no-op.
24150
-*/
24151
-static int os2ShmUnmap(
24152
- sqlite3_file *id, /* The underlying database file */
24153
- int deleteFlag /* Delete shared-memory if true */
24154
-){
24155
- os2File *pFile = (os2File*)id;
24156
- os2ShmLink *pLink = pFile->pShmLink;
24157
-
24158
- if( pLink ) {
24159
- int nRef = -1;
24160
- os2ShmLink **ppLink;
24161
- os2ShmNode *pNode = pLink->pShmNode;
24162
-
24163
- sqlite3_mutex_enter(pNode->mutex);
24164
-
24165
- for( ppLink = &pNode->pFirst;
24166
- *ppLink && *ppLink != pLink;
24167
- ppLink = &(*ppLink)->pNext ) ;
24168
-
24169
- assert(*ppLink);
24170
-
24171
- if( *ppLink ) {
24172
- *ppLink = pLink->pNext;
24173
- nRef = --pNode->nRef;
24174
- } else {
24175
- ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n",
24176
- pNode->shmBaseName))
24177
- }
24178
-
24179
- pFile->pShmLink = NULL;
24180
- sqlite3_free(pLink);
24181
-
24182
- sqlite3_mutex_leave(pNode->mutex);
24183
-
24184
- if( nRef == 0 )
24185
- os2PurgeShmNodes( deleteFlag );
24186
- }
24187
-
24188
- return SQLITE_OK;
24189
-}
24190
-
24191
-/*
24192
-** Change the lock state for a shared-memory segment.
24193
-**
24194
-** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
24195
-** different here than in posix. In xShmLock(), one can go from unlocked
24196
-** to shared and back or from unlocked to exclusive and back. But one may
24197
-** not go from shared to exclusive or from exclusive to shared.
24198
-*/
24199
-static int os2ShmLock(
24200
- sqlite3_file *id, /* Database file holding the shared memory */
24201
- int ofst, /* First lock to acquire or release */
24202
- int n, /* Number of locks to acquire or release */
24203
- int flags /* What to do with the lock */
24204
-){
24205
- u32 mask; /* Mask of locks to take or release */
24206
- int rc = SQLITE_OK; /* Result code */
24207
- os2File *pFile = (os2File*)id;
24208
- os2ShmLink *p = pFile->pShmLink; /* The shared memory being locked */
24209
- os2ShmLink *pX; /* For looping over all siblings */
24210
- os2ShmNode *pShmNode = p->pShmNode; /* Our node */
24211
-
24212
- assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
24213
- assert( n>=1 );
24214
- assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
24215
- || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
24216
- || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
24217
- || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
24218
- assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
24219
-
24220
- mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
24221
- assert( n>1 || mask==(1<<ofst) );
24222
-
24223
-
24224
- sqlite3_mutex_enter(pShmNode->mutex);
24225
-
24226
- if( flags & SQLITE_SHM_UNLOCK ){
24227
- u32 allMask = 0; /* Mask of locks held by siblings */
24228
-
24229
- /* See if any siblings hold this same lock */
24230
- for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
24231
- if( pX==p ) continue;
24232
- assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
24233
- allMask |= pX->sharedMask;
24234
- }
24235
-
24236
- /* Unlock the system-level locks */
24237
- if( (mask & allMask)==0 ){
24238
- rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
24239
- }else{
24240
- rc = SQLITE_OK;
24241
- }
24242
-
24243
- /* Undo the local locks */
24244
- if( rc==SQLITE_OK ){
24245
- p->exclMask &= ~mask;
24246
- p->sharedMask &= ~mask;
24247
- }
24248
- }else if( flags & SQLITE_SHM_SHARED ){
24249
- u32 allShared = 0; /* Union of locks held by connections other than "p" */
24250
-
24251
- /* Find out which shared locks are already held by sibling connections.
24252
- ** If any sibling already holds an exclusive lock, go ahead and return
24253
- ** SQLITE_BUSY.
24254
- */
24255
- for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
24256
- if( (pX->exclMask & mask)!=0 ){
24257
- rc = SQLITE_BUSY;
24258
- break;
24259
- }
24260
- allShared |= pX->sharedMask;
24261
- }
24262
-
24263
- /* Get shared locks at the system level, if necessary */
24264
- if( rc==SQLITE_OK ){
24265
- if( (allShared & mask)==0 ){
24266
- rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
24267
- }else{
24268
- rc = SQLITE_OK;
24269
- }
24270
- }
24271
-
24272
- /* Get the local shared locks */
24273
- if( rc==SQLITE_OK ){
24274
- p->sharedMask |= mask;
24275
- }
24276
- }else{
24277
- /* Make sure no sibling connections hold locks that will block this
24278
- ** lock. If any do, return SQLITE_BUSY right away.
24279
- */
24280
- for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
24281
- if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
24282
- rc = SQLITE_BUSY;
24283
- break;
24284
- }
24285
- }
24286
-
24287
- /* Get the exclusive locks at the system level. Then if successful
24288
- ** also mark the local connection as being locked.
24289
- */
24290
- if( rc==SQLITE_OK ){
24291
- rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
24292
- if( rc==SQLITE_OK ){
24293
- assert( (p->sharedMask & mask)==0 );
24294
- p->exclMask |= mask;
24295
- }
24296
- }
24297
- }
24298
-
24299
- sqlite3_mutex_leave(pShmNode->mutex);
24300
-
24301
- OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
24302
- p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
24303
- rc ? "failed" : "ok"));
24304
-
24305
- ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n",
24306
- ofst, n, flags, rc))
24307
-
24308
- return rc;
24309
-}
24310
-
24311
-/*
24312
-** Implement a memory barrier or memory fence on shared memory.
24313
-**
24314
-** All loads and stores begun before the barrier must complete before
24315
-** any load or store begun after the barrier.
24316
-*/
24317
-static void os2ShmBarrier(
24318
- sqlite3_file *id /* Database file holding the shared memory */
24319
-){
24320
- UNUSED_PARAMETER(id);
24321
- os2ShmEnterMutex();
24322
- os2ShmLeaveMutex();
24323
-}
24324
-
24325
-#else
24326
-# define os2ShmMap 0
24327
-# define os2ShmLock 0
24328
-# define os2ShmBarrier 0
24329
-# define os2ShmUnmap 0
24330
-#endif /* #ifndef SQLITE_OMIT_WAL */
24331
-
24332
-
24333
-/*
24334
-** This vector defines all the methods that can operate on an
24335
-** sqlite3_file for os2.
24336
-*/
24337
-static const sqlite3_io_methods os2IoMethod = {
24338
- 2, /* iVersion */
24339
- os2Close, /* xClose */
24340
- os2Read, /* xRead */
24341
- os2Write, /* xWrite */
24342
- os2Truncate, /* xTruncate */
24343
- os2Sync, /* xSync */
24344
- os2FileSize, /* xFileSize */
24345
- os2Lock, /* xLock */
24346
- os2Unlock, /* xUnlock */
24347
- os2CheckReservedLock, /* xCheckReservedLock */
24348
- os2FileControl, /* xFileControl */
24349
- os2SectorSize, /* xSectorSize */
24350
- os2DeviceCharacteristics, /* xDeviceCharacteristics */
24351
- os2ShmMap, /* xShmMap */
24352
- os2ShmLock, /* xShmLock */
24353
- os2ShmBarrier, /* xShmBarrier */
24354
- os2ShmUnmap /* xShmUnmap */
24355
-};
24356
-
24357
-
24358
-/***************************************************************************
24359
-** Here ends the I/O methods that form the sqlite3_io_methods object.
24360
-**
24361
-** The next block of code implements the VFS methods.
24362
-****************************************************************************/
24363
-
24364
-/*
24365
-** Create a temporary file name in zBuf. zBuf must be big enough to
24366
-** hold at pVfs->mxPathname characters.
24367
-*/
24368
-static int getTempname(int nBuf, char *zBuf ){
24369
- static const char zChars[] =
24370
- "abcdefghijklmnopqrstuvwxyz"
24371
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24372
- "0123456789";
24373
- int i, j;
24374
- PSZ zTempPathCp;
24375
- char zTempPath[CCHMAXPATH];
24376
- ULONG ulDriveNum, ulDriveMap;
24377
-
24378
- /* It's odd to simulate an io-error here, but really this is just
24379
- ** using the io-error infrastructure to test that SQLite handles this
24380
- ** function failing.
24381
- */
24382
- SimulateIOError( return SQLITE_IOERR );
24383
-
24384
- if( sqlite3_temp_directory ) {
24385
- sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
24386
- } else if( DosScanEnv( (PSZ)"TEMP", &zTempPathCp ) == NO_ERROR ||
24387
- DosScanEnv( (PSZ)"TMP", &zTempPathCp ) == NO_ERROR ||
24388
- DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
24389
- char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
24390
- sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
24391
- free( zTempPathUTF );
24392
- } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
24393
- zTempPath[0] = (char)('A' + ulDriveNum - 1);
24394
- zTempPath[1] = ':';
24395
- zTempPath[2] = '\0';
24396
- } else {
24397
- zTempPath[0] = '\0';
24398
- }
24399
-
24400
- /* Strip off a trailing slashes or backslashes, otherwise we would get *
24401
- * multiple (back)slashes which causes DosOpen() to fail. *
24402
- * Trailing spaces are not allowed, either. */
24403
- j = sqlite3Strlen30(zTempPath);
24404
- while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ||
24405
- zTempPath[j-1] == ' ' ) ){
24406
- j--;
24407
- }
24408
- zTempPath[j] = '\0';
24409
-
24410
- /* We use 20 bytes to randomize the name */
24411
- sqlite3_snprintf(nBuf-22, zBuf,
24412
- "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
24413
- j = sqlite3Strlen30(zBuf);
24414
- sqlite3_randomness( 20, &zBuf[j] );
24415
- for( i = 0; i < 20; i++, j++ ){
24416
- zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
24417
- }
24418
- zBuf[j] = 0;
24419
-
24420
- OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
24421
- return SQLITE_OK;
24422
-}
24423
-
24424
-
24425
-/*
24426
-** Turn a relative pathname into a full pathname. Write the full
24427
-** pathname into zFull[]. zFull[] will be at least pVfs->mxPathname
24428
-** bytes in size.
24429
-*/
24430
-static int os2FullPathname(
24431
- sqlite3_vfs *pVfs, /* Pointer to vfs object */
24432
- const char *zRelative, /* Possibly relative input path */
24433
- int nFull, /* Size of output buffer in bytes */
24434
- char *zFull /* Output buffer */
24435
-){
24436
- char *zRelativeCp = convertUtf8PathToCp( zRelative );
24437
- char zFullCp[CCHMAXPATH] = "\0";
24438
- char *zFullUTF;
24439
- APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME,
24440
- zFullCp, CCHMAXPATH );
24441
- free( zRelativeCp );
24442
- zFullUTF = convertCpPathToUtf8( zFullCp );
24443
- sqlite3_snprintf( nFull, zFull, zFullUTF );
24444
- free( zFullUTF );
24445
- return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
24446
-}
24447
-
24448
-
24449
-/*
24450
-** Open a file.
24451
-*/
24452
-static int os2Open(
24453
- sqlite3_vfs *pVfs, /* Not used */
24454
- const char *zName, /* Name of the file (UTF-8) */
24455
- sqlite3_file *id, /* Write the SQLite file handle here */
24456
- int flags, /* Open mode flags */
24457
- int *pOutFlags /* Status return flags */
24458
-){
24459
- HFILE h;
24460
- ULONG ulOpenFlags = 0;
24461
- ULONG ulOpenMode = 0;
24462
- ULONG ulAction = 0;
24463
- ULONG rc;
24464
- os2File *pFile = (os2File*)id;
24465
- const char *zUtf8Name = zName;
24466
- char *zNameCp;
24467
- char zTmpname[CCHMAXPATH];
24468
-
24469
- int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
24470
- int isCreate = (flags & SQLITE_OPEN_CREATE);
24471
- int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
24472
-#ifndef NDEBUG
24473
- int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
24474
- int isReadonly = (flags & SQLITE_OPEN_READONLY);
24475
- int eType = (flags & 0xFFFFFF00);
24476
- int isOpenJournal = (isCreate && (
24477
- eType==SQLITE_OPEN_MASTER_JOURNAL
24478
- || eType==SQLITE_OPEN_MAIN_JOURNAL
24479
- || eType==SQLITE_OPEN_WAL
24480
- ));
24481
-#endif
24482
-
24483
- UNUSED_PARAMETER(pVfs);
24484
- assert( id!=0 );
24485
-
24486
- /* Check the following statements are true:
24487
- **
24488
- ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
24489
- ** (b) if CREATE is set, then READWRITE must also be set, and
24490
- ** (c) if EXCLUSIVE is set, then CREATE must also be set.
24491
- ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
24492
- */
24493
- assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
24494
- assert(isCreate==0 || isReadWrite);
24495
- assert(isExclusive==0 || isCreate);
24496
- assert(isDelete==0 || isCreate);
24497
-
24498
- /* The main DB, main journal, WAL file and master journal are never
24499
- ** automatically deleted. Nor are they ever temporary files. */
24500
- assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
24501
- assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
24502
- assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
24503
- assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
24504
-
24505
- /* Assert that the upper layer has set one of the "file-type" flags. */
24506
- assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
24507
- || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
24508
- || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
24509
- || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
24510
- );
24511
-
24512
- memset( pFile, 0, sizeof(*pFile) );
24513
- pFile->h = (HFILE)-1;
24514
-
24515
- /* If the second argument to this function is NULL, generate a
24516
- ** temporary file name to use
24517
- */
24518
- if( !zUtf8Name ){
24519
- assert(isDelete && !isOpenJournal);
24520
- rc = getTempname(CCHMAXPATH, zTmpname);
24521
- if( rc!=SQLITE_OK ){
24522
- return rc;
24523
- }
24524
- zUtf8Name = zTmpname;
24525
- }
24526
-
24527
- if( isReadWrite ){
24528
- ulOpenMode |= OPEN_ACCESS_READWRITE;
24529
- }else{
24530
- ulOpenMode |= OPEN_ACCESS_READONLY;
24531
- }
24532
-
24533
- /* Open in random access mode for possibly better speed. Allow full
24534
- ** sharing because file locks will provide exclusive access when needed.
24535
- ** The handle should not be inherited by child processes and we don't
24536
- ** want popups from the critical error handler.
24537
- */
24538
- ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
24539
- OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
24540
-
24541
- /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
24542
- ** created. SQLite doesn't use it to indicate "exclusive access"
24543
- ** as it is usually understood.
24544
- */
24545
- if( isExclusive ){
24546
- /* Creates a new file, only if it does not already exist. */
24547
- /* If the file exists, it fails. */
24548
- ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
24549
- }else if( isCreate ){
24550
- /* Open existing file, or create if it doesn't exist */
24551
- ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
24552
- }else{
24553
- /* Opens a file, only if it exists. */
24554
- ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
24555
- }
24556
-
24557
- zNameCp = convertUtf8PathToCp( zUtf8Name );
24558
- rc = DosOpen( (PSZ)zNameCp,
24559
- &h,
24560
- &ulAction,
24561
- 0L,
24562
- FILE_NORMAL,
24563
- ulOpenFlags,
24564
- ulOpenMode,
24565
- (PEAOP2)NULL );
24566
- free( zNameCp );
24567
-
24568
- if( rc != NO_ERROR ){
24569
- OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
24570
- rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
24571
-
24572
- if( isReadWrite ){
24573
- return os2Open( pVfs, zName, id,
24574
- ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
24575
- pOutFlags );
24576
- }else{
24577
- return SQLITE_CANTOPEN;
24578
- }
24579
- }
24580
-
24581
- if( pOutFlags ){
24582
- *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
24583
- }
24584
-
24585
- os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
24586
- pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
24587
- pFile->pMethod = &os2IoMethod;
24588
- pFile->flags = flags;
24589
- pFile->h = h;
24590
-
24591
- OpenCounter(+1);
24592
- OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
24593
- return SQLITE_OK;
24594
-}
24595
-
24596
-/*
24597
-** Delete the named file.
24598
-*/
24599
-static int os2Delete(
24600
- sqlite3_vfs *pVfs, /* Not used on os2 */
24601
- const char *zFilename, /* Name of file to delete */
24602
- int syncDir /* Not used on os2 */
24603
-){
24604
- APIRET rc;
24605
- char *zFilenameCp;
24606
- SimulateIOError( return SQLITE_IOERR_DELETE );
24607
- zFilenameCp = convertUtf8PathToCp( zFilename );
24608
- rc = DosDelete( (PSZ)zFilenameCp );
24609
- free( zFilenameCp );
24610
- OSTRACE(( "DELETE \"%s\"\n", zFilename ));
24611
- return (rc == NO_ERROR ||
24612
- rc == ERROR_FILE_NOT_FOUND ||
24613
- rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
24614
-}
24615
-
24616
-/*
24617
-** Check the existance and status of a file.
24618
-*/
24619
-static int os2Access(
24620
- sqlite3_vfs *pVfs, /* Not used on os2 */
24621
- const char *zFilename, /* Name of file to check */
24622
- int flags, /* Type of test to make on this file */
24623
- int *pOut /* Write results here */
24624
-){
24625
- APIRET rc;
24626
- FILESTATUS3 fsts3ConfigInfo;
24627
- char *zFilenameCp;
24628
-
24629
- UNUSED_PARAMETER(pVfs);
24630
- SimulateIOError( return SQLITE_IOERR_ACCESS; );
24631
-
24632
- zFilenameCp = convertUtf8PathToCp( zFilename );
24633
- rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
24634
- &fsts3ConfigInfo, sizeof(FILESTATUS3) );
24635
- free( zFilenameCp );
24636
- OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
24637
- fsts3ConfigInfo.attrFile, flags, rc ));
24638
-
24639
- switch( flags ){
24640
- case SQLITE_ACCESS_EXISTS:
24641
- /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
24642
- ** as if it does not exist.
24643
- */
24644
- if( fsts3ConfigInfo.cbFile == 0 )
24645
- rc = ERROR_FILE_NOT_FOUND;
24646
- break;
24647
- case SQLITE_ACCESS_READ:
24648
- break;
24649
- case SQLITE_ACCESS_READWRITE:
24650
- if( fsts3ConfigInfo.attrFile & FILE_READONLY )
24651
- rc = ERROR_ACCESS_DENIED;
24652
- break;
24653
- default:
24654
- rc = ERROR_FILE_NOT_FOUND;
24655
- assert( !"Invalid flags argument" );
24656
- }
24657
-
24658
- *pOut = (rc == NO_ERROR);
24659
- OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
24660
-
24661
- return SQLITE_OK;
24662
-}
24663
-
24664
-
24665
-#ifndef SQLITE_OMIT_LOAD_EXTENSION
24666
-/*
24667
-** Interfaces for opening a shared library, finding entry points
24668
-** within the shared library, and closing the shared library.
24669
-*/
24670
-/*
24671
-** Interfaces for opening a shared library, finding entry points
24672
-** within the shared library, and closing the shared library.
24673
-*/
24674
-static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24675
- HMODULE hmod;
24676
- APIRET rc;
24677
- char *zFilenameCp = convertUtf8PathToCp(zFilename);
24678
- rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
24679
- free(zFilenameCp);
24680
- return rc != NO_ERROR ? 0 : (void*)hmod;
24681
-}
24682
-/*
24683
-** A no-op since the error code is returned on the DosLoadModule call.
24684
-** os2Dlopen returns zero if DosLoadModule is not successful.
24685
-*/
24686
-static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24687
-/* no-op */
24688
-}
24689
-static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
24690
- PFN pfn;
24691
- APIRET rc;
24692
- rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
24693
- if( rc != NO_ERROR ){
24694
- /* if the symbol itself was not found, search again for the same
24695
- * symbol with an extra underscore, that might be needed depending
24696
- * on the calling convention */
24697
- char _zSymbol[256] = "_";
24698
- strncat(_zSymbol, zSymbol, 254);
24699
- rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
24700
- }
24701
- return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
24702
-}
24703
-static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
24704
- DosFreeModule((HMODULE)pHandle);
24705
-}
24706
-#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24707
- #define os2DlOpen 0
24708
- #define os2DlError 0
24709
- #define os2DlSym 0
24710
- #define os2DlClose 0
24711
-#endif
24712
-
24713
-
24714
-/*
24715
-** Write up to nBuf bytes of randomness into zBuf.
24716
-*/
24717
-static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
24718
- int n = 0;
24719
-#if defined(SQLITE_TEST)
24720
- n = nBuf;
24721
- memset(zBuf, 0, nBuf);
24722
-#else
24723
- int i;
24724
- PPIB ppib;
24725
- PTIB ptib;
24726
- DATETIME dt;
24727
- static unsigned c = 0;
24728
- /* Ordered by variation probability */
24729
- static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
24730
- QSV_MAXPRMEM, QSV_MAXSHMEM,
24731
- QSV_TOTAVAILMEM, QSV_TOTRESMEM };
24732
-
24733
- /* 8 bytes; timezone and weekday don't increase the randomness much */
24734
- if( (int)sizeof(dt)-3 <= nBuf - n ){
24735
- c += 0x0100;
24736
- DosGetDateTime(&dt);
24737
- dt.year = (USHORT)((dt.year - 1900) | c);
24738
- memcpy(&zBuf[n], &dt, sizeof(dt)-3);
24739
- n += sizeof(dt)-3;
24740
- }
24741
-
24742
- /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
24743
- if( (int)sizeof(ULONG) <= nBuf - n ){
24744
- DosGetInfoBlocks(&ptib, &ppib);
24745
- *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
24746
- ptib->tib_ptib2->tib2_ultid);
24747
- n += sizeof(ULONG);
24748
- }
24749
-
24750
- /* Up to 6 * 4 bytes; variables depend on the system state */
24751
- for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
24752
- DosQuerySysInfo(svIdx[i], svIdx[i],
24753
- (PULONG)&zBuf[n], sizeof(ULONG));
24754
- n += sizeof(ULONG);
24755
- }
24756
-#endif
24757
-
24758
- return n;
24759
-}
24760
-
24761
-/*
24762
-** Sleep for a little while. Return the amount of time slept.
24763
-** The argument is the number of microseconds we want to sleep.
24764
-** The return value is the number of microseconds of sleep actually
24765
-** requested from the underlying operating system, a number which
24766
-** might be greater than or equal to the argument, but not less
24767
-** than the argument.
24768
-*/
24769
-static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
24770
- DosSleep( (microsec/1000) );
24771
- return microsec;
24772
-}
24773
-
24774
-/*
24775
-** The following variable, if set to a non-zero value, becomes the result
24776
-** returned from sqlite3OsCurrentTime(). This is used for testing.
24777
-*/
24778
-#ifdef SQLITE_TEST
24779
-SQLITE_API int sqlite3_current_time = 0;
24780
-#endif
24781
-
24782
-/*
24783
-** Find the current time (in Universal Coordinated Time). Write into *piNow
24784
-** the current time and date as a Julian Day number times 86_400_000. In
24785
-** other words, write into *piNow the number of milliseconds since the Julian
24786
-** epoch of noon in Greenwich on November 24, 4714 B.C according to the
24787
-** proleptic Gregorian calendar.
24788
-**
24789
-** On success, return 0. Return 1 if the time and date cannot be found.
24790
-*/
24791
-static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
24792
-#ifdef SQLITE_TEST
24793
- static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
24794
-#endif
24795
- int year, month, datepart, timepart;
24796
-
24797
- DATETIME dt;
24798
- DosGetDateTime( &dt );
24799
-
24800
- year = dt.year;
24801
- month = dt.month;
24802
-
24803
- /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
24804
- ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
24805
- ** Calculate the Julian days
24806
- */
24807
- datepart = (int)dt.day - 32076 +
24808
- 1461*(year + 4800 + (month - 14)/12)/4 +
24809
- 367*(month - 2 - (month - 14)/12*12)/12 -
24810
- 3*((year + 4900 + (month - 14)/12)/100)/4;
24811
-
24812
- /* Time in milliseconds, hours to noon added */
24813
- timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
24814
- ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
24815
-
24816
- *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
24817
-
24818
-#ifdef SQLITE_TEST
24819
- if( sqlite3_current_time ){
24820
- *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
24821
- }
24822
-#endif
24823
-
24824
- UNUSED_PARAMETER(pVfs);
24825
- return 0;
24826
-}
24827
-
24828
-/*
24829
-** Find the current time (in Universal Coordinated Time). Write the
24830
-** current time and date as a Julian Day number into *prNow and
24831
-** return 0. Return 1 if the time and date cannot be found.
24832
-*/
24833
-static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
24834
- int rc;
24835
- sqlite3_int64 i;
24836
- rc = os2CurrentTimeInt64(pVfs, &i);
24837
- if( !rc ){
24838
- *prNow = i/86400000.0;
24839
- }
24840
- return rc;
24841
-}
24842
-
24843
-/*
24844
-** The idea is that this function works like a combination of
24845
-** GetLastError() and FormatMessage() on windows (or errno and
24846
-** strerror_r() on unix). After an error is returned by an OS
24847
-** function, SQLite calls this function with zBuf pointing to
24848
-** a buffer of nBuf bytes. The OS layer should populate the
24849
-** buffer with a nul-terminated UTF-8 encoded error message
24850
-** describing the last IO error to have occurred within the calling
24851
-** thread.
24852
-**
24853
-** If the error message is too large for the supplied buffer,
24854
-** it should be truncated. The return value of xGetLastError
24855
-** is zero if the error message fits in the buffer, or non-zero
24856
-** otherwise (if the message was truncated). If non-zero is returned,
24857
-** then it is not necessary to include the nul-terminator character
24858
-** in the output buffer.
24859
-**
24860
-** Not supplying an error message will have no adverse effect
24861
-** on SQLite. It is fine to have an implementation that never
24862
-** returns an error message:
24863
-**
24864
-** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24865
-** assert(zBuf[0]=='\0');
24866
-** return 0;
24867
-** }
24868
-**
24869
-** However if an error message is supplied, it will be incorporated
24870
-** by sqlite into the error message available to the user using
24871
-** sqlite3_errmsg(), possibly making IO errors easier to debug.
24872
-*/
24873
-static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24874
- assert(zBuf[0]=='\0');
24875
- return 0;
24876
-}
24877
-
24878
-/*
24879
-** Initialize and deinitialize the operating system interface.
24880
-*/
24881
-SQLITE_API int sqlite3_os_init(void){
24882
- static sqlite3_vfs os2Vfs = {
24883
- 3, /* iVersion */
24884
- sizeof(os2File), /* szOsFile */
24885
- CCHMAXPATH, /* mxPathname */
24886
- 0, /* pNext */
24887
- "os2", /* zName */
24888
- 0, /* pAppData */
24889
-
24890
- os2Open, /* xOpen */
24891
- os2Delete, /* xDelete */
24892
- os2Access, /* xAccess */
24893
- os2FullPathname, /* xFullPathname */
24894
- os2DlOpen, /* xDlOpen */
24895
- os2DlError, /* xDlError */
24896
- os2DlSym, /* xDlSym */
24897
- os2DlClose, /* xDlClose */
24898
- os2Randomness, /* xRandomness */
24899
- os2Sleep, /* xSleep */
24900
- os2CurrentTime, /* xCurrentTime */
24901
- os2GetLastError, /* xGetLastError */
24902
- os2CurrentTimeInt64, /* xCurrentTimeInt64 */
24903
- 0, /* xSetSystemCall */
24904
- 0, /* xGetSystemCall */
24905
- 0 /* xNextSystemCall */
24906
- };
24907
- sqlite3_vfs_register(&os2Vfs, 1);
24908
- initUconvObjects();
24909
-/* sqlite3OSTrace = 1; */
24910
- return SQLITE_OK;
24911
-}
24912
-SQLITE_API int sqlite3_os_end(void){
24913
- freeUconvObjects();
24914
- return SQLITE_OK;
24915
-}
24916
-
24917
-#endif /* SQLITE_OS_OS2 */
24918
-
24919
-/************** End of os_os2.c **********************************************/
2492022516
/************** Begin file os_unix.c *****************************************/
2492122517
/*
2492222518
** 2004 May 22
2492322519
**
2492422520
** The author disclaims copyright to this source code. In place of
@@ -59054,18 +56650,18 @@
5905456650
/*
5905556651
** Release all resources associated with an sqlite3_backup* handle.
5905656652
*/
5905756653
SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
5905856654
sqlite3_backup **pp; /* Ptr to head of pagers backup list */
59059
- MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
56655
+ sqlite3 *pSrcDb; /* Source database connection */
5906056656
int rc; /* Value to return */
5906156657
5906256658
/* Enter the mutexes */
5906356659
if( p==0 ) return SQLITE_OK;
59064
- sqlite3_mutex_enter(p->pSrcDb->mutex);
56660
+ pSrcDb = p->pSrcDb;
56661
+ sqlite3_mutex_enter(pSrcDb->mutex);
5906556662
sqlite3BtreeEnter(p->pSrc);
59066
- MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
5906756663
if( p->pDestDb ){
5906856664
sqlite3_mutex_enter(p->pDestDb->mutex);
5906956665
}
5907056666
5907156667
/* Detach this backup from the source pager. */
@@ -59087,20 +56683,20 @@
5908756683
rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
5908856684
sqlite3Error(p->pDestDb, rc, 0);
5908956685
5909056686
/* Exit the mutexes and free the backup context structure. */
5909156687
if( p->pDestDb ){
59092
- sqlite3_mutex_leave(p->pDestDb->mutex);
56688
+ sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
5909356689
}
5909456690
sqlite3BtreeLeave(p->pSrc);
5909556691
if( p->pDestDb ){
5909656692
/* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
5909756693
** call to sqlite3_backup_init() and is destroyed by a call to
5909856694
** sqlite3_backup_finish(). */
5909956695
sqlite3_free(p);
5910056696
}
59101
- sqlite3_mutex_leave(mutex);
56697
+ sqlite3LeaveMutexAndCloseZombie(pSrcDb);
5910256698
return rc;
5910356699
}
5910456700
5910556701
/*
5910656702
** Return the number of pages still to be backed up as of the most recent
@@ -62860,10 +60456,11 @@
6286060456
SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
6286160457
sqlite3 *db;
6286260458
6286360459
if( NEVER(p==0) ) return;
6286460460
db = p->db;
60461
+ assert( sqlite3_mutex_held(db->mutex) );
6286560462
if( p->pPrev ){
6286660463
p->pPrev->pNext = p->pNext;
6286760464
}else{
6286860465
assert( db->pVdbe==p );
6286960466
db->pVdbe = p->pNext;
@@ -63699,21 +61296,15 @@
6369961296
** pointer is a harmless no-op. */
6370061297
rc = SQLITE_OK;
6370161298
}else{
6370261299
Vdbe *v = (Vdbe*)pStmt;
6370361300
sqlite3 *db = v->db;
63704
-#if SQLITE_THREADSAFE
63705
- sqlite3_mutex *mutex;
63706
-#endif
6370761301
if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
63708
-#if SQLITE_THREADSAFE
63709
- mutex = v->db->mutex;
63710
-#endif
63711
- sqlite3_mutex_enter(mutex);
61302
+ sqlite3_mutex_enter(db->mutex);
6371261303
rc = sqlite3VdbeFinalize(v);
6371361304
rc = sqlite3ApiExit(db, rc);
63714
- sqlite3_mutex_leave(mutex);
61305
+ sqlite3LeaveMutexAndCloseZombie(db);
6371561306
}
6371661307
return rc;
6371761308
}
6371861309
6371961310
/*
@@ -70017,11 +67608,10 @@
7001767608
assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
7001867609
assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
7001967610
assert( u.bl.pC!=0 );
7002067611
assert( u.bl.pC->nullRow==0 );
7002167612
assert( u.bl.pC->pseudoTableReg==0 );
70022
- assert( !u.bl.pC->isSorter );
7002367613
assert( u.bl.pC->pCursor!=0 );
7002467614
u.bl.pCrsr = u.bl.pC->pCursor;
7002567615
assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
7002667616
7002767617
/* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
@@ -88019,12 +85609,23 @@
8801985609
*/
8802085610
static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
8802185611
assert( argc==1 );
8802285612
UNUSED_PARAMETER(argc);
8802385613
switch( sqlite3_value_type(argv[0]) ){
88024
- case SQLITE_INTEGER:
8802585614
case SQLITE_FLOAT: {
85615
+ double r1, r2;
85616
+ char zBuf[50];
85617
+ r1 = sqlite3_value_double(argv[0]);
85618
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
85619
+ sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
85620
+ if( r1!=r2 ){
85621
+ sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
85622
+ }
85623
+ sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
85624
+ break;
85625
+ }
85626
+ case SQLITE_INTEGER: {
8802685627
sqlite3_result_value(context, argv[0]);
8802785628
break;
8802885629
}
8802985630
case SQLITE_BLOB: {
8803085631
char *zText = 0;
@@ -114347,18 +111948,30 @@
114347111948
sqlite3BtreeLeaveAll(db);
114348111949
#else
114349111950
UNUSED_PARAMETER(db);
114350111951
#endif
114351111952
}
111953
+
111954
+/*
111955
+** Return TRUE if database connection db has unfinalized prepared
111956
+** statements or unfinished sqlite3_backup objects.
111957
+*/
111958
+static int connectionIsBusy(sqlite3 *db){
111959
+ int j;
111960
+ assert( sqlite3_mutex_held(db->mutex) );
111961
+ if( db->pVdbe ) return 1;
111962
+ for(j=0; j<db->nDb; j++){
111963
+ Btree *pBt = db->aDb[j].pBt;
111964
+ if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
111965
+ }
111966
+ return 0;
111967
+}
114352111968
114353111969
/*
114354111970
** Close an existing SQLite database
114355111971
*/
114356
-SQLITE_API int sqlite3_close(sqlite3 *db){
114357
- HashElem *i; /* Hash table iterator */
114358
- int j;
114359
-
111972
+static int sqlite3Close(sqlite3 *db, int forceZombie){
114360111973
if( !db ){
114361111974
return SQLITE_OK;
114362111975
}
114363111976
if( !sqlite3SafetyCheckSickOrOk(db) ){
114364111977
return SQLITE_MISUSE_BKPT;
@@ -114375,28 +111988,66 @@
114375111988
** SQL statements below, as the v-table implementation may be storing
114376111989
** some prepared statements internally.
114377111990
*/
114378111991
sqlite3VtabRollback(db);
114379111992
114380
- /* If there are any outstanding VMs, return SQLITE_BUSY. */
114381
- if( db->pVdbe ){
114382
- sqlite3Error(db, SQLITE_BUSY,
114383
- "unable to close due to unfinalised statements");
111993
+ /* Legacy behavior (sqlite3_close() behavior) is to return
111994
+ ** SQLITE_BUSY if the connection can not be closed immediately.
111995
+ */
111996
+ if( !forceZombie && connectionIsBusy(db) ){
111997
+ sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
111998
+ "statements or unfinished backups");
114384111999
sqlite3_mutex_leave(db->mutex);
114385112000
return SQLITE_BUSY;
114386112001
}
114387
- assert( sqlite3SafetyCheckSickOrOk(db) );
114388
-
114389
- for(j=0; j<db->nDb; j++){
114390
- Btree *pBt = db->aDb[j].pBt;
114391
- if( pBt && sqlite3BtreeIsInBackup(pBt) ){
114392
- sqlite3Error(db, SQLITE_BUSY,
114393
- "unable to close due to unfinished backup operation");
114394
- sqlite3_mutex_leave(db->mutex);
114395
- return SQLITE_BUSY;
114396
- }
114397
- }
112002
+
112003
+ /* Convert the connection into a zombie and then close it.
112004
+ */
112005
+ db->magic = SQLITE_MAGIC_ZOMBIE;
112006
+ sqlite3LeaveMutexAndCloseZombie(db);
112007
+ return SQLITE_OK;
112008
+}
112009
+
112010
+/*
112011
+** Two variations on the public interface for closing a database
112012
+** connection. The sqlite3_close() version returns SQLITE_BUSY and
112013
+** leaves the connection option if there are unfinalized prepared
112014
+** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
112015
+** version forces the connection to become a zombie if there are
112016
+** unclosed resources, and arranges for deallocation when the last
112017
+** prepare statement or sqlite3_backup closes.
112018
+*/
112019
+SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
112020
+SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
112021
+
112022
+
112023
+/*
112024
+** Close the mutex on database connection db.
112025
+**
112026
+** Furthermore, if database connection db is a zombie (meaning that there
112027
+** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
112028
+** every sqlite3_stmt has now been finalized and every sqlite3_backup has
112029
+** finished, then free all resources.
112030
+*/
112031
+SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
112032
+ HashElem *i; /* Hash table iterator */
112033
+ int j;
112034
+
112035
+ /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
112036
+ ** or if the connection has not yet been closed by sqlite3_close_v2(),
112037
+ ** then just leave the mutex and return.
112038
+ */
112039
+ if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
112040
+ sqlite3_mutex_leave(db->mutex);
112041
+ return;
112042
+ }
112043
+
112044
+ /* If we reach this point, it means that the database connection has
112045
+ ** closed all sqlite3_stmt and sqlite3_backup objects and has been
112046
+ ** pased to sqlite3_close (meaning that it is a zombie). Therefore,
112047
+ ** go ahead and free all resources.
112048
+ */
114398112049
114399112050
/* Free any outstanding Savepoint structures. */
114400112051
sqlite3CloseSavepoints(db);
114401112052
114402112053
/* Close all database connections */
@@ -114481,11 +112132,10 @@
114481112132
assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
114482112133
if( db->lookaside.bMalloced ){
114483112134
sqlite3_free(db->lookaside.pStart);
114484112135
}
114485112136
sqlite3_free(db);
114486
- return SQLITE_OK;
114487112137
}
114488112138
114489112139
/*
114490112140
** Rollback all database files. If tripCode is not SQLITE_OK, then
114491112141
** any open cursors are invalidated ("tripped" - as in "tripping a circuit
@@ -115648,13 +113298,11 @@
115648113298
if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
115649113299
static struct OpenMode aOpenMode[] = {
115650113300
{ "ro", SQLITE_OPEN_READONLY },
115651113301
{ "rw", SQLITE_OPEN_READWRITE },
115652113302
{ "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
115653
- { "memory",
115654
- SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
115655
- | SQLITE_OPEN_MEMORY },
113303
+ { "memory", SQLITE_OPEN_MEMORY },
115656113304
{ 0, 0 }
115657113305
};
115658113306
115659113307
mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
115660113308
| SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
@@ -125936,14 +123584,13 @@
125936123584
/* #include <string.h> */
125937123585
125938123586
/*
125939123587
** Implementation of a special SQL scalar function for testing tokenizers
125940123588
** designed to be used in concert with the Tcl testing framework. This
125941
-** function must be called with two arguments:
123589
+** function must be called with two or more arguments:
125942123590
**
125943
-** SELECT <function-name>(<key-name>, <input-string>);
125944
-** SELECT <function-name>(<key-name>, <pointer>);
123591
+** SELECT <function-name>(<key-name>, ..., <input-string>);
125945123592
**
125946123593
** where <function-name> is the name passed as the second argument
125947123594
** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
125948123595
** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
125949123596
**
@@ -125976,31 +123623,31 @@
125976123623
const char *zName;
125977123624
int nName;
125978123625
const char *zInput;
125979123626
int nInput;
125980123627
125981
- const char *zArg = 0;
123628
+ const char *azArg[64];
125982123629
125983123630
const char *zToken;
125984123631
int nToken;
125985123632
int iStart;
125986123633
int iEnd;
125987123634
int iPos;
123635
+ int i;
125988123636
125989123637
Tcl_Obj *pRet;
125990123638
125991
- assert( argc==2 || argc==3 );
123639
+ if( argc<2 ){
123640
+ sqlite3_result_error(context, "insufficient arguments", -1);
123641
+ return;
123642
+ }
125992123643
125993123644
nName = sqlite3_value_bytes(argv[0]);
125994123645
zName = (const char *)sqlite3_value_text(argv[0]);
125995123646
nInput = sqlite3_value_bytes(argv[argc-1]);
125996123647
zInput = (const char *)sqlite3_value_text(argv[argc-1]);
125997123648
125998
- if( argc==3 ){
125999
- zArg = (const char *)sqlite3_value_text(argv[1]);
126000
- }
126001
-
126002123649
pHash = (Fts3Hash *)sqlite3_user_data(context);
126003123650
p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
126004123651
126005123652
if( !p ){
126006123653
char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
@@ -126010,11 +123657,15 @@
126010123657
}
126011123658
126012123659
pRet = Tcl_NewObj();
126013123660
Tcl_IncrRefCount(pRet);
126014123661
126015
- if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
123662
+ for(i=1; i<argc-1; i++){
123663
+ azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
123664
+ }
123665
+
123666
+ if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
126016123667
zErr = "error in xCreate()";
126017123668
goto finish;
126018123669
}
126019123670
pTokenizer->pModule = p;
126020123671
if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
@@ -126194,14 +123845,11 @@
126194123845
if( SQLITE_OK==rc ){
126195123846
rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
126196123847
}
126197123848
#ifdef SQLITE_TEST
126198123849
if( SQLITE_OK==rc ){
126199
- rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
126200
- }
126201
- if( SQLITE_OK==rc ){
126202
- rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
123850
+ rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
126203123851
}
126204123852
if( SQLITE_OK==rc ){
126205123853
rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
126206123854
}
126207123855
#endif
@@ -126588,11 +124236,12 @@
126588124236
** fts3SegReaderFirstDocid()
126589124237
** fts3SegReaderNextDocid()
126590124238
*/
126591124239
struct Fts3SegReader {
126592124240
int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
126593
- int bLookup; /* True for a lookup only */
124241
+ u8 bLookup; /* True for a lookup only */
124242
+ u8 rootOnly; /* True for a root-only reader */
126594124243
126595124244
sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
126596124245
sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
126597124246
sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
126598124247
sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
@@ -126622,11 +124271,11 @@
126622124271
int nOffsetList; /* For descending pending seg-readers only */
126623124272
sqlite3_int64 iDocid;
126624124273
};
126625124274
126626124275
#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
126627
-#define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
124276
+#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
126628124277
126629124278
/*
126630124279
** An instance of this structure is used to create a segment b-tree in the
126631124280
** database. The internal details of this type are only accessed by the
126632124281
** following functions:
@@ -128033,18 +125682,19 @@
128033125682
if( !pReader ){
128034125683
return SQLITE_NOMEM;
128035125684
}
128036125685
memset(pReader, 0, sizeof(Fts3SegReader));
128037125686
pReader->iIdx = iAge;
128038
- pReader->bLookup = bLookup;
125687
+ pReader->bLookup = bLookup!=0;
128039125688
pReader->iStartBlock = iStartLeaf;
128040125689
pReader->iLeafEndBlock = iEndLeaf;
128041125690
pReader->iEndBlock = iEndBlock;
128042125691
128043125692
if( nExtra ){
128044125693
/* The entire segment is stored in the root node. */
128045125694
pReader->aNode = (char *)&pReader[1];
125695
+ pReader->rootOnly = 1;
128046125696
pReader->nNode = nRoot;
128047125697
memcpy(pReader->aNode, zRoot, nRoot);
128048125698
memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
128049125699
}else{
128050125700
pReader->iCurrentBlock = iStartLeaf-1;
@@ -133438,10 +131088,12 @@
133438131088
typedef struct unicode_cursor unicode_cursor;
133439131089
133440131090
struct unicode_tokenizer {
133441131091
sqlite3_tokenizer base;
133442131092
int bRemoveDiacritic;
131093
+ int nException;
131094
+ int *aiException;
133443131095
};
133444131096
133445131097
struct unicode_cursor {
133446131098
sqlite3_tokenizer_cursor base;
133447131099
const unsigned char *aInput; /* Input text being tokenized */
@@ -133450,10 +131102,125 @@
133450131102
int iToken; /* Index of next token to be returned */
133451131103
char *zToken; /* storage for current token */
133452131104
int nAlloc; /* space allocated at zToken */
133453131105
};
133454131106
131107
+
131108
+/*
131109
+** Destroy a tokenizer allocated by unicodeCreate().
131110
+*/
131111
+static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
131112
+ if( pTokenizer ){
131113
+ unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
131114
+ sqlite3_free(p->aiException);
131115
+ sqlite3_free(p);
131116
+ }
131117
+ return SQLITE_OK;
131118
+}
131119
+
131120
+/*
131121
+** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
131122
+** statement has specified that the tokenizer for this table shall consider
131123
+** all characters in string zIn/nIn to be separators (if bAlnum==0) or
131124
+** token characters (if bAlnum==1).
131125
+**
131126
+** For each codepoint in the zIn/nIn string, this function checks if the
131127
+** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
131128
+** If so, no action is taken. Otherwise, the codepoint is added to the
131129
+** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
131130
+** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
131131
+** codepoints in the aiException[] array.
131132
+**
131133
+** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
131134
+** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
131135
+** It is not possible to change the behaviour of the tokenizer with respect
131136
+** to these codepoints.
131137
+*/
131138
+static int unicodeAddExceptions(
131139
+ unicode_tokenizer *p, /* Tokenizer to add exceptions to */
131140
+ int bAlnum, /* Replace Isalnum() return value with this */
131141
+ const char *zIn, /* Array of characters to make exceptions */
131142
+ int nIn /* Length of z in bytes */
131143
+){
131144
+ const unsigned char *z = (const unsigned char *)zIn;
131145
+ const unsigned char *zTerm = &z[nIn];
131146
+ int iCode;
131147
+ int nEntry = 0;
131148
+
131149
+ assert( bAlnum==0 || bAlnum==1 );
131150
+
131151
+ while( z<zTerm ){
131152
+ READ_UTF8(z, zTerm, iCode);
131153
+ assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
131154
+ if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
131155
+ && sqlite3FtsUnicodeIsdiacritic(iCode)==0
131156
+ ){
131157
+ nEntry++;
131158
+ }
131159
+ }
131160
+
131161
+ if( nEntry ){
131162
+ int *aNew; /* New aiException[] array */
131163
+ int nNew; /* Number of valid entries in array aNew[] */
131164
+
131165
+ aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
131166
+ if( aNew==0 ) return SQLITE_NOMEM;
131167
+ nNew = p->nException;
131168
+
131169
+ z = (const unsigned char *)zIn;
131170
+ while( z<zTerm ){
131171
+ READ_UTF8(z, zTerm, iCode);
131172
+ if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
131173
+ && sqlite3FtsUnicodeIsdiacritic(iCode)==0
131174
+ ){
131175
+ int i, j;
131176
+ for(i=0; i<nNew && aNew[i]<iCode; i++);
131177
+ for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
131178
+ aNew[i] = iCode;
131179
+ nNew++;
131180
+ }
131181
+ }
131182
+ p->aiException = aNew;
131183
+ p->nException = nNew;
131184
+ }
131185
+
131186
+ return SQLITE_OK;
131187
+}
131188
+
131189
+/*
131190
+** Return true if the p->aiException[] array contains the value iCode.
131191
+*/
131192
+static int unicodeIsException(unicode_tokenizer *p, int iCode){
131193
+ if( p->nException>0 ){
131194
+ int *a = p->aiException;
131195
+ int iLo = 0;
131196
+ int iHi = p->nException-1;
131197
+
131198
+ while( iHi>=iLo ){
131199
+ int iTest = (iHi + iLo) / 2;
131200
+ if( iCode==a[iTest] ){
131201
+ return 1;
131202
+ }else if( iCode>a[iTest] ){
131203
+ iLo = iTest+1;
131204
+ }else{
131205
+ iHi = iTest-1;
131206
+ }
131207
+ }
131208
+ }
131209
+
131210
+ return 0;
131211
+}
131212
+
131213
+/*
131214
+** Return true if, for the purposes of tokenization, codepoint iCode is
131215
+** considered a token character (not a separator).
131216
+*/
131217
+static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
131218
+ assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
131219
+ return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
131220
+}
131221
+
133455131222
/*
133456131223
** Create a new tokenizer instance.
133457131224
*/
133458131225
static int unicodeCreate(
133459131226
int nArg, /* Size of array argv[] */
@@ -133460,43 +131227,45 @@
133460131227
const char * const *azArg, /* Tokenizer creation arguments */
133461131228
sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
133462131229
){
133463131230
unicode_tokenizer *pNew; /* New tokenizer object */
133464131231
int i;
131232
+ int rc = SQLITE_OK;
131233
+
133465131234
pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
133466
- if( pNew==NULL ){
133467
- return SQLITE_NOMEM;
133468
- }
131235
+ if( pNew==NULL ) return SQLITE_NOMEM;
133469131236
memset(pNew, 0, sizeof(unicode_tokenizer));
133470131237
pNew->bRemoveDiacritic = 1;
133471131238
133472
- for(i=0; i<nArg; i++){
131239
+ for(i=0; rc==SQLITE_OK && i<nArg; i++){
133473131240
const char *z = azArg[i];
133474131241
int n = strlen(z);
133475131242
133476131243
if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
133477131244
pNew->bRemoveDiacritic = 1;
133478131245
}
133479131246
else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
133480131247
pNew->bRemoveDiacritic = 0;
133481131248
}
131249
+ else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
131250
+ rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
131251
+ }
131252
+ else if( n>=11 && memcmp("separators=", z, 11)==0 ){
131253
+ rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
131254
+ }
133482131255
else{
133483131256
/* Unrecognized argument */
133484
- return SQLITE_ERROR;
131257
+ rc = SQLITE_ERROR;
133485131258
}
133486131259
}
133487131260
133488
- *pp = &pNew->base;
133489
- return SQLITE_OK;
133490
-}
133491
-
133492
-/*
133493
-** Destroy a tokenizer allocated by unicodeCreate().
133494
-*/
133495
-static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
133496
- sqlite3_free(pTokenizer);
133497
- return SQLITE_OK;
131261
+ if( rc!=SQLITE_OK ){
131262
+ unicodeDestroy((sqlite3_tokenizer *)pNew);
131263
+ pNew = 0;
131264
+ }
131265
+ *pp = (sqlite3_tokenizer *)pNew;
131266
+ return rc;
133498131267
}
133499131268
133500131269
/*
133501131270
** Prepare to begin tokenizing a particular string. The input
133502131271
** string to be tokenized is pInput[0..nBytes-1]. A cursor
@@ -133545,18 +131314,19 @@
133545131314
/*
133546131315
** Extract the next token from a tokenization cursor. The cursor must
133547131316
** have been opened by a prior call to simpleOpen().
133548131317
*/
133549131318
static int unicodeNext(
133550
- sqlite3_tokenizer_cursor *p, /* Cursor returned by simpleOpen */
131319
+ sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
133551131320
const char **paToken, /* OUT: Token text */
133552131321
int *pnToken, /* OUT: Number of bytes at *paToken */
133553131322
int *piStart, /* OUT: Starting offset of token */
133554131323
int *piEnd, /* OUT: Ending offset of token */
133555131324
int *piPos /* OUT: Position integer of token */
133556131325
){
133557
- unicode_cursor *pCsr = (unicode_cursor *)p;
131326
+ unicode_cursor *pCsr = (unicode_cursor *)pC;
131327
+ unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
133558131328
int iCode;
133559131329
char *zOut;
133560131330
const unsigned char *z = &pCsr->aInput[pCsr->iOff];
133561131331
const unsigned char *zStart = z;
133562131332
const unsigned char *zEnd;
@@ -133565,11 +131335,11 @@
133565131335
/* Scan past any delimiter characters before the start of the next token.
133566131336
** Return SQLITE_DONE early if this takes us all the way to the end of
133567131337
** the input. */
133568131338
while( z<zTerm ){
133569131339
READ_UTF8(z, zTerm, iCode);
133570
- if( sqlite3FtsUnicodeIsalnum(iCode) ) break;
131340
+ if( unicodeIsAlnum(p, iCode) ) break;
133571131341
zStart = z;
133572131342
}
133573131343
if( zStart>=zTerm ) return SQLITE_DONE;
133574131344
133575131345
zOut = pCsr->zToken;
@@ -133585,21 +131355,19 @@
133585131355
pCsr->nAlloc += 64;
133586131356
}
133587131357
133588131358
/* Write the folded case of the last character read to the output */
133589131359
zEnd = z;
133590
- iOut = sqlite3FtsUnicodeFold(iCode,
133591
- ((unicode_tokenizer *)pCsr->base.pTokenizer)->bRemoveDiacritic
133592
- );
131360
+ iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
133593131361
if( iOut ){
133594131362
WRITE_UTF8(zOut, iOut);
133595131363
}
133596131364
133597131365
/* If the cursor is not at EOF, read the next character */
133598131366
if( z>=zTerm ) break;
133599131367
READ_UTF8(z, zTerm, iCode);
133600
- }while( sqlite3FtsUnicodeIsalnum(iCode)
131368
+ }while( unicodeIsAlnum(p, iCode)
133601131369
|| sqlite3FtsUnicodeIsdiacritic(iCode)
133602131370
);
133603131371
133604131372
/* Set the output variables and return. */
133605131373
pCsr->iOff = (z - pCsr->aInput);
@@ -133779,11 +131547,11 @@
133779131547
iHi = iTest-1;
133780131548
}
133781131549
}
133782131550
assert( aEntry[0]<key );
133783131551
assert( key>=aEntry[iRes] );
133784
- return (c >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
131552
+ return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
133785131553
}
133786131554
return 1;
133787131555
}
133788131556
133789131557
@@ -138059,11 +135827,11 @@
138059135827
return SQLITE_DONE;
138060135828
}
138061135829
138062135830
while( iStart<iEnd ){
138063135831
int iWhite = iStart;
138064
- U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
135832
+ U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
138065135833
if( u_isspace(c) ){
138066135834
iStart = iWhite;
138067135835
}else{
138068135836
break;
138069135837
}
138070135838
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.13. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -387,10 +387,11 @@
387 ** Exactly one of the following macros must be defined in order to
388 ** specify which memory allocation subsystem to use.
389 **
390 ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
391 ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
 
392 ** SQLITE_MEMDEBUG // Debugging version of system malloc()
393 **
394 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
395 ** assert() macro is enabled, each call into the Win32 native heap subsystem
396 ** will cause HeapValidate to be called. If heap validation should fail, an
@@ -400,15 +401,23 @@
400 ** pared it down to just these three.)
401 **
402 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
403 ** the default.
404 */
405 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
406 # error "At most one of the following compile-time configuration options\
407 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
 
 
 
 
 
408 #endif
409 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
 
 
 
410 # define SQLITE_SYSTEM_MALLOC 1
411 #endif
412
413 /*
414 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -662,13 +671,13 @@
662 **
663 ** See also: [sqlite3_libversion()],
664 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
665 ** [sqlite_version()] and [sqlite_source_id()].
666 */
667 #define SQLITE_VERSION "3.7.13"
668 #define SQLITE_VERSION_NUMBER 3007013
669 #define SQLITE_SOURCE_ID "2012-06-07 07:24:04 506008f000ba4af0b35da023b8c52f7a3f5033bd"
670
671 /*
672 ** CAPI3REF: Run-Time Library Version Numbers
673 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
674 **
@@ -774,11 +783,12 @@
774 **
775 ** Each open SQLite database is represented by a pointer to an instance of
776 ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
777 ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
778 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
779 ** is its destructor. There are many other interfaces (such as
 
780 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
781 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
782 ** sqlite3 object.
783 */
784 typedef struct sqlite3 sqlite3;
@@ -821,32 +831,50 @@
821 #endif
822
823 /*
824 ** CAPI3REF: Closing A Database Connection
825 **
826 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
827 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
828 ** successfully destroyed and all associated resources are deallocated.
 
 
829 **
830 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
831 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
832 ** the [sqlite3] object prior to attempting to close the object. ^If
 
 
 
 
 
 
 
 
 
 
 
 
833 ** sqlite3_close() is called on a [database connection] that still has
834 ** outstanding [prepared statements] or [BLOB handles], then it returns
835 ** SQLITE_BUSY.
 
 
836 **
837 ** ^If [sqlite3_close()] is invoked while a transaction is open,
838 ** the transaction is automatically rolled back.
839 **
840 ** The C parameter to [sqlite3_close(C)] must be either a NULL
 
841 ** pointer or an [sqlite3] object pointer obtained
842 ** from [sqlite3_open()], [sqlite3_open16()], or
843 ** [sqlite3_open_v2()], and not previously closed.
844 ** ^Calling sqlite3_close() with a NULL pointer argument is a
845 ** harmless no-op.
846 */
847 SQLITE_API int sqlite3_close(sqlite3 *);
 
848
849 /*
850 ** The type for a callback function.
851 ** This is legacy and deprecated. It is included for historical
852 ** compatibility and is not documented.
@@ -5013,15 +5041,16 @@
5013 ** CAPI3REF: Name Of The Folder Holding Database Files
5014 **
5015 ** ^(If this global variable is made to point to a string which is
5016 ** the name of a folder (a.k.a. directory), then all database files
5017 ** specified with a relative pathname and created or accessed by
5018 ** SQLite when using a built-in [sqlite3_vfs | VFS] will be assumed
5019 ** to be relative to that directory.)^ ^If this variable is a NULL
5020 ** pointer, then SQLite assumes that all database files specified
5021 ** with a relative pathname are relative to the current directory
5022 ** for the process.
 
5023 **
5024 ** Changing the value of this variable while a database connection is
5025 ** open can result in a corrupt database.
5026 **
5027 ** It is not safe to read or modify this variable in more than one
@@ -6048,21 +6077,20 @@
6048 ** of these mutex routines. An appropriate implementation
6049 ** is selected automatically at compile-time. ^(The following
6050 ** implementations are available in the SQLite core:
6051 **
6052 ** <ul>
6053 ** <li> SQLITE_MUTEX_OS2
6054 ** <li> SQLITE_MUTEX_PTHREADS
6055 ** <li> SQLITE_MUTEX_W32
6056 ** <li> SQLITE_MUTEX_NOOP
6057 ** </ul>)^
6058 **
6059 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
6060 ** that does no real locking and is appropriate for use in
6061 ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
6062 ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
6063 ** are appropriate for use on OS/2, Unix, and Windows.
6064 **
6065 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
6066 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
6067 ** implementation is included with the library. In this case the
6068 ** application must supply a custom mutex implementation using the
@@ -9287,22 +9315,20 @@
9287 #define _SQLITE_OS_H_
9288
9289 /*
9290 ** Figure out if we are dealing with Unix, Windows, or some other
9291 ** operating system. After the following block of preprocess macros,
9292 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
9293 ** will defined to either 1 or 0. One of the four will be 1. The other
9294 ** three will be 0.
9295 */
9296 #if defined(SQLITE_OS_OTHER)
9297 # if SQLITE_OS_OTHER==1
9298 # undef SQLITE_OS_UNIX
9299 # define SQLITE_OS_UNIX 0
9300 # undef SQLITE_OS_WIN
9301 # define SQLITE_OS_WIN 0
9302 # undef SQLITE_OS_OS2
9303 # define SQLITE_OS_OS2 0
9304 # else
9305 # undef SQLITE_OS_OTHER
9306 # endif
9307 #endif
9308 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
@@ -9309,23 +9335,16 @@
9309 # define SQLITE_OS_OTHER 0
9310 # ifndef SQLITE_OS_WIN
9311 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
9312 # define SQLITE_OS_WIN 1
9313 # define SQLITE_OS_UNIX 0
9314 # define SQLITE_OS_OS2 0
9315 # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
9316 # define SQLITE_OS_WIN 0
9317 # define SQLITE_OS_UNIX 0
9318 # define SQLITE_OS_OS2 1
9319 # else
9320 # define SQLITE_OS_WIN 0
9321 # define SQLITE_OS_UNIX 1
9322 # define SQLITE_OS_OS2 0
9323 # endif
9324 # else
9325 # define SQLITE_OS_UNIX 0
9326 # define SQLITE_OS_OS2 0
9327 # endif
9328 #else
9329 # ifndef SQLITE_OS_WIN
9330 # define SQLITE_OS_WIN 0
9331 # endif
@@ -9333,25 +9352,10 @@
9333
9334 #if SQLITE_OS_WIN
9335 # include <windows.h>
9336 #endif
9337
9338 #if SQLITE_OS_OS2
9339 # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
9340 # include <os2safe.h> /* has to be included before os2.h for linking to work */
9341 # endif
9342 # define INCL_DOSDATETIME
9343 # define INCL_DOSFILEMGR
9344 # define INCL_DOSERRORS
9345 # define INCL_DOSMISC
9346 # define INCL_DOSPROCESS
9347 # define INCL_DOSMODULEMGR
9348 # define INCL_DOSSEMAPHORES
9349 # include <os2.h>
9350 # include <uconv.h>
9351 #endif
9352
9353 /*
9354 ** Determine if we are dealing with Windows NT.
9355 **
9356 ** We ought to be able to determine if we are compiling for win98 or winNT
9357 ** using the _WIN32_WINNT macro as follows:
@@ -9617,23 +9621,19 @@
9617 ** start-time.
9618 **
9619 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
9620 **
9621 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
9622 **
9623 ** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2.
9624 */
9625 #if !SQLITE_THREADSAFE
9626 # define SQLITE_MUTEX_OMIT
9627 #endif
9628 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
9629 # if SQLITE_OS_UNIX
9630 # define SQLITE_MUTEX_PTHREADS
9631 # elif SQLITE_OS_WIN
9632 # define SQLITE_MUTEX_W32
9633 # elif SQLITE_OS_OS2
9634 # define SQLITE_MUTEX_OS2
9635 # else
9636 # define SQLITE_MUTEX_NOOP
9637 # endif
9638 #endif
9639
@@ -9950,10 +9950,11 @@
9950 #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
9951 #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
9952 #define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
9953 #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
9954 #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
 
9955
9956 /*
9957 ** Each SQL function is defined by an instance of the following
9958 ** structure. A pointer to this structure is stored in the sqlite.aFunc
9959 ** hash table. When multiple functions have the same name, the hash table
@@ -11811,10 +11812,11 @@
11811 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
11812 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
11813 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
11814 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
11815 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 
11816 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
11817 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
11818 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
11819 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
11820 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
@@ -17716,286 +17718,10 @@
17716 }
17717 #endif /* defined(SQLITE_MUTEX_NOOP) */
17718 #endif /* !defined(SQLITE_MUTEX_OMIT) */
17719
17720 /************** End of mutex_noop.c ******************************************/
17721 /************** Begin file mutex_os2.c ***************************************/
17722 /*
17723 ** 2007 August 28
17724 **
17725 ** The author disclaims copyright to this source code. In place of
17726 ** a legal notice, here is a blessing:
17727 **
17728 ** May you do good and not evil.
17729 ** May you find forgiveness for yourself and forgive others.
17730 ** May you share freely, never taking more than you give.
17731 **
17732 *************************************************************************
17733 ** This file contains the C functions that implement mutexes for OS/2
17734 */
17735
17736 /*
17737 ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
17738 ** See the mutex.h file for details.
17739 */
17740 #ifdef SQLITE_MUTEX_OS2
17741
17742 /********************** OS/2 Mutex Implementation **********************
17743 **
17744 ** This implementation of mutexes is built using the OS/2 API.
17745 */
17746
17747 /*
17748 ** The mutex object
17749 ** Each recursive mutex is an instance of the following structure.
17750 */
17751 struct sqlite3_mutex {
17752 HMTX mutex; /* Mutex controlling the lock */
17753 int id; /* Mutex type */
17754 #ifdef SQLITE_DEBUG
17755 int trace; /* True to trace changes */
17756 #endif
17757 };
17758
17759 #ifdef SQLITE_DEBUG
17760 #define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
17761 #else
17762 #define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
17763 #endif
17764
17765 /*
17766 ** Initialize and deinitialize the mutex subsystem.
17767 */
17768 static int os2MutexInit(void){ return SQLITE_OK; }
17769 static int os2MutexEnd(void){ return SQLITE_OK; }
17770
17771 /*
17772 ** The sqlite3_mutex_alloc() routine allocates a new
17773 ** mutex and returns a pointer to it. If it returns NULL
17774 ** that means that a mutex could not be allocated.
17775 ** SQLite will unwind its stack and return an error. The argument
17776 ** to sqlite3_mutex_alloc() is one of these integer constants:
17777 **
17778 ** <ul>
17779 ** <li> SQLITE_MUTEX_FAST
17780 ** <li> SQLITE_MUTEX_RECURSIVE
17781 ** <li> SQLITE_MUTEX_STATIC_MASTER
17782 ** <li> SQLITE_MUTEX_STATIC_MEM
17783 ** <li> SQLITE_MUTEX_STATIC_MEM2
17784 ** <li> SQLITE_MUTEX_STATIC_PRNG
17785 ** <li> SQLITE_MUTEX_STATIC_LRU
17786 ** <li> SQLITE_MUTEX_STATIC_LRU2
17787 ** </ul>
17788 **
17789 ** The first two constants cause sqlite3_mutex_alloc() to create
17790 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
17791 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
17792 ** The mutex implementation does not need to make a distinction
17793 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
17794 ** not want to. But SQLite will only request a recursive mutex in
17795 ** cases where it really needs one. If a faster non-recursive mutex
17796 ** implementation is available on the host platform, the mutex subsystem
17797 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
17798 **
17799 ** The other allowed parameters to sqlite3_mutex_alloc() each return
17800 ** a pointer to a static preexisting mutex. Six static mutexes are
17801 ** used by the current version of SQLite. Future versions of SQLite
17802 ** may add additional static mutexes. Static mutexes are for internal
17803 ** use by SQLite only. Applications that use SQLite mutexes should
17804 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
17805 ** SQLITE_MUTEX_RECURSIVE.
17806 **
17807 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
17808 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
17809 ** returns a different mutex on every call. But for the static
17810 ** mutex types, the same mutex is returned on every call that has
17811 ** the same type number.
17812 */
17813 static sqlite3_mutex *os2MutexAlloc(int iType){
17814 sqlite3_mutex *p = NULL;
17815 switch( iType ){
17816 case SQLITE_MUTEX_FAST:
17817 case SQLITE_MUTEX_RECURSIVE: {
17818 p = sqlite3MallocZero( sizeof(*p) );
17819 if( p ){
17820 p->id = iType;
17821 if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
17822 sqlite3_free( p );
17823 p = NULL;
17824 }
17825 }
17826 break;
17827 }
17828 default: {
17829 static volatile int isInit = 0;
17830 static sqlite3_mutex staticMutexes[6] = {
17831 SQLITE3_MUTEX_INITIALIZER,
17832 SQLITE3_MUTEX_INITIALIZER,
17833 SQLITE3_MUTEX_INITIALIZER,
17834 SQLITE3_MUTEX_INITIALIZER,
17835 SQLITE3_MUTEX_INITIALIZER,
17836 SQLITE3_MUTEX_INITIALIZER,
17837 };
17838 if ( !isInit ){
17839 APIRET rc;
17840 PTIB ptib;
17841 PPIB ppib;
17842 HMTX mutex;
17843 char name[32];
17844 DosGetInfoBlocks( &ptib, &ppib );
17845 sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
17846 ppib->pib_ulpid );
17847 while( !isInit ){
17848 mutex = 0;
17849 rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
17850 if( rc == NO_ERROR ){
17851 unsigned int i;
17852 if( !isInit ){
17853 for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
17854 DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
17855 }
17856 isInit = 1;
17857 }
17858 DosCloseMutexSem( mutex );
17859 }else if( rc == ERROR_DUPLICATE_NAME ){
17860 DosSleep( 1 );
17861 }else{
17862 return p;
17863 }
17864 }
17865 }
17866 assert( iType-2 >= 0 );
17867 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
17868 p = &staticMutexes[iType-2];
17869 p->id = iType;
17870 break;
17871 }
17872 }
17873 return p;
17874 }
17875
17876
17877 /*
17878 ** This routine deallocates a previously allocated mutex.
17879 ** SQLite is careful to deallocate every mutex that it allocates.
17880 */
17881 static void os2MutexFree(sqlite3_mutex *p){
17882 #ifdef SQLITE_DEBUG
17883 TID tid;
17884 PID pid;
17885 ULONG ulCount;
17886 DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17887 assert( ulCount==0 );
17888 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
17889 #endif
17890 DosCloseMutexSem( p->mutex );
17891 sqlite3_free( p );
17892 }
17893
17894 #ifdef SQLITE_DEBUG
17895 /*
17896 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
17897 ** intended for use inside assert() statements.
17898 */
17899 static int os2MutexHeld(sqlite3_mutex *p){
17900 TID tid;
17901 PID pid;
17902 ULONG ulCount;
17903 PTIB ptib;
17904 DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17905 if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
17906 return 0;
17907 DosGetInfoBlocks(&ptib, NULL);
17908 return tid==ptib->tib_ptib2->tib2_ultid;
17909 }
17910 static int os2MutexNotheld(sqlite3_mutex *p){
17911 TID tid;
17912 PID pid;
17913 ULONG ulCount;
17914 PTIB ptib;
17915 DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17916 if( ulCount==0 )
17917 return 1;
17918 DosGetInfoBlocks(&ptib, NULL);
17919 return tid!=ptib->tib_ptib2->tib2_ultid;
17920 }
17921 static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
17922 TID tid;
17923 PID pid;
17924 ULONG ulCount;
17925 DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
17926 printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
17927 }
17928 #endif
17929
17930 /*
17931 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
17932 ** to enter a mutex. If another thread is already within the mutex,
17933 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
17934 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
17935 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
17936 ** be entered multiple times by the same thread. In such cases the,
17937 ** mutex must be exited an equal number of times before another thread
17938 ** can enter. If the same thread tries to enter any other kind of mutex
17939 ** more than once, the behavior is undefined.
17940 */
17941 static void os2MutexEnter(sqlite3_mutex *p){
17942 assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17943 DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
17944 #ifdef SQLITE_DEBUG
17945 if( p->trace ) os2MutexTrace(p, "enter");
17946 #endif
17947 }
17948 static int os2MutexTry(sqlite3_mutex *p){
17949 int rc = SQLITE_BUSY;
17950 assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
17951 if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
17952 rc = SQLITE_OK;
17953 #ifdef SQLITE_DEBUG
17954 if( p->trace ) os2MutexTrace(p, "try");
17955 #endif
17956 }
17957 return rc;
17958 }
17959
17960 /*
17961 ** The sqlite3_mutex_leave() routine exits a mutex that was
17962 ** previously entered by the same thread. The behavior
17963 ** is undefined if the mutex is not currently entered or
17964 ** is not currently allocated. SQLite will never do either.
17965 */
17966 static void os2MutexLeave(sqlite3_mutex *p){
17967 assert( os2MutexHeld(p) );
17968 DosReleaseMutexSem(p->mutex);
17969 #ifdef SQLITE_DEBUG
17970 if( p->trace ) os2MutexTrace(p, "leave");
17971 #endif
17972 }
17973
17974 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17975 static const sqlite3_mutex_methods sMutex = {
17976 os2MutexInit,
17977 os2MutexEnd,
17978 os2MutexAlloc,
17979 os2MutexFree,
17980 os2MutexEnter,
17981 os2MutexTry,
17982 os2MutexLeave,
17983 #ifdef SQLITE_DEBUG
17984 os2MutexHeld,
17985 os2MutexNotheld
17986 #else
17987 0,
17988 0
17989 #endif
17990 };
17991
17992 return &sMutex;
17993 }
17994 #endif /* SQLITE_MUTEX_OS2 */
17995
17996 /************** End of mutex_os2.c *******************************************/
17997 /************** Begin file mutex_unix.c **************************************/
17998 /*
17999 ** 2007 August 28
18000 **
18001 ** The author disclaims copyright to this source code. In place of
@@ -18456,11 +18182,11 @@
18456 ** processing, the "interlocked" magic is probably not
18457 ** strictly necessary.
18458 */
18459 static long winMutex_lock = 0;
18460
18461 SQLITE_API extern void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18462
18463 static int winMutexInit(void){
18464 /* The first to increment to 1 does actual initialization */
18465 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
18466 int i;
@@ -19599,11 +19325,12 @@
19599 ** always returned.
19600 */
19601 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
19602 int digit;
19603 LONGDOUBLE_TYPE d;
19604 if( (*cnt)++ >= 16 ) return '0';
 
19605 digit = (int)*val;
19606 d = digit;
19607 digit += '0';
19608 *val = (*val - d)*10.0;
19609 return (char)digit;
@@ -19903,13 +19630,16 @@
19903 bufpt = "NaN";
19904 length = 3;
19905 break;
19906 }
19907 if( realvalue>0.0 ){
19908 while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
19909 while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
19910 while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
 
 
 
19911 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
19912 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
19913 if( exp>350 ){
19914 if( prefix=='-' ){
19915 bufpt = "-Inf";
@@ -19938,11 +19668,11 @@
19938 }else{
19939 precision = precision - exp;
19940 xtype = etFLOAT;
19941 }
19942 }else{
19943 flag_rtz = 0;
19944 }
19945 if( xtype==etEXP ){
19946 e2 = 0;
19947 }else{
19948 e2 = exp;
@@ -19953,11 +19683,11 @@
19953 pAccum->mallocFailed = 1;
19954 return;
19955 }
19956 }
19957 zOut = bufpt;
19958 nsd = 0;
19959 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19960 /* The sign in front of the number */
19961 if( prefix ){
19962 *(bufpt++) = prefix;
19963 }
@@ -21526,11 +21256,11 @@
21526 s = sign<0 ? -s : s;
21527
21528 /* if exponent, scale significand as appropriate
21529 ** and store in result. */
21530 if( e ){
21531 double scale = 1.0;
21532 /* attempt to handle extremely small/large numbers better */
21533 if( e>307 && e<342 ){
21534 while( e%308 ) { scale *= 1.0e+1; e -= 1; }
21535 if( esign<0 ){
21536 result = s / scale;
@@ -22781,2144 +22511,10 @@
22781 return azName[i];
22782 }
22783 #endif
22784
22785 /************** End of opcodes.c *********************************************/
22786 /************** Begin file os_os2.c ******************************************/
22787 /*
22788 ** 2006 Feb 14
22789 **
22790 ** The author disclaims copyright to this source code. In place of
22791 ** a legal notice, here is a blessing:
22792 **
22793 ** May you do good and not evil.
22794 ** May you find forgiveness for yourself and forgive others.
22795 ** May you share freely, never taking more than you give.
22796 **
22797 ******************************************************************************
22798 **
22799 ** This file contains code that is specific to OS/2.
22800 */
22801
22802
22803 #if SQLITE_OS_OS2
22804
22805 /*
22806 ** A Note About Memory Allocation:
22807 **
22808 ** This driver uses malloc()/free() directly rather than going through
22809 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
22810 ** are designed for use on embedded systems where memory is scarce and
22811 ** malloc failures happen frequently. OS/2 does not typically run on
22812 ** embedded systems, and when it does the developers normally have bigger
22813 ** problems to worry about than running out of memory. So there is not
22814 ** a compelling need to use the wrappers.
22815 **
22816 ** But there is a good reason to not use the wrappers. If we use the
22817 ** wrappers then we will get simulated malloc() failures within this
22818 ** driver. And that causes all kinds of problems for our tests. We
22819 ** could enhance SQLite to deal with simulated malloc failures within
22820 ** the OS driver, but the code to deal with those failure would not
22821 ** be exercised on Linux (which does not need to malloc() in the driver)
22822 ** and so we would have difficulty writing coverage tests for that
22823 ** code. Better to leave the code out, we think.
22824 **
22825 ** The point of this discussion is as follows: When creating a new
22826 ** OS layer for an embedded system, if you use this file as an example,
22827 ** avoid the use of malloc()/free(). Those routines work ok on OS/2
22828 ** desktops but not so well in embedded systems.
22829 */
22830
22831 /*
22832 ** Macros used to determine whether or not to use threads.
22833 */
22834 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
22835 # define SQLITE_OS2_THREADS 1
22836 #endif
22837
22838 /*
22839 ** Include code that is common to all os_*.c files
22840 */
22841 /************** Include os_common.h in the middle of os_os2.c ****************/
22842 /************** Begin file os_common.h ***************************************/
22843 /*
22844 ** 2004 May 22
22845 **
22846 ** The author disclaims copyright to this source code. In place of
22847 ** a legal notice, here is a blessing:
22848 **
22849 ** May you do good and not evil.
22850 ** May you find forgiveness for yourself and forgive others.
22851 ** May you share freely, never taking more than you give.
22852 **
22853 ******************************************************************************
22854 **
22855 ** This file contains macros and a little bit of code that is common to
22856 ** all of the platform-specific files (os_*.c) and is #included into those
22857 ** files.
22858 **
22859 ** This file should be #included by the os_*.c files only. It is not a
22860 ** general purpose header file.
22861 */
22862 #ifndef _OS_COMMON_H_
22863 #define _OS_COMMON_H_
22864
22865 /*
22866 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
22867 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
22868 ** switch. The following code should catch this problem at compile-time.
22869 */
22870 #ifdef MEMORY_DEBUG
22871 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22872 #endif
22873
22874 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
22875 # ifndef SQLITE_DEBUG_OS_TRACE
22876 # define SQLITE_DEBUG_OS_TRACE 0
22877 # endif
22878 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22879 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
22880 #else
22881 # define OSTRACE(X)
22882 #endif
22883
22884 /*
22885 ** Macros for performance tracing. Normally turned off. Only works
22886 ** on i486 hardware.
22887 */
22888 #ifdef SQLITE_PERFORMANCE_TRACE
22889
22890 /*
22891 ** hwtime.h contains inline assembler code for implementing
22892 ** high-performance timing routines.
22893 */
22894 /************** Include hwtime.h in the middle of os_common.h ****************/
22895 /************** Begin file hwtime.h ******************************************/
22896 /*
22897 ** 2008 May 27
22898 **
22899 ** The author disclaims copyright to this source code. In place of
22900 ** a legal notice, here is a blessing:
22901 **
22902 ** May you do good and not evil.
22903 ** May you find forgiveness for yourself and forgive others.
22904 ** May you share freely, never taking more than you give.
22905 **
22906 ******************************************************************************
22907 **
22908 ** This file contains inline asm code for retrieving "high-performance"
22909 ** counters for x86 class CPUs.
22910 */
22911 #ifndef _HWTIME_H_
22912 #define _HWTIME_H_
22913
22914 /*
22915 ** The following routine only works on pentium-class (or newer) processors.
22916 ** It uses the RDTSC opcode to read the cycle count value out of the
22917 ** processor and returns that value. This can be used for high-res
22918 ** profiling.
22919 */
22920 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
22921 (defined(i386) || defined(__i386__) || defined(_M_IX86))
22922
22923 #if defined(__GNUC__)
22924
22925 __inline__ sqlite_uint64 sqlite3Hwtime(void){
22926 unsigned int lo, hi;
22927 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
22928 return (sqlite_uint64)hi << 32 | lo;
22929 }
22930
22931 #elif defined(_MSC_VER)
22932
22933 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
22934 __asm {
22935 rdtsc
22936 ret ; return value at EDX:EAX
22937 }
22938 }
22939
22940 #endif
22941
22942 #elif (defined(__GNUC__) && defined(__x86_64__))
22943
22944 __inline__ sqlite_uint64 sqlite3Hwtime(void){
22945 unsigned long val;
22946 __asm__ __volatile__ ("rdtsc" : "=A" (val));
22947 return val;
22948 }
22949
22950 #elif (defined(__GNUC__) && defined(__ppc__))
22951
22952 __inline__ sqlite_uint64 sqlite3Hwtime(void){
22953 unsigned long long retval;
22954 unsigned long junk;
22955 __asm__ __volatile__ ("\n\
22956 1: mftbu %1\n\
22957 mftb %L0\n\
22958 mftbu %0\n\
22959 cmpw %0,%1\n\
22960 bne 1b"
22961 : "=r" (retval), "=r" (junk));
22962 return retval;
22963 }
22964
22965 #else
22966
22967 #error Need implementation of sqlite3Hwtime() for your platform.
22968
22969 /*
22970 ** To compile without implementing sqlite3Hwtime() for your platform,
22971 ** you can remove the above #error and use the following
22972 ** stub function. You will lose timing support for many
22973 ** of the debugging and testing utilities, but it should at
22974 ** least compile and run.
22975 */
22976 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
22977
22978 #endif
22979
22980 #endif /* !defined(_HWTIME_H_) */
22981
22982 /************** End of hwtime.h **********************************************/
22983 /************** Continuing where we left off in os_common.h ******************/
22984
22985 static sqlite_uint64 g_start;
22986 static sqlite_uint64 g_elapsed;
22987 #define TIMER_START g_start=sqlite3Hwtime()
22988 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
22989 #define TIMER_ELAPSED g_elapsed
22990 #else
22991 #define TIMER_START
22992 #define TIMER_END
22993 #define TIMER_ELAPSED ((sqlite_uint64)0)
22994 #endif
22995
22996 /*
22997 ** If we compile with the SQLITE_TEST macro set, then the following block
22998 ** of code will give us the ability to simulate a disk I/O error. This
22999 ** is used for testing the I/O recovery logic.
23000 */
23001 #ifdef SQLITE_TEST
23002 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
23003 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
23004 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
23005 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
23006 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
23007 SQLITE_API int sqlite3_diskfull_pending = 0;
23008 SQLITE_API int sqlite3_diskfull = 0;
23009 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
23010 #define SimulateIOError(CODE) \
23011 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
23012 || sqlite3_io_error_pending-- == 1 ) \
23013 { local_ioerr(); CODE; }
23014 static void local_ioerr(){
23015 IOTRACE(("IOERR\n"));
23016 sqlite3_io_error_hit++;
23017 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
23018 }
23019 #define SimulateDiskfullError(CODE) \
23020 if( sqlite3_diskfull_pending ){ \
23021 if( sqlite3_diskfull_pending == 1 ){ \
23022 local_ioerr(); \
23023 sqlite3_diskfull = 1; \
23024 sqlite3_io_error_hit = 1; \
23025 CODE; \
23026 }else{ \
23027 sqlite3_diskfull_pending--; \
23028 } \
23029 }
23030 #else
23031 #define SimulateIOErrorBenign(X)
23032 #define SimulateIOError(A)
23033 #define SimulateDiskfullError(A)
23034 #endif
23035
23036 /*
23037 ** When testing, keep a count of the number of open files.
23038 */
23039 #ifdef SQLITE_TEST
23040 SQLITE_API int sqlite3_open_file_count = 0;
23041 #define OpenCounter(X) sqlite3_open_file_count+=(X)
23042 #else
23043 #define OpenCounter(X)
23044 #endif
23045
23046 #endif /* !defined(_OS_COMMON_H_) */
23047
23048 /************** End of os_common.h *******************************************/
23049 /************** Continuing where we left off in os_os2.c *********************/
23050
23051 /* Forward references */
23052 typedef struct os2File os2File; /* The file structure */
23053 typedef struct os2ShmNode os2ShmNode; /* A shared descritive memory node */
23054 typedef struct os2ShmLink os2ShmLink; /* A connection to shared-memory */
23055
23056 /*
23057 ** The os2File structure is subclass of sqlite3_file specific for the OS/2
23058 ** protability layer.
23059 */
23060 struct os2File {
23061 const sqlite3_io_methods *pMethod; /* Always the first entry */
23062 HFILE h; /* Handle for accessing the file */
23063 int flags; /* Flags provided to os2Open() */
23064 int locktype; /* Type of lock currently held on this file */
23065 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
23066 char *zFullPathCp; /* Full path name of this file */
23067 os2ShmLink *pShmLink; /* Instance of shared memory on this file */
23068 };
23069
23070 #define LOCK_TIMEOUT 10L /* the default locking timeout */
23071
23072 /*
23073 ** Missing from some versions of the OS/2 toolkit -
23074 ** used to allocate from high memory if possible
23075 */
23076 #ifndef OBJ_ANY
23077 # define OBJ_ANY 0x00000400
23078 #endif
23079
23080 /*****************************************************************************
23081 ** The next group of routines implement the I/O methods specified
23082 ** by the sqlite3_io_methods object.
23083 ******************************************************************************/
23084
23085 /*
23086 ** Close a file.
23087 */
23088 static int os2Close( sqlite3_file *id ){
23089 APIRET rc;
23090 os2File *pFile = (os2File*)id;
23091
23092 assert( id!=0 );
23093 OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
23094
23095 rc = DosClose( pFile->h );
23096
23097 if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
23098 DosForceDelete( (PSZ)pFile->zFullPathCp );
23099
23100 free( pFile->zFullPathCp );
23101 pFile->zFullPathCp = NULL;
23102 pFile->locktype = NO_LOCK;
23103 pFile->h = (HFILE)-1;
23104 pFile->flags = 0;
23105
23106 OpenCounter( -1 );
23107 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23108 }
23109
23110 /*
23111 ** Read data from a file into a buffer. Return SQLITE_OK if all
23112 ** bytes were read successfully and SQLITE_IOERR if anything goes
23113 ** wrong.
23114 */
23115 static int os2Read(
23116 sqlite3_file *id, /* File to read from */
23117 void *pBuf, /* Write content into this buffer */
23118 int amt, /* Number of bytes to read */
23119 sqlite3_int64 offset /* Begin reading at this offset */
23120 ){
23121 ULONG fileLocation = 0L;
23122 ULONG got;
23123 os2File *pFile = (os2File*)id;
23124 assert( id!=0 );
23125 SimulateIOError( return SQLITE_IOERR_READ );
23126 OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
23127 if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
23128 return SQLITE_IOERR;
23129 }
23130 if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
23131 return SQLITE_IOERR_READ;
23132 }
23133 if( got == (ULONG)amt )
23134 return SQLITE_OK;
23135 else {
23136 /* Unread portions of the input buffer must be zero-filled */
23137 memset(&((char*)pBuf)[got], 0, amt-got);
23138 return SQLITE_IOERR_SHORT_READ;
23139 }
23140 }
23141
23142 /*
23143 ** Write data from a buffer into a file. Return SQLITE_OK on success
23144 ** or some other error code on failure.
23145 */
23146 static int os2Write(
23147 sqlite3_file *id, /* File to write into */
23148 const void *pBuf, /* The bytes to be written */
23149 int amt, /* Number of bytes to write */
23150 sqlite3_int64 offset /* Offset into the file to begin writing at */
23151 ){
23152 ULONG fileLocation = 0L;
23153 APIRET rc = NO_ERROR;
23154 ULONG wrote;
23155 os2File *pFile = (os2File*)id;
23156 assert( id!=0 );
23157 SimulateIOError( return SQLITE_IOERR_WRITE );
23158 SimulateDiskfullError( return SQLITE_FULL );
23159 OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
23160 if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
23161 return SQLITE_IOERR;
23162 }
23163 assert( amt>0 );
23164 while( amt > 0 &&
23165 ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
23166 wrote > 0
23167 ){
23168 amt -= wrote;
23169 pBuf = &((char*)pBuf)[wrote];
23170 }
23171
23172 return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
23173 }
23174
23175 /*
23176 ** Truncate an open file to a specified size
23177 */
23178 static int os2Truncate( sqlite3_file *id, i64 nByte ){
23179 APIRET rc;
23180 os2File *pFile = (os2File*)id;
23181 assert( id!=0 );
23182 OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
23183 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
23184
23185 /* If the user has configured a chunk-size for this file, truncate the
23186 ** file so that it consists of an integer number of chunks (i.e. the
23187 ** actual file size after the operation may be larger than the requested
23188 ** size).
23189 */
23190 if( pFile->szChunk ){
23191 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
23192 }
23193
23194 rc = DosSetFileSize( pFile->h, nByte );
23195 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
23196 }
23197
23198 #ifdef SQLITE_TEST
23199 /*
23200 ** Count the number of fullsyncs and normal syncs. This is used to test
23201 ** that syncs and fullsyncs are occuring at the right times.
23202 */
23203 SQLITE_API int sqlite3_sync_count = 0;
23204 SQLITE_API int sqlite3_fullsync_count = 0;
23205 #endif
23206
23207 /*
23208 ** Make sure all writes to a particular file are committed to disk.
23209 */
23210 static int os2Sync( sqlite3_file *id, int flags ){
23211 os2File *pFile = (os2File*)id;
23212 OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
23213 #ifdef SQLITE_TEST
23214 if( flags & SQLITE_SYNC_FULL){
23215 sqlite3_fullsync_count++;
23216 }
23217 sqlite3_sync_count++;
23218 #endif
23219 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
23220 ** no-op
23221 */
23222 #ifdef SQLITE_NO_SYNC
23223 UNUSED_PARAMETER(pFile);
23224 return SQLITE_OK;
23225 #else
23226 return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
23227 #endif
23228 }
23229
23230 /*
23231 ** Determine the current size of a file in bytes
23232 */
23233 static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
23234 APIRET rc = NO_ERROR;
23235 FILESTATUS3 fsts3FileInfo;
23236 memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
23237 assert( id!=0 );
23238 SimulateIOError( return SQLITE_IOERR_FSTAT );
23239 rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
23240 if( rc == NO_ERROR ){
23241 *pSize = fsts3FileInfo.cbFile;
23242 return SQLITE_OK;
23243 }else{
23244 return SQLITE_IOERR_FSTAT;
23245 }
23246 }
23247
23248 /*
23249 ** Acquire a reader lock.
23250 */
23251 static int getReadLock( os2File *pFile ){
23252 FILELOCK LockArea,
23253 UnlockArea;
23254 APIRET res;
23255 memset(&LockArea, 0, sizeof(LockArea));
23256 memset(&UnlockArea, 0, sizeof(UnlockArea));
23257 LockArea.lOffset = SHARED_FIRST;
23258 LockArea.lRange = SHARED_SIZE;
23259 UnlockArea.lOffset = 0L;
23260 UnlockArea.lRange = 0L;
23261 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
23262 OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
23263 return res;
23264 }
23265
23266 /*
23267 ** Undo a readlock
23268 */
23269 static int unlockReadLock( os2File *id ){
23270 FILELOCK LockArea,
23271 UnlockArea;
23272 APIRET res;
23273 memset(&LockArea, 0, sizeof(LockArea));
23274 memset(&UnlockArea, 0, sizeof(UnlockArea));
23275 LockArea.lOffset = 0L;
23276 LockArea.lRange = 0L;
23277 UnlockArea.lOffset = SHARED_FIRST;
23278 UnlockArea.lRange = SHARED_SIZE;
23279 res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
23280 OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
23281 return res;
23282 }
23283
23284 /*
23285 ** Lock the file with the lock specified by parameter locktype - one
23286 ** of the following:
23287 **
23288 ** (1) SHARED_LOCK
23289 ** (2) RESERVED_LOCK
23290 ** (3) PENDING_LOCK
23291 ** (4) EXCLUSIVE_LOCK
23292 **
23293 ** Sometimes when requesting one lock state, additional lock states
23294 ** are inserted in between. The locking might fail on one of the later
23295 ** transitions leaving the lock state different from what it started but
23296 ** still short of its goal. The following chart shows the allowed
23297 ** transitions and the inserted intermediate states:
23298 **
23299 ** UNLOCKED -> SHARED
23300 ** SHARED -> RESERVED
23301 ** SHARED -> (PENDING) -> EXCLUSIVE
23302 ** RESERVED -> (PENDING) -> EXCLUSIVE
23303 ** PENDING -> EXCLUSIVE
23304 **
23305 ** This routine will only increase a lock. The os2Unlock() routine
23306 ** erases all locks at once and returns us immediately to locking level 0.
23307 ** It is not possible to lower the locking level one step at a time. You
23308 ** must go straight to locking level 0.
23309 */
23310 static int os2Lock( sqlite3_file *id, int locktype ){
23311 int rc = SQLITE_OK; /* Return code from subroutines */
23312 APIRET res = NO_ERROR; /* Result of an OS/2 lock call */
23313 int newLocktype; /* Set pFile->locktype to this value before exiting */
23314 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
23315 FILELOCK LockArea,
23316 UnlockArea;
23317 os2File *pFile = (os2File*)id;
23318 memset(&LockArea, 0, sizeof(LockArea));
23319 memset(&UnlockArea, 0, sizeof(UnlockArea));
23320 assert( pFile!=0 );
23321 OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
23322
23323 /* If there is already a lock of this type or more restrictive on the
23324 ** os2File, do nothing. Don't use the end_lock: exit path, as
23325 ** sqlite3_mutex_enter() hasn't been called yet.
23326 */
23327 if( pFile->locktype>=locktype ){
23328 OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
23329 return SQLITE_OK;
23330 }
23331
23332 /* Make sure the locking sequence is correct
23333 */
23334 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
23335 assert( locktype!=PENDING_LOCK );
23336 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
23337
23338 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
23339 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
23340 ** the PENDING_LOCK byte is temporary.
23341 */
23342 newLocktype = pFile->locktype;
23343 if( pFile->locktype==NO_LOCK
23344 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
23345 ){
23346 LockArea.lOffset = PENDING_BYTE;
23347 LockArea.lRange = 1L;
23348 UnlockArea.lOffset = 0L;
23349 UnlockArea.lRange = 0L;
23350
23351 /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
23352 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
23353 if( res == NO_ERROR ){
23354 gotPendingLock = 1;
23355 OSTRACE(( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res ));
23356 }
23357 }
23358
23359 /* Acquire a shared lock
23360 */
23361 if( locktype==SHARED_LOCK && res == NO_ERROR ){
23362 assert( pFile->locktype==NO_LOCK );
23363 res = getReadLock(pFile);
23364 if( res == NO_ERROR ){
23365 newLocktype = SHARED_LOCK;
23366 }
23367 OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
23368 }
23369
23370 /* Acquire a RESERVED lock
23371 */
23372 if( locktype==RESERVED_LOCK && res == NO_ERROR ){
23373 assert( pFile->locktype==SHARED_LOCK );
23374 LockArea.lOffset = RESERVED_BYTE;
23375 LockArea.lRange = 1L;
23376 UnlockArea.lOffset = 0L;
23377 UnlockArea.lRange = 0L;
23378 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23379 if( res == NO_ERROR ){
23380 newLocktype = RESERVED_LOCK;
23381 }
23382 OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
23383 }
23384
23385 /* Acquire a PENDING lock
23386 */
23387 if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
23388 newLocktype = PENDING_LOCK;
23389 gotPendingLock = 0;
23390 OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
23391 pFile->h ));
23392 }
23393
23394 /* Acquire an EXCLUSIVE lock
23395 */
23396 if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
23397 assert( pFile->locktype>=SHARED_LOCK );
23398 res = unlockReadLock(pFile);
23399 OSTRACE(( "unreadlock = %d\n", res ));
23400 LockArea.lOffset = SHARED_FIRST;
23401 LockArea.lRange = SHARED_SIZE;
23402 UnlockArea.lOffset = 0L;
23403 UnlockArea.lRange = 0L;
23404 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23405 if( res == NO_ERROR ){
23406 newLocktype = EXCLUSIVE_LOCK;
23407 }else{
23408 OSTRACE(( "OS/2 error-code = %d\n", res ));
23409 getReadLock(pFile);
23410 }
23411 OSTRACE(( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res ));
23412 }
23413
23414 /* If we are holding a PENDING lock that ought to be released, then
23415 ** release it now.
23416 */
23417 if( gotPendingLock && locktype==SHARED_LOCK ){
23418 int r;
23419 LockArea.lOffset = 0L;
23420 LockArea.lRange = 0L;
23421 UnlockArea.lOffset = PENDING_BYTE;
23422 UnlockArea.lRange = 1L;
23423 r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23424 OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
23425 }
23426
23427 /* Update the state of the lock has held in the file descriptor then
23428 ** return the appropriate result code.
23429 */
23430 if( res == NO_ERROR ){
23431 rc = SQLITE_OK;
23432 }else{
23433 OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
23434 locktype, newLocktype ));
23435 rc = SQLITE_BUSY;
23436 }
23437 pFile->locktype = newLocktype;
23438 OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
23439 return rc;
23440 }
23441
23442 /*
23443 ** This routine checks if there is a RESERVED lock held on the specified
23444 ** file by this or any other process. If such a lock is held, return
23445 ** non-zero, otherwise zero.
23446 */
23447 static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
23448 int r = 0;
23449 os2File *pFile = (os2File*)id;
23450 assert( pFile!=0 );
23451 if( pFile->locktype>=RESERVED_LOCK ){
23452 r = 1;
23453 OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
23454 }else{
23455 FILELOCK LockArea,
23456 UnlockArea;
23457 APIRET rc = NO_ERROR;
23458 memset(&LockArea, 0, sizeof(LockArea));
23459 memset(&UnlockArea, 0, sizeof(UnlockArea));
23460 LockArea.lOffset = RESERVED_BYTE;
23461 LockArea.lRange = 1L;
23462 UnlockArea.lOffset = 0L;
23463 UnlockArea.lRange = 0L;
23464 rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23465 OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
23466 if( rc == NO_ERROR ){
23467 APIRET rcu = NO_ERROR; /* return code for unlocking */
23468 LockArea.lOffset = 0L;
23469 LockArea.lRange = 0L;
23470 UnlockArea.lOffset = RESERVED_BYTE;
23471 UnlockArea.lRange = 1L;
23472 rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23473 OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
23474 }
23475 r = !(rc == NO_ERROR);
23476 OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
23477 }
23478 *pOut = r;
23479 return SQLITE_OK;
23480 }
23481
23482 /*
23483 ** Lower the locking level on file descriptor id to locktype. locktype
23484 ** must be either NO_LOCK or SHARED_LOCK.
23485 **
23486 ** If the locking level of the file descriptor is already at or below
23487 ** the requested locking level, this routine is a no-op.
23488 **
23489 ** It is not possible for this routine to fail if the second argument
23490 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
23491 ** might return SQLITE_IOERR;
23492 */
23493 static int os2Unlock( sqlite3_file *id, int locktype ){
23494 int type;
23495 os2File *pFile = (os2File*)id;
23496 APIRET rc = SQLITE_OK;
23497 APIRET res = NO_ERROR;
23498 FILELOCK LockArea,
23499 UnlockArea;
23500 memset(&LockArea, 0, sizeof(LockArea));
23501 memset(&UnlockArea, 0, sizeof(UnlockArea));
23502 assert( pFile!=0 );
23503 assert( locktype<=SHARED_LOCK );
23504 OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
23505 type = pFile->locktype;
23506 if( type>=EXCLUSIVE_LOCK ){
23507 LockArea.lOffset = 0L;
23508 LockArea.lRange = 0L;
23509 UnlockArea.lOffset = SHARED_FIRST;
23510 UnlockArea.lRange = SHARED_SIZE;
23511 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23512 OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
23513 if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
23514 /* This should never happen. We should always be able to
23515 ** reacquire the read lock */
23516 OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
23517 rc = SQLITE_IOERR_UNLOCK;
23518 }
23519 }
23520 if( type>=RESERVED_LOCK ){
23521 LockArea.lOffset = 0L;
23522 LockArea.lRange = 0L;
23523 UnlockArea.lOffset = RESERVED_BYTE;
23524 UnlockArea.lRange = 1L;
23525 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23526 OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
23527 }
23528 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
23529 res = unlockReadLock(pFile);
23530 OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
23531 pFile->h, type, locktype, res ));
23532 }
23533 if( type>=PENDING_LOCK ){
23534 LockArea.lOffset = 0L;
23535 LockArea.lRange = 0L;
23536 UnlockArea.lOffset = PENDING_BYTE;
23537 UnlockArea.lRange = 1L;
23538 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
23539 OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
23540 }
23541 pFile->locktype = locktype;
23542 OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
23543 return rc;
23544 }
23545
23546 /*
23547 ** Control and query of the open file handle.
23548 */
23549 static int os2FileControl(sqlite3_file *id, int op, void *pArg){
23550 switch( op ){
23551 case SQLITE_FCNTL_LOCKSTATE: {
23552 *(int*)pArg = ((os2File*)id)->locktype;
23553 OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
23554 ((os2File*)id)->h, ((os2File*)id)->locktype ));
23555 return SQLITE_OK;
23556 }
23557 case SQLITE_FCNTL_CHUNK_SIZE: {
23558 ((os2File*)id)->szChunk = *(int*)pArg;
23559 return SQLITE_OK;
23560 }
23561 case SQLITE_FCNTL_SIZE_HINT: {
23562 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
23563 SimulateIOErrorBenign(1);
23564 os2Truncate(id, sz);
23565 SimulateIOErrorBenign(0);
23566 return SQLITE_OK;
23567 }
23568 case SQLITE_FCNTL_SYNC_OMITTED: {
23569 return SQLITE_OK;
23570 }
23571 }
23572 return SQLITE_NOTFOUND;
23573 }
23574
23575 /*
23576 ** Return the sector size in bytes of the underlying block device for
23577 ** the specified file. This is almost always 512 bytes, but may be
23578 ** larger for some devices.
23579 **
23580 ** SQLite code assumes this function cannot fail. It also assumes that
23581 ** if two files are created in the same file-system directory (i.e.
23582 ** a database and its journal file) that the sector size will be the
23583 ** same for both.
23584 */
23585 static int os2SectorSize(sqlite3_file *id){
23586 UNUSED_PARAMETER(id);
23587 return SQLITE_DEFAULT_SECTOR_SIZE;
23588 }
23589
23590 /*
23591 ** Return a vector of device characteristics.
23592 */
23593 static int os2DeviceCharacteristics(sqlite3_file *id){
23594 UNUSED_PARAMETER(id);
23595 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
23596 }
23597
23598
23599 /*
23600 ** Character set conversion objects used by conversion routines.
23601 */
23602 static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
23603 static UconvObject uclCp = NULL; /* convert between local codepage and UCS-2 */
23604
23605 /*
23606 ** Helper function to initialize the conversion objects from and to UTF-8.
23607 */
23608 static void initUconvObjects( void ){
23609 if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
23610 ucUtf8 = NULL;
23611 if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
23612 uclCp = NULL;
23613 }
23614
23615 /*
23616 ** Helper function to free the conversion objects from and to UTF-8.
23617 */
23618 static void freeUconvObjects( void ){
23619 if ( ucUtf8 )
23620 UniFreeUconvObject( ucUtf8 );
23621 if ( uclCp )
23622 UniFreeUconvObject( uclCp );
23623 ucUtf8 = NULL;
23624 uclCp = NULL;
23625 }
23626
23627 /*
23628 ** Helper function to convert UTF-8 filenames to local OS/2 codepage.
23629 ** The two-step process: first convert the incoming UTF-8 string
23630 ** into UCS-2 and then from UCS-2 to the current codepage.
23631 ** The returned char pointer has to be freed.
23632 */
23633 static char *convertUtf8PathToCp( const char *in ){
23634 UniChar tempPath[CCHMAXPATH];
23635 char *out = (char *)calloc( CCHMAXPATH, 1 );
23636
23637 if( !out )
23638 return NULL;
23639
23640 if( !ucUtf8 || !uclCp )
23641 initUconvObjects();
23642
23643 /* determine string for the conversion of UTF-8 which is CP1208 */
23644 if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23645 return out; /* if conversion fails, return the empty string */
23646
23647 /* conversion for current codepage which can be used for paths */
23648 UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
23649
23650 return out;
23651 }
23652
23653 /*
23654 ** Helper function to convert filenames from local codepage to UTF-8.
23655 ** The two-step process: first convert the incoming codepage-specific
23656 ** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
23657 ** The returned char pointer has to be freed.
23658 **
23659 ** This function is non-static to be able to use this in shell.c and
23660 ** similar applications that take command line arguments.
23661 */
23662 char *convertCpPathToUtf8( const char *in ){
23663 UniChar tempPath[CCHMAXPATH];
23664 char *out = (char *)calloc( CCHMAXPATH, 1 );
23665
23666 if( !out )
23667 return NULL;
23668
23669 if( !ucUtf8 || !uclCp )
23670 initUconvObjects();
23671
23672 /* conversion for current codepage which can be used for paths */
23673 if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
23674 return out; /* if conversion fails, return the empty string */
23675
23676 /* determine string for the conversion of UTF-8 which is CP1208 */
23677 UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
23678
23679 return out;
23680 }
23681
23682
23683 #ifndef SQLITE_OMIT_WAL
23684
23685 /*
23686 ** Use main database file for interprocess locking. If un-defined
23687 ** a separate file is created for this purpose. The file will be
23688 ** used only to set file locks. There will be no data written to it.
23689 */
23690 #define SQLITE_OS2_NO_WAL_LOCK_FILE
23691
23692 #if 0
23693 static void _ERR_TRACE( const char *fmt, ... ) {
23694 va_list ap;
23695 va_start(ap, fmt);
23696 vfprintf(stderr, fmt, ap);
23697 fflush(stderr);
23698 }
23699 #define ERR_TRACE(rc, msg) \
23700 if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
23701 #else
23702 #define ERR_TRACE(rc, msg)
23703 #endif
23704
23705 /*
23706 ** Helper functions to obtain and relinquish the global mutex. The
23707 ** global mutex is used to protect os2ShmNodeList.
23708 **
23709 ** Function os2ShmMutexHeld() is used to assert() that the global mutex
23710 ** is held when required. This function is only used as part of assert()
23711 ** statements. e.g.
23712 **
23713 ** os2ShmEnterMutex()
23714 ** assert( os2ShmMutexHeld() );
23715 ** os2ShmLeaveMutex()
23716 */
23717 static void os2ShmEnterMutex(void){
23718 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23719 }
23720 static void os2ShmLeaveMutex(void){
23721 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23722 }
23723 #ifdef SQLITE_DEBUG
23724 static int os2ShmMutexHeld(void) {
23725 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23726 }
23727 int GetCurrentProcessId(void) {
23728 PPIB pib;
23729 DosGetInfoBlocks(NULL, &pib);
23730 return (int)pib->pib_ulpid;
23731 }
23732 #endif
23733
23734 /*
23735 ** Object used to represent a the shared memory area for a single log file.
23736 ** When multiple threads all reference the same log-summary, each thread has
23737 ** its own os2File object, but they all point to a single instance of this
23738 ** object. In other words, each log-summary is opened only once per process.
23739 **
23740 ** os2ShmMutexHeld() must be true when creating or destroying
23741 ** this object or while reading or writing the following fields:
23742 **
23743 ** nRef
23744 ** pNext
23745 **
23746 ** The following fields are read-only after the object is created:
23747 **
23748 ** szRegion
23749 ** hLockFile
23750 ** shmBaseName
23751 **
23752 ** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
23753 ** os2ShmMutexHeld() is true when reading or writing any other field
23754 ** in this structure.
23755 **
23756 */
23757 struct os2ShmNode {
23758 sqlite3_mutex *mutex; /* Mutex to access this object */
23759 os2ShmNode *pNext; /* Next in list of all os2ShmNode objects */
23760
23761 int szRegion; /* Size of shared-memory regions */
23762
23763 int nRegion; /* Size of array apRegion */
23764 void **apRegion; /* Array of pointers to shared-memory regions */
23765
23766 int nRef; /* Number of os2ShmLink objects pointing to this */
23767 os2ShmLink *pFirst; /* First os2ShmLink object pointing to this */
23768
23769 HFILE hLockFile; /* File used for inter-process memory locking */
23770 char shmBaseName[1]; /* Name of the memory object !!! must last !!! */
23771 };
23772
23773
23774 /*
23775 ** Structure used internally by this VFS to record the state of an
23776 ** open shared memory connection.
23777 **
23778 ** The following fields are initialized when this object is created and
23779 ** are read-only thereafter:
23780 **
23781 ** os2Shm.pShmNode
23782 ** os2Shm.id
23783 **
23784 ** All other fields are read/write. The os2Shm.pShmNode->mutex must be held
23785 ** while accessing any read/write fields.
23786 */
23787 struct os2ShmLink {
23788 os2ShmNode *pShmNode; /* The underlying os2ShmNode object */
23789 os2ShmLink *pNext; /* Next os2Shm with the same os2ShmNode */
23790 u32 sharedMask; /* Mask of shared locks held */
23791 u32 exclMask; /* Mask of exclusive locks held */
23792 #ifdef SQLITE_DEBUG
23793 u8 id; /* Id of this connection with its os2ShmNode */
23794 #endif
23795 };
23796
23797
23798 /*
23799 ** A global list of all os2ShmNode objects.
23800 **
23801 ** The os2ShmMutexHeld() must be true while reading or writing this list.
23802 */
23803 static os2ShmNode *os2ShmNodeList = NULL;
23804
23805 /*
23806 ** Constants used for locking
23807 */
23808 #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
23809 #define OS2_SHM_BASE (PENDING_BYTE + 0x10000) /* first lock byte */
23810 #else
23811 #define OS2_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
23812 #endif
23813
23814 #define OS2_SHM_DMS (OS2_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
23815
23816 /*
23817 ** Apply advisory locks for all n bytes beginning at ofst.
23818 */
23819 #define _SHM_UNLCK 1 /* no lock */
23820 #define _SHM_RDLCK 2 /* shared lock, no wait */
23821 #define _SHM_WRLCK 3 /* exlusive lock, no wait */
23822 #define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
23823 static int os2ShmSystemLock(
23824 os2ShmNode *pNode, /* Apply locks to this open shared-memory segment */
23825 int lockType, /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
23826 int ofst, /* Offset to first byte to be locked/unlocked */
23827 int nByte /* Number of bytes to lock or unlock */
23828 ){
23829 APIRET rc;
23830 FILELOCK area;
23831 ULONG mode, timeout;
23832
23833 /* Access to the os2ShmNode object is serialized by the caller */
23834 assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
23835
23836 mode = 1; /* shared lock */
23837 timeout = 0; /* no wait */
23838 area.lOffset = ofst;
23839 area.lRange = nByte;
23840
23841 switch( lockType ) {
23842 case _SHM_WRLCK_WAIT:
23843 timeout = (ULONG)-1; /* wait forever */
23844 case _SHM_WRLCK:
23845 mode = 0; /* exclusive lock */
23846 case _SHM_RDLCK:
23847 rc = DosSetFileLocks(pNode->hLockFile,
23848 NULL, &area, timeout, mode);
23849 break;
23850 /* case _SHM_UNLCK: */
23851 default:
23852 rc = DosSetFileLocks(pNode->hLockFile,
23853 &area, NULL, 0, 0);
23854 break;
23855 }
23856
23857 OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
23858 pNode->hLockFile,
23859 rc==SQLITE_OK ? "ok" : "failed",
23860 lockType==_SHM_UNLCK ? "Unlock" : "Lock",
23861 rc));
23862
23863 ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
23864
23865 return ( rc == 0 ) ? SQLITE_OK : SQLITE_BUSY;
23866 }
23867
23868 /*
23869 ** Find an os2ShmNode in global list or allocate a new one, if not found.
23870 **
23871 ** This is not a VFS shared-memory method; it is a utility function called
23872 ** by VFS shared-memory methods.
23873 */
23874 static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
23875 os2ShmLink *pLink;
23876 os2ShmNode *pNode;
23877 int cbShmName, rc = SQLITE_OK;
23878 char shmName[CCHMAXPATH + 30];
23879 #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
23880 ULONG action;
23881 #endif
23882
23883 /* We need some additional space at the end to append the region number */
23884 cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
23885 if( cbShmName >= CCHMAXPATH-8 )
23886 return SQLITE_IOERR_SHMOPEN;
23887
23888 /* Replace colon in file name to form a valid shared memory name */
23889 shmName[10+1] = '!';
23890
23891 /* Allocate link object (we free it later in case of failure) */
23892 pLink = sqlite3_malloc( sizeof(*pLink) );
23893 if( !pLink )
23894 return SQLITE_NOMEM;
23895
23896 /* Access node list */
23897 os2ShmEnterMutex();
23898
23899 /* Find node by it's shared memory base name */
23900 for( pNode = os2ShmNodeList;
23901 pNode && stricmp(shmName, pNode->shmBaseName) != 0;
23902 pNode = pNode->pNext ) ;
23903
23904 /* Not found: allocate a new node */
23905 if( !pNode ) {
23906 pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
23907 if( pNode ) {
23908 memset(pNode, 0, sizeof(*pNode) );
23909 pNode->szRegion = szRegion;
23910 pNode->hLockFile = (HFILE)-1;
23911 strcpy(pNode->shmBaseName, shmName);
23912
23913 #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
23914 if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
23915 #else
23916 sprintf(shmName, "%s-lck", fd->zFullPathCp);
23917 if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL,
23918 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
23919 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE |
23920 OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
23921 NULL) != 0 ) {
23922 #endif
23923 sqlite3_free(pNode);
23924 rc = SQLITE_IOERR;
23925 } else {
23926 pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
23927 if( !pNode->mutex ) {
23928 sqlite3_free(pNode);
23929 rc = SQLITE_NOMEM;
23930 }
23931 }
23932 } else {
23933 rc = SQLITE_NOMEM;
23934 }
23935
23936 if( rc == SQLITE_OK ) {
23937 pNode->pNext = os2ShmNodeList;
23938 os2ShmNodeList = pNode;
23939 } else {
23940 pNode = NULL;
23941 }
23942 } else if( pNode->szRegion != szRegion ) {
23943 rc = SQLITE_IOERR_SHMSIZE;
23944 pNode = NULL;
23945 }
23946
23947 if( pNode ) {
23948 sqlite3_mutex_enter(pNode->mutex);
23949
23950 memset(pLink, 0, sizeof(*pLink));
23951
23952 pLink->pShmNode = pNode;
23953 pLink->pNext = pNode->pFirst;
23954 pNode->pFirst = pLink;
23955 pNode->nRef++;
23956
23957 fd->pShmLink = pLink;
23958
23959 sqlite3_mutex_leave(pNode->mutex);
23960
23961 } else {
23962 /* Error occured. Free our link object. */
23963 sqlite3_free(pLink);
23964 }
23965
23966 os2ShmLeaveMutex();
23967
23968 ERR_TRACE(rc, ("os2OpenSharedMemory: %d %s\n", rc, fd->zFullPathCp))
23969
23970 return rc;
23971 }
23972
23973 /*
23974 ** Purge the os2ShmNodeList list of all entries with nRef==0.
23975 **
23976 ** This is not a VFS shared-memory method; it is a utility function called
23977 ** by VFS shared-memory methods.
23978 */
23979 static void os2PurgeShmNodes( int deleteFlag ) {
23980 os2ShmNode *pNode;
23981 os2ShmNode **ppNode;
23982
23983 os2ShmEnterMutex();
23984
23985 ppNode = &os2ShmNodeList;
23986
23987 while( *ppNode ) {
23988 pNode = *ppNode;
23989
23990 if( pNode->nRef == 0 ) {
23991 *ppNode = pNode->pNext;
23992
23993 if( pNode->apRegion ) {
23994 /* Prevent other processes from resizing the shared memory */
23995 os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
23996
23997 while( pNode->nRegion-- ) {
23998 #ifdef SQLITE_DEBUG
23999 int rc =
24000 #endif
24001 DosFreeMem(pNode->apRegion[pNode->nRegion]);
24002
24003 OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
24004 (int)GetCurrentProcessId(), pNode->nRegion,
24005 rc == 0 ? "ok" : "failed"));
24006 }
24007
24008 /* Allow other processes to resize the shared memory */
24009 os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
24010
24011 sqlite3_free(pNode->apRegion);
24012 }
24013
24014 DosClose(pNode->hLockFile);
24015
24016 #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
24017 if( deleteFlag ) {
24018 char fileName[CCHMAXPATH];
24019 /* Skip "\\SHAREMEM\\" */
24020 sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
24021 /* restore colon */
24022 fileName[1] = ':';
24023
24024 DosForceDelete(fileName);
24025 }
24026 #endif
24027
24028 sqlite3_mutex_free(pNode->mutex);
24029
24030 sqlite3_free(pNode);
24031
24032 } else {
24033 ppNode = &pNode->pNext;
24034 }
24035 }
24036
24037 os2ShmLeaveMutex();
24038 }
24039
24040 /*
24041 ** This function is called to obtain a pointer to region iRegion of the
24042 ** shared-memory associated with the database file id. Shared-memory regions
24043 ** are numbered starting from zero. Each shared-memory region is szRegion
24044 ** bytes in size.
24045 **
24046 ** If an error occurs, an error code is returned and *pp is set to NULL.
24047 **
24048 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
24049 ** region has not been allocated (by any client, including one running in a
24050 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
24051 ** bExtend is non-zero and the requested shared-memory region has not yet
24052 ** been allocated, it is allocated by this function.
24053 **
24054 ** If the shared-memory region has already been allocated or is allocated by
24055 ** this call as described above, then it is mapped into this processes
24056 ** address space (if it is not already), *pp is set to point to the mapped
24057 ** memory and SQLITE_OK returned.
24058 */
24059 static int os2ShmMap(
24060 sqlite3_file *id, /* Handle open on database file */
24061 int iRegion, /* Region to retrieve */
24062 int szRegion, /* Size of regions */
24063 int bExtend, /* True to extend block if necessary */
24064 void volatile **pp /* OUT: Mapped memory */
24065 ){
24066 PVOID pvTemp;
24067 void **apRegion;
24068 os2ShmNode *pNode;
24069 int n, rc = SQLITE_OK;
24070 char shmName[CCHMAXPATH];
24071 os2File *pFile = (os2File*)id;
24072
24073 *pp = NULL;
24074
24075 if( !pFile->pShmLink )
24076 rc = os2OpenSharedMemory( pFile, szRegion );
24077
24078 if( rc == SQLITE_OK ) {
24079 pNode = pFile->pShmLink->pShmNode ;
24080
24081 sqlite3_mutex_enter(pNode->mutex);
24082
24083 assert( szRegion==pNode->szRegion );
24084
24085 /* Unmapped region ? */
24086 if( iRegion >= pNode->nRegion ) {
24087 /* Prevent other processes from resizing the shared memory */
24088 os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
24089
24090 apRegion = sqlite3_realloc(
24091 pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
24092
24093 if( apRegion ) {
24094 pNode->apRegion = apRegion;
24095
24096 while( pNode->nRegion <= iRegion ) {
24097 sprintf(shmName, "%s-%u",
24098 pNode->shmBaseName, pNode->nRegion);
24099
24100 if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName,
24101 PAG_READ | PAG_WRITE) != NO_ERROR ) {
24102 if( !bExtend )
24103 break;
24104
24105 if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
24106 PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR &&
24107 DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
24108 PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) {
24109 rc = SQLITE_NOMEM;
24110 break;
24111 }
24112 }
24113
24114 apRegion[pNode->nRegion++] = pvTemp;
24115 }
24116
24117 /* zero out remaining entries */
24118 for( n = pNode->nRegion; n <= iRegion; n++ )
24119 pNode->apRegion[n] = NULL;
24120
24121 /* Return this region (maybe zero) */
24122 *pp = pNode->apRegion[iRegion];
24123 } else {
24124 rc = SQLITE_NOMEM;
24125 }
24126
24127 /* Allow other processes to resize the shared memory */
24128 os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
24129
24130 } else {
24131 /* Region has been mapped previously */
24132 *pp = pNode->apRegion[iRegion];
24133 }
24134
24135 sqlite3_mutex_leave(pNode->mutex);
24136 }
24137
24138 ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n",
24139 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
24140
24141 return rc;
24142 }
24143
24144 /*
24145 ** Close a connection to shared-memory. Delete the underlying
24146 ** storage if deleteFlag is true.
24147 **
24148 ** If there is no shared memory associated with the connection then this
24149 ** routine is a harmless no-op.
24150 */
24151 static int os2ShmUnmap(
24152 sqlite3_file *id, /* The underlying database file */
24153 int deleteFlag /* Delete shared-memory if true */
24154 ){
24155 os2File *pFile = (os2File*)id;
24156 os2ShmLink *pLink = pFile->pShmLink;
24157
24158 if( pLink ) {
24159 int nRef = -1;
24160 os2ShmLink **ppLink;
24161 os2ShmNode *pNode = pLink->pShmNode;
24162
24163 sqlite3_mutex_enter(pNode->mutex);
24164
24165 for( ppLink = &pNode->pFirst;
24166 *ppLink && *ppLink != pLink;
24167 ppLink = &(*ppLink)->pNext ) ;
24168
24169 assert(*ppLink);
24170
24171 if( *ppLink ) {
24172 *ppLink = pLink->pNext;
24173 nRef = --pNode->nRef;
24174 } else {
24175 ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n",
24176 pNode->shmBaseName))
24177 }
24178
24179 pFile->pShmLink = NULL;
24180 sqlite3_free(pLink);
24181
24182 sqlite3_mutex_leave(pNode->mutex);
24183
24184 if( nRef == 0 )
24185 os2PurgeShmNodes( deleteFlag );
24186 }
24187
24188 return SQLITE_OK;
24189 }
24190
24191 /*
24192 ** Change the lock state for a shared-memory segment.
24193 **
24194 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
24195 ** different here than in posix. In xShmLock(), one can go from unlocked
24196 ** to shared and back or from unlocked to exclusive and back. But one may
24197 ** not go from shared to exclusive or from exclusive to shared.
24198 */
24199 static int os2ShmLock(
24200 sqlite3_file *id, /* Database file holding the shared memory */
24201 int ofst, /* First lock to acquire or release */
24202 int n, /* Number of locks to acquire or release */
24203 int flags /* What to do with the lock */
24204 ){
24205 u32 mask; /* Mask of locks to take or release */
24206 int rc = SQLITE_OK; /* Result code */
24207 os2File *pFile = (os2File*)id;
24208 os2ShmLink *p = pFile->pShmLink; /* The shared memory being locked */
24209 os2ShmLink *pX; /* For looping over all siblings */
24210 os2ShmNode *pShmNode = p->pShmNode; /* Our node */
24211
24212 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
24213 assert( n>=1 );
24214 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
24215 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
24216 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
24217 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
24218 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
24219
24220 mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
24221 assert( n>1 || mask==(1<<ofst) );
24222
24223
24224 sqlite3_mutex_enter(pShmNode->mutex);
24225
24226 if( flags & SQLITE_SHM_UNLOCK ){
24227 u32 allMask = 0; /* Mask of locks held by siblings */
24228
24229 /* See if any siblings hold this same lock */
24230 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
24231 if( pX==p ) continue;
24232 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
24233 allMask |= pX->sharedMask;
24234 }
24235
24236 /* Unlock the system-level locks */
24237 if( (mask & allMask)==0 ){
24238 rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
24239 }else{
24240 rc = SQLITE_OK;
24241 }
24242
24243 /* Undo the local locks */
24244 if( rc==SQLITE_OK ){
24245 p->exclMask &= ~mask;
24246 p->sharedMask &= ~mask;
24247 }
24248 }else if( flags & SQLITE_SHM_SHARED ){
24249 u32 allShared = 0; /* Union of locks held by connections other than "p" */
24250
24251 /* Find out which shared locks are already held by sibling connections.
24252 ** If any sibling already holds an exclusive lock, go ahead and return
24253 ** SQLITE_BUSY.
24254 */
24255 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
24256 if( (pX->exclMask & mask)!=0 ){
24257 rc = SQLITE_BUSY;
24258 break;
24259 }
24260 allShared |= pX->sharedMask;
24261 }
24262
24263 /* Get shared locks at the system level, if necessary */
24264 if( rc==SQLITE_OK ){
24265 if( (allShared & mask)==0 ){
24266 rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
24267 }else{
24268 rc = SQLITE_OK;
24269 }
24270 }
24271
24272 /* Get the local shared locks */
24273 if( rc==SQLITE_OK ){
24274 p->sharedMask |= mask;
24275 }
24276 }else{
24277 /* Make sure no sibling connections hold locks that will block this
24278 ** lock. If any do, return SQLITE_BUSY right away.
24279 */
24280 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
24281 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
24282 rc = SQLITE_BUSY;
24283 break;
24284 }
24285 }
24286
24287 /* Get the exclusive locks at the system level. Then if successful
24288 ** also mark the local connection as being locked.
24289 */
24290 if( rc==SQLITE_OK ){
24291 rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
24292 if( rc==SQLITE_OK ){
24293 assert( (p->sharedMask & mask)==0 );
24294 p->exclMask |= mask;
24295 }
24296 }
24297 }
24298
24299 sqlite3_mutex_leave(pShmNode->mutex);
24300
24301 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
24302 p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
24303 rc ? "failed" : "ok"));
24304
24305 ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n",
24306 ofst, n, flags, rc))
24307
24308 return rc;
24309 }
24310
24311 /*
24312 ** Implement a memory barrier or memory fence on shared memory.
24313 **
24314 ** All loads and stores begun before the barrier must complete before
24315 ** any load or store begun after the barrier.
24316 */
24317 static void os2ShmBarrier(
24318 sqlite3_file *id /* Database file holding the shared memory */
24319 ){
24320 UNUSED_PARAMETER(id);
24321 os2ShmEnterMutex();
24322 os2ShmLeaveMutex();
24323 }
24324
24325 #else
24326 # define os2ShmMap 0
24327 # define os2ShmLock 0
24328 # define os2ShmBarrier 0
24329 # define os2ShmUnmap 0
24330 #endif /* #ifndef SQLITE_OMIT_WAL */
24331
24332
24333 /*
24334 ** This vector defines all the methods that can operate on an
24335 ** sqlite3_file for os2.
24336 */
24337 static const sqlite3_io_methods os2IoMethod = {
24338 2, /* iVersion */
24339 os2Close, /* xClose */
24340 os2Read, /* xRead */
24341 os2Write, /* xWrite */
24342 os2Truncate, /* xTruncate */
24343 os2Sync, /* xSync */
24344 os2FileSize, /* xFileSize */
24345 os2Lock, /* xLock */
24346 os2Unlock, /* xUnlock */
24347 os2CheckReservedLock, /* xCheckReservedLock */
24348 os2FileControl, /* xFileControl */
24349 os2SectorSize, /* xSectorSize */
24350 os2DeviceCharacteristics, /* xDeviceCharacteristics */
24351 os2ShmMap, /* xShmMap */
24352 os2ShmLock, /* xShmLock */
24353 os2ShmBarrier, /* xShmBarrier */
24354 os2ShmUnmap /* xShmUnmap */
24355 };
24356
24357
24358 /***************************************************************************
24359 ** Here ends the I/O methods that form the sqlite3_io_methods object.
24360 **
24361 ** The next block of code implements the VFS methods.
24362 ****************************************************************************/
24363
24364 /*
24365 ** Create a temporary file name in zBuf. zBuf must be big enough to
24366 ** hold at pVfs->mxPathname characters.
24367 */
24368 static int getTempname(int nBuf, char *zBuf ){
24369 static const char zChars[] =
24370 "abcdefghijklmnopqrstuvwxyz"
24371 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24372 "0123456789";
24373 int i, j;
24374 PSZ zTempPathCp;
24375 char zTempPath[CCHMAXPATH];
24376 ULONG ulDriveNum, ulDriveMap;
24377
24378 /* It's odd to simulate an io-error here, but really this is just
24379 ** using the io-error infrastructure to test that SQLite handles this
24380 ** function failing.
24381 */
24382 SimulateIOError( return SQLITE_IOERR );
24383
24384 if( sqlite3_temp_directory ) {
24385 sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
24386 } else if( DosScanEnv( (PSZ)"TEMP", &zTempPathCp ) == NO_ERROR ||
24387 DosScanEnv( (PSZ)"TMP", &zTempPathCp ) == NO_ERROR ||
24388 DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
24389 char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
24390 sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
24391 free( zTempPathUTF );
24392 } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
24393 zTempPath[0] = (char)('A' + ulDriveNum - 1);
24394 zTempPath[1] = ':';
24395 zTempPath[2] = '\0';
24396 } else {
24397 zTempPath[0] = '\0';
24398 }
24399
24400 /* Strip off a trailing slashes or backslashes, otherwise we would get *
24401 * multiple (back)slashes which causes DosOpen() to fail. *
24402 * Trailing spaces are not allowed, either. */
24403 j = sqlite3Strlen30(zTempPath);
24404 while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ||
24405 zTempPath[j-1] == ' ' ) ){
24406 j--;
24407 }
24408 zTempPath[j] = '\0';
24409
24410 /* We use 20 bytes to randomize the name */
24411 sqlite3_snprintf(nBuf-22, zBuf,
24412 "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
24413 j = sqlite3Strlen30(zBuf);
24414 sqlite3_randomness( 20, &zBuf[j] );
24415 for( i = 0; i < 20; i++, j++ ){
24416 zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
24417 }
24418 zBuf[j] = 0;
24419
24420 OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
24421 return SQLITE_OK;
24422 }
24423
24424
24425 /*
24426 ** Turn a relative pathname into a full pathname. Write the full
24427 ** pathname into zFull[]. zFull[] will be at least pVfs->mxPathname
24428 ** bytes in size.
24429 */
24430 static int os2FullPathname(
24431 sqlite3_vfs *pVfs, /* Pointer to vfs object */
24432 const char *zRelative, /* Possibly relative input path */
24433 int nFull, /* Size of output buffer in bytes */
24434 char *zFull /* Output buffer */
24435 ){
24436 char *zRelativeCp = convertUtf8PathToCp( zRelative );
24437 char zFullCp[CCHMAXPATH] = "\0";
24438 char *zFullUTF;
24439 APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME,
24440 zFullCp, CCHMAXPATH );
24441 free( zRelativeCp );
24442 zFullUTF = convertCpPathToUtf8( zFullCp );
24443 sqlite3_snprintf( nFull, zFull, zFullUTF );
24444 free( zFullUTF );
24445 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
24446 }
24447
24448
24449 /*
24450 ** Open a file.
24451 */
24452 static int os2Open(
24453 sqlite3_vfs *pVfs, /* Not used */
24454 const char *zName, /* Name of the file (UTF-8) */
24455 sqlite3_file *id, /* Write the SQLite file handle here */
24456 int flags, /* Open mode flags */
24457 int *pOutFlags /* Status return flags */
24458 ){
24459 HFILE h;
24460 ULONG ulOpenFlags = 0;
24461 ULONG ulOpenMode = 0;
24462 ULONG ulAction = 0;
24463 ULONG rc;
24464 os2File *pFile = (os2File*)id;
24465 const char *zUtf8Name = zName;
24466 char *zNameCp;
24467 char zTmpname[CCHMAXPATH];
24468
24469 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
24470 int isCreate = (flags & SQLITE_OPEN_CREATE);
24471 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
24472 #ifndef NDEBUG
24473 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
24474 int isReadonly = (flags & SQLITE_OPEN_READONLY);
24475 int eType = (flags & 0xFFFFFF00);
24476 int isOpenJournal = (isCreate && (
24477 eType==SQLITE_OPEN_MASTER_JOURNAL
24478 || eType==SQLITE_OPEN_MAIN_JOURNAL
24479 || eType==SQLITE_OPEN_WAL
24480 ));
24481 #endif
24482
24483 UNUSED_PARAMETER(pVfs);
24484 assert( id!=0 );
24485
24486 /* Check the following statements are true:
24487 **
24488 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
24489 ** (b) if CREATE is set, then READWRITE must also be set, and
24490 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
24491 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
24492 */
24493 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
24494 assert(isCreate==0 || isReadWrite);
24495 assert(isExclusive==0 || isCreate);
24496 assert(isDelete==0 || isCreate);
24497
24498 /* The main DB, main journal, WAL file and master journal are never
24499 ** automatically deleted. Nor are they ever temporary files. */
24500 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
24501 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
24502 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
24503 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
24504
24505 /* Assert that the upper layer has set one of the "file-type" flags. */
24506 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
24507 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
24508 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
24509 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
24510 );
24511
24512 memset( pFile, 0, sizeof(*pFile) );
24513 pFile->h = (HFILE)-1;
24514
24515 /* If the second argument to this function is NULL, generate a
24516 ** temporary file name to use
24517 */
24518 if( !zUtf8Name ){
24519 assert(isDelete && !isOpenJournal);
24520 rc = getTempname(CCHMAXPATH, zTmpname);
24521 if( rc!=SQLITE_OK ){
24522 return rc;
24523 }
24524 zUtf8Name = zTmpname;
24525 }
24526
24527 if( isReadWrite ){
24528 ulOpenMode |= OPEN_ACCESS_READWRITE;
24529 }else{
24530 ulOpenMode |= OPEN_ACCESS_READONLY;
24531 }
24532
24533 /* Open in random access mode for possibly better speed. Allow full
24534 ** sharing because file locks will provide exclusive access when needed.
24535 ** The handle should not be inherited by child processes and we don't
24536 ** want popups from the critical error handler.
24537 */
24538 ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
24539 OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
24540
24541 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
24542 ** created. SQLite doesn't use it to indicate "exclusive access"
24543 ** as it is usually understood.
24544 */
24545 if( isExclusive ){
24546 /* Creates a new file, only if it does not already exist. */
24547 /* If the file exists, it fails. */
24548 ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
24549 }else if( isCreate ){
24550 /* Open existing file, or create if it doesn't exist */
24551 ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
24552 }else{
24553 /* Opens a file, only if it exists. */
24554 ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
24555 }
24556
24557 zNameCp = convertUtf8PathToCp( zUtf8Name );
24558 rc = DosOpen( (PSZ)zNameCp,
24559 &h,
24560 &ulAction,
24561 0L,
24562 FILE_NORMAL,
24563 ulOpenFlags,
24564 ulOpenMode,
24565 (PEAOP2)NULL );
24566 free( zNameCp );
24567
24568 if( rc != NO_ERROR ){
24569 OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
24570 rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
24571
24572 if( isReadWrite ){
24573 return os2Open( pVfs, zName, id,
24574 ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
24575 pOutFlags );
24576 }else{
24577 return SQLITE_CANTOPEN;
24578 }
24579 }
24580
24581 if( pOutFlags ){
24582 *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
24583 }
24584
24585 os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
24586 pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
24587 pFile->pMethod = &os2IoMethod;
24588 pFile->flags = flags;
24589 pFile->h = h;
24590
24591 OpenCounter(+1);
24592 OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
24593 return SQLITE_OK;
24594 }
24595
24596 /*
24597 ** Delete the named file.
24598 */
24599 static int os2Delete(
24600 sqlite3_vfs *pVfs, /* Not used on os2 */
24601 const char *zFilename, /* Name of file to delete */
24602 int syncDir /* Not used on os2 */
24603 ){
24604 APIRET rc;
24605 char *zFilenameCp;
24606 SimulateIOError( return SQLITE_IOERR_DELETE );
24607 zFilenameCp = convertUtf8PathToCp( zFilename );
24608 rc = DosDelete( (PSZ)zFilenameCp );
24609 free( zFilenameCp );
24610 OSTRACE(( "DELETE \"%s\"\n", zFilename ));
24611 return (rc == NO_ERROR ||
24612 rc == ERROR_FILE_NOT_FOUND ||
24613 rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
24614 }
24615
24616 /*
24617 ** Check the existance and status of a file.
24618 */
24619 static int os2Access(
24620 sqlite3_vfs *pVfs, /* Not used on os2 */
24621 const char *zFilename, /* Name of file to check */
24622 int flags, /* Type of test to make on this file */
24623 int *pOut /* Write results here */
24624 ){
24625 APIRET rc;
24626 FILESTATUS3 fsts3ConfigInfo;
24627 char *zFilenameCp;
24628
24629 UNUSED_PARAMETER(pVfs);
24630 SimulateIOError( return SQLITE_IOERR_ACCESS; );
24631
24632 zFilenameCp = convertUtf8PathToCp( zFilename );
24633 rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
24634 &fsts3ConfigInfo, sizeof(FILESTATUS3) );
24635 free( zFilenameCp );
24636 OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
24637 fsts3ConfigInfo.attrFile, flags, rc ));
24638
24639 switch( flags ){
24640 case SQLITE_ACCESS_EXISTS:
24641 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
24642 ** as if it does not exist.
24643 */
24644 if( fsts3ConfigInfo.cbFile == 0 )
24645 rc = ERROR_FILE_NOT_FOUND;
24646 break;
24647 case SQLITE_ACCESS_READ:
24648 break;
24649 case SQLITE_ACCESS_READWRITE:
24650 if( fsts3ConfigInfo.attrFile & FILE_READONLY )
24651 rc = ERROR_ACCESS_DENIED;
24652 break;
24653 default:
24654 rc = ERROR_FILE_NOT_FOUND;
24655 assert( !"Invalid flags argument" );
24656 }
24657
24658 *pOut = (rc == NO_ERROR);
24659 OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
24660
24661 return SQLITE_OK;
24662 }
24663
24664
24665 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24666 /*
24667 ** Interfaces for opening a shared library, finding entry points
24668 ** within the shared library, and closing the shared library.
24669 */
24670 /*
24671 ** Interfaces for opening a shared library, finding entry points
24672 ** within the shared library, and closing the shared library.
24673 */
24674 static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24675 HMODULE hmod;
24676 APIRET rc;
24677 char *zFilenameCp = convertUtf8PathToCp(zFilename);
24678 rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
24679 free(zFilenameCp);
24680 return rc != NO_ERROR ? 0 : (void*)hmod;
24681 }
24682 /*
24683 ** A no-op since the error code is returned on the DosLoadModule call.
24684 ** os2Dlopen returns zero if DosLoadModule is not successful.
24685 */
24686 static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24687 /* no-op */
24688 }
24689 static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
24690 PFN pfn;
24691 APIRET rc;
24692 rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
24693 if( rc != NO_ERROR ){
24694 /* if the symbol itself was not found, search again for the same
24695 * symbol with an extra underscore, that might be needed depending
24696 * on the calling convention */
24697 char _zSymbol[256] = "_";
24698 strncat(_zSymbol, zSymbol, 254);
24699 rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
24700 }
24701 return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
24702 }
24703 static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
24704 DosFreeModule((HMODULE)pHandle);
24705 }
24706 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24707 #define os2DlOpen 0
24708 #define os2DlError 0
24709 #define os2DlSym 0
24710 #define os2DlClose 0
24711 #endif
24712
24713
24714 /*
24715 ** Write up to nBuf bytes of randomness into zBuf.
24716 */
24717 static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
24718 int n = 0;
24719 #if defined(SQLITE_TEST)
24720 n = nBuf;
24721 memset(zBuf, 0, nBuf);
24722 #else
24723 int i;
24724 PPIB ppib;
24725 PTIB ptib;
24726 DATETIME dt;
24727 static unsigned c = 0;
24728 /* Ordered by variation probability */
24729 static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
24730 QSV_MAXPRMEM, QSV_MAXSHMEM,
24731 QSV_TOTAVAILMEM, QSV_TOTRESMEM };
24732
24733 /* 8 bytes; timezone and weekday don't increase the randomness much */
24734 if( (int)sizeof(dt)-3 <= nBuf - n ){
24735 c += 0x0100;
24736 DosGetDateTime(&dt);
24737 dt.year = (USHORT)((dt.year - 1900) | c);
24738 memcpy(&zBuf[n], &dt, sizeof(dt)-3);
24739 n += sizeof(dt)-3;
24740 }
24741
24742 /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
24743 if( (int)sizeof(ULONG) <= nBuf - n ){
24744 DosGetInfoBlocks(&ptib, &ppib);
24745 *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
24746 ptib->tib_ptib2->tib2_ultid);
24747 n += sizeof(ULONG);
24748 }
24749
24750 /* Up to 6 * 4 bytes; variables depend on the system state */
24751 for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
24752 DosQuerySysInfo(svIdx[i], svIdx[i],
24753 (PULONG)&zBuf[n], sizeof(ULONG));
24754 n += sizeof(ULONG);
24755 }
24756 #endif
24757
24758 return n;
24759 }
24760
24761 /*
24762 ** Sleep for a little while. Return the amount of time slept.
24763 ** The argument is the number of microseconds we want to sleep.
24764 ** The return value is the number of microseconds of sleep actually
24765 ** requested from the underlying operating system, a number which
24766 ** might be greater than or equal to the argument, but not less
24767 ** than the argument.
24768 */
24769 static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
24770 DosSleep( (microsec/1000) );
24771 return microsec;
24772 }
24773
24774 /*
24775 ** The following variable, if set to a non-zero value, becomes the result
24776 ** returned from sqlite3OsCurrentTime(). This is used for testing.
24777 */
24778 #ifdef SQLITE_TEST
24779 SQLITE_API int sqlite3_current_time = 0;
24780 #endif
24781
24782 /*
24783 ** Find the current time (in Universal Coordinated Time). Write into *piNow
24784 ** the current time and date as a Julian Day number times 86_400_000. In
24785 ** other words, write into *piNow the number of milliseconds since the Julian
24786 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
24787 ** proleptic Gregorian calendar.
24788 **
24789 ** On success, return 0. Return 1 if the time and date cannot be found.
24790 */
24791 static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
24792 #ifdef SQLITE_TEST
24793 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
24794 #endif
24795 int year, month, datepart, timepart;
24796
24797 DATETIME dt;
24798 DosGetDateTime( &dt );
24799
24800 year = dt.year;
24801 month = dt.month;
24802
24803 /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
24804 ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
24805 ** Calculate the Julian days
24806 */
24807 datepart = (int)dt.day - 32076 +
24808 1461*(year + 4800 + (month - 14)/12)/4 +
24809 367*(month - 2 - (month - 14)/12*12)/12 -
24810 3*((year + 4900 + (month - 14)/12)/100)/4;
24811
24812 /* Time in milliseconds, hours to noon added */
24813 timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
24814 ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
24815
24816 *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
24817
24818 #ifdef SQLITE_TEST
24819 if( sqlite3_current_time ){
24820 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
24821 }
24822 #endif
24823
24824 UNUSED_PARAMETER(pVfs);
24825 return 0;
24826 }
24827
24828 /*
24829 ** Find the current time (in Universal Coordinated Time). Write the
24830 ** current time and date as a Julian Day number into *prNow and
24831 ** return 0. Return 1 if the time and date cannot be found.
24832 */
24833 static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
24834 int rc;
24835 sqlite3_int64 i;
24836 rc = os2CurrentTimeInt64(pVfs, &i);
24837 if( !rc ){
24838 *prNow = i/86400000.0;
24839 }
24840 return rc;
24841 }
24842
24843 /*
24844 ** The idea is that this function works like a combination of
24845 ** GetLastError() and FormatMessage() on windows (or errno and
24846 ** strerror_r() on unix). After an error is returned by an OS
24847 ** function, SQLite calls this function with zBuf pointing to
24848 ** a buffer of nBuf bytes. The OS layer should populate the
24849 ** buffer with a nul-terminated UTF-8 encoded error message
24850 ** describing the last IO error to have occurred within the calling
24851 ** thread.
24852 **
24853 ** If the error message is too large for the supplied buffer,
24854 ** it should be truncated. The return value of xGetLastError
24855 ** is zero if the error message fits in the buffer, or non-zero
24856 ** otherwise (if the message was truncated). If non-zero is returned,
24857 ** then it is not necessary to include the nul-terminator character
24858 ** in the output buffer.
24859 **
24860 ** Not supplying an error message will have no adverse effect
24861 ** on SQLite. It is fine to have an implementation that never
24862 ** returns an error message:
24863 **
24864 ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24865 ** assert(zBuf[0]=='\0');
24866 ** return 0;
24867 ** }
24868 **
24869 ** However if an error message is supplied, it will be incorporated
24870 ** by sqlite into the error message available to the user using
24871 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
24872 */
24873 static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24874 assert(zBuf[0]=='\0');
24875 return 0;
24876 }
24877
24878 /*
24879 ** Initialize and deinitialize the operating system interface.
24880 */
24881 SQLITE_API int sqlite3_os_init(void){
24882 static sqlite3_vfs os2Vfs = {
24883 3, /* iVersion */
24884 sizeof(os2File), /* szOsFile */
24885 CCHMAXPATH, /* mxPathname */
24886 0, /* pNext */
24887 "os2", /* zName */
24888 0, /* pAppData */
24889
24890 os2Open, /* xOpen */
24891 os2Delete, /* xDelete */
24892 os2Access, /* xAccess */
24893 os2FullPathname, /* xFullPathname */
24894 os2DlOpen, /* xDlOpen */
24895 os2DlError, /* xDlError */
24896 os2DlSym, /* xDlSym */
24897 os2DlClose, /* xDlClose */
24898 os2Randomness, /* xRandomness */
24899 os2Sleep, /* xSleep */
24900 os2CurrentTime, /* xCurrentTime */
24901 os2GetLastError, /* xGetLastError */
24902 os2CurrentTimeInt64, /* xCurrentTimeInt64 */
24903 0, /* xSetSystemCall */
24904 0, /* xGetSystemCall */
24905 0 /* xNextSystemCall */
24906 };
24907 sqlite3_vfs_register(&os2Vfs, 1);
24908 initUconvObjects();
24909 /* sqlite3OSTrace = 1; */
24910 return SQLITE_OK;
24911 }
24912 SQLITE_API int sqlite3_os_end(void){
24913 freeUconvObjects();
24914 return SQLITE_OK;
24915 }
24916
24917 #endif /* SQLITE_OS_OS2 */
24918
24919 /************** End of os_os2.c **********************************************/
24920 /************** Begin file os_unix.c *****************************************/
24921 /*
24922 ** 2004 May 22
24923 **
24924 ** The author disclaims copyright to this source code. In place of
@@ -59054,18 +56650,18 @@
59054 /*
59055 ** Release all resources associated with an sqlite3_backup* handle.
59056 */
59057 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
59058 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
59059 MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
59060 int rc; /* Value to return */
59061
59062 /* Enter the mutexes */
59063 if( p==0 ) return SQLITE_OK;
59064 sqlite3_mutex_enter(p->pSrcDb->mutex);
 
59065 sqlite3BtreeEnter(p->pSrc);
59066 MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
59067 if( p->pDestDb ){
59068 sqlite3_mutex_enter(p->pDestDb->mutex);
59069 }
59070
59071 /* Detach this backup from the source pager. */
@@ -59087,20 +56683,20 @@
59087 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
59088 sqlite3Error(p->pDestDb, rc, 0);
59089
59090 /* Exit the mutexes and free the backup context structure. */
59091 if( p->pDestDb ){
59092 sqlite3_mutex_leave(p->pDestDb->mutex);
59093 }
59094 sqlite3BtreeLeave(p->pSrc);
59095 if( p->pDestDb ){
59096 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
59097 ** call to sqlite3_backup_init() and is destroyed by a call to
59098 ** sqlite3_backup_finish(). */
59099 sqlite3_free(p);
59100 }
59101 sqlite3_mutex_leave(mutex);
59102 return rc;
59103 }
59104
59105 /*
59106 ** Return the number of pages still to be backed up as of the most recent
@@ -62860,10 +60456,11 @@
62860 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
62861 sqlite3 *db;
62862
62863 if( NEVER(p==0) ) return;
62864 db = p->db;
 
62865 if( p->pPrev ){
62866 p->pPrev->pNext = p->pNext;
62867 }else{
62868 assert( db->pVdbe==p );
62869 db->pVdbe = p->pNext;
@@ -63699,21 +61296,15 @@
63699 ** pointer is a harmless no-op. */
63700 rc = SQLITE_OK;
63701 }else{
63702 Vdbe *v = (Vdbe*)pStmt;
63703 sqlite3 *db = v->db;
63704 #if SQLITE_THREADSAFE
63705 sqlite3_mutex *mutex;
63706 #endif
63707 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
63708 #if SQLITE_THREADSAFE
63709 mutex = v->db->mutex;
63710 #endif
63711 sqlite3_mutex_enter(mutex);
63712 rc = sqlite3VdbeFinalize(v);
63713 rc = sqlite3ApiExit(db, rc);
63714 sqlite3_mutex_leave(mutex);
63715 }
63716 return rc;
63717 }
63718
63719 /*
@@ -70017,11 +67608,10 @@
70017 assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
70018 assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
70019 assert( u.bl.pC!=0 );
70020 assert( u.bl.pC->nullRow==0 );
70021 assert( u.bl.pC->pseudoTableReg==0 );
70022 assert( !u.bl.pC->isSorter );
70023 assert( u.bl.pC->pCursor!=0 );
70024 u.bl.pCrsr = u.bl.pC->pCursor;
70025 assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
70026
70027 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
@@ -88019,12 +85609,23 @@
88019 */
88020 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
88021 assert( argc==1 );
88022 UNUSED_PARAMETER(argc);
88023 switch( sqlite3_value_type(argv[0]) ){
88024 case SQLITE_INTEGER:
88025 case SQLITE_FLOAT: {
 
 
 
 
 
 
 
 
 
 
 
 
88026 sqlite3_result_value(context, argv[0]);
88027 break;
88028 }
88029 case SQLITE_BLOB: {
88030 char *zText = 0;
@@ -114347,18 +111948,30 @@
114347 sqlite3BtreeLeaveAll(db);
114348 #else
114349 UNUSED_PARAMETER(db);
114350 #endif
114351 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114352
114353 /*
114354 ** Close an existing SQLite database
114355 */
114356 SQLITE_API int sqlite3_close(sqlite3 *db){
114357 HashElem *i; /* Hash table iterator */
114358 int j;
114359
114360 if( !db ){
114361 return SQLITE_OK;
114362 }
114363 if( !sqlite3SafetyCheckSickOrOk(db) ){
114364 return SQLITE_MISUSE_BKPT;
@@ -114375,28 +111988,66 @@
114375 ** SQL statements below, as the v-table implementation may be storing
114376 ** some prepared statements internally.
114377 */
114378 sqlite3VtabRollback(db);
114379
114380 /* If there are any outstanding VMs, return SQLITE_BUSY. */
114381 if( db->pVdbe ){
114382 sqlite3Error(db, SQLITE_BUSY,
114383 "unable to close due to unfinalised statements");
 
 
114384 sqlite3_mutex_leave(db->mutex);
114385 return SQLITE_BUSY;
114386 }
114387 assert( sqlite3SafetyCheckSickOrOk(db) );
114388
114389 for(j=0; j<db->nDb; j++){
114390 Btree *pBt = db->aDb[j].pBt;
114391 if( pBt && sqlite3BtreeIsInBackup(pBt) ){
114392 sqlite3Error(db, SQLITE_BUSY,
114393 "unable to close due to unfinished backup operation");
114394 sqlite3_mutex_leave(db->mutex);
114395 return SQLITE_BUSY;
114396 }
114397 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114398
114399 /* Free any outstanding Savepoint structures. */
114400 sqlite3CloseSavepoints(db);
114401
114402 /* Close all database connections */
@@ -114481,11 +112132,10 @@
114481 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
114482 if( db->lookaside.bMalloced ){
114483 sqlite3_free(db->lookaside.pStart);
114484 }
114485 sqlite3_free(db);
114486 return SQLITE_OK;
114487 }
114488
114489 /*
114490 ** Rollback all database files. If tripCode is not SQLITE_OK, then
114491 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
@@ -115648,13 +113298,11 @@
115648 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
115649 static struct OpenMode aOpenMode[] = {
115650 { "ro", SQLITE_OPEN_READONLY },
115651 { "rw", SQLITE_OPEN_READWRITE },
115652 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
115653 { "memory",
115654 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
115655 | SQLITE_OPEN_MEMORY },
115656 { 0, 0 }
115657 };
115658
115659 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
115660 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
@@ -125936,14 +123584,13 @@
125936 /* #include <string.h> */
125937
125938 /*
125939 ** Implementation of a special SQL scalar function for testing tokenizers
125940 ** designed to be used in concert with the Tcl testing framework. This
125941 ** function must be called with two arguments:
125942 **
125943 ** SELECT <function-name>(<key-name>, <input-string>);
125944 ** SELECT <function-name>(<key-name>, <pointer>);
125945 **
125946 ** where <function-name> is the name passed as the second argument
125947 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
125948 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
125949 **
@@ -125976,31 +123623,31 @@
125976 const char *zName;
125977 int nName;
125978 const char *zInput;
125979 int nInput;
125980
125981 const char *zArg = 0;
125982
125983 const char *zToken;
125984 int nToken;
125985 int iStart;
125986 int iEnd;
125987 int iPos;
 
125988
125989 Tcl_Obj *pRet;
125990
125991 assert( argc==2 || argc==3 );
 
 
 
125992
125993 nName = sqlite3_value_bytes(argv[0]);
125994 zName = (const char *)sqlite3_value_text(argv[0]);
125995 nInput = sqlite3_value_bytes(argv[argc-1]);
125996 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
125997
125998 if( argc==3 ){
125999 zArg = (const char *)sqlite3_value_text(argv[1]);
126000 }
126001
126002 pHash = (Fts3Hash *)sqlite3_user_data(context);
126003 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
126004
126005 if( !p ){
126006 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
@@ -126010,11 +123657,15 @@
126010 }
126011
126012 pRet = Tcl_NewObj();
126013 Tcl_IncrRefCount(pRet);
126014
126015 if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
 
 
 
 
126016 zErr = "error in xCreate()";
126017 goto finish;
126018 }
126019 pTokenizer->pModule = p;
126020 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
@@ -126194,14 +123845,11 @@
126194 if( SQLITE_OK==rc ){
126195 rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
126196 }
126197 #ifdef SQLITE_TEST
126198 if( SQLITE_OK==rc ){
126199 rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
126200 }
126201 if( SQLITE_OK==rc ){
126202 rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
126203 }
126204 if( SQLITE_OK==rc ){
126205 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
126206 }
126207 #endif
@@ -126588,11 +124236,12 @@
126588 ** fts3SegReaderFirstDocid()
126589 ** fts3SegReaderNextDocid()
126590 */
126591 struct Fts3SegReader {
126592 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
126593 int bLookup; /* True for a lookup only */
 
126594
126595 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
126596 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
126597 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
126598 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
@@ -126622,11 +124271,11 @@
126622 int nOffsetList; /* For descending pending seg-readers only */
126623 sqlite3_int64 iDocid;
126624 };
126625
126626 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
126627 #define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
126628
126629 /*
126630 ** An instance of this structure is used to create a segment b-tree in the
126631 ** database. The internal details of this type are only accessed by the
126632 ** following functions:
@@ -128033,18 +125682,19 @@
128033 if( !pReader ){
128034 return SQLITE_NOMEM;
128035 }
128036 memset(pReader, 0, sizeof(Fts3SegReader));
128037 pReader->iIdx = iAge;
128038 pReader->bLookup = bLookup;
128039 pReader->iStartBlock = iStartLeaf;
128040 pReader->iLeafEndBlock = iEndLeaf;
128041 pReader->iEndBlock = iEndBlock;
128042
128043 if( nExtra ){
128044 /* The entire segment is stored in the root node. */
128045 pReader->aNode = (char *)&pReader[1];
 
128046 pReader->nNode = nRoot;
128047 memcpy(pReader->aNode, zRoot, nRoot);
128048 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
128049 }else{
128050 pReader->iCurrentBlock = iStartLeaf-1;
@@ -133438,10 +131088,12 @@
133438 typedef struct unicode_cursor unicode_cursor;
133439
133440 struct unicode_tokenizer {
133441 sqlite3_tokenizer base;
133442 int bRemoveDiacritic;
 
 
133443 };
133444
133445 struct unicode_cursor {
133446 sqlite3_tokenizer_cursor base;
133447 const unsigned char *aInput; /* Input text being tokenized */
@@ -133450,10 +131102,125 @@
133450 int iToken; /* Index of next token to be returned */
133451 char *zToken; /* storage for current token */
133452 int nAlloc; /* space allocated at zToken */
133453 };
133454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133455 /*
133456 ** Create a new tokenizer instance.
133457 */
133458 static int unicodeCreate(
133459 int nArg, /* Size of array argv[] */
@@ -133460,43 +131227,45 @@
133460 const char * const *azArg, /* Tokenizer creation arguments */
133461 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
133462 ){
133463 unicode_tokenizer *pNew; /* New tokenizer object */
133464 int i;
 
 
133465 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
133466 if( pNew==NULL ){
133467 return SQLITE_NOMEM;
133468 }
133469 memset(pNew, 0, sizeof(unicode_tokenizer));
133470 pNew->bRemoveDiacritic = 1;
133471
133472 for(i=0; i<nArg; i++){
133473 const char *z = azArg[i];
133474 int n = strlen(z);
133475
133476 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
133477 pNew->bRemoveDiacritic = 1;
133478 }
133479 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
133480 pNew->bRemoveDiacritic = 0;
133481 }
 
 
 
 
 
 
133482 else{
133483 /* Unrecognized argument */
133484 return SQLITE_ERROR;
133485 }
133486 }
133487
133488 *pp = &pNew->base;
133489 return SQLITE_OK;
133490 }
133491
133492 /*
133493 ** Destroy a tokenizer allocated by unicodeCreate().
133494 */
133495 static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
133496 sqlite3_free(pTokenizer);
133497 return SQLITE_OK;
133498 }
133499
133500 /*
133501 ** Prepare to begin tokenizing a particular string. The input
133502 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
@@ -133545,18 +131314,19 @@
133545 /*
133546 ** Extract the next token from a tokenization cursor. The cursor must
133547 ** have been opened by a prior call to simpleOpen().
133548 */
133549 static int unicodeNext(
133550 sqlite3_tokenizer_cursor *p, /* Cursor returned by simpleOpen */
133551 const char **paToken, /* OUT: Token text */
133552 int *pnToken, /* OUT: Number of bytes at *paToken */
133553 int *piStart, /* OUT: Starting offset of token */
133554 int *piEnd, /* OUT: Ending offset of token */
133555 int *piPos /* OUT: Position integer of token */
133556 ){
133557 unicode_cursor *pCsr = (unicode_cursor *)p;
 
133558 int iCode;
133559 char *zOut;
133560 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
133561 const unsigned char *zStart = z;
133562 const unsigned char *zEnd;
@@ -133565,11 +131335,11 @@
133565 /* Scan past any delimiter characters before the start of the next token.
133566 ** Return SQLITE_DONE early if this takes us all the way to the end of
133567 ** the input. */
133568 while( z<zTerm ){
133569 READ_UTF8(z, zTerm, iCode);
133570 if( sqlite3FtsUnicodeIsalnum(iCode) ) break;
133571 zStart = z;
133572 }
133573 if( zStart>=zTerm ) return SQLITE_DONE;
133574
133575 zOut = pCsr->zToken;
@@ -133585,21 +131355,19 @@
133585 pCsr->nAlloc += 64;
133586 }
133587
133588 /* Write the folded case of the last character read to the output */
133589 zEnd = z;
133590 iOut = sqlite3FtsUnicodeFold(iCode,
133591 ((unicode_tokenizer *)pCsr->base.pTokenizer)->bRemoveDiacritic
133592 );
133593 if( iOut ){
133594 WRITE_UTF8(zOut, iOut);
133595 }
133596
133597 /* If the cursor is not at EOF, read the next character */
133598 if( z>=zTerm ) break;
133599 READ_UTF8(z, zTerm, iCode);
133600 }while( sqlite3FtsUnicodeIsalnum(iCode)
133601 || sqlite3FtsUnicodeIsdiacritic(iCode)
133602 );
133603
133604 /* Set the output variables and return. */
133605 pCsr->iOff = (z - pCsr->aInput);
@@ -133779,11 +131547,11 @@
133779 iHi = iTest-1;
133780 }
133781 }
133782 assert( aEntry[0]<key );
133783 assert( key>=aEntry[iRes] );
133784 return (c >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
133785 }
133786 return 1;
133787 }
133788
133789
@@ -138059,11 +135827,11 @@
138059 return SQLITE_DONE;
138060 }
138061
138062 while( iStart<iEnd ){
138063 int iWhite = iStart;
138064 U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
138065 if( u_isspace(c) ){
138066 iStart = iWhite;
138067 }else{
138068 break;
138069 }
138070
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.14. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -387,10 +387,11 @@
387 ** Exactly one of the following macros must be defined in order to
388 ** specify which memory allocation subsystem to use.
389 **
390 ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
391 ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
392 ** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
393 ** SQLITE_MEMDEBUG // Debugging version of system malloc()
394 **
395 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
396 ** assert() macro is enabled, each call into the Win32 native heap subsystem
397 ** will cause HeapValidate to be called. If heap validation should fail, an
@@ -400,15 +401,23 @@
401 ** pared it down to just these three.)
402 **
403 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
404 ** the default.
405 */
406 #if defined(SQLITE_SYSTEM_MALLOC) \
407 + defined(SQLITE_WIN32_MALLOC) \
408 + defined(SQLITE_ZERO_MALLOC) \
409 + defined(SQLITE_MEMDEBUG)>1
410 # error "Two or more of the following compile-time configuration options\
411 are defined but at most one is allowed:\
412 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
413 SQLITE_ZERO_MALLOC"
414 #endif
415 #if defined(SQLITE_SYSTEM_MALLOC) \
416 + defined(SQLITE_WIN32_MALLOC) \
417 + defined(SQLITE_ZERO_MALLOC) \
418 + defined(SQLITE_MEMDEBUG)==0
419 # define SQLITE_SYSTEM_MALLOC 1
420 #endif
421
422 /*
423 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -662,13 +671,13 @@
671 **
672 ** See also: [sqlite3_libversion()],
673 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
674 ** [sqlite_version()] and [sqlite_source_id()].
675 */
676 #define SQLITE_VERSION "3.7.14"
677 #define SQLITE_VERSION_NUMBER 3007014
678 #define SQLITE_SOURCE_ID "2012-06-21 17:21:52 d5e6880279210ca63e2d5e7f6d009f30566f1242"
679
680 /*
681 ** CAPI3REF: Run-Time Library Version Numbers
682 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
683 **
@@ -774,11 +783,12 @@
783 **
784 ** Each open SQLite database is represented by a pointer to an instance of
785 ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
786 ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
787 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
788 ** and [sqlite3_close_v2()] are its destructors. There are many other
789 ** interfaces (such as
790 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
791 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
792 ** sqlite3 object.
793 */
794 typedef struct sqlite3 sqlite3;
@@ -821,32 +831,50 @@
831 #endif
832
833 /*
834 ** CAPI3REF: Closing A Database Connection
835 **
836 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
837 ** for the [sqlite3] object.
838 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
839 ** the [sqlite3] object is successfully destroyed and all associated
840 ** resources are deallocated.
841 **
842 ** ^If the database connection is associated with unfinalized prepared
843 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
844 ** will leave the database connection open and return [SQLITE_BUSY].
845 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
846 ** and unfinished sqlite3_backups, then the database connection becomes
847 ** an unusable "zombie" which will automatically be deallocated when the
848 ** last prepared statement is finalized or the last sqlite3_backup is
849 ** finished. The sqlite3_close_v2() interface is intended for use with
850 ** host languages that are garbage collected, and where the order in which
851 ** destructors are called is arbitrary.
852 **
853 ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
854 ** [sqlite3_blob_close | close] all [BLOB handles], and
855 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
856 ** with the [sqlite3] object prior to attempting to close the object. ^If
857 ** sqlite3_close() is called on a [database connection] that still has
858 ** outstanding [prepared statements], [BLOB handles], and/or
859 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
860 ** of resources is deferred until all [prepared statements], [BLOB handles],
861 ** and [sqlite3_backup] objects are also destroyed.
862 **
863 ** ^If an [sqlite3] object is destroyed while a transaction is open,
864 ** the transaction is automatically rolled back.
865 **
866 ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
867 ** must be either a NULL
868 ** pointer or an [sqlite3] object pointer obtained
869 ** from [sqlite3_open()], [sqlite3_open16()], or
870 ** [sqlite3_open_v2()], and not previously closed.
871 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
872 ** argument is a harmless no-op.
873 */
874 SQLITE_API int sqlite3_close(sqlite3*);
875 SQLITE_API int sqlite3_close_v2(sqlite3*);
876
877 /*
878 ** The type for a callback function.
879 ** This is legacy and deprecated. It is included for historical
880 ** compatibility and is not documented.
@@ -5013,15 +5041,16 @@
5041 ** CAPI3REF: Name Of The Folder Holding Database Files
5042 **
5043 ** ^(If this global variable is made to point to a string which is
5044 ** the name of a folder (a.k.a. directory), then all database files
5045 ** specified with a relative pathname and created or accessed by
5046 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
5047 ** to be relative to that directory.)^ ^If this variable is a NULL
5048 ** pointer, then SQLite assumes that all database files specified
5049 ** with a relative pathname are relative to the current directory
5050 ** for the process. Only the windows VFS makes use of this global
5051 ** variable; it is ignored by the unix VFS.
5052 **
5053 ** Changing the value of this variable while a database connection is
5054 ** open can result in a corrupt database.
5055 **
5056 ** It is not safe to read or modify this variable in more than one
@@ -6048,21 +6077,20 @@
6077 ** of these mutex routines. An appropriate implementation
6078 ** is selected automatically at compile-time. ^(The following
6079 ** implementations are available in the SQLite core:
6080 **
6081 ** <ul>
 
6082 ** <li> SQLITE_MUTEX_PTHREADS
6083 ** <li> SQLITE_MUTEX_W32
6084 ** <li> SQLITE_MUTEX_NOOP
6085 ** </ul>)^
6086 **
6087 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
6088 ** that does no real locking and is appropriate for use in
6089 ** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and
6090 ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
6091 ** and Windows.
6092 **
6093 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
6094 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
6095 ** implementation is included with the library. In this case the
6096 ** application must supply a custom mutex implementation using the
@@ -9287,22 +9315,20 @@
9315 #define _SQLITE_OS_H_
9316
9317 /*
9318 ** Figure out if we are dealing with Unix, Windows, or some other
9319 ** operating system. After the following block of preprocess macros,
9320 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
9321 ** will defined to either 1 or 0. One of the four will be 1. The other
9322 ** three will be 0.
9323 */
9324 #if defined(SQLITE_OS_OTHER)
9325 # if SQLITE_OS_OTHER==1
9326 # undef SQLITE_OS_UNIX
9327 # define SQLITE_OS_UNIX 0
9328 # undef SQLITE_OS_WIN
9329 # define SQLITE_OS_WIN 0
 
 
9330 # else
9331 # undef SQLITE_OS_OTHER
9332 # endif
9333 #endif
9334 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
@@ -9309,23 +9335,16 @@
9335 # define SQLITE_OS_OTHER 0
9336 # ifndef SQLITE_OS_WIN
9337 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
9338 # define SQLITE_OS_WIN 1
9339 # define SQLITE_OS_UNIX 0
 
 
 
 
 
9340 # else
9341 # define SQLITE_OS_WIN 0
9342 # define SQLITE_OS_UNIX 1
 
9343 # endif
9344 # else
9345 # define SQLITE_OS_UNIX 0
 
9346 # endif
9347 #else
9348 # ifndef SQLITE_OS_WIN
9349 # define SQLITE_OS_WIN 0
9350 # endif
@@ -9333,25 +9352,10 @@
9352
9353 #if SQLITE_OS_WIN
9354 # include <windows.h>
9355 #endif
9356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9357 /*
9358 ** Determine if we are dealing with Windows NT.
9359 **
9360 ** We ought to be able to determine if we are compiling for win98 or winNT
9361 ** using the _WIN32_WINNT macro as follows:
@@ -9617,23 +9621,19 @@
9621 ** start-time.
9622 **
9623 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
9624 **
9625 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
 
 
9626 */
9627 #if !SQLITE_THREADSAFE
9628 # define SQLITE_MUTEX_OMIT
9629 #endif
9630 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
9631 # if SQLITE_OS_UNIX
9632 # define SQLITE_MUTEX_PTHREADS
9633 # elif SQLITE_OS_WIN
9634 # define SQLITE_MUTEX_W32
 
 
9635 # else
9636 # define SQLITE_MUTEX_NOOP
9637 # endif
9638 #endif
9639
@@ -9950,10 +9950,11 @@
9950 #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
9951 #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
9952 #define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
9953 #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
9954 #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
9955 #define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
9956
9957 /*
9958 ** Each SQL function is defined by an instance of the following
9959 ** structure. A pointer to this structure is stored in the sqlite.aFunc
9960 ** hash table. When multiple functions have the same name, the hash table
@@ -11811,10 +11812,11 @@
11812 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
11813 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
11814 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
11815 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
11816 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
11817 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
11818 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
11819 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
11820 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
11821 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
11822 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
@@ -17716,286 +17718,10 @@
17718 }
17719 #endif /* defined(SQLITE_MUTEX_NOOP) */
17720 #endif /* !defined(SQLITE_MUTEX_OMIT) */
17721
17722 /************** End of mutex_noop.c ******************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17723 /************** Begin file mutex_unix.c **************************************/
17724 /*
17725 ** 2007 August 28
17726 **
17727 ** The author disclaims copyright to this source code. In place of
@@ -18456,11 +18182,11 @@
18182 ** processing, the "interlocked" magic is probably not
18183 ** strictly necessary.
18184 */
18185 static long winMutex_lock = 0;
18186
18187 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18188
18189 static int winMutexInit(void){
18190 /* The first to increment to 1 does actual initialization */
18191 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
18192 int i;
@@ -19599,11 +19325,12 @@
19325 ** always returned.
19326 */
19327 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
19328 int digit;
19329 LONGDOUBLE_TYPE d;
19330 if( (*cnt)<=0 ) return '0';
19331 (*cnt)--;
19332 digit = (int)*val;
19333 d = digit;
19334 digit += '0';
19335 *val = (*val - d)*10.0;
19336 return (char)digit;
@@ -19903,13 +19630,16 @@
19630 bufpt = "NaN";
19631 length = 3;
19632 break;
19633 }
19634 if( realvalue>0.0 ){
19635 LONGDOUBLE_TYPE scale = 1.0;
19636 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
19637 while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
19638 while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
19639 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
19640 realvalue /= scale;
19641 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
19642 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
19643 if( exp>350 ){
19644 if( prefix=='-' ){
19645 bufpt = "-Inf";
@@ -19938,11 +19668,11 @@
19668 }else{
19669 precision = precision - exp;
19670 xtype = etFLOAT;
19671 }
19672 }else{
19673 flag_rtz = flag_altform2;
19674 }
19675 if( xtype==etEXP ){
19676 e2 = 0;
19677 }else{
19678 e2 = exp;
@@ -19953,11 +19683,11 @@
19683 pAccum->mallocFailed = 1;
19684 return;
19685 }
19686 }
19687 zOut = bufpt;
19688 nsd = 16 + flag_altform2*10;
19689 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19690 /* The sign in front of the number */
19691 if( prefix ){
19692 *(bufpt++) = prefix;
19693 }
@@ -21526,11 +21256,11 @@
21256 s = sign<0 ? -s : s;
21257
21258 /* if exponent, scale significand as appropriate
21259 ** and store in result. */
21260 if( e ){
21261 LONGDOUBLE_TYPE scale = 1.0;
21262 /* attempt to handle extremely small/large numbers better */
21263 if( e>307 && e<342 ){
21264 while( e%308 ) { scale *= 1.0e+1; e -= 1; }
21265 if( esign<0 ){
21266 result = s / scale;
@@ -22781,2144 +22511,10 @@
22511 return azName[i];
22512 }
22513 #endif
22514
22515 /************** End of opcodes.c *********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22516 /************** Begin file os_unix.c *****************************************/
22517 /*
22518 ** 2004 May 22
22519 **
22520 ** The author disclaims copyright to this source code. In place of
@@ -59054,18 +56650,18 @@
56650 /*
56651 ** Release all resources associated with an sqlite3_backup* handle.
56652 */
56653 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
56654 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
56655 sqlite3 *pSrcDb; /* Source database connection */
56656 int rc; /* Value to return */
56657
56658 /* Enter the mutexes */
56659 if( p==0 ) return SQLITE_OK;
56660 pSrcDb = p->pSrcDb;
56661 sqlite3_mutex_enter(pSrcDb->mutex);
56662 sqlite3BtreeEnter(p->pSrc);
 
56663 if( p->pDestDb ){
56664 sqlite3_mutex_enter(p->pDestDb->mutex);
56665 }
56666
56667 /* Detach this backup from the source pager. */
@@ -59087,20 +56683,20 @@
56683 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
56684 sqlite3Error(p->pDestDb, rc, 0);
56685
56686 /* Exit the mutexes and free the backup context structure. */
56687 if( p->pDestDb ){
56688 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
56689 }
56690 sqlite3BtreeLeave(p->pSrc);
56691 if( p->pDestDb ){
56692 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
56693 ** call to sqlite3_backup_init() and is destroyed by a call to
56694 ** sqlite3_backup_finish(). */
56695 sqlite3_free(p);
56696 }
56697 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
56698 return rc;
56699 }
56700
56701 /*
56702 ** Return the number of pages still to be backed up as of the most recent
@@ -62860,10 +60456,11 @@
60456 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
60457 sqlite3 *db;
60458
60459 if( NEVER(p==0) ) return;
60460 db = p->db;
60461 assert( sqlite3_mutex_held(db->mutex) );
60462 if( p->pPrev ){
60463 p->pPrev->pNext = p->pNext;
60464 }else{
60465 assert( db->pVdbe==p );
60466 db->pVdbe = p->pNext;
@@ -63699,21 +61296,15 @@
61296 ** pointer is a harmless no-op. */
61297 rc = SQLITE_OK;
61298 }else{
61299 Vdbe *v = (Vdbe*)pStmt;
61300 sqlite3 *db = v->db;
 
 
 
61301 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
61302 sqlite3_mutex_enter(db->mutex);
 
 
 
61303 rc = sqlite3VdbeFinalize(v);
61304 rc = sqlite3ApiExit(db, rc);
61305 sqlite3LeaveMutexAndCloseZombie(db);
61306 }
61307 return rc;
61308 }
61309
61310 /*
@@ -70017,11 +67608,10 @@
67608 assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
67609 assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
67610 assert( u.bl.pC!=0 );
67611 assert( u.bl.pC->nullRow==0 );
67612 assert( u.bl.pC->pseudoTableReg==0 );
 
67613 assert( u.bl.pC->pCursor!=0 );
67614 u.bl.pCrsr = u.bl.pC->pCursor;
67615 assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
67616
67617 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
@@ -88019,12 +85609,23 @@
85609 */
85610 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
85611 assert( argc==1 );
85612 UNUSED_PARAMETER(argc);
85613 switch( sqlite3_value_type(argv[0]) ){
 
85614 case SQLITE_FLOAT: {
85615 double r1, r2;
85616 char zBuf[50];
85617 r1 = sqlite3_value_double(argv[0]);
85618 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
85619 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
85620 if( r1!=r2 ){
85621 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
85622 }
85623 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
85624 break;
85625 }
85626 case SQLITE_INTEGER: {
85627 sqlite3_result_value(context, argv[0]);
85628 break;
85629 }
85630 case SQLITE_BLOB: {
85631 char *zText = 0;
@@ -114347,18 +111948,30 @@
111948 sqlite3BtreeLeaveAll(db);
111949 #else
111950 UNUSED_PARAMETER(db);
111951 #endif
111952 }
111953
111954 /*
111955 ** Return TRUE if database connection db has unfinalized prepared
111956 ** statements or unfinished sqlite3_backup objects.
111957 */
111958 static int connectionIsBusy(sqlite3 *db){
111959 int j;
111960 assert( sqlite3_mutex_held(db->mutex) );
111961 if( db->pVdbe ) return 1;
111962 for(j=0; j<db->nDb; j++){
111963 Btree *pBt = db->aDb[j].pBt;
111964 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
111965 }
111966 return 0;
111967 }
111968
111969 /*
111970 ** Close an existing SQLite database
111971 */
111972 static int sqlite3Close(sqlite3 *db, int forceZombie){
 
 
 
111973 if( !db ){
111974 return SQLITE_OK;
111975 }
111976 if( !sqlite3SafetyCheckSickOrOk(db) ){
111977 return SQLITE_MISUSE_BKPT;
@@ -114375,28 +111988,66 @@
111988 ** SQL statements below, as the v-table implementation may be storing
111989 ** some prepared statements internally.
111990 */
111991 sqlite3VtabRollback(db);
111992
111993 /* Legacy behavior (sqlite3_close() behavior) is to return
111994 ** SQLITE_BUSY if the connection can not be closed immediately.
111995 */
111996 if( !forceZombie && connectionIsBusy(db) ){
111997 sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
111998 "statements or unfinished backups");
111999 sqlite3_mutex_leave(db->mutex);
112000 return SQLITE_BUSY;
112001 }
112002
112003 /* Convert the connection into a zombie and then close it.
112004 */
112005 db->magic = SQLITE_MAGIC_ZOMBIE;
112006 sqlite3LeaveMutexAndCloseZombie(db);
112007 return SQLITE_OK;
112008 }
112009
112010 /*
112011 ** Two variations on the public interface for closing a database
112012 ** connection. The sqlite3_close() version returns SQLITE_BUSY and
112013 ** leaves the connection option if there are unfinalized prepared
112014 ** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
112015 ** version forces the connection to become a zombie if there are
112016 ** unclosed resources, and arranges for deallocation when the last
112017 ** prepare statement or sqlite3_backup closes.
112018 */
112019 SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
112020 SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
112021
112022
112023 /*
112024 ** Close the mutex on database connection db.
112025 **
112026 ** Furthermore, if database connection db is a zombie (meaning that there
112027 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
112028 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has
112029 ** finished, then free all resources.
112030 */
112031 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
112032 HashElem *i; /* Hash table iterator */
112033 int j;
112034
112035 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
112036 ** or if the connection has not yet been closed by sqlite3_close_v2(),
112037 ** then just leave the mutex and return.
112038 */
112039 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
112040 sqlite3_mutex_leave(db->mutex);
112041 return;
112042 }
112043
112044 /* If we reach this point, it means that the database connection has
112045 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
112046 ** pased to sqlite3_close (meaning that it is a zombie). Therefore,
112047 ** go ahead and free all resources.
112048 */
112049
112050 /* Free any outstanding Savepoint structures. */
112051 sqlite3CloseSavepoints(db);
112052
112053 /* Close all database connections */
@@ -114481,11 +112132,10 @@
112132 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
112133 if( db->lookaside.bMalloced ){
112134 sqlite3_free(db->lookaside.pStart);
112135 }
112136 sqlite3_free(db);
 
112137 }
112138
112139 /*
112140 ** Rollback all database files. If tripCode is not SQLITE_OK, then
112141 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
@@ -115648,13 +113298,11 @@
113298 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
113299 static struct OpenMode aOpenMode[] = {
113300 { "ro", SQLITE_OPEN_READONLY },
113301 { "rw", SQLITE_OPEN_READWRITE },
113302 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
113303 { "memory", SQLITE_OPEN_MEMORY },
 
 
113304 { 0, 0 }
113305 };
113306
113307 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
113308 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
@@ -125936,14 +123584,13 @@
123584 /* #include <string.h> */
123585
123586 /*
123587 ** Implementation of a special SQL scalar function for testing tokenizers
123588 ** designed to be used in concert with the Tcl testing framework. This
123589 ** function must be called with two or more arguments:
123590 **
123591 ** SELECT <function-name>(<key-name>, ..., <input-string>);
 
123592 **
123593 ** where <function-name> is the name passed as the second argument
123594 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
123595 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
123596 **
@@ -125976,31 +123623,31 @@
123623 const char *zName;
123624 int nName;
123625 const char *zInput;
123626 int nInput;
123627
123628 const char *azArg[64];
123629
123630 const char *zToken;
123631 int nToken;
123632 int iStart;
123633 int iEnd;
123634 int iPos;
123635 int i;
123636
123637 Tcl_Obj *pRet;
123638
123639 if( argc<2 ){
123640 sqlite3_result_error(context, "insufficient arguments", -1);
123641 return;
123642 }
123643
123644 nName = sqlite3_value_bytes(argv[0]);
123645 zName = (const char *)sqlite3_value_text(argv[0]);
123646 nInput = sqlite3_value_bytes(argv[argc-1]);
123647 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
123648
 
 
 
 
123649 pHash = (Fts3Hash *)sqlite3_user_data(context);
123650 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
123651
123652 if( !p ){
123653 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
@@ -126010,11 +123657,15 @@
123657 }
123658
123659 pRet = Tcl_NewObj();
123660 Tcl_IncrRefCount(pRet);
123661
123662 for(i=1; i<argc-1; i++){
123663 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
123664 }
123665
123666 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
123667 zErr = "error in xCreate()";
123668 goto finish;
123669 }
123670 pTokenizer->pModule = p;
123671 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
@@ -126194,14 +123845,11 @@
123845 if( SQLITE_OK==rc ){
123846 rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
123847 }
123848 #ifdef SQLITE_TEST
123849 if( SQLITE_OK==rc ){
123850 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
 
 
 
123851 }
123852 if( SQLITE_OK==rc ){
123853 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
123854 }
123855 #endif
@@ -126588,11 +124236,12 @@
124236 ** fts3SegReaderFirstDocid()
124237 ** fts3SegReaderNextDocid()
124238 */
124239 struct Fts3SegReader {
124240 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
124241 u8 bLookup; /* True for a lookup only */
124242 u8 rootOnly; /* True for a root-only reader */
124243
124244 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
124245 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
124246 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
124247 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
@@ -126622,11 +124271,11 @@
124271 int nOffsetList; /* For descending pending seg-readers only */
124272 sqlite3_int64 iDocid;
124273 };
124274
124275 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
124276 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
124277
124278 /*
124279 ** An instance of this structure is used to create a segment b-tree in the
124280 ** database. The internal details of this type are only accessed by the
124281 ** following functions:
@@ -128033,18 +125682,19 @@
125682 if( !pReader ){
125683 return SQLITE_NOMEM;
125684 }
125685 memset(pReader, 0, sizeof(Fts3SegReader));
125686 pReader->iIdx = iAge;
125687 pReader->bLookup = bLookup!=0;
125688 pReader->iStartBlock = iStartLeaf;
125689 pReader->iLeafEndBlock = iEndLeaf;
125690 pReader->iEndBlock = iEndBlock;
125691
125692 if( nExtra ){
125693 /* The entire segment is stored in the root node. */
125694 pReader->aNode = (char *)&pReader[1];
125695 pReader->rootOnly = 1;
125696 pReader->nNode = nRoot;
125697 memcpy(pReader->aNode, zRoot, nRoot);
125698 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
125699 }else{
125700 pReader->iCurrentBlock = iStartLeaf-1;
@@ -133438,10 +131088,12 @@
131088 typedef struct unicode_cursor unicode_cursor;
131089
131090 struct unicode_tokenizer {
131091 sqlite3_tokenizer base;
131092 int bRemoveDiacritic;
131093 int nException;
131094 int *aiException;
131095 };
131096
131097 struct unicode_cursor {
131098 sqlite3_tokenizer_cursor base;
131099 const unsigned char *aInput; /* Input text being tokenized */
@@ -133450,10 +131102,125 @@
131102 int iToken; /* Index of next token to be returned */
131103 char *zToken; /* storage for current token */
131104 int nAlloc; /* space allocated at zToken */
131105 };
131106
131107
131108 /*
131109 ** Destroy a tokenizer allocated by unicodeCreate().
131110 */
131111 static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
131112 if( pTokenizer ){
131113 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
131114 sqlite3_free(p->aiException);
131115 sqlite3_free(p);
131116 }
131117 return SQLITE_OK;
131118 }
131119
131120 /*
131121 ** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
131122 ** statement has specified that the tokenizer for this table shall consider
131123 ** all characters in string zIn/nIn to be separators (if bAlnum==0) or
131124 ** token characters (if bAlnum==1).
131125 **
131126 ** For each codepoint in the zIn/nIn string, this function checks if the
131127 ** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
131128 ** If so, no action is taken. Otherwise, the codepoint is added to the
131129 ** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
131130 ** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
131131 ** codepoints in the aiException[] array.
131132 **
131133 ** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
131134 ** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
131135 ** It is not possible to change the behaviour of the tokenizer with respect
131136 ** to these codepoints.
131137 */
131138 static int unicodeAddExceptions(
131139 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
131140 int bAlnum, /* Replace Isalnum() return value with this */
131141 const char *zIn, /* Array of characters to make exceptions */
131142 int nIn /* Length of z in bytes */
131143 ){
131144 const unsigned char *z = (const unsigned char *)zIn;
131145 const unsigned char *zTerm = &z[nIn];
131146 int iCode;
131147 int nEntry = 0;
131148
131149 assert( bAlnum==0 || bAlnum==1 );
131150
131151 while( z<zTerm ){
131152 READ_UTF8(z, zTerm, iCode);
131153 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
131154 if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
131155 && sqlite3FtsUnicodeIsdiacritic(iCode)==0
131156 ){
131157 nEntry++;
131158 }
131159 }
131160
131161 if( nEntry ){
131162 int *aNew; /* New aiException[] array */
131163 int nNew; /* Number of valid entries in array aNew[] */
131164
131165 aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
131166 if( aNew==0 ) return SQLITE_NOMEM;
131167 nNew = p->nException;
131168
131169 z = (const unsigned char *)zIn;
131170 while( z<zTerm ){
131171 READ_UTF8(z, zTerm, iCode);
131172 if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
131173 && sqlite3FtsUnicodeIsdiacritic(iCode)==0
131174 ){
131175 int i, j;
131176 for(i=0; i<nNew && aNew[i]<iCode; i++);
131177 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
131178 aNew[i] = iCode;
131179 nNew++;
131180 }
131181 }
131182 p->aiException = aNew;
131183 p->nException = nNew;
131184 }
131185
131186 return SQLITE_OK;
131187 }
131188
131189 /*
131190 ** Return true if the p->aiException[] array contains the value iCode.
131191 */
131192 static int unicodeIsException(unicode_tokenizer *p, int iCode){
131193 if( p->nException>0 ){
131194 int *a = p->aiException;
131195 int iLo = 0;
131196 int iHi = p->nException-1;
131197
131198 while( iHi>=iLo ){
131199 int iTest = (iHi + iLo) / 2;
131200 if( iCode==a[iTest] ){
131201 return 1;
131202 }else if( iCode>a[iTest] ){
131203 iLo = iTest+1;
131204 }else{
131205 iHi = iTest-1;
131206 }
131207 }
131208 }
131209
131210 return 0;
131211 }
131212
131213 /*
131214 ** Return true if, for the purposes of tokenization, codepoint iCode is
131215 ** considered a token character (not a separator).
131216 */
131217 static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
131218 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
131219 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
131220 }
131221
131222 /*
131223 ** Create a new tokenizer instance.
131224 */
131225 static int unicodeCreate(
131226 int nArg, /* Size of array argv[] */
@@ -133460,43 +131227,45 @@
131227 const char * const *azArg, /* Tokenizer creation arguments */
131228 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
131229 ){
131230 unicode_tokenizer *pNew; /* New tokenizer object */
131231 int i;
131232 int rc = SQLITE_OK;
131233
131234 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
131235 if( pNew==NULL ) return SQLITE_NOMEM;
 
 
131236 memset(pNew, 0, sizeof(unicode_tokenizer));
131237 pNew->bRemoveDiacritic = 1;
131238
131239 for(i=0; rc==SQLITE_OK && i<nArg; i++){
131240 const char *z = azArg[i];
131241 int n = strlen(z);
131242
131243 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
131244 pNew->bRemoveDiacritic = 1;
131245 }
131246 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
131247 pNew->bRemoveDiacritic = 0;
131248 }
131249 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
131250 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
131251 }
131252 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
131253 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
131254 }
131255 else{
131256 /* Unrecognized argument */
131257 rc = SQLITE_ERROR;
131258 }
131259 }
131260
131261 if( rc!=SQLITE_OK ){
131262 unicodeDestroy((sqlite3_tokenizer *)pNew);
131263 pNew = 0;
131264 }
131265 *pp = (sqlite3_tokenizer *)pNew;
131266 return rc;
 
 
 
 
131267 }
131268
131269 /*
131270 ** Prepare to begin tokenizing a particular string. The input
131271 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
@@ -133545,18 +131314,19 @@
131314 /*
131315 ** Extract the next token from a tokenization cursor. The cursor must
131316 ** have been opened by a prior call to simpleOpen().
131317 */
131318 static int unicodeNext(
131319 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
131320 const char **paToken, /* OUT: Token text */
131321 int *pnToken, /* OUT: Number of bytes at *paToken */
131322 int *piStart, /* OUT: Starting offset of token */
131323 int *piEnd, /* OUT: Ending offset of token */
131324 int *piPos /* OUT: Position integer of token */
131325 ){
131326 unicode_cursor *pCsr = (unicode_cursor *)pC;
131327 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
131328 int iCode;
131329 char *zOut;
131330 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
131331 const unsigned char *zStart = z;
131332 const unsigned char *zEnd;
@@ -133565,11 +131335,11 @@
131335 /* Scan past any delimiter characters before the start of the next token.
131336 ** Return SQLITE_DONE early if this takes us all the way to the end of
131337 ** the input. */
131338 while( z<zTerm ){
131339 READ_UTF8(z, zTerm, iCode);
131340 if( unicodeIsAlnum(p, iCode) ) break;
131341 zStart = z;
131342 }
131343 if( zStart>=zTerm ) return SQLITE_DONE;
131344
131345 zOut = pCsr->zToken;
@@ -133585,21 +131355,19 @@
131355 pCsr->nAlloc += 64;
131356 }
131357
131358 /* Write the folded case of the last character read to the output */
131359 zEnd = z;
131360 iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
 
 
131361 if( iOut ){
131362 WRITE_UTF8(zOut, iOut);
131363 }
131364
131365 /* If the cursor is not at EOF, read the next character */
131366 if( z>=zTerm ) break;
131367 READ_UTF8(z, zTerm, iCode);
131368 }while( unicodeIsAlnum(p, iCode)
131369 || sqlite3FtsUnicodeIsdiacritic(iCode)
131370 );
131371
131372 /* Set the output variables and return. */
131373 pCsr->iOff = (z - pCsr->aInput);
@@ -133779,11 +131547,11 @@
131547 iHi = iTest-1;
131548 }
131549 }
131550 assert( aEntry[0]<key );
131551 assert( key>=aEntry[iRes] );
131552 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
131553 }
131554 return 1;
131555 }
131556
131557
@@ -138059,11 +135827,11 @@
135827 return SQLITE_DONE;
135828 }
135829
135830 while( iStart<iEnd ){
135831 int iWhite = iStart;
135832 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
135833 if( u_isspace(c) ){
135834 iStart = iWhite;
135835 }else{
135836 break;
135837 }
135838
+42 -23
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105105
**
106106
** See also: [sqlite3_libversion()],
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110
-#define SQLITE_VERSION "3.7.13"
111
-#define SQLITE_VERSION_NUMBER 3007013
112
-#define SQLITE_SOURCE_ID "2012-06-07 07:24:04 506008f000ba4af0b35da023b8c52f7a3f5033bd"
110
+#define SQLITE_VERSION "3.7.14"
111
+#define SQLITE_VERSION_NUMBER 3007014
112
+#define SQLITE_SOURCE_ID "2012-06-21 17:21:52 d5e6880279210ca63e2d5e7f6d009f30566f1242"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -217,11 +217,12 @@
217217
**
218218
** Each open SQLite database is represented by a pointer to an instance of
219219
** the opaque structure named "sqlite3". It is useful to think of an sqlite3
220220
** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
221221
** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
222
-** is its destructor. There are many other interfaces (such as
222
+** and [sqlite3_close_v2()] are its destructors. There are many other
223
+** interfaces (such as
223224
** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
224225
** [sqlite3_busy_timeout()] to name but three) that are methods on an
225226
** sqlite3 object.
226227
*/
227228
typedef struct sqlite3 sqlite3;
@@ -264,32 +265,50 @@
264265
#endif
265266
266267
/*
267268
** CAPI3REF: Closing A Database Connection
268269
**
269
-** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
270
-** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
271
-** successfully destroyed and all associated resources are deallocated.
270
+** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
271
+** for the [sqlite3] object.
272
+** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
273
+** the [sqlite3] object is successfully destroyed and all associated
274
+** resources are deallocated.
272275
**
273
-** Applications must [sqlite3_finalize | finalize] all [prepared statements]
274
-** and [sqlite3_blob_close | close] all [BLOB handles] associated with
275
-** the [sqlite3] object prior to attempting to close the object. ^If
276
+** ^If the database connection is associated with unfinalized prepared
277
+** statements or unfinished sqlite3_backup objects then sqlite3_close()
278
+** will leave the database connection open and return [SQLITE_BUSY].
279
+** ^If sqlite3_close_v2() is called with unfinalized prepared statements
280
+** and unfinished sqlite3_backups, then the database connection becomes
281
+** an unusable "zombie" which will automatically be deallocated when the
282
+** last prepared statement is finalized or the last sqlite3_backup is
283
+** finished. The sqlite3_close_v2() interface is intended for use with
284
+** host languages that are garbage collected, and where the order in which
285
+** destructors are called is arbitrary.
286
+**
287
+** Applications should [sqlite3_finalize | finalize] all [prepared statements],
288
+** [sqlite3_blob_close | close] all [BLOB handles], and
289
+** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
290
+** with the [sqlite3] object prior to attempting to close the object. ^If
276291
** sqlite3_close() is called on a [database connection] that still has
277
-** outstanding [prepared statements] or [BLOB handles], then it returns
278
-** SQLITE_BUSY.
292
+** outstanding [prepared statements], [BLOB handles], and/or
293
+** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
294
+** of resources is deferred until all [prepared statements], [BLOB handles],
295
+** and [sqlite3_backup] objects are also destroyed.
279296
**
280
-** ^If [sqlite3_close()] is invoked while a transaction is open,
297
+** ^If an [sqlite3] object is destroyed while a transaction is open,
281298
** the transaction is automatically rolled back.
282299
**
283
-** The C parameter to [sqlite3_close(C)] must be either a NULL
300
+** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
301
+** must be either a NULL
284302
** pointer or an [sqlite3] object pointer obtained
285303
** from [sqlite3_open()], [sqlite3_open16()], or
286304
** [sqlite3_open_v2()], and not previously closed.
287
-** ^Calling sqlite3_close() with a NULL pointer argument is a
288
-** harmless no-op.
305
+** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
306
+** argument is a harmless no-op.
289307
*/
290
-SQLITE_API int sqlite3_close(sqlite3 *);
308
+SQLITE_API int sqlite3_close(sqlite3*);
309
+SQLITE_API int sqlite3_close_v2(sqlite3*);
291310
292311
/*
293312
** The type for a callback function.
294313
** This is legacy and deprecated. It is included for historical
295314
** compatibility and is not documented.
@@ -4456,15 +4475,16 @@
44564475
** CAPI3REF: Name Of The Folder Holding Database Files
44574476
**
44584477
** ^(If this global variable is made to point to a string which is
44594478
** the name of a folder (a.k.a. directory), then all database files
44604479
** specified with a relative pathname and created or accessed by
4461
-** SQLite when using a built-in [sqlite3_vfs | VFS] will be assumed
4480
+** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
44624481
** to be relative to that directory.)^ ^If this variable is a NULL
44634482
** pointer, then SQLite assumes that all database files specified
44644483
** with a relative pathname are relative to the current directory
4465
-** for the process.
4484
+** for the process. Only the windows VFS makes use of this global
4485
+** variable; it is ignored by the unix VFS.
44664486
**
44674487
** Changing the value of this variable while a database connection is
44684488
** open can result in a corrupt database.
44694489
**
44704490
** It is not safe to read or modify this variable in more than one
@@ -5491,21 +5511,20 @@
54915511
** of these mutex routines. An appropriate implementation
54925512
** is selected automatically at compile-time. ^(The following
54935513
** implementations are available in the SQLite core:
54945514
**
54955515
** <ul>
5496
-** <li> SQLITE_MUTEX_OS2
54975516
** <li> SQLITE_MUTEX_PTHREADS
54985517
** <li> SQLITE_MUTEX_W32
54995518
** <li> SQLITE_MUTEX_NOOP
55005519
** </ul>)^
55015520
**
55025521
** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
55035522
** that does no real locking and is appropriate for use in
5504
-** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5505
-** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
5506
-** are appropriate for use on OS/2, Unix, and Windows.
5523
+** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and
5524
+** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
5525
+** and Windows.
55075526
**
55085527
** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
55095528
** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
55105529
** implementation is included with the library. In this case the
55115530
** application must supply a custom mutex implementation using the
55125531
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.13"
111 #define SQLITE_VERSION_NUMBER 3007013
112 #define SQLITE_SOURCE_ID "2012-06-07 07:24:04 506008f000ba4af0b35da023b8c52f7a3f5033bd"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -217,11 +217,12 @@
217 **
218 ** Each open SQLite database is represented by a pointer to an instance of
219 ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
220 ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
221 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
222 ** is its destructor. There are many other interfaces (such as
 
223 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
224 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
225 ** sqlite3 object.
226 */
227 typedef struct sqlite3 sqlite3;
@@ -264,32 +265,50 @@
264 #endif
265
266 /*
267 ** CAPI3REF: Closing A Database Connection
268 **
269 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
270 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
271 ** successfully destroyed and all associated resources are deallocated.
 
 
272 **
273 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
274 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
275 ** the [sqlite3] object prior to attempting to close the object. ^If
 
 
 
 
 
 
 
 
 
 
 
 
276 ** sqlite3_close() is called on a [database connection] that still has
277 ** outstanding [prepared statements] or [BLOB handles], then it returns
278 ** SQLITE_BUSY.
 
 
279 **
280 ** ^If [sqlite3_close()] is invoked while a transaction is open,
281 ** the transaction is automatically rolled back.
282 **
283 ** The C parameter to [sqlite3_close(C)] must be either a NULL
 
284 ** pointer or an [sqlite3] object pointer obtained
285 ** from [sqlite3_open()], [sqlite3_open16()], or
286 ** [sqlite3_open_v2()], and not previously closed.
287 ** ^Calling sqlite3_close() with a NULL pointer argument is a
288 ** harmless no-op.
289 */
290 SQLITE_API int sqlite3_close(sqlite3 *);
 
291
292 /*
293 ** The type for a callback function.
294 ** This is legacy and deprecated. It is included for historical
295 ** compatibility and is not documented.
@@ -4456,15 +4475,16 @@
4456 ** CAPI3REF: Name Of The Folder Holding Database Files
4457 **
4458 ** ^(If this global variable is made to point to a string which is
4459 ** the name of a folder (a.k.a. directory), then all database files
4460 ** specified with a relative pathname and created or accessed by
4461 ** SQLite when using a built-in [sqlite3_vfs | VFS] will be assumed
4462 ** to be relative to that directory.)^ ^If this variable is a NULL
4463 ** pointer, then SQLite assumes that all database files specified
4464 ** with a relative pathname are relative to the current directory
4465 ** for the process.
 
4466 **
4467 ** Changing the value of this variable while a database connection is
4468 ** open can result in a corrupt database.
4469 **
4470 ** It is not safe to read or modify this variable in more than one
@@ -5491,21 +5511,20 @@
5491 ** of these mutex routines. An appropriate implementation
5492 ** is selected automatically at compile-time. ^(The following
5493 ** implementations are available in the SQLite core:
5494 **
5495 ** <ul>
5496 ** <li> SQLITE_MUTEX_OS2
5497 ** <li> SQLITE_MUTEX_PTHREADS
5498 ** <li> SQLITE_MUTEX_W32
5499 ** <li> SQLITE_MUTEX_NOOP
5500 ** </ul>)^
5501 **
5502 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5503 ** that does no real locking and is appropriate for use in
5504 ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5505 ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
5506 ** are appropriate for use on OS/2, Unix, and Windows.
5507 **
5508 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5509 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5510 ** implementation is included with the library. In this case the
5511 ** application must supply a custom mutex implementation using the
5512
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.14"
111 #define SQLITE_VERSION_NUMBER 3007014
112 #define SQLITE_SOURCE_ID "2012-06-21 17:21:52 d5e6880279210ca63e2d5e7f6d009f30566f1242"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -217,11 +217,12 @@
217 **
218 ** Each open SQLite database is represented by a pointer to an instance of
219 ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
220 ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
221 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
222 ** and [sqlite3_close_v2()] are its destructors. There are many other
223 ** interfaces (such as
224 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
225 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
226 ** sqlite3 object.
227 */
228 typedef struct sqlite3 sqlite3;
@@ -264,32 +265,50 @@
265 #endif
266
267 /*
268 ** CAPI3REF: Closing A Database Connection
269 **
270 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
271 ** for the [sqlite3] object.
272 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
273 ** the [sqlite3] object is successfully destroyed and all associated
274 ** resources are deallocated.
275 **
276 ** ^If the database connection is associated with unfinalized prepared
277 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
278 ** will leave the database connection open and return [SQLITE_BUSY].
279 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
280 ** and unfinished sqlite3_backups, then the database connection becomes
281 ** an unusable "zombie" which will automatically be deallocated when the
282 ** last prepared statement is finalized or the last sqlite3_backup is
283 ** finished. The sqlite3_close_v2() interface is intended for use with
284 ** host languages that are garbage collected, and where the order in which
285 ** destructors are called is arbitrary.
286 **
287 ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
288 ** [sqlite3_blob_close | close] all [BLOB handles], and
289 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
290 ** with the [sqlite3] object prior to attempting to close the object. ^If
291 ** sqlite3_close() is called on a [database connection] that still has
292 ** outstanding [prepared statements], [BLOB handles], and/or
293 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
294 ** of resources is deferred until all [prepared statements], [BLOB handles],
295 ** and [sqlite3_backup] objects are also destroyed.
296 **
297 ** ^If an [sqlite3] object is destroyed while a transaction is open,
298 ** the transaction is automatically rolled back.
299 **
300 ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
301 ** must be either a NULL
302 ** pointer or an [sqlite3] object pointer obtained
303 ** from [sqlite3_open()], [sqlite3_open16()], or
304 ** [sqlite3_open_v2()], and not previously closed.
305 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
306 ** argument is a harmless no-op.
307 */
308 SQLITE_API int sqlite3_close(sqlite3*);
309 SQLITE_API int sqlite3_close_v2(sqlite3*);
310
311 /*
312 ** The type for a callback function.
313 ** This is legacy and deprecated. It is included for historical
314 ** compatibility and is not documented.
@@ -4456,15 +4475,16 @@
4475 ** CAPI3REF: Name Of The Folder Holding Database Files
4476 **
4477 ** ^(If this global variable is made to point to a string which is
4478 ** the name of a folder (a.k.a. directory), then all database files
4479 ** specified with a relative pathname and created or accessed by
4480 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
4481 ** to be relative to that directory.)^ ^If this variable is a NULL
4482 ** pointer, then SQLite assumes that all database files specified
4483 ** with a relative pathname are relative to the current directory
4484 ** for the process. Only the windows VFS makes use of this global
4485 ** variable; it is ignored by the unix VFS.
4486 **
4487 ** Changing the value of this variable while a database connection is
4488 ** open can result in a corrupt database.
4489 **
4490 ** It is not safe to read or modify this variable in more than one
@@ -5491,21 +5511,20 @@
5511 ** of these mutex routines. An appropriate implementation
5512 ** is selected automatically at compile-time. ^(The following
5513 ** implementations are available in the SQLite core:
5514 **
5515 ** <ul>
 
5516 ** <li> SQLITE_MUTEX_PTHREADS
5517 ** <li> SQLITE_MUTEX_W32
5518 ** <li> SQLITE_MUTEX_NOOP
5519 ** </ul>)^
5520 **
5521 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5522 ** that does no real locking and is appropriate for use in
5523 ** a single-threaded application. ^The SQLITE_MUTEX_PTHREADS and
5524 ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
5525 ** and Windows.
5526 **
5527 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5528 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5529 ** implementation is included with the library. In this case the
5530 ** application must supply a custom mutex implementation using the
5531

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button